2 * collectd - src/smart.c
3 * Copyright (C) 2014 Vincent Bernat
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Vincent Bernat <vbe at exoscale.ch>
31 #include "utils_ignorelist.h"
36 static const char *config_keys[] =
44 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
46 static ignorelist_t *ignorelist = NULL;
47 static int ignore_sleep_mode = 0;
48 static int use_serial = 0;
50 static int smart_config (const char *key, const char *value)
52 if (ignorelist == NULL)
53 ignorelist = ignorelist_create (/* invert = */ 1);
54 if (ignorelist == NULL)
57 if (strcasecmp ("Disk", key) == 0)
59 ignorelist_add (ignorelist, value);
61 else if (strcasecmp ("IgnoreSelected", key) == 0)
66 ignorelist_set_invert (ignorelist, invert);
68 else if (strcasecmp ("IgnoreSleepMode", key) == 0)
71 ignore_sleep_mode = 1;
73 else if (strcasecmp ("UseSerial", key) == 0)
84 } /* int smart_config */
86 static void smart_submit (const char *dev, const char *type,
87 const char *type_inst, double value)
89 value_list_t vl = VALUE_LIST_INIT;
91 vl.values = &(value_t) { .gauge = value };
93 sstrncpy (vl.plugin, "smart", sizeof (vl.plugin));
94 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
95 sstrncpy (vl.type, type, sizeof (vl.type));
96 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
98 plugin_dispatch_values (&vl);
101 static void smart_handle_disk_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
104 const char *dev = userdata;
106 if (!a->current_value_valid || !a->worst_value_valid)
109 value_list_t vl = VALUE_LIST_INIT;
111 { .gauge = a->current_value },
112 { .gauge = a->worst_value },
113 { .gauge = a->threshold_valid ? a->threshold : 0 },
114 { .gauge = a->pretty_value },
118 vl.values_len = STATIC_ARRAY_SIZE (values);
119 sstrncpy (vl.plugin, "smart", sizeof (vl.plugin));
120 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
121 sstrncpy (vl.type, "smart_attribute", sizeof (vl.type));
122 sstrncpy (vl.type_instance, a->name, sizeof (vl.type_instance));
124 plugin_dispatch_values (&vl);
126 if (a->threshold_valid && a->current_value <= a->threshold)
128 notification_t notif = { NOTIF_WARNING,
136 sstrncpy (notif.host, hostname_g, sizeof (notif.host));
137 sstrncpy (notif.plugin_instance, dev, sizeof (notif.plugin_instance));
138 sstrncpy (notif.type_instance, a->name, sizeof (notif.type_instance));
139 ssnprintf (notif.message, sizeof (notif.message),
140 "attribute %s is below allowed threshold (%d < %d)",
141 a->name, a->current_value, a->threshold);
142 plugin_dispatch_notification (¬if);
146 static void smart_handle_disk (const char *dev, const char *serial)
149 SkBool awake = FALSE;
150 SkBool available = FALSE;
151 const char *shortname;
152 const SkSmartParsedData *spd;
153 uint64_t poweron, powercycles, badsectors, temperature;
155 if (use_serial && serial)
161 shortname = strrchr(dev, '/');
162 if (!shortname) return;
165 if (ignorelist_match (ignorelist, shortname) != 0) {
166 DEBUG ("smart plugin: ignoring %s.", dev);
170 DEBUG ("smart plugin: checking SMART status of %s.",
173 if (sk_disk_open (dev, &d) < 0)
175 ERROR ("smart plugin: unable to open %s.", dev);
178 if (sk_disk_identify_is_available (d, &available) < 0 || !available)
180 DEBUG ("smart plugin: disk %s cannot be identified.", dev);
183 if (sk_disk_smart_is_available (d, &available) < 0 || !available)
185 DEBUG ("smart plugin: disk %s has no SMART support.", dev);
188 if (!ignore_sleep_mode)
190 if (sk_disk_check_sleep_mode (d, &awake) < 0 || !awake)
192 DEBUG ("smart plugin: disk %s is sleeping.", dev);
196 if (sk_disk_smart_read_data (d) < 0)
198 ERROR ("smart plugin: unable to get SMART data for disk %s.", dev);
201 if (sk_disk_smart_parse (d, &spd) < 0)
203 ERROR ("smart plugin: unable to parse SMART data for disk %s.", dev);
207 /* Get some specific values */
208 if (sk_disk_smart_get_power_on (d, &poweron) < 0)
210 WARNING ("smart plugin: unable to get milliseconds since power on for %s.",
214 smart_submit (shortname, "smart_poweron", "", poweron / 1000.);
216 if (sk_disk_smart_get_power_cycle (d, &powercycles) < 0)
218 WARNING ("smart plugin: unable to get number of power cycles for %s.",
222 smart_submit (shortname, "smart_powercycles", "", powercycles);
224 if (sk_disk_smart_get_bad (d, &badsectors) < 0)
226 WARNING ("smart plugin: unable to get number of bad sectors for %s.",
230 smart_submit (shortname, "smart_badsectors", "", badsectors);
232 if (sk_disk_smart_get_temperature (d, &temperature) < 0)
234 WARNING ("smart plugin: unable to get temperature for %s.",
238 smart_submit (shortname, "smart_temperature", "", temperature / 1000. - 273.15);
240 /* Grab all attributes */
241 if (sk_disk_smart_parse_attributes(d, smart_handle_disk_attribute,
242 (char *)shortname) < 0)
244 ERROR ("smart plugin: unable to handle SMART attributes for %s.",
252 static int smart_read (void)
254 struct udev *handle_udev;
255 struct udev_enumerate *enumerate;
256 struct udev_list_entry *devices, *dev_list_entry;
257 struct udev_device *dev;
259 /* Use udev to get a list of disks */
260 handle_udev = udev_new();
263 ERROR ("smart plugin: unable to initialize udev.");
266 enumerate = udev_enumerate_new (handle_udev);
267 udev_enumerate_add_match_subsystem (enumerate, "block");
268 udev_enumerate_add_match_property (enumerate, "DEVTYPE", "disk");
269 udev_enumerate_scan_devices (enumerate);
270 devices = udev_enumerate_get_list_entry (enumerate);
271 udev_list_entry_foreach (dev_list_entry, devices)
273 const char *path, *devpath, *serial;
274 path = udev_list_entry_get_name (dev_list_entry);
275 dev = udev_device_new_from_syspath (handle_udev, path);
276 devpath = udev_device_get_devnode (dev);
277 serial = udev_device_get_property_value (dev, "ID_SERIAL");
279 /* Query status with libatasmart */
280 smart_handle_disk (devpath, serial);
281 udev_device_unref (dev);
284 udev_enumerate_unref (enumerate);
285 udev_unref (handle_udev);
288 } /* int smart_read */
290 void module_register (void)
292 plugin_register_config ("smart", smart_config,
293 config_keys, config_keys_num);
294 plugin_register_read ("smart", smart_read);
295 } /* void module_register */