rrd based system stats
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

155 lines
3.9 KiB

  1. /***************************************************************************
  2. * Copyright (C) 06/2006 by Olaf Rempel *
  3. * razzor@kopf-tisch.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include "helper.h"
  24. #include "logging.h"
  25. #include "plugins.h"
  26. #include "probe.h"
  27. #define DS_CPU 1
  28. #define DS_PROC 2
  29. #define BUFSIZE 1024
  30. struct sammler_plugin plugin;
  31. static char *buffer;
  32. static const char *cpu_ds_def = {
  33. "DS:user:COUNTER:15:0:U "
  34. "DS:nice:COUNTER:15:0:U "
  35. "DS:system:COUNTER:15:0:U "
  36. "DS:idle:COUNTER:15:0:U "
  37. "DS:iowait:COUNTER:15:0:U "
  38. "DS:irq:COUNTER:15:0:U "
  39. "DS:softirq:COUNTER:15:0:U "
  40. "DS:steal:COUNTER:15:0:U "
  41. };
  42. static const char *proc_ds_def = {
  43. "DS:intr:COUNTER:15:0:U "
  44. "DS:ctxt:COUNTER:15:0:U "
  45. "DS:fork:COUNTER:15:0:U "
  46. };
  47. static const char * get_ds(int ds_id)
  48. {
  49. switch (ds_id) {
  50. case DS_CPU:
  51. return cpu_ds_def;
  52. case DS_PROC:
  53. return proc_ds_def;
  54. default:
  55. return NULL;
  56. }
  57. }
  58. struct proc_ {
  59. unsigned long long intr;
  60. unsigned long long ctxt;
  61. unsigned long long fork;
  62. };
  63. static int probe(void)
  64. {
  65. FILE *fp;
  66. struct proc_ proc;
  67. memset(&proc, 0, sizeof(proc));
  68. fp = fopen("/proc/stat", "r");
  69. if (fp == NULL) {
  70. log_print(LOG_WARN, "plugin stat");
  71. return -1;
  72. }
  73. while (fgets(buffer, BUFSIZE, fp) != NULL) {
  74. if (!strncmp(buffer, "cpu", 3)) {
  75. char *val[9], filename[16];
  76. int numfields, len, cpu;
  77. if ((buffer[3] >= '0') && (buffer[3] <= '9')) {
  78. cpu = atoi(buffer +3);
  79. len = snprintf(filename, sizeof(filename), "cpu-%d.rrd", cpu);
  80. if (len < 0 || len >= sizeof(filename))
  81. continue;
  82. } else {
  83. strncpy(filename, "cpu.rrd", sizeof(filename));
  84. }
  85. numfields = strsplit(buffer, " \t\n", val, 9);
  86. if (numfields < 5)
  87. continue;
  88. else if (numfields == 5)
  89. val[5] = val[6] = val[7] = val[8] = "0";
  90. else if (numfields == 8)
  91. val[8] = "0";
  92. probe_submit(&plugin, filename, DS_CPU,
  93. "%s:%s:%s:%s:%s:%s:%s:%s",
  94. val[1], val[2], val[3], val[4],
  95. val[5], val[6], val[7], val[8]);
  96. } else if (!strncmp(buffer, "intr", 4)) {
  97. proc.intr = atoll(buffer + 5);
  98. } else if (!strncmp(buffer, "ctxt", 4)) {
  99. proc.ctxt = atoll(buffer + 5);
  100. } else if (!strncmp(buffer, "processes", 9)) {
  101. proc.fork = atoll(buffer + 10);
  102. }
  103. }
  104. probe_submit(&plugin, "proc.rrd", DS_PROC, "%llu:%llu:%llu",
  105. proc.intr, proc.ctxt, proc.fork);
  106. fclose(fp);
  107. return 0;
  108. }
  109. static int init(void)
  110. {
  111. buffer = malloc(BUFSIZE);
  112. return (buffer == NULL) ? -1 : 0;
  113. }
  114. static int fini(void)
  115. {
  116. free(buffer);
  117. return 0;
  118. }
  119. struct sammler_plugin plugin = {
  120. .name = "stat",
  121. .interval = 10,
  122. .init = &init,
  123. .fini = &fini,
  124. .probe = &probe,
  125. .get_ds = &get_ds,
  126. };