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_t values[1];
74 value_list_t vl = VALUE_LIST_INIT;
76 values[0].gauge = value;
78 vl.values = values;
79 vl.values_len = 1;
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);
87 }
89 static void handle_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
90 void *userdata) {
91 char const *name = userdata;
92 value_t values[4];
93 value_list_t vl = VALUE_LIST_INIT;
95 if (!a->current_value_valid || !a->worst_value_valid)
96 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;
102 vl.values = values;
103 vl.values_len = 4;
104 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
105 sstrncpy(vl.plugin, "smart", sizeof(vl.plugin));
106 sstrncpy(vl.plugin_instance, name, 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) {
113 notification_t notif = {NOTIF_WARNING, cdtime(), "", "", "smart", "",
114 "smart_attribute", "", NULL};
115 sstrncpy(notif.host, hostname_g, sizeof(notif.host));
116 sstrncpy(notif.plugin_instance, name, sizeof(notif.plugin_instance));
117 sstrncpy(notif.type_instance, a->name, sizeof(notif.type_instance));
118 ssnprintf(notif.message, sizeof(notif.message),
119 "attribute %s is below allowed threshold (%d < %d)", a->name,
120 a->current_value, a->threshold);
121 plugin_dispatch_notification(¬if);
122 }
123 }
125 static void smart_read_disk(SkDisk *d, char const *name) {
126 SkBool available = FALSE;
127 if (sk_disk_identify_is_available(d, &available) < 0 || !available) {
128 DEBUG("smart plugin: disk %s cannot be identified.", name);
129 return;
130 }
131 if (sk_disk_smart_is_available(d, &available) < 0 || !available) {
132 DEBUG("smart plugin: disk %s has no SMART support.", name);
133 return;
134 }
135 if (!ignore_sleep_mode) {
136 SkBool awake = FALSE;
137 if (sk_disk_check_sleep_mode(d, &awake) < 0 || !awake) {
138 DEBUG("smart plugin: disk %s is sleeping.", name);
139 return;
140 }
141 }
142 if (sk_disk_smart_read_data(d) < 0) {
143 ERROR("smart plugin: unable to get SMART data for disk %s.", name);
144 return;
145 }
147 if (sk_disk_smart_parse(d, &(SkSmartParsedData const *){NULL}) < 0) {
148 ERROR("smart plugin: unable to parse SMART data for disk %s.", name);
149 return;
150 }
152 /* Get some specific values */
153 uint64_t value;
154 if (sk_disk_smart_get_power_on(d, &value) >= 0)
155 smart_submit(name, "smart_poweron", "", ((gauge_t)value) / 1000.);
156 else
157 DEBUG("smart plugin: unable to get milliseconds since power on for %s.",
158 name);
160 if (sk_disk_smart_get_power_cycle(d, &value) >= 0)
161 smart_submit(name, "smart_powercycles", "", (gauge_t)value);
162 else
163 DEBUG("smart plugin: unable to get number of power cycles for %s.", name);
165 if (sk_disk_smart_get_bad(d, &value) >= 0)
166 smart_submit(name, "smart_badsectors", "", (gauge_t)value);
167 else
168 DEBUG("smart plugin: unable to get number of bad sectors for %s.", name);
170 if (sk_disk_smart_get_temperature(d, &value) >= 0)
171 smart_submit(name, "smart_temperature", "",
172 ((gauge_t)value) / 1000. - 273.15);
173 else
174 DEBUG("smart plugin: unable to get temperature for %s.", name);
176 /* Grab all attributes */
177 if (sk_disk_smart_parse_attributes(d, handle_attribute, (void *)name) < 0) {
178 ERROR("smart plugin: unable to handle SMART attributes for %s.", name);
179 }
180 }
182 static void smart_handle_disk(const char *dev, const char *serial) {
183 SkDisk *d = NULL;
184 const char *name;
186 if (use_serial && serial) {
187 name = serial;
188 } else {
189 name = strrchr(dev, '/');
190 if (!name)
191 return;
192 name++;
193 }
194 if (ignorelist_match(ignorelist, name) != 0) {
195 DEBUG("smart plugin: ignoring %s.", dev);
196 return;
197 }
199 DEBUG("smart plugin: checking SMART status of %s.", dev);
200 if (sk_disk_open(dev, &d) < 0) {
201 ERROR("smart plugin: unable to open %s.", dev);
202 return;
203 }
205 smart_read_disk(d, name);
206 sk_disk_free(d);
207 }
209 static int smart_read(void) {
210 struct udev *handle_udev;
211 struct udev_enumerate *enumerate;
212 struct udev_list_entry *devices, *dev_list_entry;
213 struct udev_device *dev;
215 /* Use udev to get a list of disks */
216 handle_udev = udev_new();
217 if (!handle_udev) {
218 ERROR("smart plugin: unable to initialize udev.");
219 return (-1);
220 }
221 enumerate = udev_enumerate_new(handle_udev);
222 udev_enumerate_add_match_subsystem(enumerate, "block");
223 udev_enumerate_add_match_property(enumerate, "DEVTYPE", "disk");
224 udev_enumerate_scan_devices(enumerate);
225 devices = udev_enumerate_get_list_entry(enumerate);
226 udev_list_entry_foreach(dev_list_entry, devices) {
227 const char *path, *devpath, *serial;
228 path = udev_list_entry_get_name(dev_list_entry);
229 dev = udev_device_new_from_syspath(handle_udev, path);
230 devpath = udev_device_get_devnode(dev);
231 serial = udev_device_get_property_value(dev, "ID_SERIAL");
233 /* Query status with libatasmart */
234 smart_handle_disk(devpath, serial);
235 udev_device_unref(dev);
236 }
238 udev_enumerate_unref(enumerate);
239 udev_unref(handle_udev);
241 return (0);
242 } /* int smart_read */
244 void module_register(void) {
245 plugin_register_config("smart", smart_config, config_keys, config_keys_num);
246 plugin_register_read("smart", smart_read);
247 } /* void module_register */