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