sm_df.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2011-2012 Willem Dijkstra
  3. * Copyright (c) 2007 Martin van der Werff
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. /*
  32. * Get current df statistics and return them in symon_buf as
  33. *
  34. * blocks : bfree : bavail : files : ffree : 0 : 0
  35. * syncwrites : asyncwrites are not available on linux
  36. */
  37. #include <sys/types.h>
  38. #include <errno.h>
  39. #include <stdio.h>
  40. #include <mntent.h>
  41. #include <string.h>
  42. #include <sys/statvfs.h>
  43. #include "conf.h"
  44. #include "error.h"
  45. #include "symon.h"
  46. #include "diskname.h"
  47. void
  48. init_df(struct stream *st)
  49. {
  50. struct disknamectx c;
  51. char drivename[MAX_PATH_LEN];
  52. FILE *fp;
  53. struct mntent *mount;
  54. if (st->arg == NULL)
  55. fatal("df: need a <disk device|name> argument");
  56. initdisknamectx(&c, st->arg, drivename, sizeof(drivename));
  57. while (nextdiskname(&c)) {
  58. fp = setmntent("/etc/mtab", "r");
  59. if (fp == NULL)
  60. fatal("df: cannot access /etc/mtab: %.200s", strerror(errno));
  61. while ((mount = getmntent(fp))) {
  62. if (strncmp(mount->mnt_fsname, drivename, sizeof(drivename)) == 0) {
  63. strlcpy(st->parg.df.mountpath, mount->mnt_dir, sizeof(st->parg.df.mountpath));
  64. info("started module df(%.200s = %.200s)", st->arg, st->parg.df.mountpath);
  65. endmntent(fp);
  66. return;
  67. }
  68. }
  69. endmntent(fp);
  70. }
  71. warning("df(%.200s): not mounted", st->arg);
  72. }
  73. void
  74. gets_df()
  75. {
  76. }
  77. /*
  78. * from src/bin/df.c:
  79. * Convert statfs returned filesystem size into BLOCKSIZE units.
  80. * Attempts to avoid overflow for large filesystems.
  81. */
  82. u_int64_t
  83. fsbtoblk(u_int64_t num, u_int64_t fsbs, u_int64_t bs)
  84. {
  85. return (((fsbs) != 0 && (fsbs) < (bs)) ?
  86. (num) / ((bs) / (fsbs)) :
  87. (num) * ((fsbs) / (bs)));
  88. }
  89. int
  90. get_df(char *symon_buf, int maxlen, struct stream *st)
  91. {
  92. struct statvfs buf;
  93. if (statvfs(st->parg.df.mountpath, &buf) == 0 ) {
  94. return snpack(symon_buf, maxlen, st->arg, MT_DF,
  95. (u_int64_t)fsbtoblk(buf.f_blocks, buf.f_bsize, SYMON_DFBLOCKSIZE),
  96. (u_int64_t)fsbtoblk(buf.f_bfree, buf.f_bsize, SYMON_DFBLOCKSIZE),
  97. (u_int64_t)fsbtoblk(buf.f_bavail, buf.f_bsize, SYMON_DFBLOCKSIZE),
  98. (u_int64_t)buf.f_files,
  99. (u_int64_t)buf.f_ffree,
  100. (u_int64_t)0,
  101. (u_int64_t)0);
  102. }
  103. warning("df(%.200s) failed", st->arg);
  104. return 0;
  105. }