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.
 
 
 

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