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