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.
 
 
 

118 lines
3.4 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 "plugins.h"
  24. #include "probe.h"
  25. #define DS_MEMORY 1
  26. #define DS_SWAP 2
  27. struct sammler_plugin plugin;
  28. static const char *mem_ds_def = {
  29. "DS:total:GAUGE:15:0:U "
  30. "DS:free:GAUGE:15:0:U "
  31. "DS:buffers:GAUGE:15:0:U "
  32. "DS:cached:GAUGE:15:0:U "
  33. };
  34. static const char *swap_ds_def = {
  35. "DS:total:GAUGE:15:0:U "
  36. "DS:free:GAUGE:15:0:U "
  37. };
  38. static const char * get_ds(int ds_id)
  39. {
  40. switch (ds_id) {
  41. case DS_MEMORY:
  42. return mem_ds_def;
  43. case DS_SWAP:
  44. return swap_ds_def;
  45. default:
  46. return NULL;
  47. }
  48. }
  49. struct meminfo_ {
  50. unsigned long long memtotal;
  51. unsigned long long memfree;
  52. unsigned long long buffers;
  53. unsigned long long cached;
  54. unsigned long long swaptotal;
  55. unsigned long long swapfree;
  56. };
  57. static int probe(void)
  58. {
  59. FILE *fp;
  60. struct meminfo_ meminfo;
  61. memset(&meminfo, 0, sizeof(meminfo));
  62. fp = fopen("/proc/meminfo", "r");
  63. if (fp == NULL) {
  64. log_print(LOG_WARN, "plugin memory");
  65. return -1;
  66. }
  67. while (fgets(plugin.buffer, plugin.bufsize, fp) != NULL) {
  68. if (!strncmp(plugin.buffer, "MemTotal:", 9))
  69. meminfo.memtotal = atoll(plugin.buffer + 10);
  70. else if (!strncmp(plugin.buffer, "MemFree:", 8))
  71. meminfo.memfree = atoll(plugin.buffer + 9);
  72. else if (!strncmp(plugin.buffer, "Buffers:", 8))
  73. meminfo.buffers = atoll(plugin.buffer + 9);
  74. else if (!strncmp(plugin.buffer, "Cached:", 7))
  75. meminfo.cached = atoll(plugin.buffer + 8);
  76. else if (!strncmp(plugin.buffer, "SwapTotal:", 10))
  77. meminfo.swaptotal = atoll(plugin.buffer + 11);
  78. else if (!strncmp(plugin.buffer, "SwapFree:", 9))
  79. meminfo.swapfree = atoll(plugin.buffer + 10);
  80. }
  81. probe_submit(&plugin, "memory.rrd", DS_MEMORY, "%llu:%llu:%llu:%llu",
  82. meminfo.memtotal, meminfo.memfree,
  83. meminfo.buffers, meminfo.cached);
  84. probe_submit(&plugin, "swap.rrd", DS_SWAP, "%llu:%llu",
  85. meminfo.swaptotal, meminfo.swapfree);
  86. fclose(fp);
  87. return 0;
  88. }
  89. struct sammler_plugin plugin = {
  90. .name = "memory",
  91. .interval = 10,
  92. .bufsize = 1024,
  93. .probe = &probe,
  94. .get_ds = &get_ds,
  95. };