sm_df.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2005 Marc Balmer
  3. * Copyright (c) 2012 Willem Dijkstra
  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 : \
  35. * syncwrites : asyncwrites
  36. */
  37. #include "conf.h"
  38. #include <sys/types.h>
  39. #include <sys/param.h>
  40. #include <sys/mount.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. #include "diskname.h"
  52. /* Globals for this module start with df_ */
  53. static struct statfs *df_stats = NULL;
  54. static int df_parts = 0;
  55. void
  56. init_df(struct stream *st)
  57. {
  58. struct disknamectx c;
  59. int n;
  60. char drivename[SYMON_DFNAMESIZE];
  61. if (st->arg == NULL)
  62. fatal("df: need a <disk device|name> argument");
  63. initdisknamectx(&c, st->arg, drivename, sizeof(drivename));
  64. gets_df();
  65. while (nextdiskname(&c)) {
  66. for (n = 0; n < df_parts; n++) {
  67. if (!strncmp(df_stats[n].f_mntfromname, drivename, SYMON_DFNAMESIZE)) {
  68. strlcpy(st->parg.df.rawdev, drivename, sizeof(st->parg.df.rawdev));
  69. info("started module df(%.200s)", st->arg);
  70. return;
  71. }
  72. }
  73. }
  74. warning("df(%.200s) failed (no such device)", st->arg);
  75. }
  76. void
  77. gets_df()
  78. {
  79. if ((df_parts = getmntinfo(&df_stats, MNT_NOWAIT)) == 0) {
  80. warning("df failed");
  81. }
  82. }
  83. /*
  84. * from src/bin/df.c:
  85. * Convert statfs returned filesystem size into BLOCKSIZE units.
  86. * Attempts to avoid overflow for large filesystems.
  87. */
  88. #define fsbtoblk(num, fsbs, bs) \
  89. (((fsbs) != 0 && (fsbs) < (bs)) ? \
  90. (num) / ((bs) / (fsbs)) : (num) * ((fsbs) / (bs)))
  91. int
  92. get_df(char *symon_buf, int maxlen, struct stream *st)
  93. {
  94. int n;
  95. for (n = 0; n < df_parts; n++) {
  96. if (!strncmp(df_stats[n].f_mntfromname, st->parg.df.rawdev, SYMON_DFNAMESIZE)) {
  97. return snpack(symon_buf, maxlen, st->arg, MT_DF,
  98. (u_int64_t)fsbtoblk(df_stats[n].f_blocks, df_stats[n].f_bsize, SYMON_DFBLOCKSIZE),
  99. (u_int64_t)fsbtoblk(df_stats[n].f_bfree, df_stats[n].f_bsize, SYMON_DFBLOCKSIZE),
  100. (u_int64_t)fsbtoblk(df_stats[n].f_bavail, df_stats[n].f_bsize, SYMON_DFBLOCKSIZE),
  101. (u_int64_t)df_stats[n].f_files,
  102. (u_int64_t)df_stats[n].f_ffree,
  103. (u_int64_t)df_stats[n].f_syncwrites,
  104. (u_int64_t)df_stats[n].f_asyncwrites);
  105. }
  106. }
  107. warning("df(%.200s) failed (no such device)", st->arg);
  108. return 0;
  109. }