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