3 * Copyright (C) 2010,2011 Michael Hanselmann
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.
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.
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
25 #include "utils_ignorelist.h"
27 #include <sys/ioctl.h>
29 #include <linux/major.h>
30 #include <linux/raid/md_u.h>
32 #ifdef HAVE_SYS_SYSMACROS_H
33 #include <sys/sysmacros.h>
36 #define PROC_DISKSTATS "/proc/diskstats"
37 #define DEV_DIR "/dev"
39 static const char *config_keys[] =
44 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
46 static ignorelist_t *ignorelist = NULL;
48 static int md_config (const char *key, const char *value)
50 if (ignorelist == NULL)
51 ignorelist = ignorelist_create (/* invert = */ 1);
52 if (ignorelist == NULL)
55 if (strcasecmp (key, "Device") == 0)
57 ignorelist_add (ignorelist, value);
59 else if (strcasecmp (key, "IgnoreSelected") == 0)
61 ignorelist_set_invert (ignorelist, IS_TRUE (value) ? 0 : 1);
71 static void md_submit (const int minor, const char *type_instance,
75 value_list_t vl = VALUE_LIST_INIT;
77 values[0].gauge = value;
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),
85 sstrncpy (vl.type, "md_disks", sizeof (vl.type));
86 sstrncpy (vl.type_instance, type_instance,
87 sizeof (vl.type_instance));
89 plugin_dispatch_values (&vl);
90 } /* void md_submit */
92 static void md_process (const int minor, const char *path)
97 mdu_array_info_t array;
98 gauge_t disks_missing;
100 fd = open (path, O_RDONLY);
103 WARNING ("md: open(%s): %s", path,
104 sstrerror (errno, errbuf, sizeof (errbuf)));
108 if (fstat (fd, &st) < 0)
110 WARNING ("md: Unable to fstat file descriptor for %s: %s", path,
111 sstrerror (errno, errbuf, sizeof (errbuf)));
116 if (! S_ISBLK (st.st_mode))
118 WARNING ("md: %s is no block device", path);
123 if (st.st_rdev != makedev (MD_MAJOR, minor))
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);
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)));
143 * The mdu_array_info_t structure contains numbers of disks in the array.
144 * However, disks are accounted for more than once:
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
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);
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 */
165 static int md_read (void)
170 fh = fopen (PROC_DISKSTATS, "r");
173 WARNING ("md: Unable to open %s: %s",
175 sstrerror (errno, errbuf, sizeof (errbuf)));
179 /* Iterate md devices */
180 while (fgets (buffer, sizeof (buffer), fh) != NULL)
187 /* Extract interesting fields */
188 if (strsplit (buffer, fields, STATIC_ARRAY_SIZE(fields)) < 3)
191 major = atoi (fields[0]);
193 if (major != MD_MAJOR)
196 minor = atoi (fields[1]);
199 if (ignorelist_match (ignorelist, name))
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.
208 ssnprintf (path, sizeof (path), "%s/%s", DEV_DIR, name);
210 md_process (minor, path);
218 void module_register (void)
220 plugin_register_config ("md", md_config, config_keys, config_keys_num);
221 plugin_register_read ("md", md_read);
222 } /* void module_register */