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 handle_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
86 void *userdata) {
87 char const *name = userdata;
89 if (!a->current_value_valid || !a->worst_value_valid)
90 return;
92 value_list_t vl = VALUE_LIST_INIT;
93 value_t values[] = {
94 {.gauge = a->current_value},
95 {.gauge = a->worst_value},
96 {.gauge = a->threshold_valid ? a->threshold : 0},
97 {.gauge = a->pretty_value},
98 };
100 vl.values = values;
101 vl.values_len = STATIC_ARRAY_SIZE(values);
102 sstrncpy(vl.plugin, "smart", sizeof(vl.plugin));
103 sstrncpy(vl.plugin_instance, name, sizeof(vl.plugin_instance));
104 sstrncpy(vl.type, "smart_attribute", sizeof(vl.type));
105 sstrncpy(vl.type_instance, a->name, sizeof(vl.type_instance));
107 plugin_dispatch_values(&vl);
109 if (a->threshold_valid && a->current_value <= a->threshold) {
110 notification_t notif = {NOTIF_WARNING, cdtime(), "", "", "smart", "",
111 "smart_attribute", "", NULL};
112 sstrncpy(notif.host, hostname_g, sizeof(notif.host));
113 sstrncpy(notif.plugin_instance, name, sizeof(notif.plugin_instance));
114 sstrncpy(notif.type_instance, a->name, sizeof(notif.type_instance));
115 ssnprintf(notif.message, sizeof(notif.message),
116 "attribute %s is below allowed threshold (%d < %d)", a->name,
117 a->current_value, a->threshold);
118 plugin_dispatch_notification(¬if);
119 }
120 }
122 static void smart_read_disk(SkDisk *d, char const *name) {
123 SkBool available = FALSE;
124 if (sk_disk_identify_is_available(d, &available) < 0 || !available) {
125 DEBUG("smart plugin: disk %s cannot be identified.", name);
126 return;
127 }
128 if (sk_disk_smart_is_available(d, &available) < 0 || !available) {
129 DEBUG("smart plugin: disk %s has no SMART support.", name);
130 return;
131 }
132 if (!ignore_sleep_mode) {
133 SkBool awake = FALSE;
134 if (sk_disk_check_sleep_mode(d, &awake) < 0 || !awake) {
135 DEBUG("smart plugin: disk %s is sleeping.", name);
136 return;
137 }
138 }
139 if (sk_disk_smart_read_data(d) < 0) {
140 ERROR("smart plugin: unable to get SMART data for disk %s.", name);
141 return;
142 }
144 if (sk_disk_smart_parse(d, &(SkSmartParsedData const *){NULL}) < 0) {
145 ERROR("smart plugin: unable to parse SMART data for disk %s.", name);
146 return;
147 }
149 /* Get some specific values */
150 uint64_t value;
151 if (sk_disk_smart_get_power_on(d, &value) >= 0)
152 smart_submit(name, "smart_poweron", "", ((gauge_t)value) / 1000.);
153 else
154 DEBUG("smart plugin: unable to get milliseconds since power on for %s.",
155 name);
157 if (sk_disk_smart_get_power_cycle(d, &value) >= 0)
158 smart_submit(name, "smart_powercycles", "", (gauge_t)value);
159 else
160 DEBUG("smart plugin: unable to get number of power cycles for %s.", name);
162 if (sk_disk_smart_get_bad(d, &value) >= 0)
163 smart_submit(name, "smart_badsectors", "", (gauge_t)value);
164 else
165 DEBUG("smart plugin: unable to get number of bad sectors for %s.", name);
167 if (sk_disk_smart_get_temperature(d, &value) >= 0)
168 smart_submit(name, "smart_temperature", "",
169 ((gauge_t)value) / 1000. - 273.15);
170 else
171 DEBUG("smart plugin: unable to get temperature for %s.", name);
173 /* Grab all attributes */
174 if (sk_disk_smart_parse_attributes(d, handle_attribute, (void *)name) < 0) {
175 ERROR("smart plugin: unable to handle SMART attributes for %s.", name);
176 }
177 }
179 static void smart_handle_disk(const char *dev, const char *serial) {
180 SkDisk *d = NULL;
181 const char *name;
183 if (use_serial && serial) {
184 name = serial;
185 } else {
186 name = strrchr(dev, '/');
187 if (!name)
188 return;
189 name++;
190 }
191 if (ignorelist_match(ignorelist, name) != 0) {
192 DEBUG("smart plugin: ignoring %s.", dev);
193 return;
194 }
196 DEBUG("smart plugin: checking SMART status of %s.", dev);
197 if (sk_disk_open(dev, &d) < 0) {
198 ERROR("smart plugin: unable to open %s.", dev);
199 return;
200 }
202 smart_read_disk(d, name);
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 */