hddtemp plugin: Prefix all messages with "hddtemp plugin:".
[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    = 0;
105 #ifdef AI_ADDRCONFIG
106         ai_hints.ai_flags   |= AI_ADDRCONFIG;
107 #endif
108         ai_hints.ai_family   = PF_UNSPEC;
109         ai_hints.ai_socktype = SOCK_STREAM;
110         ai_hints.ai_protocol = IPPROTO_TCP;
111
112         host = hddtemp_host;
113         if (host == NULL)
114                 host = HDDTEMP_DEF_HOST;
115
116         port = hddtemp_port;
117         if (strlen (port) == 0)
118                 port = HDDTEMP_DEF_PORT;
119
120         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
121         {
122                 char errbuf[1024];
123                 ERROR ("hddtemp plugin: getaddrinfo (%s, %s): %s",
124                                 host, port,
125                                 (ai_return == EAI_SYSTEM)
126                                 ? sstrerror (errno, errbuf, sizeof (errbuf))
127                                 : gai_strerror (ai_return));
128                 return (-1);
129         }
130
131         fd = -1;
132         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
133         {
134                 /* create our socket descriptor */
135                 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
136                                 ai_ptr->ai_protocol);
137                 if (fd < 0)
138                 {
139                         char errbuf[1024];
140                         ERROR ("hddtemp plugin: socket: %s",
141                                         sstrerror (errno, errbuf, sizeof (errbuf)));
142                         continue;
143                 }
144
145                 /* connect to the hddtemp daemon */
146                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr,
147                                         ai_ptr->ai_addrlen))
148                 {
149                         char errbuf[1024];
150                         INFO ("hddtemp plugin: connect (%s, %s) failed: %s",
151                                         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 plugin: 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 plugin: 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 plugin: Message from hddtemp has been "
199                                 "truncated.");
200         }
201         else if (buffer_fill == 0)
202         {
203                 WARNING ("hddtemp plugin: Peer has unexpectedly shut down "
204                                 "the socket. Buffer: `%s'", buffer);
205                 close (fd);
206                 return (-1);
207         }
208
209         close (fd);
210         return (0);
211 }
212
213 static int hddtemp_config (const char *key, const char *value)
214 {
215         if (strcasecmp (key, "Host") == 0)
216         {
217                 if (hddtemp_host != NULL)
218                         free (hddtemp_host);
219                 hddtemp_host = strdup (value);
220         }
221         else if (strcasecmp (key, "Port") == 0)
222         {
223                 int port = (int) (atof (value));
224                 if ((port > 0) && (port <= 65535))
225                         snprintf (hddtemp_port, sizeof (hddtemp_port),
226                                         "%i", port);
227                 else
228                         strncpy (hddtemp_port, value, sizeof (hddtemp_port));
229                 hddtemp_port[sizeof (hddtemp_port) - 1] = '\0';
230         }
231         else
232         {
233                 return (-1);
234         }
235
236         return (0);
237 }
238
239 /* In the init-function we initialize the `hddname_t' list used to translate
240  * disk-names. Under Linux that's done using `/proc/partitions'. Under other
241  * operating-systems, it's not done at all. */
242 static int hddtemp_init (void)
243 {
244 #if KERNEL_LINUX
245         FILE *fh;
246         char buf[1024];
247         int buflen;
248
249         char *fields[16];
250         int num_fields;
251
252         int major;
253         int minor;
254         char *name;
255         hddname_t *next;
256         hddname_t *entry;
257
258         next = first_hddname;
259         while (next != NULL)
260         {
261                 entry = next;
262                 next = entry->next;
263
264                 free (entry->name);
265                 free (entry);
266         }
267         first_hddname = NULL;
268
269         if ((fh = fopen ("/proc/partitions", "r")) != NULL)
270         {
271                 DEBUG ("hddtemp plugin: Looking at /proc/partitions...");
272
273                 while (fgets (buf, sizeof (buf), fh) != NULL)
274                 {
275                         /* Delete trailing newlines */
276                         buflen = strlen (buf);
277
278                         while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
279                                 buf[--buflen] = '\0';
280
281                         /* We want lines of the form:
282                          *
283                          *     3     1   77842926 hda1
284                          *
285                          * ...so, skip everything else. */
286                         if (buflen == 0)
287                                 continue;
288                         
289                         num_fields = strsplit (buf, fields, 16);
290
291                         if (num_fields != 4)
292                                 continue;
293
294                         major = atoi (fields[0]);
295                         minor = atoi (fields[1]);
296
297                         /* We try to keep only entries, which may correspond to
298                          * physical disks and that may have a corresponding
299                          * entry in the hddtemp daemon. Basically, this means
300                          * IDE and SCSI. */
301                         switch (major)
302                         {
303                                 /* SCSI. */
304                                 case SCSI_DISK0_MAJOR:
305                                 case SCSI_DISK1_MAJOR:
306                                 case SCSI_DISK2_MAJOR:
307                                 case SCSI_DISK3_MAJOR:
308                                 case SCSI_DISK4_MAJOR:
309                                 case SCSI_DISK5_MAJOR:
310                                 case SCSI_DISK6_MAJOR:
311                                 case SCSI_DISK7_MAJOR:
312 #ifdef SCSI_DISK8_MAJOR
313                                 case SCSI_DISK8_MAJOR:
314                                 case SCSI_DISK9_MAJOR:
315                                 case SCSI_DISK10_MAJOR:
316                                 case SCSI_DISK11_MAJOR:
317                                 case SCSI_DISK12_MAJOR:
318                                 case SCSI_DISK13_MAJOR:
319                                 case SCSI_DISK14_MAJOR:
320                                 case SCSI_DISK15_MAJOR:
321 #endif /* SCSI_DISK8_MAJOR */
322                                         /* SCSI disks minors are multiples of 16.
323                                          * Keep only those. */
324                                         if (minor % 16)
325                                                 continue;
326                                         break;
327
328                                 /* IDE. */
329                                 case IDE0_MAJOR:
330                                 case IDE1_MAJOR:
331                                 case IDE2_MAJOR:
332                                 case IDE3_MAJOR:
333                                 case IDE4_MAJOR:
334                                 case IDE5_MAJOR:
335                                 case IDE6_MAJOR:
336                                 case IDE7_MAJOR:
337                                 case IDE8_MAJOR:
338                                 case IDE9_MAJOR:
339                                         /* IDE disks minors can only be 0 or 64.
340                                          * Keep only those. */
341                                         if(minor != 0 && minor != 64)
342                                                 continue;
343                                         break;
344
345                                 /* Skip all other majors. */
346                                 default:
347                                         DEBUG ("hddtemp plugin: Skipping unknown major %i", major);
348                                         continue;
349                         } /* switch (major) */
350
351                         if ((name = strdup (fields[3])) == NULL)
352                         {
353                                 ERROR ("hddtemp plugin: strdup(%s) == NULL", fields[3]);
354                                 continue;
355                         }
356
357                         if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
358                         {
359                                 ERROR ("hddtemp plugin: malloc (%u) == NULL",
360                                                 (unsigned int) sizeof (hddname_t));
361                                 free (name);
362                                 continue;
363                         }
364
365                         DEBUG ("hddtemp plugin: Found disk: %s (%u:%u).", name, major, minor);
366
367                         entry->major = major;
368                         entry->minor = minor;
369                         entry->name  = name;
370                         entry->next  = NULL;
371
372                         if (first_hddname == NULL)
373                         {
374                                 first_hddname = entry;
375                         }
376                         else
377                         {
378                                 entry->next = first_hddname;
379                                 first_hddname = entry;
380                         }
381                 }
382                 fclose (fh);
383         }
384 #if COLLECT_DEBUG
385         else
386         {
387                 char errbuf[1024];
388                 DEBUG ("hddtemp plugin: Could not open /proc/partitions: %s",
389                                 sstrerror (errno, errbuf, sizeof (errbuf)));
390         }
391 #endif /* COLLECT_DEBUG */
392 #endif /* KERNEL_LINUX */
393
394         return (0);
395 } /* int hddtemp_init */
396
397 /*
398  * hddtemp_get_name
399  *
400  * Description:
401  *   Try to "cook" a bit the drive name as returned
402  *   by the hddtemp daemon. The intend is to transform disk
403  *   names into <major>-<minor> when possible.
404  */
405 static char *hddtemp_get_name (char *drive)
406 {
407         hddname_t *list;
408         char *ret;
409
410         for (list = first_hddname; list != NULL; list = list->next)
411                 if (strcmp (drive, list->name) == 0)
412                         break;
413
414         if (list == NULL)
415         {
416                 DEBUG ("hddtemp plugin: Don't know %s, keeping name as-is.", drive);
417                 return (strdup (drive));
418         }
419
420         if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
421                 return (NULL);
422
423         if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
424         {
425                 free (ret);
426                 return (NULL);
427         }
428
429         return (ret);
430 }
431
432 static void hddtemp_submit (char *type_instance, double value)
433 {
434         value_t values[1];
435         value_list_t vl = VALUE_LIST_INIT;
436
437         values[0].gauge = value;
438
439         vl.values = values;
440         vl.values_len = 1;
441         vl.time = time (NULL);
442         strcpy (vl.host, hostname_g);
443         strcpy (vl.plugin, "hddtemp");
444         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
445
446         plugin_dispatch_values ("temperature", &vl);
447 }
448
449 static int hddtemp_read (void)
450 {
451         char buf[1024];
452         char *fields[128];
453         char *ptr;
454         char *saveptr;
455         int num_fields;
456         int num_disks;
457         int i;
458
459         /* get data from daemon */
460         if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
461                 return (-1);
462
463         /* NB: strtok_r will eat up "||" and leading "|"'s */
464         num_fields = 0;
465         ptr = buf;
466         saveptr = NULL;
467         while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
468         {
469                 ptr = NULL;
470                 num_fields++;
471
472                 if (num_fields >= 128)
473                         break;
474         }
475
476         num_disks = num_fields / 4;
477
478         for (i = 0; i < num_disks; i++)
479         {
480                 char *name, *submit_name;
481                 double temperature;
482                 char *mode;
483
484                 mode = fields[4*i + 3];
485                 name = basename (fields[4*i + 0]);
486
487                 /* Skip non-temperature information */
488                 if (mode[0] != 'C' && mode[0] != 'F')
489                         continue;
490
491                 temperature = atof (fields[4*i + 2]);
492
493                 /* Convert farenheit to celsius */
494                 if (mode[0] == 'F')
495                         temperature = (temperature - 32.0) * 5.0 / 9.0;
496
497                 if ((submit_name = hddtemp_get_name (name)) != NULL)
498                 {
499                         hddtemp_submit (submit_name, temperature);
500                         free (submit_name);
501                 }
502                 else
503                 {
504                         hddtemp_submit (name, temperature);
505                 }
506         }
507         
508         return (0);
509 } /* int hddtemp_read */
510
511 /* module_register
512    Register collectd plugin. */
513 void module_register (void)
514 {
515         plugin_register_config ("hddtemp", hddtemp_config,
516                         config_keys, config_keys_num);
517         plugin_register_init ("hddtemp", hddtemp_init);
518         plugin_register_read ("hddtemp", hddtemp_read);
519 }