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