sm_df.c 3.4 KB

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