Finished up `df' plugin
[collectd.git] / src / df.c
1 /**
2  * collectd - src/df.c
3  * Copyright (C) 2005  Florian octo Forster
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "utils_mount.h"
27
28 #define MODULE_NAME "df"
29
30 #if HAVE_STATFS || HAVE_STATVFS
31 # define DF_HAVE_READ 1
32 #else
33 # define DF_HAVE_READ 0
34 #endif
35
36 #if HAVE_STATFS
37 #define STATANYFS statfs
38 #define BLOCKSIZE(s) (s).f_bsize
39
40 #elif HAVE_STATVFS
41 #define STATANYFS statvfs
42 #define BLOCKSIZE(s) ((s).f_frsize ? (s).f_frsize : (s).f_bsize)
43 #endif
44
45 static char *filename_template = "df-%s.rrd";
46
47 /* 104857600 == 100 MB */
48 static char *ds_def[] =
49 {
50         "DS:used:GAUGE:25:0:U",
51         "DS:free:GAUGE:25:0:U",
52         NULL
53 };
54 static int ds_num = 2;
55
56 #define BUFSIZE 512
57
58 static void df_init (void)
59 {
60         return;
61 }
62
63 static void df_write (char *host, char *inst, char *val)
64 {
65         char file[BUFSIZE];
66         int status;
67
68         status = snprintf (file, BUFSIZE, filename_template, inst);
69         if (status < 1)
70                 return;
71         else if (status >= BUFSIZE)
72                 return;
73
74         rrd_update_file (host, file, val, ds_def, ds_num);
75 }
76
77 #if DF_HAVE_READ
78 static void df_submit (char *df_name,
79                 unsigned long long df_used,
80                 unsigned long long df_free)
81 {
82         char buf[BUFSIZE];
83
84         if (snprintf (buf, BUFSIZE, "%u:%llu:%llu", (unsigned int) curtime,
85                                 df_used, df_free) >= BUFSIZE)
86                 return;
87
88         plugin_submit (MODULE_NAME, df_name, buf);
89 }
90
91 static void df_read (void)
92 {
93         struct STATANYFS statbuf;
94         cu_mount_t *mnt_list;
95         cu_mount_t *mnt_ptr;
96
97         unsigned long long blocksize;
98         unsigned long long df_free;
99         unsigned long long df_used;
100         char mnt_name[BUFSIZE];
101
102         mnt_list = NULL;
103         if (cu_mount_getlist (&mnt_list) == NULL)
104         {
105                 syslog (LOG_WARNING, "cu_mount_getlist returned `NULL'");
106                 return;
107         }
108
109         for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
110         {
111                 if (STATANYFS (mnt_ptr->dir, &statbuf) < 0)
112                 {
113                         syslog (LOG_ERR, "statv?fs failed: %s", strerror (errno));
114                         continue;
115                 }
116
117                 if (!statbuf.f_blocks)
118                         continue;
119
120                 blocksize = BLOCKSIZE(statbuf);
121                 df_free = statbuf.f_bfree * blocksize;
122                 df_used = (statbuf.f_blocks - statbuf.f_bfree) * blocksize;
123
124                 if (strcmp (mnt_ptr->dir, "/") == 0)
125                 {
126                         strncpy (mnt_name, "root", BUFSIZE);
127                 }
128                 else
129                 {
130                         int i, len;
131
132                         strncpy (mnt_name, mnt_ptr->dir + 1, BUFSIZE);
133                         len = strlen (mnt_name);
134
135                         for (i = 0; i < len; i++)
136                                 if (mnt_name[i] == '/')
137                                         mnt_name[i] = '-';
138                 }
139
140                 df_submit (mnt_name, df_used, df_free);
141         }
142
143         cu_mount_freelist (mnt_list);
144 } /* static void df_read (void) */
145 #else
146 # define df_read NULL
147 #endif /* DF_HAVE_READ */
148
149 void module_register (void)
150 {
151         plugin_register (MODULE_NAME, df_init, df_read, df_write);
152 }
153
154 #undef BUFSIZE
155 #undef MODULE_NAME