331bbb56ef9c1dc5b16079a8e86e63a53e4aca18
[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 #include "utils_debug.h"
34
35 #define MODULE_NAME "hddtemp"
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 #ifdef KERNEL_LINUX
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 /* BUFFER_SIZE
51    Size of the buffer we use to receive from the hddtemp daemon. */
52 #define BUFFER_SIZE 1024
53
54 static char *filename_format = "hddtemp-%s.rrd";
55
56 static char *ds_def[] =
57 {
58         "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
59         NULL
60 };
61 static int ds_num = 1;
62
63 static char *config_keys[] =
64 {
65         "Host",
66         "Port",
67         NULL
68 };
69 static int config_keys_num = 2;
70
71 typedef struct hddname
72 {
73         int major;
74         int minor;
75         char *name;
76         struct hddname *next;
77 } hddname_t;
78
79 static hddname_t *first_hddname = NULL;
80 static char *hddtemp_host = NULL;
81 static char *hddtemp_port = NULL;
82
83 /*
84  * NAME
85  *  hddtemp_query_daemon
86  *
87  * DESCRIPTION
88  * Connect to the hddtemp daemon and receive data.
89  *
90  * ARGUMENTS:
91  *  `buffer'            The buffer where we put the received ascii string.
92  *  `buffer_size'       Size of the buffer
93  *
94  * RETURN VALUE:
95  *   >= 0 if ok, < 0 otherwise.
96  *
97  * NOTES:
98  *  Example of possible strings, as received from daemon:
99  *    |/dev/hda|ST340014A|36|C|
100  *    |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
101  *
102  * FIXME:
103  *  we need to create a new socket each time. Is there another way?
104  *  Hm, maybe we can re-use the `sockaddr' structure? -octo
105  */
106 static int hddtemp_query_daemon (char *buffer, int buffer_size)
107 {
108         int fd;
109         ssize_t status;
110         int buffer_fill;
111
112         const char *host;
113         const char *port;
114
115         struct addrinfo  ai_hints;
116         struct addrinfo *ai_list, *ai_ptr;
117         int              ai_return;
118
119         memset (&ai_hints, '\0', sizeof (ai_hints));
120         ai_hints.ai_flags    = AI_ADDRCONFIG;
121         ai_hints.ai_family   = PF_UNSPEC;
122         ai_hints.ai_socktype = SOCK_STREAM;
123         ai_hints.ai_protocol = IPPROTO_TCP;
124
125         host = hddtemp_host;
126         if (host == NULL)
127                 host = HDDTEMP_DEF_HOST;
128
129         port = hddtemp_port;
130         if (port == NULL)
131                 port = HDDTEMP_DEF_PORT;
132
133     DBG ("Connect to %s:%s", host, port);
134
135         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
136         {
137                 syslog (LOG_ERR, "hddtemp: getaddrinfo (%s, %s): %s",
138                                 host, port,
139                                 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
140                 return (-1);
141         }
142
143         fd = -1;
144         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
145         {
146                 /* create our socket descriptor */
147                 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
148                 {
149                         syslog (LOG_ERR, "hddtemp: socket: %s",
150                                         strerror (errno));
151                         continue;
152                 }
153
154                 /* connect to the hddtemp daemon */
155                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
156                 {
157                         syslog (LOG_ERR, "hddtemp: connect (%s, %s): %s",
158                                         host, port, strerror (errno));
159                         close (fd);
160                         fd = -1;
161                         continue;
162                 }
163         }
164
165         freeaddrinfo (ai_list);
166
167         if (fd < 0){
168                 syslog (LOG_ERR, "hddtemp: 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                         if ((errno == EAGAIN) || (errno == EINTR))
181                                 continue;
182
183                         syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
184                                                 strerror (errno));
185                         return (-1);
186                 }
187                 buffer_fill += status;
188
189                 if (buffer_fill >= buffer_size)
190                         break;
191         }
192
193         if (buffer_fill >= buffer_size)
194         {
195                 buffer[buffer_size - 1] = '\0';
196                 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
197         }
198         else if (buffer_fill == 0)
199         {
200                 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
201                                 "Buffer: `%s'", buffer);
202                 close (fd);
203                 return (-1);
204         }
205
206         close (fd);
207         return (0);
208 }
209
210 static int hddtemp_config (char *key, char *value)
211 {
212         if (strcasecmp (key, "host") == 0)
213         {
214                 if (hddtemp_host != NULL)
215                         free (hddtemp_host);
216                 hddtemp_host = strdup (value);
217         }
218         else if (strcasecmp (key, "port") == 0)
219         {
220                 if (hddtemp_port != NULL)
221                         free (hddtemp_port);
222                 hddtemp_port = strdup (value);
223         }
224         else
225         {
226                 return (-1);
227         }
228
229         return (0);
230 }
231
232 static void hddtemp_init (void)
233 {
234         FILE *fh;
235         char buf[BUFFER_SIZE];
236         int buflen;
237
238         char *fields[16];
239         int num_fields;
240
241         int major;
242         int minor;
243         char *name;
244         hddname_t *next;
245         hddname_t *entry;
246
247         next = first_hddname;
248         while (next != NULL)
249         {
250                 entry = next;
251                 next = entry->next;
252
253                 free (entry->name);
254                 free (entry);
255         }
256         first_hddname = NULL;
257
258         if ((fh = fopen ("/proc/partitions", "r")) != NULL)
259         {
260         DBG ("Looking at /proc/partitions...");
261
262                 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
263                 {
264                         /* Delete trailing newlines */
265                         buflen = strlen (buf);
266
267                         while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
268                                 buf[--buflen] = '\0';
269
270             /* We want lines of the form:
271              *
272              *     3     1   77842926 hda1
273              *
274              * ...so, skip everything else. */
275                         if (buflen == 0)
276                                 continue;
277                         
278                         num_fields = strsplit (buf, fields, 16);
279
280                         if (num_fields != 4)
281                                 continue;
282
283                         major = atoi (fields[0]);
284                         minor = atoi (fields[1]);
285
286             /* We try to keep only entries, which may correspond to
287              * physical disks and that may have a corresponding entry
288              * in the hddtemp daemon. Basically, this means IDE and SCSI. */
289             switch(major){
290 #           ifdef KERNEL_LINUX
291
292             /* SCSI. */
293             case SCSI_DISK0_MAJOR:
294             case SCSI_DISK1_MAJOR:
295             case SCSI_DISK2_MAJOR:
296             case SCSI_DISK3_MAJOR:
297             case SCSI_DISK4_MAJOR:
298             case SCSI_DISK5_MAJOR:
299             case SCSI_DISK6_MAJOR:
300             case SCSI_DISK7_MAJOR:
301             case SCSI_DISK8_MAJOR:
302             case SCSI_DISK9_MAJOR:
303             case SCSI_DISK10_MAJOR:
304             case SCSI_DISK11_MAJOR:
305             case SCSI_DISK12_MAJOR:
306             case SCSI_DISK13_MAJOR:
307             case SCSI_DISK14_MAJOR:
308             case SCSI_DISK15_MAJOR:
309                 /* SCSI disks minors are multiples of 16.
310                  * Keep only those. */
311                 if(minor % 16)
312                     continue;
313
314                 break;
315
316             /* IDE. */
317             case IDE0_MAJOR:
318             case IDE1_MAJOR:
319             case IDE2_MAJOR:
320             case IDE3_MAJOR:
321             case IDE4_MAJOR:
322             case IDE5_MAJOR:
323             case IDE6_MAJOR:
324             case IDE7_MAJOR:
325             case IDE8_MAJOR:
326             case IDE9_MAJOR:
327                 /* IDE disks minors can only be 0 or 64.
328                  * Keep only those. */
329                             if(minor != 0 && minor != 64)
330                     continue;
331
332                 break;
333
334             /* Skip all other majors. */
335             default:
336                 continue;
337
338 #           else   /* not KERNEL_LINUX */
339
340             /* VS: Do we need this on other systems?
341                    We tried to open /proc/partitions at first anyway,
342                    so maybe we know we are under Linux always? */
343                         case 0:
344                             /* I know that this makes `minor' redundant, but I want
345                              * to be able to change this beavior in the future..
346                              * And 4 or 8 bytes won't hurt anybody.. -octo */
347                             continue;
348
349             /* Unknown major. Keep for now.
350              * VS: refine as more cases are precisely known. */
351             default:
352                 break;
353
354 #           endif   /* KERNEL_LINUX */
355             }
356
357                         if ((name = strdup (fields[3])) == NULL){
358                         syslog (LOG_ERR, "hddtemp: NULL strdup(%s)!", fields[3]);
359                                 continue;
360             }
361
362                         if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
363                         {
364                         syslog (LOG_ERR, "hddtemp: NULL malloc(%u)!", sizeof (hddname_t));
365                                 free (name);
366                                 continue;
367                         }
368
369             DBG ("Found disk: %s (%u:%u).", name, major, minor);
370
371                         entry->major = major;
372                         entry->minor = minor;
373                         entry->name  = name;
374                         entry->next  = NULL;
375
376                         if (first_hddname == NULL)
377                         {
378                                 first_hddname = entry;
379                         }
380                         else
381                         {
382                                 entry->next = first_hddname;
383                                 first_hddname = entry;
384                         }
385                 }
386         } else
387         DBG ("Could not open /proc/partitions.");
388 }
389
390 static void hddtemp_write (char *host, char *inst, char *val)
391 {
392         char filename[BUFFER_SIZE];
393         int status;
394
395         /* construct filename */
396         status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
397         if (status < 1)
398                 return;
399         else if (status >= BUFFER_SIZE)
400                 return;
401
402         rrd_update_file (host, filename, val, ds_def, ds_num);
403 }
404
405 /*
406  * hddtemp_get_name
407  *
408  * Description:
409  *   Try to "cook" a bit the drive name as returned
410  *   by the hddtemp daemon. The intend is to transform disk
411  *   names into <major>-<minor> when possible.
412  */
413 static char *hddtemp_get_name (char *drive)
414 {
415         hddname_t *list;
416         char *ret;
417
418         for (list = first_hddname; list != NULL; list = list->next)
419                 if (strcmp (drive, list->name) == 0)
420                         break;
421
422         if (list == NULL){
423         DBG ("Don't know %s, keeping name as-is.", drive);
424                 return (strdup (drive));
425     }
426
427         if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
428                 return (NULL);
429
430         if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
431         {
432                 free (ret);
433                 return (NULL);
434         }
435
436         return (ret);
437 }
438
439 static void hddtemp_submit (char *inst, double temperature)
440 {
441         char buf[BUFFER_SIZE];
442
443         if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature)
444             >= BUFFER_SIZE)
445                 return;
446
447         plugin_submit (MODULE_NAME, inst, buf);
448 }
449
450 static void hddtemp_read (void)
451 {
452         char buf[BUFFER_SIZE];
453         char *fields[128];
454         char *ptr;
455         int num_fields;
456         int num_disks;
457         int i;
458
459         static int wait_time = 1;
460         static int wait_left = 0;
461
462         if (wait_left >= 10)
463         {
464                 wait_left -= 10;
465                 return;
466         }
467
468         /* get data from daemon */
469         if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
470         {
471                 /* This limit is reached in log2(86400) =~ 17 steps. Since
472                  * there is a 2^n seconds wait between each step it will need
473                  * roughly one day to reach this limit. -octo */
474                 
475                 wait_time *= 2;
476                 if (wait_time > 86400)
477                         wait_time = 86400;
478
479                 wait_left = wait_time;
480
481                 return;
482         }
483         else
484         {
485                 wait_time = 1;
486                 wait_left = 0;
487         }
488
489     DBG ("Received: %s", buf);
490
491         /* NB: strtok will eat up "||" and leading "|"'s */
492         num_fields = 0;
493         ptr = buf;
494         while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
495         {
496                 ptr = NULL;
497                 num_fields++;
498
499                 if (num_fields >= 128)
500                         break;
501         }
502
503         num_disks = num_fields / 4;
504
505         for (i = 0; i < num_disks; i++)
506         {
507                 char *name, *submit_name;
508                 double temperature;
509                 char *mode;
510
511                 mode = fields[4*i + 3];
512                 name = basename (fields[4*i + 0]);
513
514                 /* Skip non-temperature information */
515                 if (mode[0] != 'C' && mode[0] != 'F'){
516             DBG ("No temp. for %s", name);
517                         continue;
518         }
519
520                 temperature = atof (fields[4*i + 2]);
521
522                 /* Convert farenheit to celsius */
523                 if (mode[0] == 'F')
524                         temperature = (temperature - 32.0) * 5.0 / 9.0;
525
526                 if ((submit_name = hddtemp_get_name (name)) != NULL)
527                 {
528                         hddtemp_submit (submit_name, temperature);
529                         free (submit_name);
530                 }
531                 else
532                 {
533                         hddtemp_submit (name, temperature);
534                 }
535         }
536 }
537
538 /* module_register
539    Register collectd plugin. */
540 void module_register (void)
541 {
542         plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
543         cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);
544 }
545
546 #undef MODULE_NAME