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 #ifdef HAVE_SYS_CAPABILITY_H
37 #include <sys/capability.h>
40 static const char *config_keys[] = {"Disk", "IgnoreSelected", "IgnoreSleepMode",
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) {
50 if (ignorelist == NULL)
51 ignorelist = ignorelist_create(/* invert = */ 1);
52 if (ignorelist == NULL)
55 if (strcasecmp("Disk", key) == 0) {
56 ignorelist_add(ignorelist, value);
57 } else if (strcasecmp("IgnoreSelected", key) == 0) {
61 ignorelist_set_invert(ignorelist, invert);
62 } else if (strcasecmp("IgnoreSleepMode", key) == 0) {
64 ignore_sleep_mode = 1;
65 } else if (strcasecmp("UseSerial", key) == 0) {
73 } /* int smart_config */
75 static void smart_submit(const char *dev, const char *type,
76 const char *type_inst, double value) {
77 value_list_t vl = VALUE_LIST_INIT;
79 vl.values = &(value_t){.gauge = value};
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 handle_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
91 char const *name = userdata;
93 if (!a->current_value_valid || !a->worst_value_valid)
96 value_list_t vl = VALUE_LIST_INIT;
98 {.gauge = a->current_value},
99 {.gauge = a->worst_value},
100 {.gauge = a->threshold_valid ? a->threshold : 0},
101 {.gauge = a->pretty_value},
105 vl.values_len = STATIC_ARRAY_SIZE(values);
106 sstrncpy(vl.plugin, "smart", sizeof(vl.plugin));
107 sstrncpy(vl.plugin_instance, name, sizeof(vl.plugin_instance));
108 sstrncpy(vl.type, "smart_attribute", sizeof(vl.type));
109 sstrncpy(vl.type_instance, a->name, sizeof(vl.type_instance));
111 plugin_dispatch_values(&vl);
113 if (a->threshold_valid && a->current_value <= a->threshold) {
114 notification_t notif = {NOTIF_WARNING, cdtime(), "", "", "smart", "",
115 "smart_attribute", "", NULL};
116 sstrncpy(notif.host, hostname_g, sizeof(notif.host));
117 sstrncpy(notif.plugin_instance, name, sizeof(notif.plugin_instance));
118 sstrncpy(notif.type_instance, a->name, sizeof(notif.type_instance));
119 snprintf(notif.message, sizeof(notif.message),
120 "attribute %s is below allowed threshold (%d < %d)", a->name,
121 a->current_value, a->threshold);
122 plugin_dispatch_notification(¬if);
126 static void smart_read_disk(SkDisk *d, char const *name) {
127 SkBool available = FALSE;
128 if (sk_disk_identify_is_available(d, &available) < 0 || !available) {
129 DEBUG("smart plugin: disk %s cannot be identified.", name);
132 if (sk_disk_smart_is_available(d, &available) < 0 || !available) {
133 DEBUG("smart plugin: disk %s has no SMART support.", name);
136 if (!ignore_sleep_mode) {
137 SkBool awake = FALSE;
138 if (sk_disk_check_sleep_mode(d, &awake) < 0 || !awake) {
139 DEBUG("smart plugin: disk %s is sleeping.", name);
143 if (sk_disk_smart_read_data(d) < 0) {
144 ERROR("smart plugin: unable to get SMART data for disk %s.", name);
148 if (sk_disk_smart_parse(d, &(SkSmartParsedData const *){NULL}) < 0) {
149 ERROR("smart plugin: unable to parse SMART data for disk %s.", name);
153 /* Get some specific values */
155 if (sk_disk_smart_get_power_on(d, &value) >= 0)
156 smart_submit(name, "smart_poweron", "", ((gauge_t)value) / 1000.);
158 DEBUG("smart plugin: unable to get milliseconds since power on for %s.",
161 if (sk_disk_smart_get_power_cycle(d, &value) >= 0)
162 smart_submit(name, "smart_powercycles", "", (gauge_t)value);
164 DEBUG("smart plugin: unable to get number of power cycles for %s.", name);
166 if (sk_disk_smart_get_bad(d, &value) >= 0)
167 smart_submit(name, "smart_badsectors", "", (gauge_t)value);
169 DEBUG("smart plugin: unable to get number of bad sectors for %s.", name);
171 if (sk_disk_smart_get_temperature(d, &value) >= 0)
172 smart_submit(name, "smart_temperature", "",
173 ((gauge_t)value) / 1000. - 273.15);
175 DEBUG("smart plugin: unable to get temperature for %s.", name);
177 /* Grab all attributes */
178 if (sk_disk_smart_parse_attributes(d, handle_attribute, (void *)name) < 0) {
179 ERROR("smart plugin: unable to handle SMART attributes for %s.", name);
183 static void smart_handle_disk(const char *dev, const char *serial) {
187 if (use_serial && serial) {
190 name = strrchr(dev, '/');
195 if (ignorelist_match(ignorelist, name) != 0) {
196 DEBUG("smart plugin: ignoring %s.", dev);
200 DEBUG("smart plugin: checking SMART status of %s.", dev);
201 if (sk_disk_open(dev, &d) < 0) {
202 ERROR("smart plugin: unable to open %s.", dev);
206 smart_read_disk(d, name);
210 static int smart_read(void) {
211 struct udev *handle_udev;
212 struct udev_enumerate *enumerate;
213 struct udev_list_entry *devices, *dev_list_entry;
214 struct udev_device *dev;
216 /* Use udev to get a list of disks */
217 handle_udev = udev_new();
219 ERROR("smart plugin: unable to initialize udev.");
222 enumerate = udev_enumerate_new(handle_udev);
223 udev_enumerate_add_match_subsystem(enumerate, "block");
224 udev_enumerate_add_match_property(enumerate, "DEVTYPE", "disk");
225 udev_enumerate_scan_devices(enumerate);
226 devices = udev_enumerate_get_list_entry(enumerate);
227 udev_list_entry_foreach(dev_list_entry, devices) {
228 const char *path, *devpath, *serial;
229 path = udev_list_entry_get_name(dev_list_entry);
230 dev = udev_device_new_from_syspath(handle_udev, path);
231 devpath = udev_device_get_devnode(dev);
232 serial = udev_device_get_property_value(dev, "ID_SERIAL");
234 /* Query status with libatasmart */
235 smart_handle_disk(devpath, serial);
236 udev_device_unref(dev);
239 udev_enumerate_unref(enumerate);
240 udev_unref(handle_udev);
243 } /* int smart_read */
245 static int smart_init(void) {
246 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_SYS_RAWIO)
247 if (check_capability(CAP_SYS_RAWIO) != 0) {
249 WARNING("smart plugin: Running collectd as root, but the "
250 "CAP_SYS_RAWIO capability is missing. The plugin's read "
251 "function will probably fail. Is your init system dropping "
254 WARNING("smart plugin: collectd doesn't have the CAP_SYS_RAWIO "
255 "capability. If you don't want to run collectd as root, try "
256 "running \"setcap cap_sys_rawio=ep\" on the collectd binary.");
260 } /* int smart_init */
262 void module_register(void) {
263 plugin_register_config("smart", smart_config, config_keys, config_keys_num);
264 plugin_register_init("smart", smart_init);
265 plugin_register_read("smart", smart_read);
266 } /* void module_register */