Merge remote-tracking branch 'github/pr/1956'
[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 static const char *config_keys[] =
37 {
38   "Disk",
39   "IgnoreSelected",
40   "IgnoreSleepMode",
41   "UseSerial"
42 };
43
44 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
45
46 static ignorelist_t *ignorelist = NULL;
47 static int ignore_sleep_mode = 0;
48 static int use_serial = 0;
49
50 static int smart_config (const char *key, const char *value)
51 {
52   if (ignorelist == NULL)
53     ignorelist = ignorelist_create (/* invert = */ 1);
54   if (ignorelist == NULL)
55     return (1);
56
57   if (strcasecmp ("Disk", key) == 0)
58   {
59     ignorelist_add (ignorelist, value);
60   }
61   else if (strcasecmp ("IgnoreSelected", key) == 0)
62   {
63     int invert = 1;
64     if (IS_TRUE (value))
65       invert = 0;
66     ignorelist_set_invert (ignorelist, invert);
67   }
68   else if (strcasecmp ("IgnoreSleepMode", key) == 0)
69   {
70     if (IS_TRUE (value))
71       ignore_sleep_mode = 1;
72   }
73   else if (strcasecmp ("UseSerial", key) == 0)
74   {
75     if (IS_TRUE (value))
76       use_serial = 1;
77   }
78   else
79   {
80     return (-1);
81   }
82
83   return (0);
84 } /* int smart_config */
85
86 static void smart_submit (const char *dev, const char *type,
87                 const char *type_inst, double value)
88 {
89         value_list_t vl = VALUE_LIST_INIT;
90
91         vl.values = &(value_t) { .gauge = value };
92         vl.values_len = 1;
93         sstrncpy (vl.plugin, "smart", sizeof (vl.plugin));
94         sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
95         sstrncpy (vl.type, type, sizeof (vl.type));
96         sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
97
98         plugin_dispatch_values (&vl);
99 }
100
101 static void smart_handle_disk_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
102                                         void* userdata)
103 {
104   const char *dev = userdata;
105
106   if (!a->current_value_valid || !a->worst_value_valid)
107     return;
108
109   value_list_t vl = VALUE_LIST_INIT;
110   value_t values[] = {
111     { .gauge = a->current_value },
112     { .gauge = a->worst_value },
113     { .gauge = a->threshold_valid ? a->threshold : 0 },
114     { .gauge = a->pretty_value },
115   };
116
117   vl.values = values;
118   vl.values_len = STATIC_ARRAY_SIZE (values);
119   sstrncpy (vl.plugin, "smart", sizeof (vl.plugin));
120   sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
121   sstrncpy (vl.type, "smart_attribute", sizeof (vl.type));
122   sstrncpy (vl.type_instance, a->name, sizeof (vl.type_instance));
123
124   plugin_dispatch_values (&vl);
125
126   if (a->threshold_valid && a->current_value <= a->threshold)
127   {
128     notification_t notif = { NOTIF_WARNING,
129                              cdtime (),
130                              "",
131                              "",
132                              "smart", "",
133                              "smart_attribute",
134                              "",
135                              NULL };
136     sstrncpy (notif.host, hostname_g, sizeof (notif.host));
137     sstrncpy (notif.plugin_instance, dev, sizeof (notif.plugin_instance));
138     sstrncpy (notif.type_instance, a->name, sizeof (notif.type_instance));
139     ssnprintf (notif.message, sizeof (notif.message),
140                "attribute %s is below allowed threshold (%d < %d)",
141                a->name, a->current_value, a->threshold);
142     plugin_dispatch_notification (&notif);
143   }
144 }
145
146 static void smart_handle_disk (const char *dev, const char *serial)
147 {
148   SkDisk *d = NULL;
149   SkBool awake = FALSE;
150   SkBool available = FALSE;
151   const char *shortname;
152   const SkSmartParsedData *spd;
153   uint64_t poweron, powercycles, badsectors, temperature;
154
155   if (use_serial && serial)
156   {
157     shortname = serial;
158   }
159   else
160   {
161     shortname = strrchr(dev, '/');
162     if (!shortname) return;
163     shortname++;
164   }
165   if (ignorelist_match (ignorelist, shortname) != 0) {
166     DEBUG ("smart plugin: ignoring %s.", dev);
167     return;
168   }
169
170   DEBUG ("smart plugin: checking SMART status of %s.",
171          dev);
172
173   if (sk_disk_open (dev, &d) < 0)
174   {
175     ERROR ("smart plugin: unable to open %s.", dev);
176     return;
177   }
178   if (sk_disk_identify_is_available (d, &available) < 0 || !available)
179   {
180     DEBUG ("smart plugin: disk %s cannot be identified.", dev);
181     goto end;
182   }
183   if (sk_disk_smart_is_available (d, &available) < 0 || !available)
184   {
185     DEBUG ("smart plugin: disk %s has no SMART support.", dev);
186     goto end;
187   }
188   if (!ignore_sleep_mode)
189   {
190     if (sk_disk_check_sleep_mode (d, &awake) < 0 || !awake)
191     {
192       DEBUG ("smart plugin: disk %s is sleeping.", dev);
193       goto end;
194     }
195   }
196   if (sk_disk_smart_read_data (d) < 0)
197   {
198     ERROR ("smart plugin: unable to get SMART data for disk %s.", dev);
199     goto end;
200   }
201   if (sk_disk_smart_parse (d, &spd) < 0)
202   {
203     ERROR ("smart plugin: unable to parse SMART data for disk %s.", dev);
204     goto end;
205   }
206
207   /* Get some specific values */
208   if (sk_disk_smart_get_power_on (d, &poweron) < 0)
209   {
210     WARNING ("smart plugin: unable to get milliseconds since power on for %s.",
211              dev);
212   }
213   else
214     smart_submit (shortname, "smart_poweron", "", poweron / 1000.);
215
216   if (sk_disk_smart_get_power_cycle (d, &powercycles) < 0)
217   {
218     WARNING ("smart plugin: unable to get number of power cycles for %s.",
219              dev);
220   }
221   else
222     smart_submit (shortname, "smart_powercycles", "", powercycles);
223
224   if (sk_disk_smart_get_bad (d, &badsectors) < 0)
225   {
226     WARNING ("smart plugin: unable to get number of bad sectors for %s.",
227              dev);
228   }
229   else
230     smart_submit (shortname, "smart_badsectors", "", badsectors);
231
232   if (sk_disk_smart_get_temperature (d, &temperature) < 0)
233   {
234     WARNING ("smart plugin: unable to get temperature for %s.",
235              dev);
236   }
237   else
238     smart_submit (shortname, "smart_temperature", "", temperature / 1000. - 273.15);
239
240   /* Grab all attributes */
241   if (sk_disk_smart_parse_attributes(d, smart_handle_disk_attribute,
242                                      (char *)shortname) < 0)
243   {
244     ERROR ("smart plugin: unable to handle SMART attributes for %s.",
245            dev);
246   }
247
248 end:
249   sk_disk_free(d);
250 }
251
252 static int smart_read (void)
253 {
254   struct udev *handle_udev;
255   struct udev_enumerate *enumerate;
256   struct udev_list_entry *devices, *dev_list_entry;
257   struct udev_device *dev;
258
259   /* Use udev to get a list of disks */
260   handle_udev = udev_new();
261   if (!handle_udev)
262   {
263     ERROR ("smart plugin: unable to initialize udev.");
264     return (-1);
265   }
266   enumerate = udev_enumerate_new (handle_udev);
267   udev_enumerate_add_match_subsystem (enumerate, "block");
268   udev_enumerate_add_match_property (enumerate, "DEVTYPE", "disk");
269   udev_enumerate_scan_devices (enumerate);
270   devices = udev_enumerate_get_list_entry (enumerate);
271   udev_list_entry_foreach (dev_list_entry, devices)
272   {
273     const char *path, *devpath, *serial;
274     path = udev_list_entry_get_name (dev_list_entry);
275     dev = udev_device_new_from_syspath (handle_udev, path);
276     devpath = udev_device_get_devnode (dev);
277     serial = udev_device_get_property_value (dev, "ID_SERIAL");
278
279     /* Query status with libatasmart */
280     smart_handle_disk (devpath, serial);
281     udev_device_unref (dev);
282   }
283
284   udev_enumerate_unref (enumerate);
285   udev_unref (handle_udev);
286
287   return (0);
288 } /* int smart_read */
289
290 void module_register (void)
291 {
292   plugin_register_config ("smart", smart_config,
293                           config_keys, config_keys_num);
294   plugin_register_read ("smart", smart_read);
295 } /* void module_register */