Merge branch 'pull/collectd-4' into collectd-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 #include "utils_debug.h"
34
35 #if HAVE_NETDB_H && HAVE_SYS_SOCKET_H && HAVE_NETINET_IN_H \
36         && HAVE_NETINET_TCP_H && HAVE_LIBGEN_H
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 # define HDDTEMP_HAVE_READ 1
43 #else
44 # define HDDTEMP_HAVE_READ 0
45 #endif
46
47 #if HAVE_LINUX_MAJOR_H
48 # include <linux/major.h>
49 #endif
50
51 #define HDDTEMP_DEF_HOST "127.0.0.1"
52 #define HDDTEMP_DEF_PORT "7634"
53
54 static data_source_t data_source_temperature[1] =
55 {
56         {"value", DS_TYPE_GAUGE, -273.15, NAN}
57 };
58
59 static data_set_t temperature_ds =
60 {
61         "temperature", 1, data_source_temperature
62 };
63
64 #if HDDTEMP_HAVE_READ
65 static const char *config_keys[] =
66 {
67         "Host",
68         "Port",
69         NULL
70 };
71 static int config_keys_num = 2;
72
73 typedef struct hddname
74 {
75         int major;
76         int minor;
77         char *name;
78         struct hddname *next;
79 } hddname_t;
80
81 static hddname_t *first_hddname = NULL;
82 static char *hddtemp_host = NULL;
83 static char *hddtemp_port = NULL;
84
85 /*
86  * NAME
87  *  hddtemp_query_daemon
88  *
89  * DESCRIPTION
90  * Connect to the hddtemp daemon and receive data.
91  *
92  * ARGUMENTS:
93  *  `buffer'            The buffer where we put the received ascii string.
94  *  `buffer_size'       Size of the buffer
95  *
96  * RETURN VALUE:
97  *   >= 0 if ok, < 0 otherwise.
98  *
99  * NOTES:
100  *  Example of possible strings, as received from daemon:
101  *    |/dev/hda|ST340014A|36|C|
102  *    |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
103  *
104  * FIXME:
105  *  we need to create a new socket each time. Is there another way?
106  *  Hm, maybe we can re-use the `sockaddr' structure? -octo
107  */
108 static int hddtemp_query_daemon (char *buffer, int buffer_size)
109 {
110         int fd;
111         ssize_t status;
112         int buffer_fill;
113
114         const char *host;
115         const char *port;
116
117         struct addrinfo  ai_hints;
118         struct addrinfo *ai_list, *ai_ptr;
119         int              ai_return;
120
121         memset (&ai_hints, '\0', sizeof (ai_hints));
122         ai_hints.ai_flags    = AI_ADDRCONFIG;
123         ai_hints.ai_family   = PF_UNSPEC;
124         ai_hints.ai_socktype = SOCK_STREAM;
125         ai_hints.ai_protocol = IPPROTO_TCP;
126
127         host = hddtemp_host;
128         if (host == NULL)
129                 host = HDDTEMP_DEF_HOST;
130
131         port = hddtemp_port;
132         if (port == NULL)
133                 port = HDDTEMP_DEF_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                         DBG ("hddtemp: connect (%s, %s): %s", host, port,
158                                         strerror (errno));
159                         close (fd);
160                         fd = -1;
161                         continue;
162                 }
163
164                 /* A socket could be opened and connecting succeeded. We're
165                  * done. */
166                 break;
167         }
168
169         freeaddrinfo (ai_list);
170
171         if (fd < 0)
172         {
173                 syslog (LOG_ERR, "hddtemp: Could not connect to daemon.");
174                 return (-1);
175         }
176
177         /* receive data from the hddtemp daemon */
178         memset (buffer, '\0', buffer_size);
179
180         buffer_fill = 0;
181         while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
182         {
183                 if (status == -1)
184                 {
185                         if ((errno == EAGAIN) || (errno == EINTR))
186                                 continue;
187
188                         syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
189                                                 strerror (errno));
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                 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
203         }
204         else if (buffer_fill == 0)
205         {
206                 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
207                                 "Buffer: `%s'", buffer);
208                 close (fd);
209                 return (-1);
210         }
211
212         close (fd);
213         return (0);
214 }
215
216 static int hddtemp_config (const char *key, const char *value)
217 {
218         if (strcasecmp (key, "host") == 0)
219         {
220                 if (hddtemp_host != NULL)
221                         free (hddtemp_host);
222                 hddtemp_host = strdup (value);
223         }
224         else if (strcasecmp (key, "port") == 0)
225         {
226                 if (hddtemp_port != NULL)
227                         free (hddtemp_port);
228                 hddtemp_port = strdup (value);
229         }
230         else
231         {
232                 return (-1);
233         }
234
235         return (0);
236 }
237
238 /* In the init-function we initialize the `hddname_t' list used to translate
239  * disk-names. Under Linux that's done using `/proc/partitions'. Under other
240  * operating-systems, it's not done at all. */
241 static int hddtemp_init (void)
242 {
243 #if KERNEL_LINUX
244         FILE *fh;
245         char buf[1024];
246         int buflen;
247
248         char *fields[16];
249         int num_fields;
250
251         int major;
252         int minor;
253         char *name;
254         hddname_t *next;
255         hddname_t *entry;
256
257         next = first_hddname;
258         while (next != NULL)
259         {
260                 entry = next;
261                 next = entry->next;
262
263                 free (entry->name);
264                 free (entry);
265         }
266         first_hddname = NULL;
267
268         if ((fh = fopen ("/proc/partitions", "r")) != NULL)
269         {
270                 DBG ("Looking at /proc/partitions...");
271
272                 while (fgets (buf, sizeof (buf), fh) != NULL)
273                 {
274                         /* Delete trailing newlines */
275                         buflen = strlen (buf);
276
277                         while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
278                                 buf[--buflen] = '\0';
279
280                         /* We want lines of the form:
281                          *
282                          *     3     1   77842926 hda1
283                          *
284                          * ...so, skip everything else. */
285                         if (buflen == 0)
286                                 continue;
287                         
288                         num_fields = strsplit (buf, fields, 16);
289
290                         if (num_fields != 4)
291                                 continue;
292
293                         major = atoi (fields[0]);
294                         minor = atoi (fields[1]);
295
296                         /* We try to keep only entries, which may correspond to
297                          * physical disks and that may have a corresponding
298                          * entry in the hddtemp daemon. Basically, this means
299                          * IDE and SCSI. */
300                         switch (major)
301                         {
302                                 /* SCSI. */
303                                 case SCSI_DISK0_MAJOR:
304                                 case SCSI_DISK1_MAJOR:
305                                 case SCSI_DISK2_MAJOR:
306                                 case SCSI_DISK3_MAJOR:
307                                 case SCSI_DISK4_MAJOR:
308                                 case SCSI_DISK5_MAJOR:
309                                 case SCSI_DISK6_MAJOR:
310                                 case SCSI_DISK7_MAJOR:
311                                 case SCSI_DISK8_MAJOR:
312                                 case SCSI_DISK9_MAJOR:
313                                 case SCSI_DISK10_MAJOR:
314                                 case SCSI_DISK11_MAJOR:
315                                 case SCSI_DISK12_MAJOR:
316                                 case SCSI_DISK13_MAJOR:
317                                 case SCSI_DISK14_MAJOR:
318                                 case SCSI_DISK15_MAJOR:
319                                         /* SCSI disks minors are multiples of 16.
320                                          * Keep only those. */
321                                         if (minor % 16)
322                                                 continue;
323                                         break;
324
325                                 /* IDE. */
326                                 case IDE0_MAJOR:
327                                 case IDE1_MAJOR:
328                                 case IDE2_MAJOR:
329                                 case IDE3_MAJOR:
330                                 case IDE4_MAJOR:
331                                 case IDE5_MAJOR:
332                                 case IDE6_MAJOR:
333                                 case IDE7_MAJOR:
334                                 case IDE8_MAJOR:
335                                 case IDE9_MAJOR:
336                                         /* IDE disks minors can only be 0 or 64.
337                                          * Keep only those. */
338                                         if(minor != 0 && minor != 64)
339                                                 continue;
340                                         break;
341
342                                 /* Skip all other majors. */
343                                 default:
344                                         DBG ("Skipping unknown major %i", major);
345                                         continue;
346                         } /* switch (major) */
347
348                         if ((name = strdup (fields[3])) == NULL)
349                         {
350                                 syslog (LOG_ERR, "hddtemp: strdup(%s) == NULL", fields[3]);
351                                 continue;
352                         }
353
354                         if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
355                         {
356                                 syslog (LOG_ERR, "hddtemp: malloc (%u) == NULL",
357                                                 (unsigned int) sizeof (hddname_t));
358                                 free (name);
359                                 continue;
360                         }
361
362                         DBG ("Found disk: %s (%u:%u).", name, major, minor);
363
364                         entry->major = major;
365                         entry->minor = minor;
366                         entry->name  = name;
367                         entry->next  = NULL;
368
369                         if (first_hddname == NULL)
370                         {
371                                 first_hddname = entry;
372                         }
373                         else
374                         {
375                                 entry->next = first_hddname;
376                                 first_hddname = entry;
377                         }
378                 }
379                 fclose (fh);
380         }
381         else
382                 DBG ("Could not open /proc/partitions: %s",
383                                 strerror (errno));
384 #endif /* KERNEL_LINUX */
385
386         return (0);
387 } /* int hddtemp_init */
388
389 /*
390  * hddtemp_get_name
391  *
392  * Description:
393  *   Try to "cook" a bit the drive name as returned
394  *   by the hddtemp daemon. The intend is to transform disk
395  *   names into <major>-<minor> when possible.
396  */
397 static char *hddtemp_get_name (char *drive)
398 {
399         hddname_t *list;
400         char *ret;
401
402         for (list = first_hddname; list != NULL; list = list->next)
403                 if (strcmp (drive, list->name) == 0)
404                         break;
405
406         if (list == NULL)
407         {
408                 DBG ("Don't know %s, keeping name as-is.", drive);
409                 return (strdup (drive));
410         }
411
412         if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
413                 return (NULL);
414
415         if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
416         {
417                 free (ret);
418                 return (NULL);
419         }
420
421         return (ret);
422 }
423
424 static void hddtemp_submit (char *type_instance, double value)
425 {
426         value_t values[1];
427         value_list_t vl = VALUE_LIST_INIT;
428
429         values[0].gauge = value;
430
431         vl.values = values;
432         vl.values_len = 1;
433         vl.time = time (NULL);
434         strcpy (vl.host, hostname_g);
435         strcpy (vl.plugin, "hddtemp");
436         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
437
438         plugin_dispatch_values ("temperature", &vl);
439 }
440
441 static int hddtemp_read (void)
442 {
443         char buf[1024];
444         char *fields[128];
445         char *ptr;
446         char *saveptr;
447         int num_fields;
448         int num_disks;
449         int i;
450
451         /* get data from daemon */
452         if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
453                 return (-1);
454
455         /* NB: strtok_r will eat up "||" and leading "|"'s */
456         num_fields = 0;
457         ptr = buf;
458         saveptr = NULL;
459         while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
460         {
461                 ptr = NULL;
462                 num_fields++;
463
464                 if (num_fields >= 128)
465                         break;
466         }
467
468         num_disks = num_fields / 4;
469
470         for (i = 0; i < num_disks; i++)
471         {
472                 char *name, *submit_name;
473                 double temperature;
474                 char *mode;
475
476                 mode = fields[4*i + 3];
477                 name = basename (fields[4*i + 0]);
478
479                 /* Skip non-temperature information */
480                 if (mode[0] != 'C' && mode[0] != 'F')
481                         continue;
482
483                 temperature = atof (fields[4*i + 2]);
484
485                 /* Convert farenheit to celsius */
486                 if (mode[0] == 'F')
487                         temperature = (temperature - 32.0) * 5.0 / 9.0;
488
489                 if ((submit_name = hddtemp_get_name (name)) != NULL)
490                 {
491                         hddtemp_submit (submit_name, temperature);
492                         free (submit_name);
493                 }
494                 else
495                 {
496                         hddtemp_submit (name, temperature);
497                 }
498         }
499         
500         return (0);
501 } /* int hddtemp_read */
502 #endif /* HDDTEMP_HAVE_READ */
503
504 /* module_register
505    Register collectd plugin. */
506 void module_register (void)
507 {
508         plugin_register_data_set (&temperature_ds);
509         
510 #if HDDTEMP_HAVE_READ
511         plugin_register_config ("hddtemp", hddtemp_config,
512                         config_keys, config_keys_num);
513         plugin_register_init ("hddtemp", hddtemp_init);
514         plugin_register_read ("hddtemp", hddtemp_read);
515 #endif /* HDDTEMP_HAVE_READ */
516 }