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 #define MODULE_NAME "hddtemp"
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
38 #include <libgen.h> /* for basename */
44 The ip address 127.0.0.1, as a 32 bit. */
45 #define LOCALHOST_ADDR 0x7F000001
48 The tcp port the hddtemp daemon is listening on. */
49 #define HDDTEMP_PORT 7634
52 Size of the buffer we use to receive from the hddtemp daemon. */
53 #define BUFFER_SIZE 1024
55 static char *filename_format = "hddtemp-%s.rrd";
57 static char *ds_def[] =
59 "DS:value:GAUGE:25:U:U",
62 static int ds_num = 1;
64 typedef struct hddname
72 static hddname_t *first_hddname = NULL;
74 /* hddtemp_query_daemon
75 Connect to the hddtemp daemon and receive data.
78 buffer: the buffer where we put the received ascii string.
79 buffer_size: size of the buffer
82 >= 0 if ok, < 0 otherwise.
84 Example of possible strings, as received from daemon:
86 |/dev/hda|ST340014A|36|C|
87 |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
89 FIXME: we need to create a new socket each time. Is there another way? */
91 static int hddtemp_query_daemon (char *buffer, int buffer_size)
95 const struct sockaddr_in addr =
97 AF_INET, /* sin_family */
98 htons(HDDTEMP_PORT), /* sin_port */
100 htonl(LOCALHOST_ADDR), /* s_addr */
104 /* create our socket descriptor */
105 if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
107 syslog (LOG_ERR, "hddtemp: could not create socket: %s", strerror (errno));
111 /* connect to the hddtemp daemon */
112 if (connect (sock, (const struct sockaddr *) &addr, sizeof (addr)))
114 syslog (LOG_ERR, "hddtemp: Could not connect to the hddtemp daemon: %s", strerror (errno));
119 /* receive data from the hddtemp daemon */
120 memset (buffer, '\0', buffer_size);
121 size = recv (sock, buffer, buffer_size, 0);
123 if (size >= buffer_size)
125 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
129 /* FIXME: Since the server closes the connection this returns zero. At
130 * least my machine does. -octo */
134 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. Buffer: `%s'", buffer);
141 syslog (LOG_ERR, "hddtemp: Could not receive from the hddtemp daemon: %s", strerror (errno));
150 static void hddtemp_init (void)
153 char buf[BUFFER_SIZE];
165 next = first_hddname;
174 first_hddname = NULL;
176 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
178 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
180 /* Delete trailing newlines */
181 buflen = strlen (buf);
182 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
183 buf[--buflen] = '\0';
187 num_fields = strsplit (buf, fields, 16);
192 major = atoi (fields[0]);
193 minor = atoi (fields[1]);
195 /* I know that this makes `minor' redundant, but I want
196 * to be able to change this beavior in the future..
197 * And 4 or 8 bytes won't hurt anybody.. -octo */
203 if ((name = strdup (fields[3])) == NULL)
206 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
212 entry->major = major;
213 entry->minor = minor;
217 if (first_hddname == NULL)
219 first_hddname = entry;
223 entry->next = first_hddname;
224 first_hddname = entry;
232 static void hddtemp_write (char *host, char *inst, char *val)
234 char filename[BUFFER_SIZE];
237 /* construct filename */
238 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
241 else if (status >= BUFFER_SIZE)
244 rrd_update_file (host, filename, val, ds_def, ds_num);
247 static char *hddtemp_get_name (char *drive)
252 for (list = first_hddname; list != NULL; list = list->next)
253 if (strcmp (drive, list->name) == 0)
257 return (strdup (drive));
259 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
262 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
271 static void hddtemp_submit (char *inst, double temperature)
273 char buf[BUFFER_SIZE];
275 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
278 plugin_submit (MODULE_NAME, inst, buf);
281 static void hddtemp_read (void)
283 char buf[BUFFER_SIZE];
290 static int wait_time = 1;
291 static int wait_left = 0;
299 /* get data from daemon */
300 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
302 /* This limit is reached in log2(86400) =~ 17 steps. Since
303 * there is a 2^n seconds wait between each step it will need
304 * roughly one day to reach this limit. -octo */
307 if (wait_time > 86400)
310 wait_left = wait_time;
320 /* NB: strtok will eat up "||" and leading "|"'s */
323 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
328 if (num_fields >= 128)
332 num_disks = num_fields / 4;
334 for (i = 0; i < num_disks; i++)
336 char *name, *submit_name;
340 mode = fields[4*i + 3];
342 /* Skip non-temperature information */
343 if (mode[0] != 'C' && mode[0] != 'F')
346 name = basename (fields[4*i + 0]);
347 temperature = atof (fields[4*i + 2]);
349 /* Convert farenheit to celsius */
351 temperature = (temperature - 32.0) * 5.0 / 9.0;
353 if ((submit_name = hddtemp_get_name (name)) != NULL)
355 hddtemp_submit (submit_name, temperature);
360 hddtemp_submit (name, temperature);
366 Register collectd plugin. */
367 void module_register (void)
369 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
372 #endif /* COLLECT_HDDTEMP */