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) {
78 value_list_t vl = VALUE_LIST_INIT;
80 values[0].gauge = value;
84 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
85 sstrncpy(vl.plugin, "smart", sizeof(vl.plugin));
86 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
87 sstrncpy(vl.type, type, sizeof(vl.type));
88 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
90 plugin_dispatch_values(&vl);
93 static void handle_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
95 char const *name = userdata;
97 value_list_t vl = VALUE_LIST_INIT;
99 if (!a->current_value_valid || !a->worst_value_valid)
101 values[0].gauge = a->current_value;
102 values[1].gauge = a->worst_value;
103 values[2].gauge = a->threshold_valid ? a->threshold : 0;
104 values[3].gauge = a->pretty_value;
108 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
109 sstrncpy(vl.plugin, "smart", sizeof(vl.plugin));
110 sstrncpy(vl.plugin_instance, name, sizeof(vl.plugin_instance));
111 sstrncpy(vl.type, "smart_attribute", sizeof(vl.type));
112 sstrncpy(vl.type_instance, a->name, sizeof(vl.type_instance));
114 plugin_dispatch_values(&vl);
116 if (a->threshold_valid && a->current_value <= a->threshold) {
117 notification_t notif = {NOTIF_WARNING, cdtime(), "", "", "smart", "",
118 "smart_attribute", "", NULL};
119 sstrncpy(notif.host, hostname_g, sizeof(notif.host));
120 sstrncpy(notif.plugin_instance, name, sizeof(notif.plugin_instance));
121 sstrncpy(notif.type_instance, a->name, sizeof(notif.type_instance));
122 ssnprintf(notif.message, sizeof(notif.message),
123 "attribute %s is below allowed threshold (%d < %d)", a->name,
124 a->current_value, a->threshold);
125 plugin_dispatch_notification(¬if);
129 static void smart_read_disk(SkDisk *d, char const *name) {
130 SkBool available = FALSE;
131 if (sk_disk_identify_is_available(d, &available) < 0 || !available) {
132 DEBUG("smart plugin: disk %s cannot be identified.", name);
135 if (sk_disk_smart_is_available(d, &available) < 0 || !available) {
136 DEBUG("smart plugin: disk %s has no SMART support.", name);
139 if (!ignore_sleep_mode) {
140 SkBool awake = FALSE;
141 if (sk_disk_check_sleep_mode(d, &awake) < 0 || !awake) {
142 DEBUG("smart plugin: disk %s is sleeping.", name);
146 if (sk_disk_smart_read_data(d) < 0) {
147 ERROR("smart plugin: unable to get SMART data for disk %s.", name);
151 if (sk_disk_smart_parse(d, &(SkSmartParsedData const *){NULL}) < 0) {
152 ERROR("smart plugin: unable to parse SMART data for disk %s.", name);
156 /* Get some specific values */
158 if (sk_disk_smart_get_power_on(d, &value) >= 0)
159 smart_submit(name, "smart_poweron", "", ((gauge_t)value) / 1000.);
161 DEBUG("smart plugin: unable to get milliseconds since power on for %s.",
164 if (sk_disk_smart_get_power_cycle(d, &value) >= 0)
165 smart_submit(name, "smart_powercycles", "", (gauge_t)value);
167 DEBUG("smart plugin: unable to get number of power cycles for %s.", name);
169 if (sk_disk_smart_get_bad(d, &value) >= 0)
170 smart_submit(name, "smart_badsectors", "", (gauge_t)value);
172 DEBUG("smart plugin: unable to get number of bad sectors for %s.", name);
174 if (sk_disk_smart_get_temperature(d, &value) >= 0)
175 smart_submit(name, "smart_temperature", "",
176 ((gauge_t)value) / 1000. - 273.15);
178 DEBUG("smart plugin: unable to get temperature for %s.", name);
180 /* Grab all attributes */
181 if (sk_disk_smart_parse_attributes(d, handle_attribute, (void *)name) < 0) {
182 ERROR("smart plugin: unable to handle SMART attributes for %s.", name);
186 static void smart_handle_disk(const char *dev, const char *serial) {
190 if (use_serial && serial) {
193 name = strrchr(dev, '/');
198 if (ignorelist_match(ignorelist, name) != 0) {
199 DEBUG("smart plugin: ignoring %s.", dev);
203 DEBUG("smart plugin: checking SMART status of %s.", dev);
204 if (sk_disk_open(dev, &d) < 0) {
205 ERROR("smart plugin: unable to open %s.", dev);
209 smart_read_disk(d, name);
213 static int smart_read(void) {
214 struct udev *handle_udev;
215 struct udev_enumerate *enumerate;
216 struct udev_list_entry *devices, *dev_list_entry;
217 struct udev_device *dev;
219 /* Use udev to get a list of disks */
220 handle_udev = udev_new();
222 ERROR("smart plugin: unable to initialize udev.");
225 enumerate = udev_enumerate_new(handle_udev);
226 udev_enumerate_add_match_subsystem(enumerate, "block");
227 udev_enumerate_add_match_property(enumerate, "DEVTYPE", "disk");
228 udev_enumerate_scan_devices(enumerate);
229 devices = udev_enumerate_get_list_entry(enumerate);
230 udev_list_entry_foreach(dev_list_entry, devices) {
231 const char *path, *devpath, *serial;
232 path = udev_list_entry_get_name(dev_list_entry);
233 dev = udev_device_new_from_syspath(handle_udev, path);
234 devpath = udev_device_get_devnode(dev);
235 serial = udev_device_get_property_value(dev, "ID_SERIAL");
237 /* Query status with libatasmart */
238 smart_handle_disk(devpath, serial);
239 udev_device_unref(dev);
242 udev_enumerate_unref(enumerate);
243 udev_unref(handle_udev);
246 } /* int smart_read */
248 static int smart_init(void) {
249 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_SYS_RAWIO)
250 if (check_capability(CAP_SYS_RAWIO) != 0) {
252 WARNING("smart plugin: Running collectd as root, but the "
253 "CAP_SYS_RAWIO capability is missing. The plugin's read "
254 "function will probably fail. Is your init system dropping "
257 WARNING("smart plugin: collectd doesn't have the CAP_SYS_RAWIO "
258 "capability. If you don't want to run collectd as root, try "
259 "running \"setcap cap_sys_rawio=ep\" on the collectd binary.");
263 } /* int smart_init */
265 void module_register(void) {
266 plugin_register_config("smart", smart_config, config_keys, config_keys_num);
267 plugin_register_init("smart", smart_init);
268 plugin_register_read("smart", smart_read);
269 } /* void module_register */