Merge pull request #2151 from mfournier/check_capability-rewrite-fix
[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_t values[1];
78   value_list_t vl = VALUE_LIST_INIT;
79
80   values[0].gauge = value;
81
82   vl.values = values;
83   vl.values_len = 1;
84   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
85   sstrncpy(vl.plugin, "smart", sizeof(vl.plugin));
86   sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
87   sstrncpy(vl.type, type, sizeof(vl.type));
88   sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
89
90   plugin_dispatch_values(&vl);
91 }
92
93 static void handle_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
94                              void *userdata) {
95   char const *name = userdata;
96   value_t values[4];
97   value_list_t vl = VALUE_LIST_INIT;
98
99   if (!a->current_value_valid || !a->worst_value_valid)
100     return;
101   values[0].gauge = a->current_value;
102   values[1].gauge = a->worst_value;
103   values[2].gauge = a->threshold_valid ? a->threshold : 0;
104   values[3].gauge = a->pretty_value;
105
106   vl.values = values;
107   vl.values_len = 4;
108   sstrncpy(vl.host, hostname_g, sizeof(vl.host));
109   sstrncpy(vl.plugin, "smart", sizeof(vl.plugin));
110   sstrncpy(vl.plugin_instance, name, sizeof(vl.plugin_instance));
111   sstrncpy(vl.type, "smart_attribute", sizeof(vl.type));
112   sstrncpy(vl.type_instance, a->name, sizeof(vl.type_instance));
113
114   plugin_dispatch_values(&vl);
115
116   if (a->threshold_valid && a->current_value <= a->threshold) {
117     notification_t notif = {NOTIF_WARNING,     cdtime(), "",  "", "smart", "",
118                             "smart_attribute", "",       NULL};
119     sstrncpy(notif.host, hostname_g, sizeof(notif.host));
120     sstrncpy(notif.plugin_instance, name, sizeof(notif.plugin_instance));
121     sstrncpy(notif.type_instance, a->name, sizeof(notif.type_instance));
122     ssnprintf(notif.message, sizeof(notif.message),
123               "attribute %s is below allowed threshold (%d < %d)", a->name,
124               a->current_value, a->threshold);
125     plugin_dispatch_notification(&notif);
126   }
127 }
128
129 static void smart_read_disk(SkDisk *d, char const *name) {
130   SkBool available = FALSE;
131   if (sk_disk_identify_is_available(d, &available) < 0 || !available) {
132     DEBUG("smart plugin: disk %s cannot be identified.", name);
133     return;
134   }
135   if (sk_disk_smart_is_available(d, &available) < 0 || !available) {
136     DEBUG("smart plugin: disk %s has no SMART support.", name);
137     return;
138   }
139   if (!ignore_sleep_mode) {
140     SkBool awake = FALSE;
141     if (sk_disk_check_sleep_mode(d, &awake) < 0 || !awake) {
142       DEBUG("smart plugin: disk %s is sleeping.", name);
143       return;
144     }
145   }
146   if (sk_disk_smart_read_data(d) < 0) {
147     ERROR("smart plugin: unable to get SMART data for disk %s.", name);
148     return;
149   }
150
151   if (sk_disk_smart_parse(d, &(SkSmartParsedData const *){NULL}) < 0) {
152     ERROR("smart plugin: unable to parse SMART data for disk %s.", name);
153     return;
154   }
155
156   /* Get some specific values */
157   uint64_t value;
158   if (sk_disk_smart_get_power_on(d, &value) >= 0)
159     smart_submit(name, "smart_poweron", "", ((gauge_t)value) / 1000.);
160   else
161     DEBUG("smart plugin: unable to get milliseconds since power on for %s.",
162           name);
163
164   if (sk_disk_smart_get_power_cycle(d, &value) >= 0)
165     smart_submit(name, "smart_powercycles", "", (gauge_t)value);
166   else
167     DEBUG("smart plugin: unable to get number of power cycles for %s.", name);
168
169   if (sk_disk_smart_get_bad(d, &value) >= 0)
170     smart_submit(name, "smart_badsectors", "", (gauge_t)value);
171   else
172     DEBUG("smart plugin: unable to get number of bad sectors for %s.", name);
173
174   if (sk_disk_smart_get_temperature(d, &value) >= 0)
175     smart_submit(name, "smart_temperature", "",
176                  ((gauge_t)value) / 1000. - 273.15);
177   else
178     DEBUG("smart plugin: unable to get temperature for %s.", name);
179
180   /* Grab all attributes */
181   if (sk_disk_smart_parse_attributes(d, handle_attribute, (void *)name) < 0) {
182     ERROR("smart plugin: unable to handle SMART attributes for %s.", name);
183   }
184 }
185
186 static void smart_handle_disk(const char *dev, const char *serial) {
187   SkDisk *d = NULL;
188   const char *name;
189
190   if (use_serial && serial) {
191     name = serial;
192   } else {
193     name = strrchr(dev, '/');
194     if (!name)
195       return;
196     name++;
197   }
198   if (ignorelist_match(ignorelist, name) != 0) {
199     DEBUG("smart plugin: ignoring %s.", dev);
200     return;
201   }
202
203   DEBUG("smart plugin: checking SMART status of %s.", dev);
204   if (sk_disk_open(dev, &d) < 0) {
205     ERROR("smart plugin: unable to open %s.", dev);
206     return;
207   }
208
209   smart_read_disk(d, name);
210   sk_disk_free(d);
211 }
212
213 static int smart_read(void) {
214   struct udev *handle_udev;
215   struct udev_enumerate *enumerate;
216   struct udev_list_entry *devices, *dev_list_entry;
217   struct udev_device *dev;
218
219   /* Use udev to get a list of disks */
220   handle_udev = udev_new();
221   if (!handle_udev) {
222     ERROR("smart plugin: unable to initialize udev.");
223     return (-1);
224   }
225   enumerate = udev_enumerate_new(handle_udev);
226   udev_enumerate_add_match_subsystem(enumerate, "block");
227   udev_enumerate_add_match_property(enumerate, "DEVTYPE", "disk");
228   udev_enumerate_scan_devices(enumerate);
229   devices = udev_enumerate_get_list_entry(enumerate);
230   udev_list_entry_foreach(dev_list_entry, devices) {
231     const char *path, *devpath, *serial;
232     path = udev_list_entry_get_name(dev_list_entry);
233     dev = udev_device_new_from_syspath(handle_udev, path);
234     devpath = udev_device_get_devnode(dev);
235     serial = udev_device_get_property_value(dev, "ID_SERIAL");
236
237     /* Query status with libatasmart */
238     smart_handle_disk(devpath, serial);
239     udev_device_unref(dev);
240   }
241
242   udev_enumerate_unref(enumerate);
243   udev_unref(handle_udev);
244
245   return (0);
246 } /* int smart_read */
247
248 static int smart_init(void) {
249 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_SYS_RAWIO)
250   if (check_capability(CAP_SYS_RAWIO) != 0) {
251     if (getuid() == 0)
252       WARNING("smart plugin: Running collectd as root, but the "
253               "CAP_SYS_RAWIO capability is missing. The plugin's read "
254               "function will probably fail. Is your init system dropping "
255               "capabilities?");
256     else
257       WARNING("smart plugin: collectd doesn't have the CAP_SYS_RAWIO "
258               "capability. If you don't want to run collectd as root, try "
259               "running \"setcap cap_sys_rawio=ep\" on the collectd binary.");
260   }
261 #endif
262   return (0);
263 } /* int smart_init */
264
265 void module_register(void) {
266   plugin_register_config("smart", smart_config, config_keys, config_keys_num);
267   plugin_register_init("smart", smart_init);
268   plugin_register_read("smart", smart_read);
269 } /* void module_register */