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)
90 value_list_t vl = VALUE_LIST_INIT;
92 values[0].gauge = value;
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));
102 plugin_dispatch_values (&vl);
105 static void smart_handle_disk_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
108 const char *dev = userdata;
110 value_list_t vl = VALUE_LIST_INIT;
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;
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));
126 plugin_dispatch_values (&vl);
128 if (a->threshold_valid && a->current_value <= a->threshold)
130 notification_t notif = { NOTIF_WARNING,
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 (¬if);
148 static void smart_handle_disk (const char *dev, const char *serial)
151 SkBool awake = FALSE;
152 SkBool available = FALSE;
153 const char *shortname;
154 const SkSmartParsedData *spd;
155 uint64_t poweron, powercycles, badsectors, temperature;
157 if (use_serial && serial)
163 shortname = strrchr(dev, '/');
164 if (!shortname) return;
167 if (ignorelist_match (ignorelist, shortname) != 0) {
168 DEBUG ("smart plugin: ignoring %s.", dev);
172 DEBUG ("smart plugin: checking SMART status of %s.",
175 if (sk_disk_open (dev, &d) < 0)
177 ERROR ("smart plugin: unable to open %s.", dev);
180 if (sk_disk_identify_is_available (d, &available) < 0 || !available)
182 DEBUG ("smart plugin: disk %s cannot be identified.", dev);
185 if (sk_disk_smart_is_available (d, &available) < 0 || !available)
187 DEBUG ("smart plugin: disk %s has no SMART support.", dev);
190 if (!ignore_sleep_mode)
192 if (sk_disk_check_sleep_mode (d, &awake) < 0 || !awake)
194 DEBUG ("smart plugin: disk %s is sleeping.", dev);
198 if (sk_disk_smart_read_data (d) < 0)
200 ERROR ("smart plugin: unable to get SMART data for disk %s.", dev);
203 if (sk_disk_smart_parse (d, &spd) < 0)
205 ERROR ("smart plugin: unable to parse SMART data for disk %s.", dev);
209 /* Get some specific values */
210 if (sk_disk_smart_get_power_on (d, &poweron) < 0)
212 WARNING ("smart plugin: unable to get milliseconds since power on for %s.",
216 smart_submit (shortname, "smart_poweron", "", poweron / 1000.);
218 if (sk_disk_smart_get_power_cycle (d, &powercycles) < 0)
220 WARNING ("smart plugin: unable to get number of power cycles for %s.",
224 smart_submit (shortname, "smart_powercycles", "", powercycles);
226 if (sk_disk_smart_get_bad (d, &badsectors) < 0)
228 WARNING ("smart plugin: unable to get number of bad sectors for %s.",
232 smart_submit (shortname, "smart_badsectors", "", badsectors);
234 if (sk_disk_smart_get_temperature (d, &temperature) < 0)
236 WARNING ("smart plugin: unable to get temperature for %s.",
240 smart_submit (shortname, "smart_temperature", "", temperature / 1000. - 273.15);
242 /* Grab all attributes */
243 if (sk_disk_smart_parse_attributes(d, smart_handle_disk_attribute,
244 (char *)shortname) < 0)
246 ERROR ("smart plugin: unable to handle SMART attributes for %s.",
254 static int smart_read (void)
256 struct udev *handle_udev;
257 struct udev_enumerate *enumerate;
258 struct udev_list_entry *devices, *dev_list_entry;
259 struct udev_device *dev;
261 /* Use udev to get a list of disks */
262 handle_udev = udev_new();
265 ERROR ("smart plugin: unable to initialize udev.");
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)
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");
281 /* Query status with libatasmart */
282 smart_handle_disk (devpath, serial);
283 udev_device_unref (dev);
286 udev_enumerate_unref (enumerate);
287 udev_unref (handle_udev);
290 } /* int smart_read */
292 void module_register (void)
294 plugin_register_config ("smart", smart_config,
295 config_keys, config_keys_num);
296 plugin_register_read ("smart", smart_read);
297 } /* void module_register */