Merge branch 'collectd-4.0'
[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 #if HAVE_NETDB_H && HAVE_SYS_SOCKET_H && HAVE_NETINET_IN_H \
35         && HAVE_NETINET_TCP_H && HAVE_LIBGEN_H
36 # include <netdb.h>
37 # include <sys/socket.h>
38 # include <netinet/in.h>
39 # include <netinet/tcp.h>
40 # include <libgen.h> /* for basename */
41 # define HDDTEMP_HAVE_READ 1
42 #else
43 # define HDDTEMP_HAVE_READ 0
44 #endif
45
46 #if HAVE_LINUX_MAJOR_H
47 # include <linux/major.h>
48 #endif
49
50 #define HDDTEMP_DEF_HOST "127.0.0.1"
51 #define HDDTEMP_DEF_PORT "7634"
52
53 #if HDDTEMP_HAVE_READ
54 static const char *config_keys[] =
55 {
56         "Host",
57         "Port",
58         NULL
59 };
60 static int config_keys_num = 2;
61
62 typedef struct hddname
63 {
64         int major;
65         int minor;
66         char *name;
67         struct hddname *next;
68 } hddname_t;
69
70 static hddname_t *first_hddname = NULL;
71 static char *hddtemp_host = NULL;
72 static char hddtemp_port[16];
73
74 /*
75  * NAME
76  *  hddtemp_query_daemon
77  *
78  * DESCRIPTION
79  * Connect to the hddtemp daemon and receive data.
80  *
81  * ARGUMENTS:
82  *  `buffer'            The buffer where we put the received ascii string.
83  *  `buffer_size'       Size of the buffer
84  *
85  * RETURN VALUE:
86  *   >= 0 if ok, < 0 otherwise.
87  *
88  * NOTES:
89  *  Example of possible strings, as received from daemon:
90  *    |/dev/hda|ST340014A|36|C|
91  *    |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
92  *
93  * FIXME:
94  *  we need to create a new socket each time. Is there another way?
95  *  Hm, maybe we can re-use the `sockaddr' structure? -octo
96  */
97 static int hddtemp_query_daemon (char *buffer, int buffer_size)
98 {
99         int fd;
100         ssize_t status;
101         int buffer_fill;
102
103         const char *host;
104         const char *port;
105
106         struct addrinfo  ai_hints;
107         struct addrinfo *ai_list, *ai_ptr;
108         int              ai_return;
109
110         memset (&ai_hints, '\0', sizeof (ai_hints));
111         ai_hints.ai_flags    = AI_ADDRCONFIG;
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: 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                 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
140                 {
141                         char errbuf[1024];
142                         ERROR ("hddtemp: socket: %s",
143                                         sstrerror (errno, errbuf, sizeof (errbuf)));
144                         continue;
145                 }
146
147                 /* connect to the hddtemp daemon */
148                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
149                 {
150                         char errbuf[1024];
151                         DEBUG ("hddtemp: connect (%s, %s): %s", host, port,
152                                         sstrerror (errno, errbuf, sizeof (errbuf)));
153                         close (fd);
154                         fd = -1;
155                         continue;
156                 }
157
158                 /* A socket could be opened and connecting succeeded. We're
159                  * done. */
160                 break;
161         }
162
163         freeaddrinfo (ai_list);
164
165         if (fd < 0)
166         {
167                 ERROR ("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                         char errbuf[1024];
180
181                         if ((errno == EAGAIN) || (errno == EINTR))
182                                 continue;
183
184                         ERROR ("hddtemp: Error reading from socket: %s",
185                                         sstrerror (errno, errbuf, sizeof (errbuf)));
186                         close (fd);
187                         return (-1);
188                 }
189                 buffer_fill += status;
190
191                 if (buffer_fill >= buffer_size)
192                         break;
193         }
194
195         if (buffer_fill >= buffer_size)
196         {
197                 buffer[buffer_size - 1] = '\0';
198                 WARNING ("hddtemp: Message from hddtemp has been truncated.");
199         }
200         else if (buffer_fill == 0)
201         {
202                 WARNING ("hddtemp: Peer has unexpectedly shut down the socket. "
203                                 "Buffer: `%s'", buffer);
204                 close (fd);
205                 return (-1);
206         }
207
208         close (fd);
209         return (0);
210 }
211
212 static int hddtemp_config (const char *key, const char *value)
213 {
214         if (strcasecmp (key, "Host") == 0)
215         {
216                 if (hddtemp_host != NULL)
217                         free (hddtemp_host);
218                 hddtemp_host = strdup (value);
219         }
220         else if (strcasecmp (key, "Port") == 0)
221         {
222                 int port = (int) (atof (value));
223                 if ((port > 0) && (port <= 65535))
224                         snprintf (hddtemp_port, sizeof (hddtemp_port),
225                                         "%i", port);
226                 else
227                         strncpy (hddtemp_port, value, sizeof (hddtemp_port));
228                 hddtemp_port[sizeof (hddtemp_port) - 1] = '\0';
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                 DEBUG ("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                                         DEBUG ("Skipping unknown major %i", major);
345                                         continue;
346                         } /* switch (major) */
347
348                         if ((name = strdup (fields[3])) == NULL)
349                         {
350                                 ERROR ("hddtemp: strdup(%s) == NULL", fields[3]);
351                                 continue;
352                         }
353
354                         if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
355                         {
356                                 ERROR ("hddtemp: malloc (%u) == NULL",
357                                                 (unsigned int) sizeof (hddname_t));
358                                 free (name);
359                                 continue;
360                         }
361
362                         DEBUG ("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 #if COLLECT_DEBUG
382         else
383         {
384                 char errbuf[1024];
385                 DEBUG ("Could not open /proc/partitions: %s",
386                                 sstrerror (errno, errbuf, sizeof (errbuf)));
387         }
388 #endif /* COLLECT_DEBUG */
389 #endif /* KERNEL_LINUX */
390
391         return (0);
392 } /* int hddtemp_init */
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                 DEBUG ("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 *type_instance, double value)
430 {
431         value_t values[1];
432         value_list_t vl = VALUE_LIST_INIT;
433
434         values[0].gauge = value;
435
436         vl.values = values;
437         vl.values_len = 1;
438         vl.time = time (NULL);
439         strcpy (vl.host, hostname_g);
440         strcpy (vl.plugin, "hddtemp");
441         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
442
443         plugin_dispatch_values ("temperature", &vl);
444 }
445
446 static int hddtemp_read (void)
447 {
448         char buf[1024];
449         char *fields[128];
450         char *ptr;
451         char *saveptr;
452         int num_fields;
453         int num_disks;
454         int i;
455
456         /* get data from daemon */
457         if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
458                 return (-1);
459
460         /* NB: strtok_r will eat up "||" and leading "|"'s */
461         num_fields = 0;
462         ptr = buf;
463         saveptr = NULL;
464         while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
465         {
466                 ptr = NULL;
467                 num_fields++;
468
469                 if (num_fields >= 128)
470                         break;
471         }
472
473         num_disks = num_fields / 4;
474
475         for (i = 0; i < num_disks; i++)
476         {
477                 char *name, *submit_name;
478                 double temperature;
479                 char *mode;
480
481                 mode = fields[4*i + 3];
482                 name = basename (fields[4*i + 0]);
483
484                 /* Skip non-temperature information */
485                 if (mode[0] != 'C' && mode[0] != 'F')
486                         continue;
487
488                 temperature = atof (fields[4*i + 2]);
489
490                 /* Convert farenheit to celsius */
491                 if (mode[0] == 'F')
492                         temperature = (temperature - 32.0) * 5.0 / 9.0;
493
494                 if ((submit_name = hddtemp_get_name (name)) != NULL)
495                 {
496                         hddtemp_submit (submit_name, temperature);
497                         free (submit_name);
498                 }
499                 else
500                 {
501                         hddtemp_submit (name, temperature);
502                 }
503         }
504         
505         return (0);
506 } /* int hddtemp_read */
507 #endif /* HDDTEMP_HAVE_READ */
508
509 /* module_register
510    Register collectd plugin. */
511 void module_register (void)
512 {
513 #if HDDTEMP_HAVE_READ
514         plugin_register_config ("hddtemp", hddtemp_config,
515                         config_keys, config_keys_num);
516         plugin_register_init ("hddtemp", hddtemp_init);
517         plugin_register_read ("hddtemp", hddtemp_read);
518 #endif /* HDDTEMP_HAVE_READ */
519 }