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"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_ignorelist.h"
32 #include <atasmart.h>
33 #include <libudev.h>
35 static const char *config_keys[] =
36 {
37 "Disk",
38 "IgnoreSelected"
39 };
41 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43 static ignorelist_t *ignorelist = NULL;
45 static int smart_config (const char *key, const char *value)
46 {
47 if (ignorelist == NULL)
48 ignorelist = ignorelist_create (/* invert = */ 1);
49 if (ignorelist == NULL)
50 return (1);
52 if (strcasecmp ("Disk", key) == 0)
53 {
54 ignorelist_add (ignorelist, value);
55 }
56 else if (strcasecmp ("IgnoreSelected", key) == 0)
57 {
58 int invert = 1;
59 if (IS_TRUE (value))
60 invert = 0;
61 ignorelist_set_invert (ignorelist, invert);
62 }
63 else
64 {
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 {
74 value_t values[1];
75 value_list_t vl = VALUE_LIST_INIT;
77 values[0].gauge = value;
79 vl.values = values;
80 vl.values_len = 1;
81 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
82 sstrncpy (vl.plugin, "smart", sizeof (vl.plugin));
83 sstrncpy (vl.plugin_instance, dev, sizeof (vl.plugin_instance));
84 sstrncpy (vl.type, type, sizeof (vl.type));
85 sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
87 plugin_dispatch_values (&vl);
88 }
90 static void smart_handle_disk_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
91 void* userdata)
92 {
93 const char *dev = userdata;
94 value_t values[4];
95 value_list_t vl = VALUE_LIST_INIT;
97 if (!a->current_value_valid || !a->worst_value_valid) return;
98 values[0].gauge = a->current_value;
99 values[1].gauge = a->worst_value;
100 values[2].gauge = a->threshold_valid?a->threshold:0;
101 values[3].gauge = a->pretty_value;
103 vl.values = values;
104 vl.values_len = 4;
105 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
106 sstrncpy (vl.plugin, "smart", sizeof (vl.plugin));
107 sstrncpy (vl.plugin_instance, dev, 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 {
115 notification_t notif = { NOTIF_WARNING,
116 cdtime (),
117 "",
118 "",
119 "smart", "",
120 "smart_attribute",
121 "",
122 NULL };
123 sstrncpy (notif.host, hostname_g, sizeof (notif.host));
124 sstrncpy (notif.plugin_instance, dev, sizeof (notif.plugin_instance));
125 sstrncpy (notif.type_instance, a->name, sizeof (notif.type_instance));
126 ssnprintf (notif.message, sizeof (notif.message),
127 "attribute %s is below allowed threshold (%d < %d)",
128 a->name, a->current_value, a->threshold);
129 plugin_dispatch_notification (¬if);
130 }
131 }
133 static void smart_handle_disk (const char *dev)
134 {
135 SkDisk *d = NULL;
136 SkBool awake = FALSE;
137 SkBool available = FALSE;
138 const char *shortname;
139 const SkSmartParsedData *spd;
140 uint64_t poweron, powercycles, badsectors, temperature;
142 shortname = strrchr(dev, '/');
143 if (!shortname) return;
144 shortname++;
145 if (ignorelist_match (ignorelist, shortname) != 0) {
146 DEBUG ("smart plugin: ignoring %s.", dev);
147 return;
148 }
150 DEBUG ("smart plugin: checking SMART status of %s.",
151 dev);
153 if (sk_disk_open (dev, &d) < 0)
154 {
155 ERROR ("smart plugin: unable to open %s.", dev);
156 return;
157 }
158 if (sk_disk_identify_is_available (d, &available) < 0 || !available)
159 {
160 DEBUG ("smart plugin: disk %s cannot be identified.", dev);
161 goto end;
162 }
163 if (sk_disk_smart_is_available (d, &available) < 0 || !available)
164 {
165 DEBUG ("smart plugin: disk %s has no SMART support.", dev);
166 goto end;
167 }
168 if (sk_disk_check_sleep_mode (d, &awake) < 0 || !awake)
169 {
170 DEBUG ("smart plugin: disk %s is sleeping.", dev);
171 goto end;
172 }
173 if (sk_disk_smart_read_data (d) < 0)
174 {
175 ERROR ("smart plugin: unable to get SMART data for disk %s.", dev);
176 goto end;
177 }
178 if (sk_disk_smart_parse (d, &spd) < 0)
179 {
180 ERROR ("smart plugin: unable to parse SMART data for disk %s.", dev);
181 goto end;
182 }
184 /* Get some specific values */
185 if (sk_disk_smart_get_power_on (d, &poweron) < 0)
186 {
187 WARNING ("smart plugin: unable to get milliseconds since power on for %s.",
188 dev);
189 }
190 else
191 smart_submit (shortname, "smart_poweron", "", poweron / 1000.);
193 if (sk_disk_smart_get_power_cycle (d, &powercycles) < 0)
194 {
195 WARNING ("smart plugin: unable to get number of power cycles for %s.",
196 dev);
197 }
198 else
199 smart_submit (shortname, "smart_powercycles", "", powercycles);
201 if (sk_disk_smart_get_bad (d, &badsectors) < 0)
202 {
203 WARNING ("smart plugin: unable to get number of bad sectors for %s.",
204 dev);
205 }
206 else
207 smart_submit (shortname, "smart_badsectors", "", badsectors);
209 if (sk_disk_smart_get_temperature (d, &temperature) < 0)
210 {
211 WARNING ("smart plugin: unable to get temperature for %s.",
212 dev);
213 }
214 else
215 smart_submit (shortname, "smart_temperature", "", temperature / 1000. - 273.15);
217 /* Grab all attributes */
218 if (sk_disk_smart_parse_attributes(d, smart_handle_disk_attribute,
219 (char *)shortname) < 0)
220 {
221 ERROR ("smart plugin: unable to handle SMART attributes for %s.",
222 dev);
223 }
225 end:
226 sk_disk_free(d);
227 }
229 static int smart_read (void)
230 {
231 struct udev *handle_udev;
232 struct udev_enumerate *enumerate;
233 struct udev_list_entry *devices, *dev_list_entry;
234 struct udev_device *dev;
236 /* Use udev to get a list of disks */
237 handle_udev = udev_new();
238 if (!handle_udev)
239 {
240 ERROR ("smart plugin: unable to initialize udev.");
241 return (-1);
242 }
243 enumerate = udev_enumerate_new (handle_udev);
244 udev_enumerate_add_match_subsystem (enumerate, "block");
245 udev_enumerate_add_match_property (enumerate, "DEVTYPE", "disk");
246 udev_enumerate_scan_devices (enumerate);
247 devices = udev_enumerate_get_list_entry (enumerate);
248 udev_list_entry_foreach (dev_list_entry, devices)
249 {
250 const char *path, *devpath;
251 path = udev_list_entry_get_name (dev_list_entry);
252 dev = udev_device_new_from_syspath (handle_udev, path);
253 devpath = udev_device_get_devnode (dev);
255 /* Query status with libatasmart */
256 smart_handle_disk (devpath);
257 udev_device_unref (dev);
258 }
260 udev_enumerate_unref (enumerate);
261 udev_unref (handle_udev);
263 return (0);
264 } /* int smart_read */
266 void module_register (void)
267 {
268 plugin_register_config ("smart", smart_config,
269 config_keys, config_keys_num);
270 plugin_register_read ("smart", smart_read);
271 } /* void module_register */