2 * collectd - src/hddtemp.c
3 * Copyright (C) 2005 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>
27 #include "configfile.h"
29 #define MODULE_NAME "hddtemp"
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35 #include <libgen.h> /* for basename */
37 #define HDDTEMP_DEF_HOST "127.0.0.1"
38 #define HDDTEMP_DEF_PORT "7634"
41 Size of the buffer we use to receive from the hddtemp daemon. */
42 #define BUFFER_SIZE 1024
44 static char *filename_format = "hddtemp-%s.rrd";
46 static char *ds_def[] =
48 "DS:value:GAUGE:25:U:U",
51 static int ds_num = 1;
53 static char *config_keys[] =
59 static int config_keys_num = 2;
61 typedef struct hddname
69 static hddname_t *first_hddname = NULL;
70 static char *hddtemp_host = NULL;
71 static char *hddtemp_port = NULL;
75 * hddtemp_query_daemon
78 * Connect to the hddtemp daemon and receive data.
81 * `buffer' The buffer where we put the received ascii string.
82 * `buffer_size' Size of the buffer
85 * >= 0 if ok, < 0 otherwise.
88 * Example of possible strings, as received from daemon:
89 * |/dev/hda|ST340014A|36|C|
90 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
93 * we need to create a new socket each time. Is there another way?
94 * Hm, maybe we can re-use the `sockaddr' structure? -octo
96 static int hddtemp_query_daemon (char *buffer, int buffer_size)
105 struct addrinfo ai_hints;
106 struct addrinfo *ai_list, *ai_ptr;
109 memset (&ai_hints, '\0', sizeof (ai_hints));
110 ai_hints.ai_flags = AI_ADDRCONFIG;
111 ai_hints.ai_family = PF_UNSPEC;
112 ai_hints.ai_socktype = SOCK_STREAM;
113 ai_hints.ai_protocol = IPPROTO_TCP;
117 host = HDDTEMP_DEF_HOST;
121 port = HDDTEMP_DEF_PORT;
123 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
125 syslog (LOG_ERR, "hddtemp: getaddrinfo (%s, %s): %s",
127 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
132 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
134 /* create our socket descriptor */
135 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
137 syslog (LOG_ERR, "hddtemp: socket: %s",
142 /* connect to the hddtemp daemon */
143 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
145 syslog (LOG_ERR, "hddtemp: connect (%s, %s): %s",
146 host, port, strerror (errno));
153 freeaddrinfo (ai_list);
158 /* receive data from the hddtemp daemon */
159 memset (buffer, '\0', buffer_size);
162 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
166 if ((errno == EAGAIN) || (errno == EINTR))
169 syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
173 buffer_fill += status;
175 if (buffer_fill >= buffer_size)
179 if (buffer_fill >= buffer_size)
181 buffer[buffer_size - 1] = '\0';
182 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
184 else if (buffer_fill == 0)
186 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
187 "Buffer: `%s'", buffer);
196 static int hddtemp_config (char *key, char *value)
198 if (strcasecmp (key, "host") == 0)
200 if (hddtemp_host != NULL)
202 hddtemp_host = strdup (value);
204 else if (strcasecmp (key, "port") == 0)
206 if (hddtemp_port != NULL)
208 hddtemp_port = strdup (value);
218 static void hddtemp_init (void)
221 char buf[BUFFER_SIZE];
233 next = first_hddname;
242 first_hddname = NULL;
244 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
246 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
248 /* Delete trailing newlines */
249 buflen = strlen (buf);
250 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
251 buf[--buflen] = '\0';
255 num_fields = strsplit (buf, fields, 16);
260 major = atoi (fields[0]);
261 minor = atoi (fields[1]);
263 /* I know that this makes `minor' redundant, but I want
264 * to be able to change this beavior in the future..
265 * And 4 or 8 bytes won't hurt anybody.. -octo */
271 if ((name = strdup (fields[3])) == NULL)
274 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
280 entry->major = major;
281 entry->minor = minor;
285 if (first_hddname == NULL)
287 first_hddname = entry;
291 entry->next = first_hddname;
292 first_hddname = entry;
300 static void hddtemp_write (char *host, char *inst, char *val)
302 char filename[BUFFER_SIZE];
305 /* construct filename */
306 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
309 else if (status >= BUFFER_SIZE)
312 rrd_update_file (host, filename, val, ds_def, ds_num);
315 static char *hddtemp_get_name (char *drive)
320 for (list = first_hddname; list != NULL; list = list->next)
321 if (strcmp (drive, list->name) == 0)
325 return (strdup (drive));
327 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
330 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
339 static void hddtemp_submit (char *inst, double temperature)
341 char buf[BUFFER_SIZE];
343 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
346 plugin_submit (MODULE_NAME, inst, buf);
349 static void hddtemp_read (void)
351 char buf[BUFFER_SIZE];
358 static int wait_time = 1;
359 static int wait_left = 0;
367 /* get data from daemon */
368 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
370 /* This limit is reached in log2(86400) =~ 17 steps. Since
371 * there is a 2^n seconds wait between each step it will need
372 * roughly one day to reach this limit. -octo */
375 if (wait_time > 86400)
378 wait_left = wait_time;
388 /* NB: strtok will eat up "||" and leading "|"'s */
391 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
396 if (num_fields >= 128)
400 num_disks = num_fields / 4;
402 for (i = 0; i < num_disks; i++)
404 char *name, *submit_name;
408 mode = fields[4*i + 3];
410 /* Skip non-temperature information */
411 if (mode[0] != 'C' && mode[0] != 'F')
414 name = basename (fields[4*i + 0]);
415 temperature = atof (fields[4*i + 2]);
417 /* Convert farenheit to celsius */
419 temperature = (temperature - 32.0) * 5.0 / 9.0;
421 if ((submit_name = hddtemp_get_name (name)) != NULL)
423 hddtemp_submit (submit_name, temperature);
428 hddtemp_submit (name, temperature);
434 Register collectd plugin. */
435 void module_register (void)
437 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
438 cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);