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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "utils_ignorelist.h"
27 #include <sys/ioctl.h>
29 #include <linux/major.h>
30 #include <linux/raid/md_u.h>
32 #define PROC_DISKSTATS "/proc/diskstats"
33 #define DEV_DIR "/dev"
35 static const char *config_keys[] =
36 {
37 "Device",
38 "IgnoreSelected"
39 };
40 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
42 static ignorelist_t *ignorelist = NULL;
44 static int md_config (const char *key, const char *value)
45 {
46 if (ignorelist == NULL)
47 ignorelist = ignorelist_create (/* invert = */ 1);
48 if (ignorelist == NULL)
49 return (1);
51 if (strcasecmp (key, "Device") == 0)
52 {
53 ignorelist_add (ignorelist, value);
54 }
55 else if (strcasecmp (key, "IgnoreSelected") == 0)
56 {
57 ignorelist_set_invert (ignorelist, IS_TRUE (value) ? 0 : 1);
58 }
59 else
60 {
61 return (-1);
62 }
64 return (0);
65 }
67 static void md_submit (const int minor, const char *type_instance,
68 gauge_t value)
69 {
70 value_t values[1];
71 value_list_t vl = VALUE_LIST_INIT;
73 values[0].gauge = value;
75 vl.values = values;
76 vl.values_len = 1;
77 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
78 sstrncpy (vl.plugin, "md", sizeof (vl.plugin));
79 ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
80 "%i", minor);
81 sstrncpy (vl.type, "md_disks", sizeof (vl.type));
82 sstrncpy (vl.type_instance, type_instance,
83 sizeof (vl.type_instance));
85 plugin_dispatch_values (&vl);
86 } /* void md_submit */
88 static void md_process (const int minor, const char *path)
89 {
90 char errbuf[1024];
91 int fd;
92 struct stat st;
93 mdu_array_info_t array;
95 fd = open (path, O_RDONLY);
96 if (fd < 0)
97 {
98 WARNING ("md: open(%s): %s", path,
99 sstrerror (errno, errbuf, sizeof (errbuf)));
100 return;
101 }
103 if (fstat (fd, &st) < 0)
104 {
105 WARNING ("md: Unable to fstat file descriptor for %s: %s", path,
106 sstrerror (errno, errbuf, sizeof (errbuf)));
107 close (fd);
108 return;
109 }
111 if (! S_ISBLK (st.st_mode))
112 {
113 WARNING ("md: %s is no block device", path);
114 close (fd);
115 return;
116 }
118 if (st.st_rdev != makedev (MD_MAJOR, minor))
119 {
120 WARNING ("md: Major/minor of %s are %i:%i, should be %i:%i",
121 path, (int)major(st.st_rdev), (int)minor(st.st_rdev),
122 (int)MD_MAJOR, minor);
123 close (fd);
124 return;
125 }
127 /* Retrieve md information */
128 if (ioctl (fd, GET_ARRAY_INFO, &array) < 0) {
129 WARNING ("md: Unable to retrieve array info from %s: %s", path,
130 sstrerror (errno, errbuf, sizeof (errbuf)));
131 close (fd);
132 return;
133 }
135 close (fd);
137 md_submit (minor, "number", (gauge_t) array.nr_disks);
138 md_submit (minor, "raid", (gauge_t) array.raid_disks);
139 md_submit (minor, "active", (gauge_t) array.active_disks);
140 md_submit (minor, "working", (gauge_t) array.working_disks);
141 md_submit (minor, "failed", (gauge_t) array.failed_disks);
142 md_submit (minor, "spare", (gauge_t) array.spare_disks);
144 return;
145 } /* void md_process */
147 static int md_read (void)
148 {
149 FILE *fh;
150 char buffer[1024];
152 fh = fopen (PROC_DISKSTATS, "r");
153 if (fh == NULL) {
154 char errbuf[1024];
155 WARNING ("md: Unable to open %s: %s",
156 PROC_DISKSTATS ,
157 sstrerror (errno, errbuf, sizeof (errbuf)));
158 return (-1);
159 }
161 /* Iterate md devices */
162 while (fgets (buffer, sizeof (buffer), fh) != NULL)
163 {
164 char path[PATH_MAX];
165 char *fields[4];
166 char *name;
167 int major, minor;
169 /* Extract interesting fields */
170 if (strsplit (buffer, fields, STATIC_ARRAY_SIZE(fields)) < 3)
171 continue;
173 major = atoi (fields[0]);
175 if (major != MD_MAJOR)
176 continue;
178 minor = atoi (fields[1]);
179 name = fields[2];
181 if (ignorelist_match (ignorelist, name))
182 continue;
184 /* FIXME: Don't hardcode path. Walk /dev collecting major,
185 * minor and name, then use lookup table to find device.
186 * Alternatively create a temporary device file with correct
187 * major/minor, but that again can be tricky if the filesystem
188 * with the device file is mounted using the "nodev" option.
189 */
190 ssnprintf (path, sizeof (path), "%s/%s", DEV_DIR, name);
192 md_process (minor, path);
193 }
195 fclose (fh);
197 return (0);
198 } /* int md_read */
200 void module_register (void)
201 {
202 plugin_register_config ("md", md_config, config_keys, config_keys_num);
203 plugin_register_read ("md", md_read);
204 } /* void module_register */