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
26 #include "utils_ignorelist.h"
28 #include <sys/ioctl.h>
30 #include <linux/major.h>
31 #include <linux/raid/md_u.h>
33 #ifdef HAVE_SYS_SYSMACROS_H
34 #include <sys/sysmacros.h>
37 #define PROC_DISKSTATS "/proc/diskstats"
38 #define DEV_DIR "/dev"
40 static const char *config_keys[] = {"Device", "IgnoreSelected"};
41 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
43 static ignorelist_t *ignorelist = NULL;
45 static int md_config(const char *key, const char *value) {
46 if (ignorelist == NULL)
47 ignorelist = ignorelist_create(/* invert = */ 1);
48 if (ignorelist == NULL)
51 if (strcasecmp(key, "Device") == 0) {
52 ignorelist_add(ignorelist, value);
53 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
54 ignorelist_set_invert(ignorelist, IS_TRUE(value) ? 0 : 1);
62 static void md_submit(const int minor, const char *type_instance,
64 value_list_t vl = VALUE_LIST_INIT;
66 vl.values = &(value_t){.gauge = value};
68 sstrncpy(vl.plugin, "md", sizeof(vl.plugin));
69 snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", minor);
70 sstrncpy(vl.type, "md_disks", sizeof(vl.type));
71 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
73 plugin_dispatch_values(&vl);
74 } /* void md_submit */
76 static void md_process(const int minor, const char *path) {
80 mdu_array_info_t array;
81 gauge_t disks_missing;
83 fd = open(path, O_RDONLY);
85 WARNING("md: open(%s): %s", path, sstrerror(errno, errbuf, sizeof(errbuf)));
89 if (fstat(fd, &st) < 0) {
90 WARNING("md: Unable to fstat file descriptor for %s: %s", path,
91 sstrerror(errno, errbuf, sizeof(errbuf)));
96 if (!S_ISBLK(st.st_mode)) {
97 WARNING("md: %s is no block device", path);
102 if (st.st_rdev != makedev(MD_MAJOR, minor)) {
103 WARNING("md: Major/minor of %s are %i:%i, should be %i:%i", path,
104 (int)major(st.st_rdev), (int)minor(st.st_rdev), (int)MD_MAJOR,
110 /* Retrieve md information */
111 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
112 WARNING("md: Unable to retrieve array info from %s: %s", path,
113 sstrerror(errno, errbuf, sizeof(errbuf)));
121 * The mdu_array_info_t structure contains numbers of disks in the array.
122 * However, disks are accounted for more than once:
124 * active: Number of active (in sync) disks.
125 * spare: Number of stand-by disks.
126 * working: Number of working disks. (active + sync)
127 * failed: Number of failed disks.
128 * nr: Number of physically present disks. (working + failed)
129 * raid: Number of disks in the RAID. This may be larger than "nr" if
130 * disks are missing and smaller than "nr" when spare disks are
133 md_submit(minor, "active", (gauge_t)array.active_disks);
134 md_submit(minor, "failed", (gauge_t)array.failed_disks);
135 md_submit(minor, "spare", (gauge_t)array.spare_disks);
138 if (array.raid_disks > array.nr_disks)
139 disks_missing = (gauge_t)(array.raid_disks - array.nr_disks);
140 md_submit(minor, "missing", disks_missing);
141 } /* void md_process */
143 static int md_read(void) {
147 fh = fopen(PROC_DISKSTATS, "r");
150 WARNING("md: Unable to open %s: %s", PROC_DISKSTATS,
151 sstrerror(errno, errbuf, sizeof(errbuf)));
155 /* Iterate md devices */
156 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
162 /* Extract interesting fields */
163 if (strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields)) < 3)
166 major = atoi(fields[0]);
168 if (major != MD_MAJOR)
171 minor = atoi(fields[1]);
174 if (ignorelist_match(ignorelist, name))
177 /* FIXME: Don't hardcode path. Walk /dev collecting major,
178 * minor and name, then use lookup table to find device.
179 * Alternatively create a temporary device file with correct
180 * major/minor, but that again can be tricky if the filesystem
181 * with the device file is mounted using the "nodev" option.
183 snprintf(path, sizeof(path), "%s/%s", DEV_DIR, name);
185 md_process(minor, path);
193 void module_register(void) {
194 plugin_register_config("md", md_config, config_keys, config_keys_num);
195 plugin_register_read("md", md_read);
196 } /* void module_register */