Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / hddtemp.c
1 /**
2  * collectd - src/hddtemp.c
3  * Copyright (C) 2005,2006  Vincent StehlĂ©
4  * Copyright (C) 2006,2007  Florian octo Forster
5  * Copyright (C) 2008       Sebastian Harl
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  * Authors:
22  *   Vincent StehlĂ© <vincent.stehle at free.fr>
23  *   Florian octo Forster <octo at verplant.org>
24  *   Sebastian Harl <sh at tokkee.org>
25  *
26  * TODO:
27  *   Do a pass, some day, and spare some memory. We consume too much for now
28  *   in string buffers and the like.
29  *
30  **/
31
32 #include "collectd.h"
33 #include "common.h"
34 #include "plugin.h"
35 #include "configfile.h"
36
37 # include <netdb.h>
38 # include <sys/socket.h>
39 # include <netinet/in.h>
40 # include <netinet/tcp.h>
41 # include <libgen.h> /* for basename */
42
43 #if HAVE_LINUX_MAJOR_H
44 # include <linux/major.h>
45 #endif
46
47 #define HDDTEMP_DEF_HOST "127.0.0.1"
48 #define HDDTEMP_DEF_PORT "7634"
49
50 static const char *config_keys[] =
51 {
52         "Host",
53         "Port",
54         "TranslateDevicename"
55 };
56 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
57
58 typedef struct hddname
59 {
60         int major;
61         int minor;
62         char *name;
63         struct hddname *next;
64 } hddname_t;
65
66 static hddname_t *first_hddname = NULL;
67 static char *hddtemp_host = NULL;
68 static char hddtemp_port[16];
69 static int translate_devicename = 1;
70
71 /*
72  * NAME
73  *  hddtemp_query_daemon
74  *
75  * DESCRIPTION
76  * Connect to the hddtemp daemon and receive data.
77  *
78  * ARGUMENTS:
79  *  `buffer'            The buffer where we put the received ascii string.
80  *  `buffer_size'       Size of the buffer
81  *
82  * RETURN VALUE:
83  *   >= 0 if ok, < 0 otherwise.
84  *
85  * NOTES:
86  *  Example of possible strings, as received from daemon:
87  *    |/dev/hda|ST340014A|36|C|
88  *    |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
89  *
90  * FIXME:
91  *  we need to create a new socket each time. Is there another way?
92  *  Hm, maybe we can re-use the `sockaddr' structure? -octo
93  */
94 static int hddtemp_query_daemon (char *buffer, int buffer_size)
95 {
96         int fd;
97         ssize_t status;
98         int buffer_fill;
99
100         const char *host;
101         const char *port;
102
103         struct addrinfo  ai_hints;
104         struct addrinfo *ai_list, *ai_ptr;
105         int              ai_return;
106
107         memset (&ai_hints, '\0', sizeof (ai_hints));
108         ai_hints.ai_flags    = 0;
109 #ifdef AI_ADDRCONFIG
110         ai_hints.ai_flags   |= AI_ADDRCONFIG;
111 #endif
112         ai_hints.ai_family   = PF_UNSPEC;
113         ai_hints.ai_socktype = SOCK_STREAM;
114         ai_hints.ai_protocol = IPPROTO_TCP;
115
116         host = hddtemp_host;
117         if (host == NULL)
118                 host = HDDTEMP_DEF_HOST;
119
120         port = hddtemp_port;
121         if (strlen (port) == 0)
122                 port = HDDTEMP_DEF_PORT;
123
124         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
125         {
126                 char errbuf[1024];
127                 ERROR ("hddtemp plugin: getaddrinfo (%s, %s): %s",
128                                 host, port,
129                                 (ai_return == EAI_SYSTEM)
130                                 ? sstrerror (errno, errbuf, sizeof (errbuf))
131                                 : gai_strerror (ai_return));
132                 return (-1);
133         }
134
135         fd = -1;
136         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
137         {
138                 /* create our socket descriptor */
139                 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
140                                 ai_ptr->ai_protocol);
141                 if (fd < 0)
142                 {
143                         char errbuf[1024];
144                         ERROR ("hddtemp plugin: socket: %s",
145                                         sstrerror (errno, errbuf, sizeof (errbuf)));
146                         continue;
147                 }
148
149                 /* connect to the hddtemp daemon */
150                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr,
151                                         ai_ptr->ai_addrlen))
152                 {
153                         char errbuf[1024];
154                         INFO ("hddtemp plugin: connect (%s, %s) failed: %s",
155                                         host, port,
156                                         sstrerror (errno, errbuf, sizeof (errbuf)));
157                         close (fd);
158                         fd = -1;
159                         continue;
160                 }
161
162                 /* A socket could be opened and connecting succeeded. We're
163                  * done. */
164                 break;
165         }
166
167         freeaddrinfo (ai_list);
168
169         if (fd < 0)
170         {
171                 ERROR ("hddtemp plugin: Could not connect to daemon.");
172                 return (-1);
173         }
174
175         /* receive data from the hddtemp daemon */
176         memset (buffer, '\0', buffer_size);
177
178         buffer_fill = 0;
179         while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
180         {
181                 if (status == -1)
182                 {
183                         char errbuf[1024];
184
185                         if ((errno == EAGAIN) || (errno == EINTR))
186                                 continue;
187
188                         ERROR ("hddtemp plugin: Error reading from socket: %s",
189                                         sstrerror (errno, errbuf, sizeof (errbuf)));
190                         close (fd);
191                         return (-1);
192                 }
193                 buffer_fill += status;
194
195                 if (buffer_fill >= buffer_size)
196                         break;
197         }
198
199         if (buffer_fill >= buffer_size)
200         {
201                 buffer[buffer_size - 1] = '\0';
202                 WARNING ("hddtemp plugin: Message from hddtemp has been "
203                                 "truncated.");
204         }
205         else if (buffer_fill == 0)
206         {
207                 WARNING ("hddtemp plugin: Peer has unexpectedly shut down "
208                                 "the socket. Buffer: `%s'", buffer);
209                 close (fd);
210                 return (-1);
211         }
212
213         close (fd);
214         return (0);
215 }
216
217 static int hddtemp_config (const char *key, const char *value)
218 {
219         if (strcasecmp (key, "Host") == 0)
220         {
221                 if (hddtemp_host != NULL)
222                         free (hddtemp_host);
223                 hddtemp_host = strdup (value);
224         }
225         else if (strcasecmp (key, "Port") == 0)
226         {
227                 int port = (int) (atof (value));
228                 if ((port > 0) && (port <= 65535))
229                         ssnprintf (hddtemp_port, sizeof (hddtemp_port),
230                                         "%i", port);
231                 else
232                         sstrncpy (hddtemp_port, value, sizeof (hddtemp_port));
233         }
234         else if (strcasecmp (key, "TranslateDevicename") == 0)
235         {
236                 if ((strcasecmp ("true", value) == 0)
237                                 || (strcasecmp ("yes", value) == 0)
238                                 || (strcasecmp ("on", value) == 0))
239                         translate_devicename = 1;
240                 else
241                         translate_devicename = 0;
242         }
243         else
244         {
245                 return (-1);
246         }
247
248         return (0);
249 }
250
251 /* In the init-function we initialize the `hddname_t' list used to translate
252  * disk-names. Under Linux that's done using `/proc/partitions'. Under other
253  * operating-systems, it's not done at all. */
254 static int hddtemp_init (void)
255 {
256 #if KERNEL_LINUX
257         FILE *fh;
258         char buf[1024];
259         int buflen;
260
261         char *fields[16];
262         int num_fields;
263
264         int major;
265         int minor;
266         char *name;
267         hddname_t *next;
268         hddname_t *entry;
269
270         next = first_hddname;
271         while (next != NULL)
272         {
273                 entry = next;
274                 next = entry->next;
275
276                 free (entry->name);
277                 free (entry);
278         }
279         first_hddname = NULL;
280
281         if ((fh = fopen ("/proc/partitions", "r")) != NULL)
282         {
283                 DEBUG ("hddtemp plugin: Looking at /proc/partitions...");
284
285                 while (fgets (buf, sizeof (buf), fh) != NULL)
286                 {
287                         /* Delete trailing newlines */
288                         buflen = strlen (buf);
289
290                         while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
291                                 buf[--buflen] = '\0';
292
293                         /* We want lines of the form:
294                          *
295                          *     3     1   77842926 hda1
296                          *
297                          * ...so, skip everything else. */
298                         if (buflen == 0)
299                                 continue;
300                         
301                         num_fields = strsplit (buf, fields, 16);
302
303                         if (num_fields != 4)
304                                 continue;
305
306                         major = atoi (fields[0]);
307                         minor = atoi (fields[1]);
308
309                         /* We try to keep only entries, which may correspond to
310                          * physical disks and that may have a corresponding
311                          * entry in the hddtemp daemon. Basically, this means
312                          * IDE and SCSI. */
313                         switch (major)
314                         {
315                                 /* SCSI. */
316                                 case SCSI_DISK0_MAJOR:
317                                 case SCSI_DISK1_MAJOR:
318                                 case SCSI_DISK2_MAJOR:
319                                 case SCSI_DISK3_MAJOR:
320                                 case SCSI_DISK4_MAJOR:
321                                 case SCSI_DISK5_MAJOR:
322                                 case SCSI_DISK6_MAJOR:
323                                 case SCSI_DISK7_MAJOR:
324 #ifdef SCSI_DISK8_MAJOR
325                                 case SCSI_DISK8_MAJOR:
326                                 case SCSI_DISK9_MAJOR:
327                                 case SCSI_DISK10_MAJOR:
328                                 case SCSI_DISK11_MAJOR:
329                                 case SCSI_DISK12_MAJOR:
330                                 case SCSI_DISK13_MAJOR:
331                                 case SCSI_DISK14_MAJOR:
332                                 case SCSI_DISK15_MAJOR:
333 #endif /* SCSI_DISK8_MAJOR */
334                                         /* SCSI disks minors are multiples of 16.
335                                          * Keep only those. */
336                                         if (minor % 16)
337                                                 continue;
338                                         break;
339
340                                 /* IDE. */
341                                 case IDE0_MAJOR:
342                                 case IDE1_MAJOR:
343                                 case IDE2_MAJOR:
344                                 case IDE3_MAJOR:
345                                 case IDE4_MAJOR:
346                                 case IDE5_MAJOR:
347                                 case IDE6_MAJOR:
348                                 case IDE7_MAJOR:
349                                 case IDE8_MAJOR:
350                                 case IDE9_MAJOR:
351                                         /* IDE disks minors can only be 0 or 64.
352                                          * Keep only those. */
353                                         if(minor != 0 && minor != 64)
354                                                 continue;
355                                         break;
356
357                                 /* Skip all other majors. */
358                                 default:
359                                         DEBUG ("hddtemp plugin: Skipping unknown major %i", major);
360                                         continue;
361                         } /* switch (major) */
362
363                         if ((name = strdup (fields[3])) == NULL)
364                         {
365                                 ERROR ("hddtemp plugin: strdup(%s) == NULL", fields[3]);
366                                 continue;
367                         }
368
369                         if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
370                         {
371                                 ERROR ("hddtemp plugin: malloc (%u) == NULL",
372                                                 (unsigned int) sizeof (hddname_t));
373                                 free (name);
374                                 continue;
375                         }
376
377                         DEBUG ("hddtemp plugin: Found disk: %s (%u:%u).", name, major, minor);
378
379                         entry->major = major;
380                         entry->minor = minor;
381                         entry->name  = name;
382                         entry->next  = NULL;
383
384                         if (first_hddname == NULL)
385                         {
386                                 first_hddname = entry;
387                         }
388                         else
389                         {
390                                 entry->next = first_hddname;
391                                 first_hddname = entry;
392                         }
393                 }
394                 fclose (fh);
395         }
396 #if COLLECT_DEBUG
397         else
398         {
399                 char errbuf[1024];
400                 DEBUG ("hddtemp plugin: Could not open /proc/partitions: %s",
401                                 sstrerror (errno, errbuf, sizeof (errbuf)));
402         }
403 #endif /* COLLECT_DEBUG */
404 #endif /* KERNEL_LINUX */
405
406         return (0);
407 } /* int hddtemp_init */
408
409 /*
410  * hddtemp_get_major_minor
411  *
412  * Description:
413  *   Try to "cook" a bit the drive name as returned
414  *   by the hddtemp daemon. The intend is to transform disk
415  *   names into <major>-<minor> when possible.
416  */
417 static char *hddtemp_get_major_minor (char *drive)
418 {
419         hddname_t *list;
420         char *ret;
421
422         for (list = first_hddname; list != NULL; list = list->next)
423                 if (strcmp (drive, list->name) == 0)
424                         break;
425
426         if (list == NULL)
427         {
428                 DEBUG ("hddtemp plugin: Don't know %s, keeping name as-is.", drive);
429                 return (strdup (drive));
430         }
431
432         if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
433                 return (NULL);
434
435         if (ssnprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
436         {
437                 free (ret);
438                 return (NULL);
439         }
440
441         return (ret);
442 }
443
444 static void hddtemp_submit (char *type_instance, double value)
445 {
446         value_t values[1];
447         value_list_t vl = VALUE_LIST_INIT;
448
449         values[0].gauge = value;
450
451         vl.values = values;
452         vl.values_len = 1;
453         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
454         sstrncpy (vl.plugin, "hddtemp", sizeof (vl.plugin));
455         sstrncpy (vl.type, "temperature", sizeof (vl.type));
456         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
457
458         plugin_dispatch_values (&vl);
459 }
460
461 static int hddtemp_read (void)
462 {
463         char buf[1024];
464         char *fields[128];
465         char *ptr;
466         char *saveptr;
467         int num_fields;
468         int num_disks;
469         int i;
470
471         /* get data from daemon */
472         if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
473                 return (-1);
474
475         /* NB: strtok_r will eat up "||" and leading "|"'s */
476         num_fields = 0;
477         ptr = buf;
478         saveptr = NULL;
479         while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
480         {
481                 ptr = NULL;
482                 num_fields++;
483
484                 if (num_fields >= 128)
485                         break;
486         }
487
488         num_disks = num_fields / 4;
489
490         for (i = 0; i < num_disks; i++)
491         {
492                 char *name, *major_minor;
493                 double temperature;
494                 char *mode;
495
496                 mode = fields[4*i + 3];
497                 name = basename (fields[4*i + 0]);
498
499                 /* Skip non-temperature information */
500                 if (mode[0] != 'C' && mode[0] != 'F')
501                         continue;
502
503                 temperature = atof (fields[4*i + 2]);
504
505                 /* Convert farenheit to celsius */
506                 if (mode[0] == 'F')
507                         temperature = (temperature - 32.0) * 5.0 / 9.0;
508
509                 if (translate_devicename
510                                 && (major_minor = hddtemp_get_major_minor (name)) != NULL)
511                 {
512                         hddtemp_submit (major_minor, temperature);
513                         free (major_minor);
514                 }
515                 else
516                 {
517                         hddtemp_submit (name, temperature);
518                 }
519         }
520         
521         return (0);
522 } /* int hddtemp_read */
523
524 /* module_register
525    Register collectd plugin. */
526 void module_register (void)
527 {
528         plugin_register_config ("hddtemp", hddtemp_config,
529                         config_keys, config_keys_num);
530         plugin_register_init ("hddtemp", hddtemp_init);
531         plugin_register_read ("hddtemp", hddtemp_read);
532 }