octo@crystal:~/Projects/collectd $ svn merge -r646:651 trunk tags/collectd-3.9.1
[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 #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 /* 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         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
134         {
135                 syslog (LOG_ERR, "hddtemp: getaddrinfo (%s, %s): %s",
136                                 host, port,
137                                 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
138                 return (-1);
139         }
140
141         fd = -1;
142         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
143         {
144                 /* create our socket descriptor */
145                 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
146                 {
147                         syslog (LOG_ERR, "hddtemp: socket: %s",
148                                         strerror (errno));
149                         continue;
150                 }
151
152                 /* connect to the hddtemp daemon */
153                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
154                 {
155                         DBG ("hddtemp: connect (%s, %s): %s", host, port,
156                                         strerror (errno));
157                         close (fd);
158                         fd = -1;
159                         continue;
160                 }
161         }
162
163         freeaddrinfo (ai_list);
164
165         if (fd < 0)
166         {
167                 syslog (LOG_ERR, "hddtemp: Could not connect to daemon.");
168                 return (-1);
169         }
170
171         /* receive data from the hddtemp daemon */
172         memset (buffer, '\0', buffer_size);
173
174         buffer_fill = 0;
175         while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
176         {
177                 if (status == -1)
178                 {
179                         if ((errno == EAGAIN) || (errno == EINTR))
180                                 continue;
181
182                         syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
183                                                 strerror (errno));
184                         return (-1);
185                 }
186                 buffer_fill += status;
187
188                 if (buffer_fill >= buffer_size)
189                         break;
190         }
191
192         if (buffer_fill >= buffer_size)
193         {
194                 buffer[buffer_size - 1] = '\0';
195                 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
196         }
197         else if (buffer_fill == 0)
198         {
199                 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
200                                 "Buffer: `%s'", buffer);
201                 close (fd);
202                 return (-1);
203         }
204
205         close (fd);
206         return (0);
207 }
208
209 static int hddtemp_config (char *key, char *value)
210 {
211         if (strcasecmp (key, "host") == 0)
212         {
213                 if (hddtemp_host != NULL)
214                         free (hddtemp_host);
215                 hddtemp_host = strdup (value);
216         }
217         else if (strcasecmp (key, "port") == 0)
218         {
219                 if (hddtemp_port != NULL)
220                         free (hddtemp_port);
221                 hddtemp_port = strdup (value);
222         }
223         else
224         {
225                 return (-1);
226         }
227
228         return (0);
229 }
230
231 /* In the init-function we initialize the `hddname_t' list used to translate
232  * disk-names. Under Linux that's done using `/proc/partitions'. Under other
233  * operating-systems, it's not done at all. */
234 static void hddtemp_init (void)
235 {
236 #if KERNEL_LINUX
237         FILE *fh;
238         char buf[BUFFER_SIZE];
239         int buflen;
240
241         char *fields[16];
242         int num_fields;
243
244         int major;
245         int minor;
246         char *name;
247         hddname_t *next;
248         hddname_t *entry;
249
250         next = first_hddname;
251         while (next != NULL)
252         {
253                 entry = next;
254                 next = entry->next;
255
256                 free (entry->name);
257                 free (entry);
258         }
259         first_hddname = NULL;
260
261         if ((fh = fopen ("/proc/partitions", "r")) != NULL)
262         {
263                 DBG ("Looking at /proc/partitions...");
264
265                 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
266                 {
267                         /* Delete trailing newlines */
268                         buflen = strlen (buf);
269
270                         while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
271                                 buf[--buflen] = '\0';
272
273                         /* We want lines of the form:
274                          *
275                          *     3     1   77842926 hda1
276                          *
277                          * ...so, skip everything else. */
278                         if (buflen == 0)
279                                 continue;
280                         
281                         num_fields = strsplit (buf, fields, 16);
282
283                         if (num_fields != 4)
284                                 continue;
285
286                         major = atoi (fields[0]);
287                         minor = atoi (fields[1]);
288
289                         /* We try to keep only entries, which may correspond to
290                          * physical disks and that may have a corresponding
291                          * entry in the hddtemp daemon. Basically, this means
292                          * IDE and SCSI. */
293                         switch (major)
294                         {
295                                 /* SCSI. */
296                                 case SCSI_DISK0_MAJOR:
297                                 case SCSI_DISK1_MAJOR:
298                                 case SCSI_DISK2_MAJOR:
299                                 case SCSI_DISK3_MAJOR:
300                                 case SCSI_DISK4_MAJOR:
301                                 case SCSI_DISK5_MAJOR:
302                                 case SCSI_DISK6_MAJOR:
303                                 case SCSI_DISK7_MAJOR:
304                                 case SCSI_DISK8_MAJOR:
305                                 case SCSI_DISK9_MAJOR:
306                                 case SCSI_DISK10_MAJOR:
307                                 case SCSI_DISK11_MAJOR:
308                                 case SCSI_DISK12_MAJOR:
309                                 case SCSI_DISK13_MAJOR:
310                                 case SCSI_DISK14_MAJOR:
311                                 case SCSI_DISK15_MAJOR:
312                                         /* SCSI disks minors are multiples of 16.
313                                          * Keep only those. */
314                                         if (minor % 16)
315                                                 continue;
316                                         break;
317
318                                 /* IDE. */
319                                 case IDE0_MAJOR:
320                                 case IDE1_MAJOR:
321                                 case IDE2_MAJOR:
322                                 case IDE3_MAJOR:
323                                 case IDE4_MAJOR:
324                                 case IDE5_MAJOR:
325                                 case IDE6_MAJOR:
326                                 case IDE7_MAJOR:
327                                 case IDE8_MAJOR:
328                                 case IDE9_MAJOR:
329                                         /* IDE disks minors can only be 0 or 64.
330                                          * Keep only those. */
331                                         if(minor != 0 && minor != 64)
332                                                 continue;
333                                         break;
334
335                                 /* Skip all other majors. */
336                                 default:
337                                         DBG ("Skipping unknown major %i", major);
338                                         continue;
339                         } /* switch (major) */
340
341                         if ((name = strdup (fields[3])) == NULL)
342                         {
343                                 syslog (LOG_ERR, "hddtemp: strdup(%s) == NULL", fields[3]);
344                                 continue;
345                         }
346
347                         if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
348                         {
349                                 syslog (LOG_ERR, "hddtemp: malloc (%u) == NULL",
350                                                 (unsigned int) sizeof (hddname_t));
351                                 free (name);
352                                 continue;
353                         }
354
355                         DBG ("Found disk: %s (%u:%u).", name, major, minor);
356
357                         entry->major = major;
358                         entry->minor = minor;
359                         entry->name  = name;
360                         entry->next  = NULL;
361
362                         if (first_hddname == NULL)
363                         {
364                                 first_hddname = entry;
365                         }
366                         else
367                         {
368                                 entry->next = first_hddname;
369                                 first_hddname = entry;
370                         }
371                 }
372         }
373         else
374                 DBG ("Could not open /proc/partitions: %s",
375                                 strerror (errno));
376 #endif /* KERNEL_LINUX */
377 }
378
379 static void hddtemp_write (char *host, char *inst, char *val)
380 {
381         char filename[BUFFER_SIZE];
382         int status;
383
384         /* construct filename */
385         status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
386         if (status < 1)
387                 return;
388         else if (status >= BUFFER_SIZE)
389                 return;
390
391         rrd_update_file (host, filename, val, ds_def, ds_num);
392 }
393
394 /*
395  * hddtemp_get_name
396  *
397  * Description:
398  *   Try to "cook" a bit the drive name as returned
399  *   by the hddtemp daemon. The intend is to transform disk
400  *   names into <major>-<minor> when possible.
401  */
402 static char *hddtemp_get_name (char *drive)
403 {
404         hddname_t *list;
405         char *ret;
406
407         for (list = first_hddname; list != NULL; list = list->next)
408                 if (strcmp (drive, list->name) == 0)
409                         break;
410
411         if (list == NULL)
412         {
413                 DBG ("Don't know %s, keeping name as-is.", drive);
414                 return (strdup (drive));
415         }
416
417         if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
418                 return (NULL);
419
420         if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
421         {
422                 free (ret);
423                 return (NULL);
424         }
425
426         return (ret);
427 }
428
429 static void hddtemp_submit (char *inst, double temperature)
430 {
431         char buf[BUFFER_SIZE];
432
433         if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature)
434             >= BUFFER_SIZE)
435                 return;
436
437         plugin_submit (MODULE_NAME, inst, buf);
438 }
439
440 static void hddtemp_read (void)
441 {
442         char buf[BUFFER_SIZE];
443         char *fields[128];
444         char *ptr;
445         int num_fields;
446         int num_disks;
447         int i;
448
449         static int wait_time = 1;
450         static int wait_left = 0;
451
452         if (wait_left >= 10)
453         {
454                 wait_left -= 10;
455                 return;
456         }
457
458         /* get data from daemon */
459         if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
460         {
461                 /* This limit is reached in log2(86400) =~ 17 steps. Since
462                  * there is a 2^n seconds wait between each step it will need
463                  * roughly one day to reach this limit. -octo */
464                 
465                 wait_time *= 2;
466                 if (wait_time > 86400)
467                         wait_time = 86400;
468
469                 wait_left = wait_time;
470
471                 return;
472         }
473         else
474         {
475                 wait_time = 1;
476                 wait_left = 0;
477         }
478
479         /* NB: strtok will eat up "||" and leading "|"'s */
480         num_fields = 0;
481         ptr = buf;
482         while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
483         {
484                 ptr = NULL;
485                 num_fields++;
486
487                 if (num_fields >= 128)
488                         break;
489         }
490
491         num_disks = num_fields / 4;
492
493         for (i = 0; i < num_disks; i++)
494         {
495                 char *name, *submit_name;
496                 double temperature;
497                 char *mode;
498
499                 mode = fields[4*i + 3];
500                 name = basename (fields[4*i + 0]);
501
502                 /* Skip non-temperature information */
503                 if (mode[0] != 'C' && mode[0] != 'F')
504                         continue;
505
506                 temperature = atof (fields[4*i + 2]);
507
508                 /* Convert farenheit to celsius */
509                 if (mode[0] == 'F')
510                         temperature = (temperature - 32.0) * 5.0 / 9.0;
511
512                 if ((submit_name = hddtemp_get_name (name)) != NULL)
513                 {
514                         hddtemp_submit (submit_name, temperature);
515                         free (submit_name);
516                 }
517                 else
518                 {
519                         hddtemp_submit (name, temperature);
520                 }
521         }
522 }
523
524 /* module_register
525    Register collectd plugin. */
526 void module_register (void)
527 {
528         plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
529         cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);
530 }
531
532 #undef MODULE_NAME