sm_df.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2005 Marc Balmer
  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 : \
  34. * syncwrites : asyncwrites
  35. */
  36. #include "conf.h"
  37. #include <sys/types.h>
  38. #include <sys/param.h>
  39. #include <sys/mount.h>
  40. #include <sys/statvfs.h>
  41. #include <ufs/ufs/dinode.h>
  42. #include <ufs/ffs/fs.h>
  43. #include <errno.h>
  44. #include <fcntl.h>
  45. #include <fstab.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49. #include "error.h"
  50. #include "symon.h"
  51. /* Globals for this module start with df_ */
  52. static struct statvfs *df_stats = NULL;
  53. static int df_parts = 0;
  54. void
  55. init_df(struct stream *st)
  56. {
  57. strlcpy(st->parg.df.rawdev, "/dev/", sizeof(st->parg.df.rawdev));
  58. strlcat(st->parg.df.rawdev, st->arg, sizeof(st->parg.df.rawdev));
  59. info("started module df(%.200s)", st->arg);
  60. }
  61. void
  62. gets_df()
  63. {
  64. if ((df_parts = getmntinfo(&df_stats, MNT_NOWAIT)) == 0) {
  65. warning("df failed");
  66. }
  67. }
  68. /*
  69. * from src/bin/df.c:
  70. * Convert statfs returned filesystem size into BLOCKSIZE units.
  71. * Attempts to avoid overflow for large filesystems.
  72. */
  73. #define fsbtoblk(num, fsbs, bs) \
  74. (((fsbs) != 0 && (fsbs) < (bs)) ? \
  75. (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
  76. int
  77. get_df(char *symon_buf, int maxlen, struct stream *st)
  78. {
  79. int n;
  80. for (n = 0; n < df_parts; n++) {
  81. if (!strncmp(df_stats[n].f_mntfromname, st->parg.df.rawdev, SYMON_DFNAMESIZE)) {
  82. return snpack(symon_buf, maxlen, st->arg, MT_DF,
  83. (u_int64_t)fsbtoblk(df_stats[n].f_blocks, df_stats[n].f_bsize, SYMON_DFBLOCKSIZE),
  84. (u_int64_t)fsbtoblk(df_stats[n].f_bfree, df_stats[n].f_bsize, SYMON_DFBLOCKSIZE),
  85. (u_int64_t)fsbtoblk(df_stats[n].f_bavail, df_stats[n].f_bsize, SYMON_DFBLOCKSIZE),
  86. (u_int64_t)df_stats[n].f_files,
  87. (u_int64_t)df_stats[n].f_ffree,
  88. (u_int64_t)df_stats[n].f_syncwrites,
  89. (u_int64_t)df_stats[n].f_asyncwrites);
  90. }
  91. }
  92. warning("df(%.200s) failed (no such device)", st->arg);
  93. return 0;
  94. }