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 */
39 The ip address 127.0.0.1, as a 32 bit. */
40 #define LOCALHOST_ADDR 0x7F000001
43 The tcp port the hddtemp daemon is listening on. */
44 #define HDDTEMP_PORT 7634
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:25: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?
105 static int hddtemp_query_daemon (char *buffer, int buffer_size)
114 struct addrinfo ai_hints;
115 struct addrinfo *ai_list, *ai_ptr;
118 memset (&ai_hints, '\0', sizeof (ai_hints));
119 ai_hints.ai_flags = AI_ADDRCONFIG;
120 ai_hints.ai_family = PF_UNSPEC;
121 ai_hints.ai_socktype = SOCK_STREAM;
122 ai_hints.ai_protocol = IPPROTO_TCP;
126 host = HDDTEMP_DEF_HOST;
130 port = HDDTEMP_DEF_PORT;
132 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
134 syslog (LOG_ERR, "hddtemp: getaddrinfo (%s, %s): %s",
136 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
141 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
143 /* create our socket descriptor */
144 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
146 syslog (LOG_ERR, "hddtemp: socket: %s",
151 /* connect to the hddtemp daemon */
152 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
154 syslog (LOG_ERR, "hddtemp: connect (%s, %s): %s",
155 host, port, strerror (errno));
162 freeaddrinfo (ai_list);
167 /* receive data from the hddtemp daemon */
168 memset (buffer, '\0', buffer_size);
171 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
175 if ((errno == EAGAIN) || (errno == EINTR))
178 syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
182 buffer_fill += status;
184 if (buffer_fill >= buffer_size)
188 if (buffer_fill >= buffer_size)
190 buffer[buffer_size - 1] = '\0';
191 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
193 else if (buffer_fill == 0)
195 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
196 "Buffer: `%s'", buffer);
205 static int hddtemp_config (char *key, char *value)
207 if (strcasecmp (key, "host") == 0)
209 if (hddtemp_host != NULL)
211 hddtemp_host = strdup (value);
213 else if (strcasecmp (key, "port") == 0)
215 if (hddtemp_port != NULL)
217 hddtemp_port = strdup (value);
227 static void hddtemp_init (void)
230 char buf[BUFFER_SIZE];
242 next = first_hddname;
251 first_hddname = NULL;
253 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
255 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
257 /* Delete trailing newlines */
258 buflen = strlen (buf);
259 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
260 buf[--buflen] = '\0';
264 num_fields = strsplit (buf, fields, 16);
269 major = atoi (fields[0]);
270 minor = atoi (fields[1]);
272 /* I know that this makes `minor' redundant, but I want
273 * to be able to change this beavior in the future..
274 * And 4 or 8 bytes won't hurt anybody.. -octo */
280 if ((name = strdup (fields[3])) == NULL)
283 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
289 entry->major = major;
290 entry->minor = minor;
294 if (first_hddname == NULL)
296 first_hddname = entry;
300 entry->next = first_hddname;
301 first_hddname = entry;
309 static void hddtemp_write (char *host, char *inst, char *val)
311 char filename[BUFFER_SIZE];
314 /* construct filename */
315 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
318 else if (status >= BUFFER_SIZE)
321 rrd_update_file (host, filename, val, ds_def, ds_num);
324 static char *hddtemp_get_name (char *drive)
329 for (list = first_hddname; list != NULL; list = list->next)
330 if (strcmp (drive, list->name) == 0)
334 return (strdup (drive));
336 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
339 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
348 static void hddtemp_submit (char *inst, double temperature)
350 char buf[BUFFER_SIZE];
352 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
355 plugin_submit (MODULE_NAME, inst, buf);
358 static void hddtemp_read (void)
360 char buf[BUFFER_SIZE];
367 static int wait_time = 1;
368 static int wait_left = 0;
376 /* get data from daemon */
377 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
379 /* This limit is reached in log2(86400) =~ 17 steps. Since
380 * there is a 2^n seconds wait between each step it will need
381 * roughly one day to reach this limit. -octo */
384 if (wait_time > 86400)
387 wait_left = wait_time;
397 /* NB: strtok will eat up "||" and leading "|"'s */
400 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
405 if (num_fields >= 128)
409 num_disks = num_fields / 4;
411 for (i = 0; i < num_disks; i++)
413 char *name, *submit_name;
417 mode = fields[4*i + 3];
419 /* Skip non-temperature information */
420 if (mode[0] != 'C' && mode[0] != 'F')
423 name = basename (fields[4*i + 0]);
424 temperature = atof (fields[4*i + 2]);
426 /* Convert farenheit to celsius */
428 temperature = (temperature - 32.0) * 5.0 / 9.0;
430 if ((submit_name = hddtemp_get_name (name)) != NULL)
432 hddtemp_submit (submit_name, temperature);
437 hddtemp_submit (name, temperature);
443 Register collectd plugin. */
444 void module_register (void)
446 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
447 cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);