2 * collectd - src/hddtemp.c
3 * Copyright (C) 2005,2006 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>
24 * Do a pass, some day, and spare some memory. We consume too much for now
25 * in string buffers and the like.
32 #include "configfile.h"
33 #include "utils_debug.h"
35 #if HAVE_NETDB_H && HAVE_SYS_SOCKET_H && HAVE_NETINET_IN_H \
36 && HAVE_NETINET_TCP_H && HAVE_LIBGEN_H
38 # include <sys/socket.h>
39 # include <netinet/in.h>
40 # include <netinet/tcp.h>
41 # include <libgen.h> /* for basename */
42 # define HDDTEMP_HAVE_READ 1
44 # define HDDTEMP_HAVE_READ 0
47 #if HAVE_LINUX_MAJOR_H
48 # include <linux/major.h>
51 #define HDDTEMP_DEF_HOST "127.0.0.1"
52 #define HDDTEMP_DEF_PORT "7634"
54 static data_source_t data_source_temperature[1] =
56 {"value", DS_TYPE_GAUGE, -273.15, NAN}
59 static data_set_t temperature_ds =
61 "temperature", 1, data_source_temperature
65 static const char *config_keys[] =
71 static int config_keys_num = 2;
73 typedef struct hddname
81 static hddname_t *first_hddname = NULL;
82 static char *hddtemp_host = NULL;
83 static char *hddtemp_port = NULL;
87 * hddtemp_query_daemon
90 * Connect to the hddtemp daemon and receive data.
93 * `buffer' The buffer where we put the received ascii string.
94 * `buffer_size' Size of the buffer
97 * >= 0 if ok, < 0 otherwise.
100 * Example of possible strings, as received from daemon:
101 * |/dev/hda|ST340014A|36|C|
102 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
105 * we need to create a new socket each time. Is there another way?
106 * Hm, maybe we can re-use the `sockaddr' structure? -octo
108 static int hddtemp_query_daemon (char *buffer, int buffer_size)
117 struct addrinfo ai_hints;
118 struct addrinfo *ai_list, *ai_ptr;
121 memset (&ai_hints, '\0', sizeof (ai_hints));
122 ai_hints.ai_flags = AI_ADDRCONFIG;
123 ai_hints.ai_family = PF_UNSPEC;
124 ai_hints.ai_socktype = SOCK_STREAM;
125 ai_hints.ai_protocol = IPPROTO_TCP;
129 host = HDDTEMP_DEF_HOST;
133 port = HDDTEMP_DEF_PORT;
135 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
137 syslog (LOG_ERR, "hddtemp: getaddrinfo (%s, %s): %s",
139 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
144 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
146 /* create our socket descriptor */
147 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
149 syslog (LOG_ERR, "hddtemp: socket: %s",
154 /* connect to the hddtemp daemon */
155 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
157 DBG ("hddtemp: connect (%s, %s): %s", host, port,
164 /* A socket could be opened and connecting succeeded. We're
169 freeaddrinfo (ai_list);
173 syslog (LOG_ERR, "hddtemp: Could not connect to daemon.");
177 /* receive data from the hddtemp daemon */
178 memset (buffer, '\0', buffer_size);
181 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
185 if ((errno == EAGAIN) || (errno == EINTR))
188 syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
193 buffer_fill += status;
195 if (buffer_fill >= buffer_size)
199 if (buffer_fill >= buffer_size)
201 buffer[buffer_size - 1] = '\0';
202 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
204 else if (buffer_fill == 0)
206 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
207 "Buffer: `%s'", buffer);
216 static int hddtemp_config (const char *key, const char *value)
218 if (strcasecmp (key, "host") == 0)
220 if (hddtemp_host != NULL)
222 hddtemp_host = strdup (value);
224 else if (strcasecmp (key, "port") == 0)
226 if (hddtemp_port != NULL)
228 hddtemp_port = strdup (value);
238 /* In the init-function we initialize the `hddname_t' list used to translate
239 * disk-names. Under Linux that's done using `/proc/partitions'. Under other
240 * operating-systems, it's not done at all. */
241 static int hddtemp_init (void)
257 next = first_hddname;
266 first_hddname = NULL;
268 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
270 DBG ("Looking at /proc/partitions...");
272 while (fgets (buf, sizeof (buf), fh) != NULL)
274 /* Delete trailing newlines */
275 buflen = strlen (buf);
277 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
278 buf[--buflen] = '\0';
280 /* We want lines of the form:
284 * ...so, skip everything else. */
288 num_fields = strsplit (buf, fields, 16);
293 major = atoi (fields[0]);
294 minor = atoi (fields[1]);
296 /* We try to keep only entries, which may correspond to
297 * physical disks and that may have a corresponding
298 * entry in the hddtemp daemon. Basically, this means
303 case SCSI_DISK0_MAJOR:
304 case SCSI_DISK1_MAJOR:
305 case SCSI_DISK2_MAJOR:
306 case SCSI_DISK3_MAJOR:
307 case SCSI_DISK4_MAJOR:
308 case SCSI_DISK5_MAJOR:
309 case SCSI_DISK6_MAJOR:
310 case SCSI_DISK7_MAJOR:
311 case SCSI_DISK8_MAJOR:
312 case SCSI_DISK9_MAJOR:
313 case SCSI_DISK10_MAJOR:
314 case SCSI_DISK11_MAJOR:
315 case SCSI_DISK12_MAJOR:
316 case SCSI_DISK13_MAJOR:
317 case SCSI_DISK14_MAJOR:
318 case SCSI_DISK15_MAJOR:
319 /* SCSI disks minors are multiples of 16.
320 * Keep only those. */
336 /* IDE disks minors can only be 0 or 64.
337 * Keep only those. */
338 if(minor != 0 && minor != 64)
342 /* Skip all other majors. */
344 DBG ("Skipping unknown major %i", major);
346 } /* switch (major) */
348 if ((name = strdup (fields[3])) == NULL)
350 syslog (LOG_ERR, "hddtemp: strdup(%s) == NULL", fields[3]);
354 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
356 syslog (LOG_ERR, "hddtemp: malloc (%u) == NULL",
357 (unsigned int) sizeof (hddname_t));
362 DBG ("Found disk: %s (%u:%u).", name, major, minor);
364 entry->major = major;
365 entry->minor = minor;
369 if (first_hddname == NULL)
371 first_hddname = entry;
375 entry->next = first_hddname;
376 first_hddname = entry;
382 DBG ("Could not open /proc/partitions: %s",
384 #endif /* KERNEL_LINUX */
387 } /* int hddtemp_init */
393 * Try to "cook" a bit the drive name as returned
394 * by the hddtemp daemon. The intend is to transform disk
395 * names into <major>-<minor> when possible.
397 static char *hddtemp_get_name (char *drive)
402 for (list = first_hddname; list != NULL; list = list->next)
403 if (strcmp (drive, list->name) == 0)
408 DBG ("Don't know %s, keeping name as-is.", drive);
409 return (strdup (drive));
412 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
415 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
424 static void hddtemp_submit (char *type_instance, double value)
427 value_list_t vl = VALUE_LIST_INIT;
429 values[0].gauge = value;
433 vl.time = time (NULL);
434 strcpy (vl.host, hostname);
435 strcpy (vl.plugin, "hddtemp");
436 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
438 plugin_dispatch_values ("temperature", &vl);
441 static int hddtemp_read (void)
450 /* get data from daemon */
451 if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
454 /* NB: strtok will eat up "||" and leading "|"'s */
457 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
462 if (num_fields >= 128)
466 num_disks = num_fields / 4;
468 for (i = 0; i < num_disks; i++)
470 char *name, *submit_name;
474 mode = fields[4*i + 3];
475 name = basename (fields[4*i + 0]);
477 /* Skip non-temperature information */
478 if (mode[0] != 'C' && mode[0] != 'F')
481 temperature = atof (fields[4*i + 2]);
483 /* Convert farenheit to celsius */
485 temperature = (temperature - 32.0) * 5.0 / 9.0;
487 if ((submit_name = hddtemp_get_name (name)) != NULL)
489 hddtemp_submit (submit_name, temperature);
494 hddtemp_submit (name, temperature);
499 } /* int hddtemp_read */
500 #endif /* HDDTEMP_HAVE_READ */
503 Register collectd plugin. */
504 void module_register (void)
506 plugin_register_data_set (&temperature_ds);
508 #if HDDTEMP_HAVE_READ
509 plugin_register_config ("hddtemp", hddtemp_config,
510 config_keys, config_keys_num);
511 plugin_register_init ("hddtemp", hddtemp_init);
512 plugin_register_read ("hddtemp", hddtemp_read);
513 #endif /* HDDTEMP_HAVE_READ */