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