Merge branch 'pull/master'
[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         NULL
52 };
53 static int config_keys_num = 2;
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
67 /*
68  * NAME
69  *  hddtemp_query_daemon
70  *
71  * DESCRIPTION
72  * Connect to the hddtemp daemon and receive data.
73  *
74  * ARGUMENTS:
75  *  `buffer'            The buffer where we put the received ascii string.
76  *  `buffer_size'       Size of the buffer
77  *
78  * RETURN VALUE:
79  *   >= 0 if ok, < 0 otherwise.
80  *
81  * NOTES:
82  *  Example of possible strings, as received from daemon:
83  *    |/dev/hda|ST340014A|36|C|
84  *    |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
85  *
86  * FIXME:
87  *  we need to create a new socket each time. Is there another way?
88  *  Hm, maybe we can re-use the `sockaddr' structure? -octo
89  */
90 static int hddtemp_query_daemon (char *buffer, int buffer_size)
91 {
92         int fd;
93         ssize_t status;
94         int buffer_fill;
95
96         const char *host;
97         const char *port;
98
99         struct addrinfo  ai_hints;
100         struct addrinfo *ai_list, *ai_ptr;
101         int              ai_return;
102
103         memset (&ai_hints, '\0', sizeof (ai_hints));
104         ai_hints.ai_flags    = AI_ADDRCONFIG;
105         ai_hints.ai_family   = PF_UNSPEC;
106         ai_hints.ai_socktype = SOCK_STREAM;
107         ai_hints.ai_protocol = IPPROTO_TCP;
108
109         host = hddtemp_host;
110         if (host == NULL)
111                 host = HDDTEMP_DEF_HOST;
112
113         port = hddtemp_port;
114         if (strlen (port) == 0)
115                 port = HDDTEMP_DEF_PORT;
116
117         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
118         {
119                 char errbuf[1024];
120                 ERROR ("hddtemp: getaddrinfo (%s, %s): %s",
121                                 host, port,
122                                 (ai_return == EAI_SYSTEM)
123                                 ? sstrerror (errno, errbuf, sizeof (errbuf))
124                                 : gai_strerror (ai_return));
125                 return (-1);
126         }
127
128         fd = -1;
129         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
130         {
131                 /* create our socket descriptor */
132                 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
133                 {
134                         char errbuf[1024];
135                         ERROR ("hddtemp: socket: %s",
136                                         sstrerror (errno, errbuf, sizeof (errbuf)));
137                         continue;
138                 }
139
140                 /* connect to the hddtemp daemon */
141                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
142                 {
143                         char errbuf[1024];
144                         DEBUG ("hddtemp: connect (%s, %s): %s", host, port,
145                                         sstrerror (errno, errbuf, sizeof (errbuf)));
146                         close (fd);
147                         fd = -1;
148                         continue;
149                 }
150
151                 /* A socket could be opened and connecting succeeded. We're
152                  * done. */
153                 break;
154         }
155
156         freeaddrinfo (ai_list);
157
158         if (fd < 0)
159         {
160                 ERROR ("hddtemp: Could not connect to daemon.");
161                 return (-1);
162         }
163
164         /* receive data from the hddtemp daemon */
165         memset (buffer, '\0', buffer_size);
166
167         buffer_fill = 0;
168         while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
169         {
170                 if (status == -1)
171                 {
172                         char errbuf[1024];
173
174                         if ((errno == EAGAIN) || (errno == EINTR))
175                                 continue;
176
177                         ERROR ("hddtemp: Error reading from socket: %s",
178                                         sstrerror (errno, errbuf, sizeof (errbuf)));
179                         close (fd);
180                         return (-1);
181                 }
182                 buffer_fill += status;
183
184                 if (buffer_fill >= buffer_size)
185                         break;
186         }
187
188         if (buffer_fill >= buffer_size)
189         {
190                 buffer[buffer_size - 1] = '\0';
191                 WARNING ("hddtemp: Message from hddtemp has been truncated.");
192         }
193         else if (buffer_fill == 0)
194         {
195                 WARNING ("hddtemp: Peer has unexpectedly shut down the socket. "
196                                 "Buffer: `%s'", buffer);
197                 close (fd);
198                 return (-1);
199         }
200
201         close (fd);
202         return (0);
203 }
204
205 static int hddtemp_config (const char *key, const char *value)
206 {
207         if (strcasecmp (key, "Host") == 0)
208         {
209                 if (hddtemp_host != NULL)
210                         free (hddtemp_host);
211                 hddtemp_host = strdup (value);
212         }
213         else if (strcasecmp (key, "Port") == 0)
214         {
215                 int port = (int) (atof (value));
216                 if ((port > 0) && (port <= 65535))
217                         snprintf (hddtemp_port, sizeof (hddtemp_port),
218                                         "%i", port);
219                 else
220                         strncpy (hddtemp_port, value, sizeof (hddtemp_port));
221                 hddtemp_port[sizeof (hddtemp_port) - 1] = '\0';
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 int hddtemp_init (void)
235 {
236 #if KERNEL_LINUX
237         FILE *fh;
238         char buf[1024];
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                 DEBUG ("Looking at /proc/partitions...");
264
265                 while (fgets (buf, sizeof (buf), 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                                         DEBUG ("Skipping unknown major %i", major);
338                                         continue;
339                         } /* switch (major) */
340
341                         if ((name = strdup (fields[3])) == NULL)
342                         {
343                                 ERROR ("hddtemp: strdup(%s) == NULL", fields[3]);
344                                 continue;
345                         }
346
347                         if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
348                         {
349                                 ERROR ("hddtemp: malloc (%u) == NULL",
350                                                 (unsigned int) sizeof (hddname_t));
351                                 free (name);
352                                 continue;
353                         }
354
355                         DEBUG ("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                 fclose (fh);
373         }
374 #if COLLECT_DEBUG
375         else
376         {
377                 char errbuf[1024];
378                 DEBUG ("Could not open /proc/partitions: %s",
379                                 sstrerror (errno, errbuf, sizeof (errbuf)));
380         }
381 #endif /* COLLECT_DEBUG */
382 #endif /* KERNEL_LINUX */
383
384         return (0);
385 } /* int hddtemp_init */
386
387 /*
388  * hddtemp_get_name
389  *
390  * Description:
391  *   Try to "cook" a bit the drive name as returned
392  *   by the hddtemp daemon. The intend is to transform disk
393  *   names into <major>-<minor> when possible.
394  */
395 static char *hddtemp_get_name (char *drive)
396 {
397         hddname_t *list;
398         char *ret;
399
400         for (list = first_hddname; list != NULL; list = list->next)
401                 if (strcmp (drive, list->name) == 0)
402                         break;
403
404         if (list == NULL)
405         {
406                 DEBUG ("Don't know %s, keeping name as-is.", drive);
407                 return (strdup (drive));
408         }
409
410         if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
411                 return (NULL);
412
413         if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
414         {
415                 free (ret);
416                 return (NULL);
417         }
418
419         return (ret);
420 }
421
422 static void hddtemp_submit (char *type_instance, double value)
423 {
424         value_t values[1];
425         value_list_t vl = VALUE_LIST_INIT;
426
427         values[0].gauge = value;
428
429         vl.values = values;
430         vl.values_len = 1;
431         vl.time = time (NULL);
432         strcpy (vl.host, hostname_g);
433         strcpy (vl.plugin, "hddtemp");
434         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
435
436         plugin_dispatch_values ("temperature", &vl);
437 }
438
439 static int hddtemp_read (void)
440 {
441         char buf[1024];
442         char *fields[128];
443         char *ptr;
444         char *saveptr;
445         int num_fields;
446         int num_disks;
447         int i;
448
449         /* get data from daemon */
450         if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
451                 return (-1);
452
453         /* NB: strtok_r will eat up "||" and leading "|"'s */
454         num_fields = 0;
455         ptr = buf;
456         saveptr = NULL;
457         while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
458         {
459                 ptr = NULL;
460                 num_fields++;
461
462                 if (num_fields >= 128)
463                         break;
464         }
465
466         num_disks = num_fields / 4;
467
468         for (i = 0; i < num_disks; i++)
469         {
470                 char *name, *submit_name;
471                 double temperature;
472                 char *mode;
473
474                 mode = fields[4*i + 3];
475                 name = basename (fields[4*i + 0]);
476
477                 /* Skip non-temperature information */
478                 if (mode[0] != 'C' && mode[0] != 'F')
479                         continue;
480
481                 temperature = atof (fields[4*i + 2]);
482
483                 /* Convert farenheit to celsius */
484                 if (mode[0] == 'F')
485                         temperature = (temperature - 32.0) * 5.0 / 9.0;
486
487                 if ((submit_name = hddtemp_get_name (name)) != NULL)
488                 {
489                         hddtemp_submit (submit_name, temperature);
490                         free (submit_name);
491                 }
492                 else
493                 {
494                         hddtemp_submit (name, temperature);
495                 }
496         }
497         
498         return (0);
499 } /* int hddtemp_read */
500
501 /* module_register
502    Register collectd plugin. */
503 void module_register (void)
504 {
505         plugin_register_config ("hddtemp", hddtemp_config,
506                         config_keys, config_keys_num);
507         plugin_register_init ("hddtemp", hddtemp_init);
508         plugin_register_read ("hddtemp", hddtemp_read);
509 }