6c01e677d94b8ac4647b99ab95263a5648f0525f
[collectd.git] / src / smart.c
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  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "utils_ignorelist.h"
31
32 #include <atasmart.h>
33 #include <libudev.h>
34
35 static const char *config_keys[] =
36 {
37   "Disk",
38   "IgnoreSelected"
39 };
40
41 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
42
43 static ignorelist_t *ignorelist = NULL;
44
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);
51
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   }
67
68   return (0);
69 } /* int smart_config */
70
71 static void smart_submit (const char *dev, char *type, char *type_inst, double value)
72 {
73         value_t values[1];
74         value_list_t vl = VALUE_LIST_INIT;
75
76         values[0].gauge = value;
77
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));
85
86         plugin_dispatch_values (&vl);
87 }
88
89 static void smart_handle_disk_attribute(SkDisk *d, const SkSmartAttributeParsedData *a,
90                                         void* userdata)
91 {
92   const char *dev = userdata;
93   value_t values[4];
94   value_list_t vl = VALUE_LIST_INIT;
95
96   if (!a->current_value_valid || !a->worst_value_valid) 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;
101
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, dev, sizeof (vl.plugin_instance));
107   sstrncpy (vl.type, "smart_attribute", sizeof (vl.type));
108   sstrncpy (vl.type_instance, a->name, sizeof (vl.type_instance));
109
110   plugin_dispatch_values (&vl);
111 }
112
113 static void smart_handle_disk (const char *dev)
114 {
115   SkDisk *d = NULL;
116   SkBool awake = FALSE;
117   SkBool available = FALSE;
118   const char *shortname;
119   const SkSmartParsedData *spd;
120   uint64_t poweron, powercycles, badsectors, temperature;
121
122   shortname = strrchr(dev, '/');
123   if (!shortname) return;
124   shortname++;
125   if (ignorelist_match (ignorelist, shortname) != 0) {
126     DEBUG ("smart plugin: ignoring %s.", dev);
127     return;
128   }
129
130   DEBUG ("smart plugin: checking SMART status of %s.",
131          dev);
132
133   if (sk_disk_open (dev, &d) < 0)
134   {
135     ERROR ("smart plugin: unable to open %s.", dev);
136     return;
137   }
138   if (sk_disk_identify_is_available (d, &available) < 0 || !available)
139   {
140     DEBUG ("smart plugin: disk %s cannot be identified.", dev);
141     goto end;
142   }
143   if (sk_disk_smart_is_available (d, &available) < 0 || !available)
144   {
145     DEBUG ("smart plugin: disk %s has no SMART support.", dev);
146     goto end;
147   }
148   if (sk_disk_check_sleep_mode (d, &awake) < 0 || !awake)
149   {
150     DEBUG ("smart plugin: disk %s is sleeping.", dev);
151     goto end;
152   }
153   if (sk_disk_smart_read_data (d) < 0)
154   {
155     ERROR ("smart plugin: unable to get SMART data for disk %s.", dev);
156     goto end;
157   }
158   if (sk_disk_smart_parse (d, &spd) < 0)
159   {
160     ERROR ("smart plugin: unable to parse SMART data for disk %s.", dev);
161     goto end;
162   }
163
164   /* Get some specific values */
165   if (sk_disk_smart_get_power_on (d, &poweron) < 0)
166   {
167     WARNING ("smart plugin: unable to get milliseconds since power on for %s.",
168              dev);
169   }
170   else
171     smart_submit (shortname, "smart_poweron", "", poweron / 1000.);
172
173   if (sk_disk_smart_get_power_cycle (d, &powercycles) < 0)
174   {
175     WARNING ("smart plugin: unable to get number of power cycles for %s.",
176              dev);
177   }
178   else
179     smart_submit (shortname, "smart_powercycles", "", powercycles);
180
181   if (sk_disk_smart_get_bad (d, &badsectors) < 0)
182   {
183     WARNING ("smart plugin: unable to get number of bad sectors for %s.",
184              dev);
185   }
186   else
187     smart_submit (shortname, "smart_badsectors", "", badsectors);
188
189   if (sk_disk_smart_get_temperature (d, &temperature) < 0)
190   {
191     WARNING ("smart plugin: unable to get temperature for %s.",
192              dev);
193   }
194   else
195     smart_submit (shortname, "smart_temperature", "", temperature / 1000. - 273.15);
196
197   /* Grab all attributes */
198   if (sk_disk_smart_parse_attributes(d, smart_handle_disk_attribute,
199                                      (char *)shortname) < 0)
200   {
201     ERROR ("smart plugin: unable to handle SMART attributes for %s.",
202            dev);
203   }
204
205 end:
206   sk_disk_free(d);
207 }
208
209 static int smart_read (void)
210 {
211   struct udev *handle_udev;
212   struct udev_enumerate *enumerate;
213   struct udev_list_entry *devices, *dev_list_entry;
214   struct udev_device *dev;
215
216   /* Use udev to get a list of disks */
217   handle_udev = udev_new();
218   if (!handle_udev)
219   {
220     ERROR ("smart plugin: unable to initialize udev.");
221     return (-1);
222   }
223   enumerate = udev_enumerate_new (handle_udev);
224   udev_enumerate_add_match_subsystem (enumerate, "block");
225   udev_enumerate_add_match_property (enumerate, "DEVTYPE", "disk");
226   udev_enumerate_scan_devices (enumerate);
227   devices = udev_enumerate_get_list_entry (enumerate);
228   udev_list_entry_foreach (dev_list_entry, devices)
229   {
230     const char *path, *devpath;
231     path = udev_list_entry_get_name (dev_list_entry);
232     dev = udev_device_new_from_syspath (handle_udev, path);
233     devpath = udev_device_get_devnode (dev);
234
235     /* Query status with libatasmart */
236     smart_handle_disk (devpath);
237   }
238
239   udev_enumerate_unref (enumerate);
240   udev_unref (handle_udev);
241
242   return (0);
243 } /* int smart_read */
244
245 void module_register (void)
246 {
247   plugin_register_config ("smart", smart_config,
248                           config_keys, config_keys_num);
249   plugin_register_read ("smart", smart_read);
250 } /* void module_register */