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 /*
33 * Get current df statistics and return them in symon_buf as
34 *
35 * blocks : bfree : bavail : files : ffree : 0 : 0
36 * syncwrites : asyncwrites are not available on linux
37 */
38
39
40 #include <sys/types.h>
41 #include <errno.h>
42 #include <stdio.h>
43 #include <mntent.h>
44 #include <string.h>
45 #include <sys/statvfs.h>
46
47 #include "conf.h"
48 #include "error.h"
49 #include "symon.h"
50 #include "diskname.h"
51
52 void
init_df(struct stream * st)53 init_df(struct stream *st)
54 {
55 struct disknamectx c;
56 char drivename[MAX_PATH_LEN];
57 FILE *fp;
58 struct mntent *mount;
59
60 if (st->arg == NULL)
61 fatal("df: need a <disk device|name> argument");
62
63 initdisknamectx(&c, st->arg, drivename, sizeof(drivename));
64
65 while (nextdiskname(&c)) {
66 fp = setmntent("/etc/mtab", "r");
67
68 if (fp == NULL)
69 fatal("df: cannot access /etc/mtab: %.200s", strerror(errno));
70
71 while ((mount = getmntent(fp))) {
72 if (strncmp(mount->mnt_fsname, drivename, sizeof(drivename)) == 0) {
73 strlcpy(st->parg.df.mountpath, mount->mnt_dir, sizeof(st->parg.df.mountpath));
74 info("started module df(%.200s = %.200s)", st->arg, st->parg.df.mountpath);
75 endmntent(fp);
76 return;
77 }
78 }
79
80 endmntent(fp);
81 }
82
83 warning("df(%.200s): not mounted", st->arg);
84 }
85
86 void
gets_df()87 gets_df()
88 {
89 }
90
91 /*
92 * from src/bin/df.c:
93 * Convert statfs returned filesystem size into BLOCKSIZE units.
94 * Attempts to avoid overflow for large filesystems.
95 */
96 u_int64_t
fsbtoblk(u_int64_t num,u_int64_t fsbs,u_int64_t bs)97 fsbtoblk(u_int64_t num, u_int64_t fsbs, u_int64_t bs)
98 {
99 return (((fsbs) != 0 && (fsbs) < (bs)) ?
100 (num) / ((bs) / (fsbs)) :
101 (num) * ((fsbs) / (bs)));
102 }
103
104 int
get_df(char * symon_buf,int maxlen,struct stream * st)105 get_df(char *symon_buf, int maxlen, struct stream *st)
106 {
107 struct statvfs buf;
108
109 if (statvfs(st->parg.df.mountpath, &buf) == 0 ) {
110 return snpack(symon_buf, maxlen, st->arg, MT_DF,
111 (u_int64_t)fsbtoblk(buf.f_blocks, buf.f_bsize, SYMON_DFBLOCKSIZE),
112 (u_int64_t)fsbtoblk(buf.f_bfree, buf.f_bsize, SYMON_DFBLOCKSIZE),
113 (u_int64_t)fsbtoblk(buf.f_bavail, buf.f_bsize, SYMON_DFBLOCKSIZE),
114 (u_int64_t)buf.f_files,
115 (u_int64_t)buf.f_ffree,
116 (u_int64_t)0,
117 (u_int64_t)0);
118 }
119
120 warning("df(%.200s) failed", st->arg);
121 return 0;
122 }
123