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 #if HAVE_NETDB_H && HAVE_SYS_SOCKET_H && HAVE_NETINET_IN_H \
35 && HAVE_NETINET_TCP_H && HAVE_LIBGEN_H
36 # include <netdb.h>
37 # include <sys/socket.h>
38 # include <netinet/in.h>
39 # include <netinet/tcp.h>
40 # include <libgen.h> /* for basename */
41 # define HDDTEMP_HAVE_READ 1
42 #else
43 # define HDDTEMP_HAVE_READ 0
44 #endif
46 #if HAVE_LINUX_MAJOR_H
47 # include <linux/major.h>
48 #endif
50 #define HDDTEMP_DEF_HOST "127.0.0.1"
51 #define HDDTEMP_DEF_PORT "7634"
53 static data_source_t data_source_temperature[1] =
54 {
55 {"value", DS_TYPE_GAUGE, -273.15, NAN}
56 };
58 static data_set_t temperature_ds =
59 {
60 "temperature", 1, data_source_temperature
61 };
63 #if HDDTEMP_HAVE_READ
64 static const char *config_keys[] =
65 {
66 "Host",
67 "Port",
68 NULL
69 };
70 static int config_keys_num = 2;
72 typedef struct hddname
73 {
74 int major;
75 int minor;
76 char *name;
77 struct hddname *next;
78 } hddname_t;
80 static hddname_t *first_hddname = NULL;
81 static char *hddtemp_host = NULL;
82 static char *hddtemp_port = NULL;
84 /*
85 * NAME
86 * hddtemp_query_daemon
87 *
88 * DESCRIPTION
89 * Connect to the hddtemp daemon and receive data.
90 *
91 * ARGUMENTS:
92 * `buffer' The buffer where we put the received ascii string.
93 * `buffer_size' Size of the buffer
94 *
95 * RETURN VALUE:
96 * >= 0 if ok, < 0 otherwise.
97 *
98 * NOTES:
99 * Example of possible strings, as received from daemon:
100 * |/dev/hda|ST340014A|36|C|
101 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
102 *
103 * FIXME:
104 * we need to create a new socket each time. Is there another way?
105 * Hm, maybe we can re-use the `sockaddr' structure? -octo
106 */
107 static int hddtemp_query_daemon (char *buffer, int buffer_size)
108 {
109 int fd;
110 ssize_t status;
111 int buffer_fill;
113 const char *host;
114 const char *port;
116 struct addrinfo ai_hints;
117 struct addrinfo *ai_list, *ai_ptr;
118 int ai_return;
120 memset (&ai_hints, '\0', sizeof (ai_hints));
121 ai_hints.ai_flags = AI_ADDRCONFIG;
122 ai_hints.ai_family = PF_UNSPEC;
123 ai_hints.ai_socktype = SOCK_STREAM;
124 ai_hints.ai_protocol = IPPROTO_TCP;
126 host = hddtemp_host;
127 if (host == NULL)
128 host = HDDTEMP_DEF_HOST;
130 port = hddtemp_port;
131 if (port == NULL)
132 port = HDDTEMP_DEF_PORT;
134 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
135 {
136 char errbuf[1024];
137 ERROR ("hddtemp: getaddrinfo (%s, %s): %s",
138 host, port,
139 (ai_return == EAI_SYSTEM)
140 ? sstrerror (errno, errbuf, sizeof (errbuf))
141 : gai_strerror (ai_return));
142 return (-1);
143 }
145 fd = -1;
146 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
147 {
148 /* create our socket descriptor */
149 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
150 {
151 char errbuf[1024];
152 ERROR ("hddtemp: socket: %s",
153 sstrerror (errno, errbuf, sizeof (errbuf)));
154 continue;
155 }
157 /* connect to the hddtemp daemon */
158 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
159 {
160 char errbuf[1024];
161 DEBUG ("hddtemp: connect (%s, %s): %s", host, port,
162 sstrerror (errno, errbuf, sizeof (errbuf)));
163 close (fd);
164 fd = -1;
165 continue;
166 }
168 /* A socket could be opened and connecting succeeded. We're
169 * done. */
170 break;
171 }
173 freeaddrinfo (ai_list);
175 if (fd < 0)
176 {
177 ERROR ("hddtemp: Could not connect to daemon.");
178 return (-1);
179 }
181 /* receive data from the hddtemp daemon */
182 memset (buffer, '\0', buffer_size);
184 buffer_fill = 0;
185 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
186 {
187 if (status == -1)
188 {
189 char errbuf[1024];
191 if ((errno == EAGAIN) || (errno == EINTR))
192 continue;
194 ERROR ("hddtemp: Error reading from socket: %s",
195 sstrerror (errno, errbuf, sizeof (errbuf)));
196 close (fd);
197 return (-1);
198 }
199 buffer_fill += status;
201 if (buffer_fill >= buffer_size)
202 break;
203 }
205 if (buffer_fill >= buffer_size)
206 {
207 buffer[buffer_size - 1] = '\0';
208 WARNING ("hddtemp: Message from hddtemp has been truncated.");
209 }
210 else if (buffer_fill == 0)
211 {
212 WARNING ("hddtemp: Peer has unexpectedly shut down the socket. "
213 "Buffer: `%s'", buffer);
214 close (fd);
215 return (-1);
216 }
218 close (fd);
219 return (0);
220 }
222 static int hddtemp_config (const char *key, const char *value)
223 {
224 if (strcasecmp (key, "host") == 0)
225 {
226 if (hddtemp_host != NULL)
227 free (hddtemp_host);
228 hddtemp_host = strdup (value);
229 }
230 else if (strcasecmp (key, "port") == 0)
231 {
232 if (hddtemp_port != NULL)
233 free (hddtemp_port);
234 hddtemp_port = strdup (value);
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 case SCSI_DISK8_MAJOR:
318 case SCSI_DISK9_MAJOR:
319 case SCSI_DISK10_MAJOR:
320 case SCSI_DISK11_MAJOR:
321 case SCSI_DISK12_MAJOR:
322 case SCSI_DISK13_MAJOR:
323 case SCSI_DISK14_MAJOR:
324 case SCSI_DISK15_MAJOR:
325 /* SCSI disks minors are multiples of 16.
326 * Keep only those. */
327 if (minor % 16)
328 continue;
329 break;
331 /* IDE. */
332 case IDE0_MAJOR:
333 case IDE1_MAJOR:
334 case IDE2_MAJOR:
335 case IDE3_MAJOR:
336 case IDE4_MAJOR:
337 case IDE5_MAJOR:
338 case IDE6_MAJOR:
339 case IDE7_MAJOR:
340 case IDE8_MAJOR:
341 case IDE9_MAJOR:
342 /* IDE disks minors can only be 0 or 64.
343 * Keep only those. */
344 if(minor != 0 && minor != 64)
345 continue;
346 break;
348 /* Skip all other majors. */
349 default:
350 DEBUG ("Skipping unknown major %i", major);
351 continue;
352 } /* switch (major) */
354 if ((name = strdup (fields[3])) == NULL)
355 {
356 ERROR ("hddtemp: strdup(%s) == NULL", fields[3]);
357 continue;
358 }
360 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
361 {
362 ERROR ("hddtemp: malloc (%u) == NULL",
363 (unsigned int) sizeof (hddname_t));
364 free (name);
365 continue;
366 }
368 DEBUG ("Found disk: %s (%u:%u).", name, major, minor);
370 entry->major = major;
371 entry->minor = minor;
372 entry->name = name;
373 entry->next = NULL;
375 if (first_hddname == NULL)
376 {
377 first_hddname = entry;
378 }
379 else
380 {
381 entry->next = first_hddname;
382 first_hddname = entry;
383 }
384 }
385 fclose (fh);
386 }
387 #if COLLECT_DEBUG
388 else
389 {
390 char errbuf[1024];
391 DEBUG ("Could not open /proc/partitions: %s",
392 sstrerror (errno, errbuf, sizeof (errbuf)));
393 }
394 #endif /* COLLECT_DEBUG */
395 #endif /* KERNEL_LINUX */
397 return (0);
398 } /* int hddtemp_init */
400 /*
401 * hddtemp_get_name
402 *
403 * Description:
404 * Try to "cook" a bit the drive name as returned
405 * by the hddtemp daemon. The intend is to transform disk
406 * names into <major>-<minor> when possible.
407 */
408 static char *hddtemp_get_name (char *drive)
409 {
410 hddname_t *list;
411 char *ret;
413 for (list = first_hddname; list != NULL; list = list->next)
414 if (strcmp (drive, list->name) == 0)
415 break;
417 if (list == NULL)
418 {
419 DEBUG ("Don't know %s, keeping name as-is.", drive);
420 return (strdup (drive));
421 }
423 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
424 return (NULL);
426 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
427 {
428 free (ret);
429 return (NULL);
430 }
432 return (ret);
433 }
435 static void hddtemp_submit (char *type_instance, double value)
436 {
437 value_t values[1];
438 value_list_t vl = VALUE_LIST_INIT;
440 values[0].gauge = value;
442 vl.values = values;
443 vl.values_len = 1;
444 vl.time = time (NULL);
445 strcpy (vl.host, hostname_g);
446 strcpy (vl.plugin, "hddtemp");
447 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
449 plugin_dispatch_values ("temperature", &vl);
450 }
452 static int hddtemp_read (void)
453 {
454 char buf[1024];
455 char *fields[128];
456 char *ptr;
457 char *saveptr;
458 int num_fields;
459 int num_disks;
460 int i;
462 /* get data from daemon */
463 if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
464 return (-1);
466 /* NB: strtok_r will eat up "||" and leading "|"'s */
467 num_fields = 0;
468 ptr = buf;
469 saveptr = NULL;
470 while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
471 {
472 ptr = NULL;
473 num_fields++;
475 if (num_fields >= 128)
476 break;
477 }
479 num_disks = num_fields / 4;
481 for (i = 0; i < num_disks; i++)
482 {
483 char *name, *submit_name;
484 double temperature;
485 char *mode;
487 mode = fields[4*i + 3];
488 name = basename (fields[4*i + 0]);
490 /* Skip non-temperature information */
491 if (mode[0] != 'C' && mode[0] != 'F')
492 continue;
494 temperature = atof (fields[4*i + 2]);
496 /* Convert farenheit to celsius */
497 if (mode[0] == 'F')
498 temperature = (temperature - 32.0) * 5.0 / 9.0;
500 if ((submit_name = hddtemp_get_name (name)) != NULL)
501 {
502 hddtemp_submit (submit_name, temperature);
503 free (submit_name);
504 }
505 else
506 {
507 hddtemp_submit (name, temperature);
508 }
509 }
511 return (0);
512 } /* int hddtemp_read */
513 #endif /* HDDTEMP_HAVE_READ */
515 /* module_register
516 Register collectd plugin. */
517 void module_register (modreg_e load)
518 {
519 if (load & MR_DATASETS)
520 plugin_register_data_set (&temperature_ds);
522 #if HDDTEMP_HAVE_READ
523 if (load & MR_READ)
524 {
525 plugin_register_config ("hddtemp", hddtemp_config,
526 config_keys, config_keys_num);
527 plugin_register_init ("hddtemp", hddtemp_init);
528 plugin_register_read ("hddtemp", hddtemp_read);
529 }
530 #endif /* HDDTEMP_HAVE_READ */
531 }