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 **/
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32 #include "configfile.h"
33 #include "utils_debug.h"
35 #define MODULE_NAME "hddtemp"
37 #include <netdb.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
41 #include <libgen.h> /* for basename */
43 #if HAVE_LINUX_MAJOR_H
44 # include <linux/major.h>
45 #endif
47 #define HDDTEMP_DEF_HOST "127.0.0.1"
48 #define HDDTEMP_DEF_PORT "7634"
50 /* BUFFER_SIZE
51 Size of the buffer we use to receive from the hddtemp daemon. */
52 #define BUFFER_SIZE 1024
54 static char *filename_format = "hddtemp-%s.rrd";
56 static char *ds_def[] =
57 {
58 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
59 NULL
60 };
61 static int ds_num = 1;
63 static char *config_keys[] =
64 {
65 "Host",
66 "Port",
67 NULL
68 };
69 static int config_keys_num = 2;
71 typedef struct hddname
72 {
73 int major;
74 int minor;
75 char *name;
76 struct hddname *next;
77 } hddname_t;
79 static hddname_t *first_hddname = NULL;
80 static char *hddtemp_host = NULL;
81 static char *hddtemp_port = NULL;
83 /*
84 * NAME
85 * hddtemp_query_daemon
86 *
87 * DESCRIPTION
88 * Connect to the hddtemp daemon and receive data.
89 *
90 * ARGUMENTS:
91 * `buffer' The buffer where we put the received ascii string.
92 * `buffer_size' Size of the buffer
93 *
94 * RETURN VALUE:
95 * >= 0 if ok, < 0 otherwise.
96 *
97 * NOTES:
98 * Example of possible strings, as received from daemon:
99 * |/dev/hda|ST340014A|36|C|
100 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
101 *
102 * FIXME:
103 * we need to create a new socket each time. Is there another way?
104 * Hm, maybe we can re-use the `sockaddr' structure? -octo
105 */
106 static int hddtemp_query_daemon (char *buffer, int buffer_size)
107 {
108 int fd;
109 ssize_t status;
110 int buffer_fill;
112 const char *host;
113 const char *port;
115 struct addrinfo ai_hints;
116 struct addrinfo *ai_list, *ai_ptr;
117 int ai_return;
119 memset (&ai_hints, '\0', sizeof (ai_hints));
120 ai_hints.ai_flags = AI_ADDRCONFIG;
121 ai_hints.ai_family = PF_UNSPEC;
122 ai_hints.ai_socktype = SOCK_STREAM;
123 ai_hints.ai_protocol = IPPROTO_TCP;
125 host = hddtemp_host;
126 if (host == NULL)
127 host = HDDTEMP_DEF_HOST;
129 port = hddtemp_port;
130 if (port == NULL)
131 port = HDDTEMP_DEF_PORT;
133 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
134 {
135 syslog (LOG_ERR, "hddtemp: getaddrinfo (%s, %s): %s",
136 host, port,
137 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
138 return (-1);
139 }
141 fd = -1;
142 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
143 {
144 /* create our socket descriptor */
145 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
146 {
147 syslog (LOG_ERR, "hddtemp: socket: %s",
148 strerror (errno));
149 continue;
150 }
152 /* connect to the hddtemp daemon */
153 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
154 {
155 DBG ("hddtemp: connect (%s, %s): %s", host, port,
156 strerror (errno));
157 close (fd);
158 fd = -1;
159 continue;
160 }
162 /* A socket could be opened and connecting succeeded. We're
163 * done. */
164 break;
165 }
167 freeaddrinfo (ai_list);
169 if (fd < 0)
170 {
171 syslog (LOG_ERR, "hddtemp: Could not connect to daemon.");
172 return (-1);
173 }
175 /* receive data from the hddtemp daemon */
176 memset (buffer, '\0', buffer_size);
178 buffer_fill = 0;
179 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
180 {
181 if (status == -1)
182 {
183 if ((errno == EAGAIN) || (errno == EINTR))
184 continue;
186 syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
187 strerror (errno));
188 return (-1);
189 }
190 buffer_fill += status;
192 if (buffer_fill >= buffer_size)
193 break;
194 }
196 if (buffer_fill >= buffer_size)
197 {
198 buffer[buffer_size - 1] = '\0';
199 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
200 }
201 else if (buffer_fill == 0)
202 {
203 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
204 "Buffer: `%s'", buffer);
205 close (fd);
206 return (-1);
207 }
209 close (fd);
210 return (0);
211 }
213 static int hddtemp_config (char *key, 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 if (hddtemp_port != NULL)
224 free (hddtemp_port);
225 hddtemp_port = strdup (value);
226 }
227 else
228 {
229 return (-1);
230 }
232 return (0);
233 }
235 /* In the init-function we initialize the `hddname_t' list used to translate
236 * disk-names. Under Linux that's done using `/proc/partitions'. Under other
237 * operating-systems, it's not done at all. */
238 static void hddtemp_init (void)
239 {
240 #if KERNEL_LINUX
241 FILE *fh;
242 char buf[BUFFER_SIZE];
243 int buflen;
245 char *fields[16];
246 int num_fields;
248 int major;
249 int minor;
250 char *name;
251 hddname_t *next;
252 hddname_t *entry;
254 next = first_hddname;
255 while (next != NULL)
256 {
257 entry = next;
258 next = entry->next;
260 free (entry->name);
261 free (entry);
262 }
263 first_hddname = NULL;
265 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
266 {
267 DBG ("Looking at /proc/partitions...");
269 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
270 {
271 /* Delete trailing newlines */
272 buflen = strlen (buf);
274 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
275 buf[--buflen] = '\0';
277 /* We want lines of the form:
278 *
279 * 3 1 77842926 hda1
280 *
281 * ...so, skip everything else. */
282 if (buflen == 0)
283 continue;
285 num_fields = strsplit (buf, fields, 16);
287 if (num_fields != 4)
288 continue;
290 major = atoi (fields[0]);
291 minor = atoi (fields[1]);
293 /* We try to keep only entries, which may correspond to
294 * physical disks and that may have a corresponding
295 * entry in the hddtemp daemon. Basically, this means
296 * IDE and SCSI. */
297 switch (major)
298 {
299 /* SCSI. */
300 case SCSI_DISK0_MAJOR:
301 case SCSI_DISK1_MAJOR:
302 case SCSI_DISK2_MAJOR:
303 case SCSI_DISK3_MAJOR:
304 case SCSI_DISK4_MAJOR:
305 case SCSI_DISK5_MAJOR:
306 case SCSI_DISK6_MAJOR:
307 case SCSI_DISK7_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 /* SCSI disks minors are multiples of 16.
317 * Keep only those. */
318 if (minor % 16)
319 continue;
320 break;
322 /* IDE. */
323 case IDE0_MAJOR:
324 case IDE1_MAJOR:
325 case IDE2_MAJOR:
326 case IDE3_MAJOR:
327 case IDE4_MAJOR:
328 case IDE5_MAJOR:
329 case IDE6_MAJOR:
330 case IDE7_MAJOR:
331 case IDE8_MAJOR:
332 case IDE9_MAJOR:
333 /* IDE disks minors can only be 0 or 64.
334 * Keep only those. */
335 if(minor != 0 && minor != 64)
336 continue;
337 break;
339 /* Skip all other majors. */
340 default:
341 DBG ("Skipping unknown major %i", major);
342 continue;
343 } /* switch (major) */
345 if ((name = strdup (fields[3])) == NULL)
346 {
347 syslog (LOG_ERR, "hddtemp: strdup(%s) == NULL", fields[3]);
348 continue;
349 }
351 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
352 {
353 syslog (LOG_ERR, "hddtemp: malloc (%u) == NULL",
354 (unsigned int) sizeof (hddname_t));
355 free (name);
356 continue;
357 }
359 DBG ("Found disk: %s (%u:%u).", name, major, minor);
361 entry->major = major;
362 entry->minor = minor;
363 entry->name = name;
364 entry->next = NULL;
366 if (first_hddname == NULL)
367 {
368 first_hddname = entry;
369 }
370 else
371 {
372 entry->next = first_hddname;
373 first_hddname = entry;
374 }
375 }
376 }
377 else
378 DBG ("Could not open /proc/partitions: %s",
379 strerror (errno));
380 #endif /* KERNEL_LINUX */
381 }
383 static void hddtemp_write (char *host, char *inst, char *val)
384 {
385 char filename[BUFFER_SIZE];
386 int status;
388 /* construct filename */
389 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
390 if (status < 1)
391 return;
392 else if (status >= BUFFER_SIZE)
393 return;
395 rrd_update_file (host, filename, val, ds_def, ds_num);
396 }
398 /*
399 * hddtemp_get_name
400 *
401 * Description:
402 * Try to "cook" a bit the drive name as returned
403 * by the hddtemp daemon. The intend is to transform disk
404 * names into <major>-<minor> when possible.
405 */
406 static char *hddtemp_get_name (char *drive)
407 {
408 hddname_t *list;
409 char *ret;
411 for (list = first_hddname; list != NULL; list = list->next)
412 if (strcmp (drive, list->name) == 0)
413 break;
415 if (list == NULL)
416 {
417 DBG ("Don't know %s, keeping name as-is.", drive);
418 return (strdup (drive));
419 }
421 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
422 return (NULL);
424 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
425 {
426 free (ret);
427 return (NULL);
428 }
430 return (ret);
431 }
433 static void hddtemp_submit (char *inst, double temperature)
434 {
435 char buf[BUFFER_SIZE];
437 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature)
438 >= BUFFER_SIZE)
439 return;
441 plugin_submit (MODULE_NAME, inst, buf);
442 }
444 static void hddtemp_read (void)
445 {
446 char buf[BUFFER_SIZE];
447 char *fields[128];
448 char *ptr;
449 int num_fields;
450 int num_disks;
451 int i;
453 static int wait_time = 1;
454 static int wait_left = 0;
456 if (wait_left >= 10)
457 {
458 wait_left -= 10;
459 return;
460 }
462 /* get data from daemon */
463 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
464 {
465 /* This limit is reached in log2(86400) =~ 17 steps. Since
466 * there is a 2^n seconds wait between each step it will need
467 * roughly one day to reach this limit. -octo */
469 wait_time *= 2;
470 if (wait_time > 86400)
471 wait_time = 86400;
473 wait_left = wait_time;
475 return;
476 }
477 else
478 {
479 wait_time = 1;
480 wait_left = 0;
481 }
483 /* NB: strtok will eat up "||" and leading "|"'s */
484 num_fields = 0;
485 ptr = buf;
486 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
487 {
488 ptr = NULL;
489 num_fields++;
491 if (num_fields >= 128)
492 break;
493 }
495 num_disks = num_fields / 4;
497 for (i = 0; i < num_disks; i++)
498 {
499 char *name, *submit_name;
500 double temperature;
501 char *mode;
503 mode = fields[4*i + 3];
504 name = basename (fields[4*i + 0]);
506 /* Skip non-temperature information */
507 if (mode[0] != 'C' && mode[0] != 'F')
508 continue;
510 temperature = atof (fields[4*i + 2]);
512 /* Convert farenheit to celsius */
513 if (mode[0] == 'F')
514 temperature = (temperature - 32.0) * 5.0 / 9.0;
516 if ((submit_name = hddtemp_get_name (name)) != NULL)
517 {
518 hddtemp_submit (submit_name, temperature);
519 free (submit_name);
520 }
521 else
522 {
523 hddtemp_submit (name, temperature);
524 }
525 }
526 }
528 /* module_register
529 Register collectd plugin. */
530 void module_register (void)
531 {
532 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
533 cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);
534 }
536 #undef MODULE_NAME