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[] =
41 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43 static ignorelist_t *ignorelist = NULL;
45 static int smart_config (const char *key, const char *value)
47 if (ignorelist == NULL)
48 ignorelist = ignorelist_create (/* invert = */ 1);
49 if (ignorelist == NULL)
52 if (strcasecmp ("Disk", key) == 0)
54 ignorelist_add (ignorelist, value);
56 else if (strcasecmp ("IgnoreSelected", key) == 0)
61 ignorelist_set_invert (ignorelist, invert);
69 } /* int smart_config */
71 static void smart_submit (const char *dev, char *type, char *type_inst, double value)
74 value_list_t vl = VALUE_LIST_INIT;
76 values[0].gauge = value;
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));
86 plugin_dispatch_values (&vl);
89 static void smart_handle_disk_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
92 const char *dev = userdata;
94 value_list_t vl = VALUE_LIST_INIT;
96 if (!a->current_value_valid || !a->worst_value_valid) return;
97 values[0].gauge = a->current_value;
98 values[1].gauge = a->worst_value;
99 values[2].gauge = a->threshold_valid?a->threshold:0;
100 values[3].gauge = a->pretty_value;
104 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
105 sstrncpy (vl.plugin, "smart", sizeof (vl.plugin));
106 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
107 sstrncpy (vl.type, "smart_attribute", sizeof (vl.type));
108 sstrncpy (vl.type_instance, a->name, sizeof (vl.type_instance));
110 plugin_dispatch_values (&vl);
112 if (a->threshold_valid && a->current_value <= a->threshold)
114 notification_t notif = { NOTIF_WARNING,
122 sstrncpy (notif.host, hostname_g, sizeof (notif.host));
123 sstrncpy (notif.plugin_instance, dev, sizeof (notif.plugin_instance));
124 sstrncpy (notif.type_instance, a->name, sizeof (notif.type_instance));
125 ssnprintf (notif.message, sizeof (notif.message),
126 "attribute %s is below allowed threshold (%d < %d)",
127 a->name, a->current_value, a->threshold);
128 plugin_dispatch_notification (¬if);
132 static void smart_handle_disk (const char *dev)
135 SkBool awake = FALSE;
136 SkBool available = FALSE;
137 const char *shortname;
138 const SkSmartParsedData *spd;
139 uint64_t poweron, powercycles, badsectors, temperature;
141 shortname = strrchr(dev, '/');
142 if (!shortname) return;
144 if (ignorelist_match (ignorelist, shortname) != 0) {
145 DEBUG ("smart plugin: ignoring %s.", dev);
149 DEBUG ("smart plugin: checking SMART status of %s.",
152 if (sk_disk_open (dev, &d) < 0)
154 ERROR ("smart plugin: unable to open %s.", dev);
157 if (sk_disk_identify_is_available (d, &available) < 0 || !available)
159 DEBUG ("smart plugin: disk %s cannot be identified.", dev);
162 if (sk_disk_smart_is_available (d, &available) < 0 || !available)
164 DEBUG ("smart plugin: disk %s has no SMART support.", dev);
167 if (sk_disk_check_sleep_mode (d, &awake) < 0 || !awake)
169 DEBUG ("smart plugin: disk %s is sleeping.", dev);
172 if (sk_disk_smart_read_data (d) < 0)
174 ERROR ("smart plugin: unable to get SMART data for disk %s.", dev);
177 if (sk_disk_smart_parse (d, &spd) < 0)
179 ERROR ("smart plugin: unable to parse SMART data for disk %s.", dev);
183 /* Get some specific values */
184 if (sk_disk_smart_get_power_on (d, &poweron) < 0)
186 WARNING ("smart plugin: unable to get milliseconds since power on for %s.",
190 smart_submit (shortname, "smart_poweron", "", poweron / 1000.);
192 if (sk_disk_smart_get_power_cycle (d, &powercycles) < 0)
194 WARNING ("smart plugin: unable to get number of power cycles for %s.",
198 smart_submit (shortname, "smart_powercycles", "", powercycles);
200 if (sk_disk_smart_get_bad (d, &badsectors) < 0)
202 WARNING ("smart plugin: unable to get number of bad sectors for %s.",
206 smart_submit (shortname, "smart_badsectors", "", badsectors);
208 if (sk_disk_smart_get_temperature (d, &temperature) < 0)
210 WARNING ("smart plugin: unable to get temperature for %s.",
214 smart_submit (shortname, "smart_temperature", "", temperature / 1000. - 273.15);
216 /* Grab all attributes */
217 if (sk_disk_smart_parse_attributes(d, smart_handle_disk_attribute,
218 (char *)shortname) < 0)
220 ERROR ("smart plugin: unable to handle SMART attributes for %s.",
228 static int smart_read (void)
230 struct udev *handle_udev;
231 struct udev_enumerate *enumerate;
232 struct udev_list_entry *devices, *dev_list_entry;
233 struct udev_device *dev;
235 /* Use udev to get a list of disks */
236 handle_udev = udev_new();
239 ERROR ("smart plugin: unable to initialize udev.");
242 enumerate = udev_enumerate_new (handle_udev);
243 udev_enumerate_add_match_subsystem (enumerate, "block");
244 udev_enumerate_add_match_property (enumerate, "DEVTYPE", "disk");
245 udev_enumerate_scan_devices (enumerate);
246 devices = udev_enumerate_get_list_entry (enumerate);
247 udev_list_entry_foreach (dev_list_entry, devices)
249 const char *path, *devpath;
250 path = udev_list_entry_get_name (dev_list_entry);
251 dev = udev_device_new_from_syspath (handle_udev, path);
252 devpath = udev_device_get_devnode (dev);
254 /* Query status with libatasmart */
255 smart_handle_disk (devpath);
258 udev_enumerate_unref (enumerate);
259 udev_unref (handle_udev);
262 } /* int smart_read */
264 void module_register (void)
266 plugin_register_config ("smart", smart_config,
267 config_keys, config_keys_num);
268 plugin_register_read ("smart", smart_read);
269 } /* void module_register */