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