3 * Copyright (C) 2005 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
26 #include "utils_mount.h"
28 #define MODULE_NAME "df"
30 #if HAVE_STATFS || HAVE_STATVFS
31 # define DF_HAVE_READ 1
33 # define DF_HAVE_READ 0
37 # define STATANYFS statvfs
38 # define BLOCKSIZE(s) ((s).f_frsize ? (s).f_frsize : (s).f_bsize)
40 # define STATANYFS statfs
41 # define BLOCKSIZE(s) (s).f_bsize
44 static char *filename_template = "df-%s.rrd";
46 static char *ds_def[] =
48 "DS:used:GAUGE:25:0:U",
49 "DS:free:GAUGE:25:0:U",
52 static int ds_num = 2;
56 static void df_init (void)
61 static void df_write (char *host, char *inst, char *val)
66 status = snprintf (file, BUFSIZE, filename_template, inst);
69 else if (status >= BUFSIZE)
72 rrd_update_file (host, file, val, ds_def, ds_num);
76 static void df_submit (char *df_name,
77 unsigned long long df_used,
78 unsigned long long df_free)
82 if (snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
83 df_used, df_free) >= BUFSIZE)
86 plugin_submit (MODULE_NAME, df_name, buf);
89 static void df_read (void)
91 struct STATANYFS statbuf;
95 unsigned long long blocksize;
96 unsigned long long df_free;
97 unsigned long long df_used;
98 char mnt_name[BUFSIZE];
101 if (cu_mount_getlist (&mnt_list) == NULL)
103 syslog (LOG_WARNING, "cu_mount_getlist returned `NULL'");
107 for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
109 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
111 syslog (LOG_ERR, "statv?fs failed: %s", strerror (errno));
115 if (!statbuf.f_blocks)
118 blocksize = BLOCKSIZE(statbuf);
119 df_free = statbuf.f_bfree * blocksize;
120 df_used = (statbuf.f_blocks - statbuf.f_bfree) * blocksize;
122 if (strcmp (mnt_ptr->dir, "/") == 0)
124 strncpy (mnt_name, "root", BUFSIZE);
130 strncpy (mnt_name, mnt_ptr->dir + 1, BUFSIZE);
131 len = strlen (mnt_name);
133 for (i = 0; i < len; i++)
134 if (mnt_name[i] == '/')
138 df_submit (mnt_name, df_used, df_free);
141 cu_mount_freelist (mnt_list);
142 } /* static void df_read (void) */
144 # define df_read NULL
145 #endif /* DF_HAVE_READ */
147 void module_register (void)
149 plugin_register (MODULE_NAME, df_init, df_read, df_write);