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