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.
 
 
 

138 lines
3.8 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 "plugins.h"
  25. #include "probe.h"
  26. #define DS_CPU 1
  27. #define DS_PROC 2
  28. struct sammler_plugin plugin;
  29. static const char *cpu_ds_def = {
  30. "DS:user:COUNTER:15:0:U "
  31. "DS:nice:COUNTER:15:0:U "
  32. "DS:system:COUNTER:15:0:U "
  33. "DS:idle:COUNTER:15:0:U "
  34. "DS:iowait:COUNTER:15:0:U "
  35. "DS:irq:COUNTER:15:0:U "
  36. "DS:softirq:COUNTER:15:0:U "
  37. "DS:steal:COUNTER:15:0:U "
  38. };
  39. static const char *proc_ds_def = {
  40. "DS:intr:COUNTER:15:0:U "
  41. "DS:ctxt:COUNTER:15:0:U "
  42. "DS:fork:COUNTER:15:0:U "
  43. };
  44. static const char * get_ds(int ds_id)
  45. {
  46. switch (ds_id) {
  47. case DS_CPU:
  48. return cpu_ds_def;
  49. case DS_PROC:
  50. return proc_ds_def;
  51. default:
  52. return NULL;
  53. }
  54. }
  55. struct proc_ {
  56. unsigned long long intr;
  57. unsigned long long ctxt;
  58. unsigned long long fork;
  59. };
  60. static int probe(void)
  61. {
  62. FILE *fp;
  63. struct proc_ proc;
  64. memset(&proc, 0, sizeof(proc));
  65. fp = fopen("/proc/stat", "r");
  66. if (fp == NULL) {
  67. log_print(LOG_WARN, "plugin stat");
  68. return -1;
  69. }
  70. while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) {
  71. if (!strncmp(plugin.buffer, "cpu", 3)) {
  72. char *val[9], filename[16];
  73. int numfields, len, cpu;
  74. if ((plugin.buffer[3] >= '0') && (plugin.buffer[3] <= '9')) {
  75. cpu = atoi(plugin.buffer +3);
  76. len = snprintf(filename, sizeof(filename), "cpu-%d.rrd", cpu);
  77. if (len < 0 || len >= sizeof(filename))
  78. continue;
  79. } else {
  80. strncpy(filename, "cpu.rrd", sizeof(filename));
  81. }
  82. numfields = strsplit(plugin.buffer, " \t\n", val, 9);
  83. if (numfields < 5)
  84. continue;
  85. else if (numfields == 5)
  86. val[5] = val[6] = val[7] = val[8] = "0";
  87. else if (numfields == 8)
  88. val[8] = "0";
  89. probe_submit(&plugin, filename, DS_CPU,
  90. "%s:%s:%s:%s:%s:%s:%s:%s",
  91. val[1], val[2], val[3], val[4],
  92. val[5], val[6], val[7], val[8]);
  93. } else if (!strncmp(plugin.buffer, "intr", 4)) {
  94. proc.intr = atoll(plugin.buffer + 5);
  95. } else if (!strncmp(plugin.buffer, "ctxt", 4)) {
  96. proc.ctxt = atoll(plugin.buffer + 5);
  97. } else if (!strncmp(plugin.buffer, "processes", 9)) {
  98. proc.fork = atoll(plugin.buffer + 10);
  99. }
  100. }
  101. probe_submit(&plugin, "proc.rrd", DS_PROC, "%llu:%llu:%llu",
  102. proc.intr, proc.ctxt, proc.fork);
  103. fclose(fp);
  104. return 0;
  105. }
  106. struct sammler_plugin plugin = {
  107. .name = "stat",
  108. .interval = 10,
  109. .bufsize = 1024,
  110. .probe = &probe,
  111. .get_ds = &get_ds,
  112. };