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 "plugin.h"
25 #include "utils/common/common.h"
26 #include "utils/ignorelist/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[] = {"Device", "IgnoreSelected"};
41 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
42
43 static ignorelist_t *ignorelist;
44
md_config(const char * key,const char * value)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)
49 return 1;
50
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);
55 } else {
56 return -1;
57 }
58
59 return 0;
60 }
61
md_submit(const int minor,const char * type_instance,gauge_t value)62 static void md_submit(const int minor, const char *type_instance,
63 gauge_t value) {
64 value_list_t vl = VALUE_LIST_INIT;
65
66 vl.values = &(value_t){.gauge = value};
67 vl.values_len = 1;
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));
72
73 plugin_dispatch_values(&vl);
74 } /* void md_submit */
75
md_process(const int minor,const char * path)76 static void md_process(const int minor, const char *path) {
77 int fd;
78 struct stat st;
79 mdu_array_info_t array;
80 gauge_t disks_missing;
81
82 fd = open(path, O_RDONLY);
83 if (fd < 0) {
84 WARNING("md: open(%s): %s", path, STRERRNO);
85 return;
86 }
87
88 if (fstat(fd, &st) < 0) {
89 WARNING("md: Unable to fstat file descriptor for %s: %s", path, STRERRNO);
90 close(fd);
91 return;
92 }
93
94 if (!S_ISBLK(st.st_mode)) {
95 WARNING("md: %s is no block device", path);
96 close(fd);
97 return;
98 }
99
100 if (st.st_rdev != makedev(MD_MAJOR, minor)) {
101 WARNING("md: Major/minor of %s are %i:%i, should be %i:%i", path,
102 (int)major(st.st_rdev), (int)minor(st.st_rdev), (int)MD_MAJOR,
103 minor);
104 close(fd);
105 return;
106 }
107
108 /* Retrieve md information */
109 if (ioctl(fd, GET_ARRAY_INFO, &array) < 0) {
110 WARNING("md: Unable to retrieve array info from %s: %s", path, STRERRNO);
111 close(fd);
112 return;
113 }
114
115 close(fd);
116
117 /*
118 * The mdu_array_info_t structure contains numbers of disks in the array.
119 * However, disks are accounted for more than once:
120 *
121 * active: Number of active (in sync) disks.
122 * spare: Number of stand-by disks.
123 * working: Number of working disks. (active + sync)
124 * failed: Number of failed disks.
125 * nr: Number of physically present disks. (working + failed)
126 * raid: Number of disks in the RAID. This may be larger than "nr" if
127 * disks are missing and smaller than "nr" when spare disks are
128 * around.
129 */
130 md_submit(minor, "active", (gauge_t)array.active_disks);
131 md_submit(minor, "failed", (gauge_t)array.failed_disks);
132 md_submit(minor, "spare", (gauge_t)array.spare_disks);
133
134 disks_missing = 0.0;
135 if (array.raid_disks > array.nr_disks)
136 disks_missing = (gauge_t)(array.raid_disks - array.nr_disks);
137 md_submit(minor, "missing", disks_missing);
138 } /* void md_process */
139
md_read(void)140 static int md_read(void) {
141 FILE *fh;
142 char buffer[1024];
143
144 fh = fopen(PROC_DISKSTATS, "r");
145 if (fh == NULL) {
146 WARNING("md: Unable to open %s: %s", PROC_DISKSTATS, STRERRNO);
147 return -1;
148 }
149
150 /* Iterate md devices */
151 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
152 char path[PATH_MAX];
153 char *fields[4];
154 char *name;
155 int major, minor;
156
157 /* Extract interesting fields */
158 if (strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields)) < 3)
159 continue;
160
161 major = atoi(fields[0]);
162
163 if (major != MD_MAJOR)
164 continue;
165
166 minor = atoi(fields[1]);
167 name = fields[2];
168
169 if (ignorelist_match(ignorelist, name))
170 continue;
171
172 /* FIXME: Don't hardcode path. Walk /dev collecting major,
173 * minor and name, then use lookup table to find device.
174 * Alternatively create a temporary device file with correct
175 * major/minor, but that again can be tricky if the filesystem
176 * with the device file is mounted using the "nodev" option.
177 */
178 snprintf(path, sizeof(path), "%s/%s", DEV_DIR, name);
179
180 md_process(minor, path);
181 }
182
183 fclose(fh);
184
185 return 0;
186 } /* int md_read */
187
module_register(void)188 void module_register(void) {
189 plugin_register_config("md", md_config, config_keys, config_keys_num);
190 plugin_register_read("md", md_read);
191 } /* void module_register */
192