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[] =
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
44 static ignorelist_t *ignorelist = NULL;
45 static int ignore_sleep_mode = 0;
47 static int smart_config (const char *key, const char *value)
49 if (ignorelist == NULL)
50 ignorelist = ignorelist_create (/* invert = */ 1);
51 if (ignorelist == NULL)
54 if (strcasecmp ("Disk", key) == 0)
56 ignorelist_add (ignorelist, value);
58 else if (strcasecmp ("IgnoreSelected", key) == 0)
63 ignorelist_set_invert (ignorelist, invert);
65 else if (strcasecmp ("IgnoreSleepMode", key) == 0)
68 ignore_sleep_mode = 1;
76 } /* int smart_config */
78 static void smart_submit (const char *dev, const char *type,
79 const char *type_inst, double value)
82 value_list_t vl = VALUE_LIST_INIT;
84 values[0].gauge = value;
88 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
89 sstrncpy (vl.plugin, "smart", sizeof (vl.plugin));
90 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
91 sstrncpy (vl.type, type, sizeof (vl.type));
92 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
94 plugin_dispatch_values (&vl);
97 static void smart_handle_disk_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
100 const char *dev = userdata;
102 value_list_t vl = VALUE_LIST_INIT;
104 if (!a->current_value_valid || !a->worst_value_valid) return;
105 values[0].gauge = a->current_value;
106 values[1].gauge = a->worst_value;
107 values[2].gauge = a->threshold_valid?a->threshold:0;
108 values[3].gauge = a->pretty_value;
112 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
113 sstrncpy (vl.plugin, "smart", sizeof (vl.plugin));
114 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
115 sstrncpy (vl.type, "smart_attribute", sizeof (vl.type));
116 sstrncpy (vl.type_instance, a->name, sizeof (vl.type_instance));
118 plugin_dispatch_values (&vl);
120 if (a->threshold_valid && a->current_value <= a->threshold)
122 notification_t notif = { NOTIF_WARNING,
130 sstrncpy (notif.host, hostname_g, sizeof (notif.host));
131 sstrncpy (notif.plugin_instance, dev, sizeof (notif.plugin_instance));
132 sstrncpy (notif.type_instance, a->name, sizeof (notif.type_instance));
133 ssnprintf (notif.message, sizeof (notif.message),
134 "attribute %s is below allowed threshold (%d < %d)",
135 a->name, a->current_value, a->threshold);
136 plugin_dispatch_notification (¬if);
140 static void smart_handle_disk (const char *dev)
143 SkBool awake = FALSE;
144 SkBool available = FALSE;
145 const char *shortname;
146 const SkSmartParsedData *spd;
147 uint64_t poweron, powercycles, badsectors, temperature;
149 shortname = strrchr(dev, '/');
150 if (!shortname) return;
152 if (ignorelist_match (ignorelist, shortname) != 0) {
153 DEBUG ("smart plugin: ignoring %s.", dev);
157 DEBUG ("smart plugin: checking SMART status of %s.",
160 if (sk_disk_open (dev, &d) < 0)
162 ERROR ("smart plugin: unable to open %s.", dev);
165 if (sk_disk_identify_is_available (d, &available) < 0 || !available)
167 DEBUG ("smart plugin: disk %s cannot be identified.", dev);
170 if (sk_disk_smart_is_available (d, &available) < 0 || !available)
172 DEBUG ("smart plugin: disk %s has no SMART support.", dev);
175 if (!ignore_sleep_mode)
177 if (sk_disk_check_sleep_mode (d, &awake) < 0 || !awake)
179 DEBUG ("smart plugin: disk %s is sleeping.", dev);
183 if (sk_disk_smart_read_data (d) < 0)
185 ERROR ("smart plugin: unable to get SMART data for disk %s.", dev);
188 if (sk_disk_smart_parse (d, &spd) < 0)
190 ERROR ("smart plugin: unable to parse SMART data for disk %s.", dev);
194 /* Get some specific values */
195 if (sk_disk_smart_get_power_on (d, &poweron) < 0)
197 WARNING ("smart plugin: unable to get milliseconds since power on for %s.",
201 smart_submit (shortname, "smart_poweron", "", poweron / 1000.);
203 if (sk_disk_smart_get_power_cycle (d, &powercycles) < 0)
205 WARNING ("smart plugin: unable to get number of power cycles for %s.",
209 smart_submit (shortname, "smart_powercycles", "", powercycles);
211 if (sk_disk_smart_get_bad (d, &badsectors) < 0)
213 WARNING ("smart plugin: unable to get number of bad sectors for %s.",
217 smart_submit (shortname, "smart_badsectors", "", badsectors);
219 if (sk_disk_smart_get_temperature (d, &temperature) < 0)
221 WARNING ("smart plugin: unable to get temperature for %s.",
225 smart_submit (shortname, "smart_temperature", "", temperature / 1000. - 273.15);
227 /* Grab all attributes */
228 if (sk_disk_smart_parse_attributes(d, smart_handle_disk_attribute,
229 (char *)shortname) < 0)
231 ERROR ("smart plugin: unable to handle SMART attributes for %s.",
239 static int smart_read (void)
241 struct udev *handle_udev;
242 struct udev_enumerate *enumerate;
243 struct udev_list_entry *devices, *dev_list_entry;
244 struct udev_device *dev;
246 /* Use udev to get a list of disks */
247 handle_udev = udev_new();
250 ERROR ("smart plugin: unable to initialize udev.");
253 enumerate = udev_enumerate_new (handle_udev);
254 udev_enumerate_add_match_subsystem (enumerate, "block");
255 udev_enumerate_add_match_property (enumerate, "DEVTYPE", "disk");
256 udev_enumerate_scan_devices (enumerate);
257 devices = udev_enumerate_get_list_entry (enumerate);
258 udev_list_entry_foreach (dev_list_entry, devices)
260 const char *path, *devpath;
261 path = udev_list_entry_get_name (dev_list_entry);
262 dev = udev_device_new_from_syspath (handle_udev, path);
263 devpath = udev_device_get_devnode (dev);
265 /* Query status with libatasmart */
266 smart_handle_disk (devpath);
267 udev_device_unref (dev);
270 udev_enumerate_unref (enumerate);
271 udev_unref (handle_udev);
274 } /* int smart_read */
276 void module_register (void)
278 plugin_register_config ("smart", smart_config,
279 config_keys, config_keys_num);
280 plugin_register_read ("smart", smart_read);
281 } /* void module_register */