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));
130 if ((srv_ent = gethostbyname (host)) == NULL)
132 syslog (LOG_WARNING, "hddtemp: Could not resolve hostname `%s'",
137 memcpy (&srv_addr.sin_addr.s_addr, srv_ent->h_addr, srv_ent->h_length);
138 srv_addr.sin_port = htons (port);
139 srv_addr.sin_family = AF_INET;
141 /* create our socket descriptor */
142 if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
144 syslog (LOG_ERR, "hddtemp: could not create socket: %s",
149 /* connect to the hddtemp daemon */
150 if (connect (sock, (struct sockaddr *) &srv_addr, sizeof (srv_addr)))
152 syslog (LOG_ERR, "hddtemp: Could not connect to the hddtemp "
153 "daemon at %s:%i: %s",
154 host, port, strerror (errno));
159 /* receive data from the hddtemp daemon */
160 memset (buffer, '\0', buffer_size);
163 while ((status = read (sock, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
167 if ((errno == EAGAIN) || (errno == EINTR))
170 syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
174 buffer_fill += status;
176 if (buffer_fill >= buffer_size)
180 if (buffer_fill >= buffer_size)
182 buffer[buffer_size - 1] = '\0';
183 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
185 else if (buffer_fill == 0)
187 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
188 "Buffer: `%s'", buffer);
197 static int hddtemp_config (char *key, char *value)
199 if (strcasecmp (key, "host") == 0)
201 if (hddtemp_host != NULL)
203 hddtemp_host = strdup (value);
205 else if (strcasecmp (key, "port") == 0)
207 hddtemp_port = atoi (value);
217 static void hddtemp_init (void)
220 char buf[BUFFER_SIZE];
232 next = first_hddname;
241 first_hddname = NULL;
243 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
245 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
247 /* Delete trailing newlines */
248 buflen = strlen (buf);
249 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
250 buf[--buflen] = '\0';
254 num_fields = strsplit (buf, fields, 16);
259 major = atoi (fields[0]);
260 minor = atoi (fields[1]);
262 /* I know that this makes `minor' redundant, but I want
263 * to be able to change this beavior in the future..
264 * And 4 or 8 bytes won't hurt anybody.. -octo */
270 if ((name = strdup (fields[3])) == NULL)
273 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
279 entry->major = major;
280 entry->minor = minor;
284 if (first_hddname == NULL)
286 first_hddname = entry;
290 entry->next = first_hddname;
291 first_hddname = entry;
299 static void hddtemp_write (char *host, char *inst, char *val)
301 char filename[BUFFER_SIZE];
304 /* construct filename */
305 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
308 else if (status >= BUFFER_SIZE)
311 rrd_update_file (host, filename, val, ds_def, ds_num);
314 static char *hddtemp_get_name (char *drive)
319 for (list = first_hddname; list != NULL; list = list->next)
320 if (strcmp (drive, list->name) == 0)
324 return (strdup (drive));
326 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
329 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
338 static void hddtemp_submit (char *inst, double temperature)
340 char buf[BUFFER_SIZE];
342 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
345 plugin_submit (MODULE_NAME, inst, buf);
348 static void hddtemp_read (void)
350 char buf[BUFFER_SIZE];
357 static int wait_time = 1;
358 static int wait_left = 0;
366 /* get data from daemon */
367 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
369 /* This limit is reached in log2(86400) =~ 17 steps. Since
370 * there is a 2^n seconds wait between each step it will need
371 * roughly one day to reach this limit. -octo */
374 if (wait_time > 86400)
377 wait_left = wait_time;
387 /* NB: strtok will eat up "||" and leading "|"'s */
390 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
395 if (num_fields >= 128)
399 num_disks = num_fields / 4;
401 for (i = 0; i < num_disks; i++)
403 char *name, *submit_name;
407 mode = fields[4*i + 3];
409 /* Skip non-temperature information */
410 if (mode[0] != 'C' && mode[0] != 'F')
413 name = basename (fields[4*i + 0]);
414 temperature = atof (fields[4*i + 2]);
416 /* Convert farenheit to celsius */
418 temperature = (temperature - 32.0) * 5.0 / 9.0;
420 if ((submit_name = hddtemp_get_name (name)) != NULL)
422 hddtemp_submit (submit_name, temperature);
427 hddtemp_submit (name, temperature);
433 Register collectd plugin. */
434 void module_register (void)
436 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
437 cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);