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 int hddtemp_port = 0;
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 hostent *srv_ent;
115 struct sockaddr_in srv_addr;
119 host = HDDTEMP_DEF_HOST;
123 port = HDDTEMP_DEF_PORT;
126 * Resolve `host' address and set up `srv_addr'
128 memset (&srv_addr, '\0', sizeof (srv_addr));
134 if ((srv_ent = gethostbyname (host)) == NULL)
136 syslog (LOG_WARNING, "hddtemp: Could not resolve hostname `%s'",
141 memcpy (&srv_addr.sin_addr.s_addr, srv_ent->h_addr, srv_ent->h_length);
142 srv_addr.sin_port = htons (port);
143 srv_addr.sin_family = AF_INET;
145 /* create our socket descriptor */
146 if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
148 syslog (LOG_ERR, "hddtemp: could not create socket: %s",
153 /* connect to the hddtemp daemon */
154 if (connect (sock, (struct sockaddr *) &srv_addr, sizeof (srv_addr)))
156 syslog (LOG_ERR, "hddtemp: Could not connect to the hddtemp "
157 "daemon at %s:%i: %s",
158 host, port, strerror (errno));
163 /* receive data from the hddtemp daemon */
164 memset (buffer, '\0', buffer_size);
167 while ((status = read (sock, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
171 if ((errno == EAGAIN) || (errno == EINTR))
174 syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
178 buffer_fill += status;
180 if (buffer_fill >= buffer_size)
184 if (buffer_fill >= buffer_size)
186 buffer[buffer_size - 1] = '\0';
187 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
189 else if (buffer_fill == 0)
191 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
192 "Buffer: `%s'", buffer);
201 static int hddtemp_config (char *key, char *value)
203 if (strcasecmp (key, "host") == 0)
205 if (hddtemp_host != NULL)
207 hddtemp_host = strdup (value);
209 else if (strcasecmp (key, "port") == 0)
211 hddtemp_port = atoi (value);
221 static void hddtemp_init (void)
224 char buf[BUFFER_SIZE];
236 next = first_hddname;
245 first_hddname = NULL;
247 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
249 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
251 /* Delete trailing newlines */
252 buflen = strlen (buf);
253 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
254 buf[--buflen] = '\0';
258 num_fields = strsplit (buf, fields, 16);
263 major = atoi (fields[0]);
264 minor = atoi (fields[1]);
266 /* I know that this makes `minor' redundant, but I want
267 * to be able to change this beavior in the future..
268 * And 4 or 8 bytes won't hurt anybody.. -octo */
274 if ((name = strdup (fields[3])) == NULL)
277 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
283 entry->major = major;
284 entry->minor = minor;
288 if (first_hddname == NULL)
290 first_hddname = entry;
294 entry->next = first_hddname;
295 first_hddname = entry;
303 static void hddtemp_write (char *host, char *inst, char *val)
305 char filename[BUFFER_SIZE];
308 /* construct filename */
309 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
312 else if (status >= BUFFER_SIZE)
315 rrd_update_file (host, filename, val, ds_def, ds_num);
318 static char *hddtemp_get_name (char *drive)
323 for (list = first_hddname; list != NULL; list = list->next)
324 if (strcmp (drive, list->name) == 0)
328 return (strdup (drive));
330 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
333 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
342 static void hddtemp_submit (char *inst, double temperature)
344 char buf[BUFFER_SIZE];
346 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
349 plugin_submit (MODULE_NAME, inst, buf);
352 static void hddtemp_read (void)
354 char buf[BUFFER_SIZE];
361 static int wait_time = 1;
362 static int wait_left = 0;
370 /* get data from daemon */
371 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
373 /* This limit is reached in log2(86400) =~ 17 steps. Since
374 * there is a 2^n seconds wait between each step it will need
375 * roughly one day to reach this limit. -octo */
378 if (wait_time > 86400)
381 wait_left = wait_time;
391 /* NB: strtok will eat up "||" and leading "|"'s */
394 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
399 if (num_fields >= 128)
403 num_disks = num_fields / 4;
405 for (i = 0; i < num_disks; i++)
407 char *name, *submit_name;
411 mode = fields[4*i + 3];
413 /* Skip non-temperature information */
414 if (mode[0] != 'C' && mode[0] != 'F')
417 name = basename (fields[4*i + 0]);
418 temperature = atof (fields[4*i + 2]);
420 /* Convert farenheit to celsius */
422 temperature = (temperature - 32.0) * 5.0 / 9.0;
424 if ((submit_name = hddtemp_get_name (name)) != NULL)
426 hddtemp_submit (submit_name, temperature);
431 hddtemp_submit (name, temperature);
437 Register collectd plugin. */
438 void module_register (void)
440 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
441 cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);