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>
30 #include "utils_ignorelist.h"
35 static const char *config_keys[] =
43 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
45 static ignorelist_t *ignorelist = NULL;
46 static int ignore_sleep_mode = 0;
47 static int use_serial = 0;
49 static int smart_config (const char *key, const char *value)
51 if (ignorelist == NULL)
52 ignorelist = ignorelist_create (/* invert = */ 1);
53 if (ignorelist == NULL)
56 if (strcasecmp ("Disk", key) == 0)
58 ignorelist_add (ignorelist, value);
60 else if (strcasecmp ("IgnoreSelected", key) == 0)
65 ignorelist_set_invert (ignorelist, invert);
67 else if (strcasecmp ("IgnoreSleepMode", key) == 0)
70 ignore_sleep_mode = 1;
72 else if (strcasecmp ("UseSerial", key) == 0)
83 } /* int smart_config */
85 static void smart_submit (const char *dev, const char *type,
86 const char *type_inst, double value)
89 value_list_t vl = VALUE_LIST_INIT;
91 values[0].gauge = value;
95 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
96 sstrncpy (vl.plugin, "smart", sizeof (vl.plugin));
97 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
98 sstrncpy (vl.type, type, sizeof (vl.type));
99 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
101 plugin_dispatch_values (&vl);
104 static void smart_handle_disk_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
107 const char *dev = userdata;
109 value_list_t vl = VALUE_LIST_INIT;
111 if (!a->current_value_valid || !a->worst_value_valid) return;
112 values[0].gauge = a->current_value;
113 values[1].gauge = a->worst_value;
114 values[2].gauge = a->threshold_valid?a->threshold:0;
115 values[3].gauge = a->pretty_value;
119 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
120 sstrncpy (vl.plugin, "smart", sizeof (vl.plugin));
121 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
122 sstrncpy (vl.type, "smart_attribute", sizeof (vl.type));
123 sstrncpy (vl.type_instance, a->name, sizeof (vl.type_instance));
125 plugin_dispatch_values (&vl);
127 if (a->threshold_valid && a->current_value <= a->threshold)
129 notification_t notif = { NOTIF_WARNING,
137 sstrncpy (notif.host, hostname_g, sizeof (notif.host));
138 sstrncpy (notif.plugin_instance, dev, sizeof (notif.plugin_instance));
139 sstrncpy (notif.type_instance, a->name, sizeof (notif.type_instance));
140 ssnprintf (notif.message, sizeof (notif.message),
141 "attribute %s is below allowed threshold (%d < %d)",
142 a->name, a->current_value, a->threshold);
143 plugin_dispatch_notification (¬if);
147 static void smart_handle_disk (const char *dev, const char *serial)
150 SkBool awake = FALSE;
151 SkBool available = FALSE;
152 const char *shortname;
153 const SkSmartParsedData *spd;
154 uint64_t poweron, powercycles, badsectors, temperature;
156 if (use_serial && serial)
162 shortname = strrchr(dev, '/');
163 if (!shortname) return;
166 if (ignorelist_match (ignorelist, shortname) != 0) {
167 DEBUG ("smart plugin: ignoring %s.", dev);
171 DEBUG ("smart plugin: checking SMART status of %s.",
174 if (sk_disk_open (dev, &d) < 0)
176 ERROR ("smart plugin: unable to open %s.", dev);
179 if (sk_disk_identify_is_available (d, &available) < 0 || !available)
181 DEBUG ("smart plugin: disk %s cannot be identified.", dev);
184 if (sk_disk_smart_is_available (d, &available) < 0 || !available)
186 DEBUG ("smart plugin: disk %s has no SMART support.", dev);
189 if (!ignore_sleep_mode)
191 if (sk_disk_check_sleep_mode (d, &awake) < 0 || !awake)
193 DEBUG ("smart plugin: disk %s is sleeping.", dev);
197 if (sk_disk_smart_read_data (d) < 0)
199 ERROR ("smart plugin: unable to get SMART data for disk %s.", dev);
202 if (sk_disk_smart_parse (d, &spd) < 0)
204 ERROR ("smart plugin: unable to parse SMART data for disk %s.", dev);
208 /* Get some specific values */
209 if (sk_disk_smart_get_power_on (d, &poweron) < 0)
211 WARNING ("smart plugin: unable to get milliseconds since power on for %s.",
215 smart_submit (shortname, "smart_poweron", "", poweron / 1000.);
217 if (sk_disk_smart_get_power_cycle (d, &powercycles) < 0)
219 WARNING ("smart plugin: unable to get number of power cycles for %s.",
223 smart_submit (shortname, "smart_powercycles", "", powercycles);
225 if (sk_disk_smart_get_bad (d, &badsectors) < 0)
227 WARNING ("smart plugin: unable to get number of bad sectors for %s.",
231 smart_submit (shortname, "smart_badsectors", "", badsectors);
233 if (sk_disk_smart_get_temperature (d, &temperature) < 0)
235 WARNING ("smart plugin: unable to get temperature for %s.",
239 smart_submit (shortname, "smart_temperature", "", temperature / 1000. - 273.15);
241 /* Grab all attributes */
242 if (sk_disk_smart_parse_attributes(d, smart_handle_disk_attribute,
243 (char *)shortname) < 0)
245 ERROR ("smart plugin: unable to handle SMART attributes for %s.",
253 static int smart_read (void)
255 struct udev *handle_udev;
256 struct udev_enumerate *enumerate;
257 struct udev_list_entry *devices, *dev_list_entry;
258 struct udev_device *dev;
260 /* Use udev to get a list of disks */
261 handle_udev = udev_new();
264 ERROR ("smart plugin: unable to initialize udev.");
267 enumerate = udev_enumerate_new (handle_udev);
268 udev_enumerate_add_match_subsystem (enumerate, "block");
269 udev_enumerate_add_match_property (enumerate, "DEVTYPE", "disk");
270 udev_enumerate_scan_devices (enumerate);
271 devices = udev_enumerate_get_list_entry (enumerate);
272 udev_list_entry_foreach (dev_list_entry, devices)
274 const char *path, *devpath, *serial;
275 path = udev_list_entry_get_name (dev_list_entry);
276 dev = udev_device_new_from_syspath (handle_udev, path);
277 devpath = udev_device_get_devnode (dev);
278 serial = udev_device_get_property_value (dev, "ID_SERIAL");
280 /* Query status with libatasmart */
281 smart_handle_disk (devpath, serial);
282 udev_device_unref (dev);
285 udev_enumerate_unref (enumerate);
286 udev_unref (handle_udev);
289 } /* int smart_read */
291 void module_register (void)
293 plugin_register_config ("smart", smart_config,
294 config_keys, config_keys_num);
295 plugin_register_read ("smart", smart_read);
296 } /* void module_register */