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.
 
 
 

145 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 <mntent.h>
  24. #include <sys/vfs.h>
  25. #include "logging.h"
  26. #include "plugins.h"
  27. #include "probe.h"
  28. #define MAXFSNAME 16
  29. struct sammler_plugin plugin;
  30. static const char *ds_def = {
  31. "DS:block_total:GAUGE:15:0:U "
  32. "DS:block_free:GAUGE:15:0:U "
  33. };
  34. static const char * get_ds(int ds_id)
  35. {
  36. return ds_def;
  37. }
  38. static char * get_valid_fs(int *xcnt)
  39. {
  40. FILE *fp = fopen("/proc/filesystems", "r");
  41. if (fp == NULL)
  42. return NULL;
  43. int cnt = 0;
  44. char buffer[64];
  45. while (fgets(buffer, sizeof(buffer), fp) != NULL) {
  46. if (!strncmp(buffer, "nodev", 5))
  47. continue;
  48. cnt++;
  49. }
  50. char *valid_arr = malloc(cnt * MAXFSNAME);
  51. if (valid_arr == NULL)
  52. return NULL;
  53. rewind(fp);
  54. int i = 0;
  55. while (fgets(buffer, sizeof(buffer), fp) != NULL) {
  56. if (!strncmp(buffer, "nodev", 5))
  57. continue;
  58. char *end = memccpy(valid_arr + (i++ * MAXFSNAME), buffer +1, '\n', MAXFSNAME);
  59. *(end -1) = '\0';
  60. }
  61. fclose(fp);
  62. *xcnt = cnt;
  63. return valid_arr;
  64. }
  65. static int probe(void)
  66. {
  67. FILE *fp = setmntent("/etc/mtab", "r");
  68. if (fp == NULL) {
  69. log_print(LOG_WARN, "plugin mount");
  70. return -1;
  71. }
  72. int cnt;
  73. char *valid_arr = get_valid_fs(&cnt);
  74. if (valid_arr == NULL) {
  75. log_print(LOG_WARN, "plugin mount");
  76. endmntent(fp);
  77. return -1;
  78. }
  79. struct mntent *mnt;
  80. while ((mnt = getmntent(fp)) != NULL) {
  81. int i, valid = 0;
  82. for (i = 0; i < cnt; i++)
  83. if (strncmp(mnt->mnt_type, valid_arr + (i * MAXFSNAME), MAXFSNAME) == 0)
  84. valid = 1;
  85. if (valid == 0)
  86. continue;
  87. struct statfs fs;
  88. if (statfs(mnt->mnt_dir, &fs) == -1) {
  89. log_print(LOG_WARN, "plugin mount: statfs(%s)", mnt->mnt_dir);
  90. continue;
  91. }
  92. if (fs.f_blocks == 0)
  93. continue;
  94. char *slash = mnt->mnt_fsname;
  95. while (slash && (slash = strchr(slash, '/'))) {
  96. slash = strchr(slash, '/');
  97. *slash++ = '_';
  98. }
  99. char filename[64];
  100. int len = snprintf(filename, sizeof(filename), "mount%s.rrd", mnt->mnt_fsname);
  101. if (len < 0 || len >= sizeof(filename)) {
  102. log_print(LOG_WARN, "plugin mount: file name too long", mnt->mnt_fsname);
  103. continue;
  104. }
  105. probe_submit(&plugin, filename, 0, "%lu:%lu",
  106. fs.f_blocks * (fs.f_bsize /1024),
  107. fs.f_bfree * (fs.f_bsize /1024));
  108. }
  109. free(valid_arr);
  110. endmntent(fp);
  111. return 0;
  112. }
  113. struct sammler_plugin plugin = {
  114. .name = "mount",
  115. .interval = 10,
  116. .probe = &probe,
  117. .get_ds = &get_ds,
  118. };