Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / smart.c
1 /**
2  * collectd - src/smart.c
3  * Copyright (C) 2014       Vincent Bernat
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Vincent Bernat <vbe at exoscale.ch>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31 #include "utils_ignorelist.h"
32
33 #include <atasmart.h>
34 #include <libudev.h>
35
36 #ifdef HAVE_SYS_CAPABILITY_H
37 #include <sys/capability.h>
38 #endif
39
40 static const char *config_keys[] = {"Disk", "IgnoreSelected", "IgnoreSleepMode",
41                                     "UseSerial"};
42
43 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
44
45 static ignorelist_t *ignorelist = NULL;
46 static int ignore_sleep_mode = 0;
47 static int use_serial = 0;
48
49 static int smart_config(const char *key, const char *value) {
50   if (ignorelist == NULL)
51     ignorelist = ignorelist_create(/* invert = */ 1);
52   if (ignorelist == NULL)
53     return 1;
54
55   if (strcasecmp("Disk", key) == 0) {
56     ignorelist_add(ignorelist, value);
57   } else if (strcasecmp("IgnoreSelected", key) == 0) {
58     int invert = 1;
59     if (IS_TRUE(value))
60       invert = 0;
61     ignorelist_set_invert(ignorelist, invert);
62   } else if (strcasecmp("IgnoreSleepMode", key) == 0) {
63     if (IS_TRUE(value))
64       ignore_sleep_mode = 1;
65   } else if (strcasecmp("UseSerial", key) == 0) {
66     if (IS_TRUE(value))
67       use_serial = 1;
68   } else {
69     return -1;
70   }
71
72   return 0;
73 } /* int smart_config */
74
75 static void smart_submit(const char *dev, const char *type,
76                          const char *type_inst, double value) {
77   value_list_t vl = VALUE_LIST_INIT;
78
79   vl.values = &(value_t){.gauge = value};
80   vl.values_len = 1;
81   sstrncpy(vl.plugin, "smart", sizeof(vl.plugin));
82   sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
83   sstrncpy(vl.type, type, sizeof(vl.type));
84   sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
85
86   plugin_dispatch_values(&vl);
87 }
88
89 static void handle_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
90                              void *userdata) {
91   char const *name = userdata;
92
93   if (!a->current_value_valid || !a->worst_value_valid)
94     return;
95
96   value_list_t vl = VALUE_LIST_INIT;
97   value_t values[] = {
98       {.gauge = a->current_value},
99       {.gauge = a->worst_value},
100       {.gauge = a->threshold_valid ? a->threshold : 0},
101       {.gauge = a->pretty_value},
102   };
103
104   vl.values = values;
105   vl.values_len = STATIC_ARRAY_SIZE(values);
106   sstrncpy(vl.plugin, "smart", sizeof(vl.plugin));
107   sstrncpy(vl.plugin_instance, name, sizeof(vl.plugin_instance));
108   sstrncpy(vl.type, "smart_attribute", sizeof(vl.type));
109   sstrncpy(vl.type_instance, a->name, sizeof(vl.type_instance));
110
111   plugin_dispatch_values(&vl);
112
113   if (a->threshold_valid && a->current_value <= a->threshold) {
114     notification_t notif = {NOTIF_WARNING,     cdtime(), "",  "", "smart", "",
115                             "smart_attribute", "",       NULL};
116     sstrncpy(notif.host, hostname_g, sizeof(notif.host));
117     sstrncpy(notif.plugin_instance, name, sizeof(notif.plugin_instance));
118     sstrncpy(notif.type_instance, a->name, sizeof(notif.type_instance));
119     snprintf(notif.message, sizeof(notif.message),
120              "attribute %s is below allowed threshold (%d < %d)", a->name,
121              a->current_value, a->threshold);
122     plugin_dispatch_notification(&notif);
123   }
124 }
125
126 static void smart_read_disk(SkDisk *d, char const *name) {
127   SkBool available = FALSE;
128   if (sk_disk_identify_is_available(d, &available) < 0 || !available) {
129     DEBUG("smart plugin: disk %s cannot be identified.", name);
130     return;
131   }
132   if (sk_disk_smart_is_available(d, &available) < 0 || !available) {
133     DEBUG("smart plugin: disk %s has no SMART support.", name);
134     return;
135   }
136   if (!ignore_sleep_mode) {
137     SkBool awake = FALSE;
138     if (sk_disk_check_sleep_mode(d, &awake) < 0 || !awake) {
139       DEBUG("smart plugin: disk %s is sleeping.", name);
140       return;
141     }
142   }
143   if (sk_disk_smart_read_data(d) < 0) {
144     ERROR("smart plugin: unable to get SMART data for disk %s.", name);
145     return;
146   }
147
148   if (sk_disk_smart_parse(d, &(SkSmartParsedData const *){NULL}) < 0) {
149     ERROR("smart plugin: unable to parse SMART data for disk %s.", name);
150     return;
151   }
152
153   /* Get some specific values */
154   uint64_t value;
155   if (sk_disk_smart_get_power_on(d, &value) >= 0)
156     smart_submit(name, "smart_poweron", "", ((gauge_t)value) / 1000.);
157   else
158     DEBUG("smart plugin: unable to get milliseconds since power on for %s.",
159           name);
160
161   if (sk_disk_smart_get_power_cycle(d, &value) >= 0)
162     smart_submit(name, "smart_powercycles", "", (gauge_t)value);
163   else
164     DEBUG("smart plugin: unable to get number of power cycles for %s.", name);
165
166   if (sk_disk_smart_get_bad(d, &value) >= 0)
167     smart_submit(name, "smart_badsectors", "", (gauge_t)value);
168   else
169     DEBUG("smart plugin: unable to get number of bad sectors for %s.", name);
170
171   if (sk_disk_smart_get_temperature(d, &value) >= 0)
172     smart_submit(name, "smart_temperature", "",
173                  ((gauge_t)value) / 1000. - 273.15);
174   else
175     DEBUG("smart plugin: unable to get temperature for %s.", name);
176
177   /* Grab all attributes */
178   if (sk_disk_smart_parse_attributes(d, handle_attribute, (void *)name) < 0) {
179     ERROR("smart plugin: unable to handle SMART attributes for %s.", name);
180   }
181 }
182
183 static void smart_handle_disk(const char *dev, const char *serial) {
184   SkDisk *d = NULL;
185   const char *name;
186
187   if (use_serial && serial) {
188     name = serial;
189   } else {
190     name = strrchr(dev, '/');
191     if (!name)
192       return;
193     name++;
194   }
195   if (ignorelist_match(ignorelist, name) != 0) {
196     DEBUG("smart plugin: ignoring %s.", dev);
197     return;
198   }
199
200   DEBUG("smart plugin: checking SMART status of %s.", dev);
201   if (sk_disk_open(dev, &d) < 0) {
202     ERROR("smart plugin: unable to open %s.", dev);
203     return;
204   }
205
206   smart_read_disk(d, name);
207   sk_disk_free(d);
208 }
209
210 static int smart_read(void) {
211   struct udev *handle_udev;
212   struct udev_enumerate *enumerate;
213   struct udev_list_entry *devices, *dev_list_entry;
214   struct udev_device *dev;
215
216   /* Use udev to get a list of disks */
217   handle_udev = udev_new();
218   if (!handle_udev) {
219     ERROR("smart plugin: unable to initialize udev.");
220     return -1;
221   }
222   enumerate = udev_enumerate_new(handle_udev);
223   udev_enumerate_add_match_subsystem(enumerate, "block");
224   udev_enumerate_add_match_property(enumerate, "DEVTYPE", "disk");
225   udev_enumerate_scan_devices(enumerate);
226   devices = udev_enumerate_get_list_entry(enumerate);
227   udev_list_entry_foreach(dev_list_entry, devices) {
228     const char *path, *devpath, *serial;
229     path = udev_list_entry_get_name(dev_list_entry);
230     dev = udev_device_new_from_syspath(handle_udev, path);
231     devpath = udev_device_get_devnode(dev);
232     serial = udev_device_get_property_value(dev, "ID_SERIAL");
233
234     /* Query status with libatasmart */
235     smart_handle_disk(devpath, serial);
236     udev_device_unref(dev);
237   }
238
239   udev_enumerate_unref(enumerate);
240   udev_unref(handle_udev);
241
242   return 0;
243 } /* int smart_read */
244
245 static int smart_init(void) {
246 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_SYS_RAWIO)
247   if (check_capability(CAP_SYS_RAWIO) != 0) {
248     if (getuid() == 0)
249       WARNING("smart plugin: Running collectd as root, but the "
250               "CAP_SYS_RAWIO capability is missing. The plugin's read "
251               "function will probably fail. Is your init system dropping "
252               "capabilities?");
253     else
254       WARNING("smart plugin: collectd doesn't have the CAP_SYS_RAWIO "
255               "capability. If you don't want to run collectd as root, try "
256               "running \"setcap cap_sys_rawio=ep\" on the collectd binary.");
257   }
258 #endif
259   return 0;
260 } /* int smart_init */
261
262 void module_register(void) {
263   plugin_register_config("smart", smart_config, config_keys, config_keys_num);
264   plugin_register_init("smart", smart_init);
265   plugin_register_read("smart", smart_read);
266 } /* void module_register */