1 /**
2 * collectd - src/hddtemp.c
3 * Copyright (C) 2005,2006 Vincent Stehlé
4 *
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.
9 *
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.
14 *
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
18 *
19 * Authors:
20 * Vincent Stehlé <vincent.stehle at free.fr>
21 * Florian octo Forster <octo at verplant.org>
22 *
23 * TODO:
24 * Do a pass, some day, and spare some memory. We consume too much for now
25 * in string buffers and the like.
26 *
27 **/
29 #include "collectd.h"
30 #include "common.h"
31 #include "plugin.h"
32 #include "configfile.h"
34 # include <netdb.h>
35 # include <sys/socket.h>
36 # include <netinet/in.h>
37 # include <netinet/tcp.h>
38 # include <libgen.h> /* for basename */
40 #if HAVE_LINUX_MAJOR_H
41 # include <linux/major.h>
42 #endif
44 #define HDDTEMP_DEF_HOST "127.0.0.1"
45 #define HDDTEMP_DEF_PORT "7634"
47 static const char *config_keys[] =
48 {
49 "Host",
50 "Port",
51 "TranslateDevicename"
52 };
53 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
55 typedef struct hddname
56 {
57 int major;
58 int minor;
59 char *name;
60 struct hddname *next;
61 } hddname_t;
63 static hddname_t *first_hddname = NULL;
64 static char *hddtemp_host = NULL;
65 static char hddtemp_port[16];
66 static int translate_devicename = 1;
68 /*
69 * NAME
70 * hddtemp_query_daemon
71 *
72 * DESCRIPTION
73 * Connect to the hddtemp daemon and receive data.
74 *
75 * ARGUMENTS:
76 * `buffer' The buffer where we put the received ascii string.
77 * `buffer_size' Size of the buffer
78 *
79 * RETURN VALUE:
80 * >= 0 if ok, < 0 otherwise.
81 *
82 * NOTES:
83 * Example of possible strings, as received from daemon:
84 * |/dev/hda|ST340014A|36|C|
85 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
86 *
87 * FIXME:
88 * we need to create a new socket each time. Is there another way?
89 * Hm, maybe we can re-use the `sockaddr' structure? -octo
90 */
91 static int hddtemp_query_daemon (char *buffer, int buffer_size)
92 {
93 int fd;
94 ssize_t status;
95 int buffer_fill;
97 const char *host;
98 const char *port;
100 struct addrinfo ai_hints;
101 struct addrinfo *ai_list, *ai_ptr;
102 int ai_return;
104 memset (&ai_hints, '\0', sizeof (ai_hints));
105 ai_hints.ai_flags = 0;
106 #ifdef AI_ADDRCONFIG
107 ai_hints.ai_flags |= AI_ADDRCONFIG;
108 #endif
109 ai_hints.ai_family = PF_UNSPEC;
110 ai_hints.ai_socktype = SOCK_STREAM;
111 ai_hints.ai_protocol = IPPROTO_TCP;
113 host = hddtemp_host;
114 if (host == NULL)
115 host = HDDTEMP_DEF_HOST;
117 port = hddtemp_port;
118 if (strlen (port) == 0)
119 port = HDDTEMP_DEF_PORT;
121 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
122 {
123 char errbuf[1024];
124 ERROR ("hddtemp: getaddrinfo (%s, %s): %s",
125 host, port,
126 (ai_return == EAI_SYSTEM)
127 ? sstrerror (errno, errbuf, sizeof (errbuf))
128 : gai_strerror (ai_return));
129 return (-1);
130 }
132 fd = -1;
133 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
134 {
135 /* create our socket descriptor */
136 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
137 {
138 char errbuf[1024];
139 ERROR ("hddtemp: socket: %s",
140 sstrerror (errno, errbuf, sizeof (errbuf)));
141 continue;
142 }
144 /* connect to the hddtemp daemon */
145 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
146 {
147 char errbuf[1024];
148 INFO ("hddtemp: connect (%s, %s): %s", host, port,
149 sstrerror (errno, errbuf, sizeof (errbuf)));
150 close (fd);
151 fd = -1;
152 continue;
153 }
155 /* A socket could be opened and connecting succeeded. We're
156 * done. */
157 break;
158 }
160 freeaddrinfo (ai_list);
162 if (fd < 0)
163 {
164 ERROR ("hddtemp: Could not connect to daemon.");
165 return (-1);
166 }
168 /* receive data from the hddtemp daemon */
169 memset (buffer, '\0', buffer_size);
171 buffer_fill = 0;
172 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
173 {
174 if (status == -1)
175 {
176 char errbuf[1024];
178 if ((errno == EAGAIN) || (errno == EINTR))
179 continue;
181 ERROR ("hddtemp: Error reading from socket: %s",
182 sstrerror (errno, errbuf, sizeof (errbuf)));
183 close (fd);
184 return (-1);
185 }
186 buffer_fill += status;
188 if (buffer_fill >= buffer_size)
189 break;
190 }
192 if (buffer_fill >= buffer_size)
193 {
194 buffer[buffer_size - 1] = '\0';
195 WARNING ("hddtemp: Message from hddtemp has been truncated.");
196 }
197 else if (buffer_fill == 0)
198 {
199 WARNING ("hddtemp: Peer has unexpectedly shut down the socket. "
200 "Buffer: `%s'", buffer);
201 close (fd);
202 return (-1);
203 }
205 close (fd);
206 return (0);
207 }
209 static int hddtemp_config (const char *key, const char *value)
210 {
211 if (strcasecmp (key, "Host") == 0)
212 {
213 if (hddtemp_host != NULL)
214 free (hddtemp_host);
215 hddtemp_host = strdup (value);
216 }
217 else if (strcasecmp (key, "Port") == 0)
218 {
219 int port = (int) (atof (value));
220 if ((port > 0) && (port <= 65535))
221 snprintf (hddtemp_port, sizeof (hddtemp_port),
222 "%i", port);
223 else
224 strncpy (hddtemp_port, value, sizeof (hddtemp_port));
225 hddtemp_port[sizeof (hddtemp_port) - 1] = '\0';
226 }
227 else if (strcasecmp (key, "TranslateDevicename") == 0)
228 {
229 if ((strcasecmp ("true", value) == 0)
230 || (strcasecmp ("yes", value) == 0)
231 || (strcasecmp ("on", value) == 0))
232 translate_devicename = 1;
233 else
234 translate_devicename = 0;
235 }
236 else
237 {
238 return (-1);
239 }
241 return (0);
242 }
244 /* In the init-function we initialize the `hddname_t' list used to translate
245 * disk-names. Under Linux that's done using `/proc/partitions'. Under other
246 * operating-systems, it's not done at all. */
247 static int hddtemp_init (void)
248 {
249 #if KERNEL_LINUX
250 FILE *fh;
251 char buf[1024];
252 int buflen;
254 char *fields[16];
255 int num_fields;
257 int major;
258 int minor;
259 char *name;
260 hddname_t *next;
261 hddname_t *entry;
263 next = first_hddname;
264 while (next != NULL)
265 {
266 entry = next;
267 next = entry->next;
269 free (entry->name);
270 free (entry);
271 }
272 first_hddname = NULL;
274 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
275 {
276 DEBUG ("Looking at /proc/partitions...");
278 while (fgets (buf, sizeof (buf), fh) != NULL)
279 {
280 /* Delete trailing newlines */
281 buflen = strlen (buf);
283 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
284 buf[--buflen] = '\0';
286 /* We want lines of the form:
287 *
288 * 3 1 77842926 hda1
289 *
290 * ...so, skip everything else. */
291 if (buflen == 0)
292 continue;
294 num_fields = strsplit (buf, fields, 16);
296 if (num_fields != 4)
297 continue;
299 major = atoi (fields[0]);
300 minor = atoi (fields[1]);
302 /* We try to keep only entries, which may correspond to
303 * physical disks and that may have a corresponding
304 * entry in the hddtemp daemon. Basically, this means
305 * IDE and SCSI. */
306 switch (major)
307 {
308 /* SCSI. */
309 case SCSI_DISK0_MAJOR:
310 case SCSI_DISK1_MAJOR:
311 case SCSI_DISK2_MAJOR:
312 case SCSI_DISK3_MAJOR:
313 case SCSI_DISK4_MAJOR:
314 case SCSI_DISK5_MAJOR:
315 case SCSI_DISK6_MAJOR:
316 case SCSI_DISK7_MAJOR:
317 #ifdef SCSI_DISK8_MAJOR
318 case SCSI_DISK8_MAJOR:
319 case SCSI_DISK9_MAJOR:
320 case SCSI_DISK10_MAJOR:
321 case SCSI_DISK11_MAJOR:
322 case SCSI_DISK12_MAJOR:
323 case SCSI_DISK13_MAJOR:
324 case SCSI_DISK14_MAJOR:
325 case SCSI_DISK15_MAJOR:
326 #endif /* SCSI_DISK8_MAJOR */
327 /* SCSI disks minors are multiples of 16.
328 * Keep only those. */
329 if (minor % 16)
330 continue;
331 break;
333 /* IDE. */
334 case IDE0_MAJOR:
335 case IDE1_MAJOR:
336 case IDE2_MAJOR:
337 case IDE3_MAJOR:
338 case IDE4_MAJOR:
339 case IDE5_MAJOR:
340 case IDE6_MAJOR:
341 case IDE7_MAJOR:
342 case IDE8_MAJOR:
343 case IDE9_MAJOR:
344 /* IDE disks minors can only be 0 or 64.
345 * Keep only those. */
346 if(minor != 0 && minor != 64)
347 continue;
348 break;
350 /* Skip all other majors. */
351 default:
352 DEBUG ("Skipping unknown major %i", major);
353 continue;
354 } /* switch (major) */
356 if ((name = strdup (fields[3])) == NULL)
357 {
358 ERROR ("hddtemp: strdup(%s) == NULL", fields[3]);
359 continue;
360 }
362 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
363 {
364 ERROR ("hddtemp: malloc (%u) == NULL",
365 (unsigned int) sizeof (hddname_t));
366 free (name);
367 continue;
368 }
370 DEBUG ("Found disk: %s (%u:%u).", name, major, minor);
372 entry->major = major;
373 entry->minor = minor;
374 entry->name = name;
375 entry->next = NULL;
377 if (first_hddname == NULL)
378 {
379 first_hddname = entry;
380 }
381 else
382 {
383 entry->next = first_hddname;
384 first_hddname = entry;
385 }
386 }
387 fclose (fh);
388 }
389 #if COLLECT_DEBUG
390 else
391 {
392 char errbuf[1024];
393 DEBUG ("Could not open /proc/partitions: %s",
394 sstrerror (errno, errbuf, sizeof (errbuf)));
395 }
396 #endif /* COLLECT_DEBUG */
397 #endif /* KERNEL_LINUX */
399 return (0);
400 } /* int hddtemp_init */
402 /*
403 * hddtemp_get_major_minor
404 *
405 * Description:
406 * Try to "cook" a bit the drive name as returned
407 * by the hddtemp daemon. The intend is to transform disk
408 * names into <major>-<minor> when possible.
409 */
410 static char *hddtemp_get_major_minor (char *drive)
411 {
412 hddname_t *list;
413 char *ret;
415 for (list = first_hddname; list != NULL; list = list->next)
416 if (strcmp (drive, list->name) == 0)
417 break;
419 if (list == NULL)
420 {
421 DEBUG ("Don't know %s, keeping name as-is.", drive);
422 return (strdup (drive));
423 }
425 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
426 return (NULL);
428 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
429 {
430 free (ret);
431 return (NULL);
432 }
434 return (ret);
435 }
437 static void hddtemp_submit (char *type_instance, double value)
438 {
439 value_t values[1];
440 value_list_t vl = VALUE_LIST_INIT;
442 values[0].gauge = value;
444 vl.values = values;
445 vl.values_len = 1;
446 vl.time = time (NULL);
447 strcpy (vl.host, hostname_g);
448 strcpy (vl.plugin, "hddtemp");
449 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
451 plugin_dispatch_values ("temperature", &vl);
452 }
454 static int hddtemp_read (void)
455 {
456 char buf[1024];
457 char *fields[128];
458 char *ptr;
459 char *saveptr;
460 int num_fields;
461 int num_disks;
462 int i;
464 /* get data from daemon */
465 if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
466 return (-1);
468 /* NB: strtok_r will eat up "||" and leading "|"'s */
469 num_fields = 0;
470 ptr = buf;
471 saveptr = NULL;
472 while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
473 {
474 ptr = NULL;
475 num_fields++;
477 if (num_fields >= 128)
478 break;
479 }
481 num_disks = num_fields / 4;
483 for (i = 0; i < num_disks; i++)
484 {
485 char *name, *major_minor;
486 double temperature;
487 char *mode;
489 mode = fields[4*i + 3];
490 name = basename (fields[4*i + 0]);
492 /* Skip non-temperature information */
493 if (mode[0] != 'C' && mode[0] != 'F')
494 continue;
496 temperature = atof (fields[4*i + 2]);
498 /* Convert farenheit to celsius */
499 if (mode[0] == 'F')
500 temperature = (temperature - 32.0) * 5.0 / 9.0;
502 if (translate_devicename
503 && (major_minor = hddtemp_get_major_minor (name)) != NULL)
504 {
505 hddtemp_submit (major_minor, temperature);
506 free (major_minor);
507 }
508 else
509 {
510 hddtemp_submit (name, temperature);
511 }
512 }
514 return (0);
515 } /* int hddtemp_read */
517 /* module_register
518 Register collectd plugin. */
519 void module_register (void)
520 {
521 plugin_register_config ("hddtemp", hddtemp_config,
522 config_keys, config_keys_num);
523 plugin_register_init ("hddtemp", hddtemp_init);
524 plugin_register_read ("hddtemp", hddtemp_read);
525 }