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>
28 #define MODULE_NAME "hddtemp"
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
33 #include <libgen.h> /* for basename */
36 The ip address 127.0.0.1, as a 32 bit. */
37 #define LOCALHOST_ADDR 0x7F000001
40 The tcp port the hddtemp daemon is listening on. */
41 #define HDDTEMP_PORT 7634
44 Size of the buffer we use to receive from the hddtemp daemon. */
45 #define BUFFER_SIZE 1024
47 static char *filename_format = "hddtemp-%s.rrd";
49 static char *ds_def[] =
51 "DS:value:GAUGE:25:U:U",
54 static int ds_num = 1;
56 typedef struct hddname
64 static hddname_t *first_hddname = NULL;
68 * hddtemp_query_daemon
71 * Connect to the hddtemp daemon and receive data.
74 * `buffer' The buffer where we put the received ascii string.
75 * `buffer_size' Size of the buffer
78 * >= 0 if ok, < 0 otherwise.
81 * Example of possible strings, as received from daemon:
82 * |/dev/hda|ST340014A|36|C|
83 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
86 * we need to create a new socket each time. Is there another way?
88 static int hddtemp_query_daemon (char *buffer, int buffer_size)
92 const struct sockaddr_in addr =
94 AF_INET, /* sin_family */
95 htons(HDDTEMP_PORT), /* sin_port */
97 htonl(LOCALHOST_ADDR), /* s_addr */
101 /* create our socket descriptor */
102 if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
104 syslog (LOG_ERR, "hddtemp: could not create socket: %s", strerror (errno));
108 /* connect to the hddtemp daemon */
109 if (connect (sock, (const struct sockaddr *) &addr, sizeof (addr)))
111 syslog (LOG_ERR, "hddtemp: Could not connect to the hddtemp daemon: %s", strerror (errno));
116 /* receive data from the hddtemp daemon */
117 memset (buffer, '\0', buffer_size);
118 size = recv (sock, buffer, buffer_size, 0);
120 if (size >= buffer_size)
122 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
126 /* FIXME: Since the server closes the connection this returns zero. At
127 * least my machine does. -octo */
131 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. Buffer: `%s'", buffer);
138 syslog (LOG_ERR, "hddtemp: Could not receive from the hddtemp daemon: %s", strerror (errno));
147 static void hddtemp_init (void)
150 char buf[BUFFER_SIZE];
162 next = first_hddname;
171 first_hddname = NULL;
173 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
175 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
177 /* Delete trailing newlines */
178 buflen = strlen (buf);
179 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
180 buf[--buflen] = '\0';
184 num_fields = strsplit (buf, fields, 16);
189 major = atoi (fields[0]);
190 minor = atoi (fields[1]);
192 /* I know that this makes `minor' redundant, but I want
193 * to be able to change this beavior in the future..
194 * And 4 or 8 bytes won't hurt anybody.. -octo */
200 if ((name = strdup (fields[3])) == NULL)
203 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
209 entry->major = major;
210 entry->minor = minor;
214 if (first_hddname == NULL)
216 first_hddname = entry;
220 entry->next = first_hddname;
221 first_hddname = entry;
229 static void hddtemp_write (char *host, char *inst, char *val)
231 char filename[BUFFER_SIZE];
234 /* construct filename */
235 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
238 else if (status >= BUFFER_SIZE)
241 rrd_update_file (host, filename, val, ds_def, ds_num);
244 static char *hddtemp_get_name (char *drive)
249 for (list = first_hddname; list != NULL; list = list->next)
250 if (strcmp (drive, list->name) == 0)
254 return (strdup (drive));
256 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
259 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
268 static void hddtemp_submit (char *inst, double temperature)
270 char buf[BUFFER_SIZE];
272 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
275 plugin_submit (MODULE_NAME, inst, buf);
278 static void hddtemp_read (void)
280 char buf[BUFFER_SIZE];
287 static int wait_time = 1;
288 static int wait_left = 0;
296 /* get data from daemon */
297 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
299 /* This limit is reached in log2(86400) =~ 17 steps. Since
300 * there is a 2^n seconds wait between each step it will need
301 * roughly one day to reach this limit. -octo */
304 if (wait_time > 86400)
307 wait_left = wait_time;
317 /* NB: strtok will eat up "||" and leading "|"'s */
320 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
325 if (num_fields >= 128)
329 num_disks = num_fields / 4;
331 for (i = 0; i < num_disks; i++)
333 char *name, *submit_name;
337 mode = fields[4*i + 3];
339 /* Skip non-temperature information */
340 if (mode[0] != 'C' && mode[0] != 'F')
343 name = basename (fields[4*i + 0]);
344 temperature = atof (fields[4*i + 2]);
346 /* Convert farenheit to celsius */
348 temperature = (temperature - 32.0) * 5.0 / 9.0;
350 if ((submit_name = hddtemp_get_name (name)) != NULL)
352 hddtemp_submit (submit_name, temperature);
357 hddtemp_submit (name, temperature);
363 Register collectd plugin. */
364 void module_register (void)
366 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);