Merge branch 'collectd-5.4' into collectd-5.5
[collectd.git] / src / md.c
1 /**
2  * collectd - src/md.c
3  * Copyright (C) 2010,2011  Michael Hanselmann
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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Author:
19  *   Michael Hanselmann
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_ignorelist.h"
26
27 #include <sys/ioctl.h>
28
29 #include <linux/major.h>
30 #include <linux/raid/md_u.h>
31
32 #ifdef HAVE_SYS_SYSMACROS_H
33 #include <sys/sysmacros.h>
34 #endif
35
36 #define PROC_DISKSTATS "/proc/diskstats"
37 #define DEV_DIR "/dev"
38
39 static const char *config_keys[] =
40 {
41   "Device",
42   "IgnoreSelected"
43 };
44 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
45
46 static ignorelist_t *ignorelist = NULL;
47
48 static int md_config (const char *key, const char *value)
49 {
50   if (ignorelist == NULL)
51     ignorelist = ignorelist_create (/* invert = */ 1);
52   if (ignorelist == NULL)
53     return (1);
54
55   if (strcasecmp (key, "Device") == 0)
56   {
57     ignorelist_add (ignorelist, value);
58   }
59   else if (strcasecmp (key, "IgnoreSelected") == 0)
60   {
61     ignorelist_set_invert (ignorelist, IS_TRUE (value) ? 0 : 1);
62   }
63   else
64   {
65     return (-1);
66   }
67
68   return (0);
69 }
70
71 static void md_submit (const int minor, const char *type_instance,
72     gauge_t value)
73 {
74   value_t values[1];
75   value_list_t vl = VALUE_LIST_INIT;
76
77   values[0].gauge = value;
78
79   vl.values = values;
80   vl.values_len = 1;
81   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
82   sstrncpy (vl.plugin, "md", sizeof (vl.plugin));
83   ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
84       "%i", minor);
85   sstrncpy (vl.type, "md_disks", sizeof (vl.type));
86   sstrncpy (vl.type_instance, type_instance,
87       sizeof (vl.type_instance));
88
89   plugin_dispatch_values (&vl);
90 } /* void md_submit */
91
92 static void md_process (const int minor, const char *path)
93 {
94   char errbuf[1024];
95   int fd;
96   struct stat st;
97   mdu_array_info_t array;
98   gauge_t disks_missing;
99
100   fd = open (path, O_RDONLY);
101   if (fd < 0)
102   {
103     WARNING ("md: open(%s): %s", path,
104         sstrerror (errno, errbuf, sizeof (errbuf)));
105     return;
106   }
107
108   if (fstat (fd, &st) < 0)
109   {
110     WARNING ("md: Unable to fstat file descriptor for %s: %s", path,
111         sstrerror (errno, errbuf, sizeof (errbuf)));
112     close (fd);
113     return;
114   }
115
116   if (! S_ISBLK (st.st_mode))
117   {
118     WARNING ("md: %s is no block device", path);
119     close (fd);
120     return;
121   }
122
123   if (st.st_rdev != makedev (MD_MAJOR, minor))
124   {
125     WARNING ("md: Major/minor of %s are %i:%i, should be %i:%i",
126         path, (int)major(st.st_rdev), (int)minor(st.st_rdev),
127         (int)MD_MAJOR, minor);
128     close (fd);
129     return;
130   }
131
132   /* Retrieve md information */
133   if (ioctl (fd, GET_ARRAY_INFO, &array) < 0) {
134     WARNING ("md: Unable to retrieve array info from %s: %s", path,
135         sstrerror (errno, errbuf, sizeof (errbuf)));
136     close (fd);
137     return;
138   }
139
140   close (fd);
141
142   /*
143    * The mdu_array_info_t structure contains numbers of disks in the array.
144    * However, disks are accounted for more than once:
145    *
146    * active:  Number of active (in sync) disks.
147    * spare:   Number of stand-by disks.
148    * working: Number of working disks. (active + sync)
149    * failed:  Number of failed disks.
150    * nr:      Number of physically present disks. (working + failed)
151    * raid:    Number of disks in the RAID. This may be larger than "nr" if
152    *          disks are missing and smaller than "nr" when spare disks are
153    *          around.
154    */
155   md_submit (minor, "active",  (gauge_t) array.active_disks);
156   md_submit (minor, "failed",  (gauge_t) array.failed_disks);
157   md_submit (minor, "spare",   (gauge_t) array.spare_disks);
158
159   disks_missing = 0.0;
160   if (array.raid_disks > array.nr_disks)
161     disks_missing = (gauge_t) (array.raid_disks - array.nr_disks);
162   md_submit (minor, "missing", disks_missing);
163 } /* void md_process */
164
165 static int md_read (void)
166 {
167   FILE *fh;
168   char buffer[1024];
169
170   fh = fopen (PROC_DISKSTATS, "r");
171   if (fh == NULL) {
172     char errbuf[1024];
173     WARNING ("md: Unable to open %s: %s",
174         PROC_DISKSTATS ,
175         sstrerror (errno, errbuf, sizeof (errbuf)));
176     return (-1);
177   }
178
179   /* Iterate md devices */
180   while (fgets (buffer, sizeof (buffer), fh) != NULL)
181   {
182     char path[PATH_MAX];
183     char *fields[4];
184     char *name;
185     int major, minor;
186
187     /* Extract interesting fields */
188     if (strsplit (buffer, fields, STATIC_ARRAY_SIZE(fields)) < 3)
189       continue;
190
191     major = atoi (fields[0]);
192
193     if (major != MD_MAJOR)
194       continue;
195
196     minor = atoi (fields[1]);
197     name = fields[2];
198
199     if (ignorelist_match (ignorelist, name))
200       continue;
201
202     /* FIXME: Don't hardcode path. Walk /dev collecting major,
203      * minor and name, then use lookup table to find device.
204      * Alternatively create a temporary device file with correct
205      * major/minor, but that again can be tricky if the filesystem
206      * with the device file is mounted using the "nodev" option.
207      */
208     ssnprintf (path, sizeof (path), "%s/%s", DEV_DIR, name);
209
210     md_process (minor, path);
211   }
212
213   fclose (fh);
214
215   return (0);
216 } /* int md_read */
217
218 void module_register (void)
219 {
220   plugin_register_config ("md", md_config, config_keys, config_keys_num);
221   plugin_register_read ("md", md_read);
222 } /* void module_register */