1 /**
2 * collectd - src/smart.c
3 * Copyright (C) 2014 Vincent Bernat
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Vincent Bernat <vbe at exoscale.ch>
25 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
31 #include "utils_ignorelist.h"
33 #include <atasmart.h>
34 #include <libudev.h>
36 static const char *config_keys[] = {"Disk", "IgnoreSelected", "IgnoreSleepMode",
37 "UseSerial"};
39 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
41 static ignorelist_t *ignorelist = NULL;
42 static int ignore_sleep_mode = 0;
43 static int use_serial = 0;
45 static int smart_config(const char *key, const char *value) {
46 if (ignorelist == NULL)
47 ignorelist = ignorelist_create(/* invert = */ 1);
48 if (ignorelist == NULL)
49 return (1);
51 if (strcasecmp("Disk", key) == 0) {
52 ignorelist_add(ignorelist, value);
53 } else if (strcasecmp("IgnoreSelected", key) == 0) {
54 int invert = 1;
55 if (IS_TRUE(value))
56 invert = 0;
57 ignorelist_set_invert(ignorelist, invert);
58 } else if (strcasecmp("IgnoreSleepMode", key) == 0) {
59 if (IS_TRUE(value))
60 ignore_sleep_mode = 1;
61 } else if (strcasecmp("UseSerial", key) == 0) {
62 if (IS_TRUE(value))
63 use_serial = 1;
64 } else {
65 return (-1);
66 }
68 return (0);
69 } /* int smart_config */
71 static void smart_submit(const char *dev, const char *type,
72 const char *type_inst, double value) {
73 value_list_t vl = VALUE_LIST_INIT;
75 vl.values = &(value_t){.gauge = value};
76 vl.values_len = 1;
77 sstrncpy(vl.plugin, "smart", sizeof(vl.plugin));
78 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
79 sstrncpy(vl.type, type, sizeof(vl.type));
80 sstrncpy(vl.type_instance, type_inst, sizeof(vl.type_instance));
82 plugin_dispatch_values(&vl);
83 }
85 static void smart_handle_disk_attribute(SkDisk *d,
86 const SkSmartAttributeParsedData *a,
87 void *userdata) {
88 const char *dev = userdata;
90 if (!a->current_value_valid || !a->worst_value_valid)
91 return;
93 value_list_t vl = VALUE_LIST_INIT;
94 value_t values[] = {
95 {.gauge = a->current_value},
96 {.gauge = a->worst_value},
97 {.gauge = a->threshold_valid ? a->threshold : 0},
98 {.gauge = a->pretty_value},
99 };
101 vl.values = values;
102 vl.values_len = STATIC_ARRAY_SIZE(values);
103 sstrncpy(vl.plugin, "smart", sizeof(vl.plugin));
104 sstrncpy(vl.plugin_instance, dev, sizeof(vl.plugin_instance));
105 sstrncpy(vl.type, "smart_attribute", sizeof(vl.type));
106 sstrncpy(vl.type_instance, a->name, sizeof(vl.type_instance));
108 plugin_dispatch_values(&vl);
110 if (a->threshold_valid && a->current_value <= a->threshold) {
111 notification_t notif = {NOTIF_WARNING, cdtime(), "", "", "smart", "",
112 "smart_attribute", "", NULL};
113 sstrncpy(notif.host, hostname_g, sizeof(notif.host));
114 sstrncpy(notif.plugin_instance, dev, sizeof(notif.plugin_instance));
115 sstrncpy(notif.type_instance, a->name, sizeof(notif.type_instance));
116 ssnprintf(notif.message, sizeof(notif.message),
117 "attribute %s is below allowed threshold (%d < %d)", a->name,
118 a->current_value, a->threshold);
119 plugin_dispatch_notification(¬if);
120 }
121 }
123 static void smart_handle_disk(const char *dev, const char *serial) {
124 SkDisk *d = NULL;
125 SkBool awake = FALSE;
126 SkBool available = FALSE;
127 const char *shortname;
128 const SkSmartParsedData *spd;
129 uint64_t poweron, powercycles, badsectors, temperature;
131 if (use_serial && serial) {
132 shortname = serial;
133 } else {
134 shortname = strrchr(dev, '/');
135 if (!shortname)
136 return;
137 shortname++;
138 }
139 if (ignorelist_match(ignorelist, shortname) != 0) {
140 DEBUG("smart plugin: ignoring %s.", dev);
141 return;
142 }
144 DEBUG("smart plugin: checking SMART status of %s.", dev);
146 if (sk_disk_open(dev, &d) < 0) {
147 ERROR("smart plugin: unable to open %s.", dev);
148 return;
149 }
150 if (sk_disk_identify_is_available(d, &available) < 0 || !available) {
151 DEBUG("smart plugin: disk %s cannot be identified.", dev);
152 goto end;
153 }
154 if (sk_disk_smart_is_available(d, &available) < 0 || !available) {
155 DEBUG("smart plugin: disk %s has no SMART support.", dev);
156 goto end;
157 }
158 if (!ignore_sleep_mode) {
159 if (sk_disk_check_sleep_mode(d, &awake) < 0 || !awake) {
160 DEBUG("smart plugin: disk %s is sleeping.", dev);
161 goto end;
162 }
163 }
164 if (sk_disk_smart_read_data(d) < 0) {
165 ERROR("smart plugin: unable to get SMART data for disk %s.", dev);
166 goto end;
167 }
168 if (sk_disk_smart_parse(d, &spd) < 0) {
169 ERROR("smart plugin: unable to parse SMART data for disk %s.", dev);
170 goto end;
171 }
173 /* Get some specific values */
174 if (sk_disk_smart_get_power_on(d, &poweron) < 0) {
175 WARNING("smart plugin: unable to get milliseconds since power on for %s.",
176 dev);
177 } else
178 smart_submit(shortname, "smart_poweron", "", poweron / 1000.);
180 if (sk_disk_smart_get_power_cycle(d, &powercycles) < 0) {
181 WARNING("smart plugin: unable to get number of power cycles for %s.", dev);
182 } else
183 smart_submit(shortname, "smart_powercycles", "", powercycles);
185 if (sk_disk_smart_get_bad(d, &badsectors) < 0) {
186 WARNING("smart plugin: unable to get number of bad sectors for %s.", dev);
187 } else
188 smart_submit(shortname, "smart_badsectors", "", badsectors);
190 if (sk_disk_smart_get_temperature(d, &temperature) < 0) {
191 WARNING("smart plugin: unable to get temperature for %s.", dev);
192 } else
193 smart_submit(shortname, "smart_temperature", "",
194 temperature / 1000. - 273.15);
196 /* Grab all attributes */
197 if (sk_disk_smart_parse_attributes(d, smart_handle_disk_attribute,
198 (char *)shortname) < 0) {
199 ERROR("smart plugin: unable to handle SMART attributes for %s.", dev);
200 }
202 end:
203 sk_disk_free(d);
204 }
206 static int smart_read(void) {
207 struct udev *handle_udev;
208 struct udev_enumerate *enumerate;
209 struct udev_list_entry *devices, *dev_list_entry;
210 struct udev_device *dev;
212 /* Use udev to get a list of disks */
213 handle_udev = udev_new();
214 if (!handle_udev) {
215 ERROR("smart plugin: unable to initialize udev.");
216 return (-1);
217 }
218 enumerate = udev_enumerate_new(handle_udev);
219 udev_enumerate_add_match_subsystem(enumerate, "block");
220 udev_enumerate_add_match_property(enumerate, "DEVTYPE", "disk");
221 udev_enumerate_scan_devices(enumerate);
222 devices = udev_enumerate_get_list_entry(enumerate);
223 udev_list_entry_foreach(dev_list_entry, devices) {
224 const char *path, *devpath, *serial;
225 path = udev_list_entry_get_name(dev_list_entry);
226 dev = udev_device_new_from_syspath(handle_udev, path);
227 devpath = udev_device_get_devnode(dev);
228 serial = udev_device_get_property_value(dev, "ID_SERIAL");
230 /* Query status with libatasmart */
231 smart_handle_disk(devpath, serial);
232 udev_device_unref(dev);
233 }
235 udev_enumerate_unref(enumerate);
236 udev_unref(handle_udev);
238 return (0);
239 } /* int smart_read */
241 void module_register(void) {
242 plugin_register_config("smart", smart_config, config_keys, config_keys_num);
243 plugin_register_read("smart", smart_read);
244 } /* void module_register */