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