2 * collectd - src/hddtemp.c
3 * Copyright (C) 2005,2006 Vincent Stehlé
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.
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.
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
20 * Vincent Stehlé <vincent.stehle at free.fr>
21 * Florian octo Forster <octo at verplant.org>
24 * Do a pass, some day, and spare some memory. We consume too much for now
25 * in string buffers and the like.
32 #include "configfile.h"
33 #include "utils_debug.h"
35 #define MODULE_NAME "hddtemp"
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>
47 #define HDDTEMP_DEF_HOST "127.0.0.1"
48 #define HDDTEMP_DEF_PORT "7634"
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[] =
58 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
61 static int ds_num = 1;
63 static char *config_keys[] =
69 static int config_keys_num = 2;
71 typedef struct hddname
79 static hddname_t *first_hddname = NULL;
80 static char *hddtemp_host = NULL;
81 static char *hddtemp_port = NULL;
85 * hddtemp_query_daemon
88 * Connect to the hddtemp daemon and receive data.
91 * `buffer' The buffer where we put the received ascii string.
92 * `buffer_size' Size of the buffer
95 * >= 0 if ok, < 0 otherwise.
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|*|
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
106 static int hddtemp_query_daemon (char *buffer, int buffer_size)
115 struct addrinfo ai_hints;
116 struct addrinfo *ai_list, *ai_ptr;
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;
127 host = HDDTEMP_DEF_HOST;
131 port = HDDTEMP_DEF_PORT;
133 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
135 syslog (LOG_ERR, "hddtemp: getaddrinfo (%s, %s): %s",
137 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
142 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
144 /* create our socket descriptor */
145 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
147 syslog (LOG_ERR, "hddtemp: socket: %s",
152 /* connect to the hddtemp daemon */
153 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
155 DBG ("hddtemp: connect (%s, %s): %s", host, port,
162 /* A socket could be opened and connecting succeeded. We're
167 freeaddrinfo (ai_list);
171 syslog (LOG_ERR, "hddtemp: Could not connect to daemon.");
175 /* receive data from the hddtemp daemon */
176 memset (buffer, '\0', buffer_size);
179 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
183 if ((errno == EAGAIN) || (errno == EINTR))
186 syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
191 buffer_fill += status;
193 if (buffer_fill >= buffer_size)
197 if (buffer_fill >= buffer_size)
199 buffer[buffer_size - 1] = '\0';
200 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
202 else if (buffer_fill == 0)
204 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
205 "Buffer: `%s'", buffer);
214 static int hddtemp_config (char *key, char *value)
216 if (strcasecmp (key, "host") == 0)
218 if (hddtemp_host != NULL)
220 hddtemp_host = strdup (value);
222 else if (strcasecmp (key, "port") == 0)
224 if (hddtemp_port != NULL)
226 hddtemp_port = strdup (value);
236 /* In the init-function we initialize the `hddname_t' list used to translate
237 * disk-names. Under Linux that's done using `/proc/partitions'. Under other
238 * operating-systems, it's not done at all. */
239 static void hddtemp_init (void)
243 char buf[BUFFER_SIZE];
255 next = first_hddname;
264 first_hddname = NULL;
266 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
268 DBG ("Looking at /proc/partitions...");
270 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
272 /* Delete trailing newlines */
273 buflen = strlen (buf);
275 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
276 buf[--buflen] = '\0';
278 /* We want lines of the form:
282 * ...so, skip everything else. */
286 num_fields = strsplit (buf, fields, 16);
291 major = atoi (fields[0]);
292 minor = atoi (fields[1]);
294 /* We try to keep only entries, which may correspond to
295 * physical disks and that may have a corresponding
296 * entry in the hddtemp daemon. Basically, this means
301 case SCSI_DISK0_MAJOR:
302 case SCSI_DISK1_MAJOR:
303 case SCSI_DISK2_MAJOR:
304 case SCSI_DISK3_MAJOR:
305 case SCSI_DISK4_MAJOR:
306 case SCSI_DISK5_MAJOR:
307 case SCSI_DISK6_MAJOR:
308 case SCSI_DISK7_MAJOR:
309 case SCSI_DISK8_MAJOR:
310 case SCSI_DISK9_MAJOR:
311 case SCSI_DISK10_MAJOR:
312 case SCSI_DISK11_MAJOR:
313 case SCSI_DISK12_MAJOR:
314 case SCSI_DISK13_MAJOR:
315 case SCSI_DISK14_MAJOR:
316 case SCSI_DISK15_MAJOR:
317 /* SCSI disks minors are multiples of 16.
318 * Keep only those. */
334 /* IDE disks minors can only be 0 or 64.
335 * Keep only those. */
336 if(minor != 0 && minor != 64)
340 /* Skip all other majors. */
342 DBG ("Skipping unknown major %i", major);
344 } /* switch (major) */
346 if ((name = strdup (fields[3])) == NULL)
348 syslog (LOG_ERR, "hddtemp: strdup(%s) == NULL", fields[3]);
352 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
354 syslog (LOG_ERR, "hddtemp: malloc (%u) == NULL",
355 (unsigned int) sizeof (hddname_t));
360 DBG ("Found disk: %s (%u:%u).", name, major, minor);
362 entry->major = major;
363 entry->minor = minor;
367 if (first_hddname == NULL)
369 first_hddname = entry;
373 entry->next = first_hddname;
374 first_hddname = entry;
380 DBG ("Could not open /proc/partitions: %s",
382 #endif /* KERNEL_LINUX */
385 static void hddtemp_write (char *host, char *inst, char *val)
387 char filename[BUFFER_SIZE];
390 /* construct filename */
391 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
394 else if (status >= BUFFER_SIZE)
397 rrd_update_file (host, filename, val, ds_def, ds_num);
404 * Try to "cook" a bit the drive name as returned
405 * by the hddtemp daemon. The intend is to transform disk
406 * names into <major>-<minor> when possible.
408 static char *hddtemp_get_name (char *drive)
413 for (list = first_hddname; list != NULL; list = list->next)
414 if (strcmp (drive, list->name) == 0)
419 DBG ("Don't know %s, keeping name as-is.", drive);
420 return (strdup (drive));
423 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
426 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
435 static void hddtemp_submit (char *inst, double temperature)
437 char buf[BUFFER_SIZE];
439 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature)
443 plugin_submit (MODULE_NAME, inst, buf);
446 static void hddtemp_read (void)
448 char buf[BUFFER_SIZE];
455 static int wait_time = 1;
456 static int wait_left = 0;
464 /* get data from daemon */
465 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
467 /* This limit is reached in log2(86400) =~ 17 steps. Since
468 * there is a 2^n seconds wait between each step it will need
469 * roughly one day to reach this limit. -octo */
472 if (wait_time > 86400)
475 wait_left = wait_time;
485 /* NB: strtok will eat up "||" and leading "|"'s */
488 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
493 if (num_fields >= 128)
497 num_disks = num_fields / 4;
499 for (i = 0; i < num_disks; i++)
501 char *name, *submit_name;
505 mode = fields[4*i + 3];
506 name = basename (fields[4*i + 0]);
508 /* Skip non-temperature information */
509 if (mode[0] != 'C' && mode[0] != 'F')
512 temperature = atof (fields[4*i + 2]);
514 /* Convert farenheit to celsius */
516 temperature = (temperature - 32.0) * 5.0 / 9.0;
518 if ((submit_name = hddtemp_get_name (name)) != NULL)
520 hddtemp_submit (submit_name, temperature);
525 hddtemp_submit (name, temperature);
531 Register collectd plugin. */
532 void module_register (void)
534 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
535 cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);