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