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