2 * collectd - src/hddtemp.c
3 * Copyright (C) 2005,2006 Vincent Stehlé
4 * Copyright (C) 2006,2007 Florian octo Forster
5 * Copyright (C) 2008 Sebastian Harl
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Vincent Stehlé <vincent.stehle at free.fr>
23 * Florian octo Forster <octo at verplant.org>
24 * Sebastian Harl <sh at tokkee.org>
27 * Do a pass, some day, and spare some memory. We consume too much for now
28 * in string buffers and the like.
35 #include "configfile.h"
38 # include <sys/socket.h>
39 # include <netinet/in.h>
40 # include <netinet/tcp.h>
41 # include <libgen.h> /* for basename */
43 #if HAVE_LINUX_MAJOR_H
44 # include <linux/major.h>
47 #define HDDTEMP_DEF_HOST "127.0.0.1"
48 #define HDDTEMP_DEF_PORT "7634"
50 static const char *config_keys[] =
56 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
58 typedef struct hddname
66 static hddname_t *first_hddname = NULL;
67 static char *hddtemp_host = NULL;
68 static char hddtemp_port[16];
69 static int translate_devicename = 1;
73 * hddtemp_query_daemon
76 * Connect to the hddtemp daemon and receive data.
79 * `buffer' The buffer where we put the received ascii string.
80 * `buffer_size' Size of the buffer
83 * >= 0 if ok, < 0 otherwise.
86 * Example of possible strings, as received from daemon:
87 * |/dev/hda|ST340014A|36|C|
88 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
91 * we need to create a new socket each time. Is there another way?
92 * Hm, maybe we can re-use the `sockaddr' structure? -octo
94 static int hddtemp_query_daemon (char *buffer, int buffer_size)
103 struct addrinfo ai_hints;
104 struct addrinfo *ai_list, *ai_ptr;
107 memset (&ai_hints, '\0', sizeof (ai_hints));
108 ai_hints.ai_flags = 0;
110 ai_hints.ai_flags |= AI_ADDRCONFIG;
112 ai_hints.ai_family = PF_UNSPEC;
113 ai_hints.ai_socktype = SOCK_STREAM;
114 ai_hints.ai_protocol = IPPROTO_TCP;
118 host = HDDTEMP_DEF_HOST;
121 if (strlen (port) == 0)
122 port = HDDTEMP_DEF_PORT;
124 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
127 ERROR ("hddtemp plugin: getaddrinfo (%s, %s): %s",
129 (ai_return == EAI_SYSTEM)
130 ? sstrerror (errno, errbuf, sizeof (errbuf))
131 : gai_strerror (ai_return));
136 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
138 /* create our socket descriptor */
139 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
140 ai_ptr->ai_protocol);
144 ERROR ("hddtemp plugin: socket: %s",
145 sstrerror (errno, errbuf, sizeof (errbuf)));
149 /* connect to the hddtemp daemon */
150 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr,
154 INFO ("hddtemp plugin: connect (%s, %s) failed: %s",
156 sstrerror (errno, errbuf, sizeof (errbuf)));
162 /* A socket could be opened and connecting succeeded. We're
167 freeaddrinfo (ai_list);
171 ERROR ("hddtemp plugin: Could not connect to daemon.");
175 /* receive data from the hddtemp daemon */
176 memset (buffer, '\0', buffer_size);
179 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
185 if ((errno == EAGAIN) || (errno == EINTR))
188 ERROR ("hddtemp plugin: Error reading from socket: %s",
189 sstrerror (errno, errbuf, sizeof (errbuf)));
193 buffer_fill += status;
195 if (buffer_fill >= buffer_size)
199 if (buffer_fill >= buffer_size)
201 buffer[buffer_size - 1] = '\0';
202 WARNING ("hddtemp plugin: Message from hddtemp has been "
205 else if (buffer_fill == 0)
207 WARNING ("hddtemp plugin: Peer has unexpectedly shut down "
208 "the socket. Buffer: `%s'", buffer);
217 static int hddtemp_config (const char *key, const char *value)
219 if (strcasecmp (key, "Host") == 0)
221 if (hddtemp_host != NULL)
223 hddtemp_host = strdup (value);
225 else if (strcasecmp (key, "Port") == 0)
227 int port = (int) (atof (value));
228 if ((port > 0) && (port <= 65535))
229 ssnprintf (hddtemp_port, sizeof (hddtemp_port),
232 sstrncpy (hddtemp_port, value, sizeof (hddtemp_port));
234 else if (strcasecmp (key, "TranslateDevicename") == 0)
237 translate_devicename = 1;
239 translate_devicename = 0;
249 /* In the init-function we initialize the `hddname_t' list used to translate
250 * disk-names. Under Linux that's done using `/proc/partitions'. Under other
251 * operating-systems, it's not done at all. */
252 static int hddtemp_init (void)
268 next = first_hddname;
277 first_hddname = NULL;
279 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
281 DEBUG ("hddtemp plugin: Looking at /proc/partitions...");
283 while (fgets (buf, sizeof (buf), fh) != NULL)
285 /* Delete trailing newlines */
286 buflen = strlen (buf);
288 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
289 buf[--buflen] = '\0';
291 /* We want lines of the form:
295 * ...so, skip everything else. */
299 num_fields = strsplit (buf, fields, 16);
304 major = atoi (fields[0]);
305 minor = atoi (fields[1]);
307 /* We try to keep only entries, which may correspond to
308 * physical disks and that may have a corresponding
309 * entry in the hddtemp daemon. Basically, this means
314 case SCSI_DISK0_MAJOR:
315 case SCSI_DISK1_MAJOR:
316 case SCSI_DISK2_MAJOR:
317 case SCSI_DISK3_MAJOR:
318 case SCSI_DISK4_MAJOR:
319 case SCSI_DISK5_MAJOR:
320 case SCSI_DISK6_MAJOR:
321 case SCSI_DISK7_MAJOR:
322 #ifdef SCSI_DISK8_MAJOR
323 case SCSI_DISK8_MAJOR:
324 case SCSI_DISK9_MAJOR:
325 case SCSI_DISK10_MAJOR:
326 case SCSI_DISK11_MAJOR:
327 case SCSI_DISK12_MAJOR:
328 case SCSI_DISK13_MAJOR:
329 case SCSI_DISK14_MAJOR:
330 case SCSI_DISK15_MAJOR:
331 #endif /* SCSI_DISK8_MAJOR */
332 /* SCSI disks minors are multiples of 16.
333 * Keep only those. */
349 /* IDE disks minors can only be 0 or 64.
350 * Keep only those. */
351 if(minor != 0 && minor != 64)
355 /* Skip all other majors. */
357 DEBUG ("hddtemp plugin: Skipping unknown major %i", major);
359 } /* switch (major) */
361 if ((name = strdup (fields[3])) == NULL)
363 ERROR ("hddtemp plugin: strdup(%s) == NULL", fields[3]);
367 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
369 ERROR ("hddtemp plugin: malloc (%u) == NULL",
370 (unsigned int) sizeof (hddname_t));
375 DEBUG ("hddtemp plugin: Found disk: %s (%u:%u).", name, major, minor);
377 entry->major = major;
378 entry->minor = minor;
382 if (first_hddname == NULL)
384 first_hddname = entry;
388 entry->next = first_hddname;
389 first_hddname = entry;
398 DEBUG ("hddtemp plugin: Could not open /proc/partitions: %s",
399 sstrerror (errno, errbuf, sizeof (errbuf)));
401 #endif /* COLLECT_DEBUG */
402 #endif /* KERNEL_LINUX */
405 } /* int hddtemp_init */
408 * hddtemp_get_major_minor
411 * Try to "cook" a bit the drive name as returned
412 * by the hddtemp daemon. The intend is to transform disk
413 * names into <major>-<minor> when possible.
415 static char *hddtemp_get_major_minor (char *drive)
420 for (list = first_hddname; list != NULL; list = list->next)
421 if (strcmp (drive, list->name) == 0)
426 DEBUG ("hddtemp plugin: Don't know %s, keeping name as-is.", drive);
427 return (strdup (drive));
430 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
433 if (ssnprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
442 static void hddtemp_submit (char *type_instance, double value)
445 value_list_t vl = VALUE_LIST_INIT;
447 values[0].gauge = value;
451 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
452 sstrncpy (vl.plugin, "hddtemp", sizeof (vl.plugin));
453 sstrncpy (vl.type, "temperature", sizeof (vl.type));
454 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
456 plugin_dispatch_values (&vl);
459 static int hddtemp_read (void)
469 /* get data from daemon */
470 if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
473 /* NB: strtok_r will eat up "||" and leading "|"'s */
477 while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
482 if (num_fields >= 128)
486 num_disks = num_fields / 4;
488 for (i = 0; i < num_disks; i++)
490 char *name, *major_minor;
494 mode = fields[4*i + 3];
495 name = basename (fields[4*i + 0]);
497 /* Skip non-temperature information */
498 if (mode[0] != 'C' && mode[0] != 'F')
501 temperature = atof (fields[4*i + 2]);
503 /* Convert farenheit to celsius */
505 temperature = (temperature - 32.0) * 5.0 / 9.0;
507 if (translate_devicename
508 && (major_minor = hddtemp_get_major_minor (name)) != NULL)
510 hddtemp_submit (major_minor, temperature);
515 hddtemp_submit (name, temperature);
520 } /* int hddtemp_read */
523 Register collectd plugin. */
524 void module_register (void)
526 plugin_register_config ("hddtemp", hddtemp_config,
527 config_keys, config_keys_num);
528 plugin_register_init ("hddtemp", hddtemp_init);
529 plugin_register_read ("hddtemp", hddtemp_read);