a89765a73447443c2124804aac2d0373eb48a083
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"
33 #include "utils_debug.h"
35 #define MODULE_NAME "hddtemp"
37 #include <netdb.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>
45 #endif
47 #define HDDTEMP_DEF_HOST "127.0.0.1"
48 #define HDDTEMP_DEF_PORT "7634"
50 /* BUFFER_SIZE
51 Size of the buffer we use to receive from the hddtemp daemon. */
52 #define BUFFER_SIZE 1024
54 static char *filename_format = "hddtemp-%s.rrd";
56 static char *ds_def[] =
57 {
58 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
59 NULL
60 };
61 static int ds_num = 1;
63 static char *config_keys[] =
64 {
65 "Host",
66 "Port",
67 NULL
68 };
69 static int config_keys_num = 2;
71 typedef struct hddname
72 {
73 int major;
74 int minor;
75 char *name;
76 struct hddname *next;
77 } hddname_t;
79 static hddname_t *first_hddname = NULL;
80 static char *hddtemp_host = NULL;
81 static char *hddtemp_port = NULL;
83 /*
84 * NAME
85 * hddtemp_query_daemon
86 *
87 * DESCRIPTION
88 * Connect to the hddtemp daemon and receive data.
89 *
90 * ARGUMENTS:
91 * `buffer' The buffer where we put the received ascii string.
92 * `buffer_size' Size of the buffer
93 *
94 * RETURN VALUE:
95 * >= 0 if ok, < 0 otherwise.
96 *
97 * NOTES:
98 * Example of possible strings, as received from daemon:
99 * |/dev/hda|ST340014A|36|C|
100 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
101 *
102 * FIXME:
103 * we need to create a new socket each time. Is there another way?
104 * Hm, maybe we can re-use the `sockaddr' structure? -octo
105 */
106 static int hddtemp_query_daemon (char *buffer, int buffer_size)
107 {
108 int fd;
109 ssize_t status;
110 int buffer_fill;
112 const char *host;
113 const char *port;
115 struct addrinfo ai_hints;
116 struct addrinfo *ai_list, *ai_ptr;
117 int ai_return;
119 memset (&ai_hints, '\0', sizeof (ai_hints));
120 ai_hints.ai_flags = AI_ADDRCONFIG;
121 ai_hints.ai_family = PF_UNSPEC;
122 ai_hints.ai_socktype = SOCK_STREAM;
123 ai_hints.ai_protocol = IPPROTO_TCP;
125 host = hddtemp_host;
126 if (host == NULL)
127 host = HDDTEMP_DEF_HOST;
129 port = hddtemp_port;
130 if (port == NULL)
131 port = HDDTEMP_DEF_PORT;
133 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
134 {
135 syslog (LOG_ERR, "hddtemp: getaddrinfo (%s, %s): %s",
136 host, port,
137 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
138 return (-1);
139 }
141 fd = -1;
142 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
143 {
144 /* create our socket descriptor */
145 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
146 {
147 syslog (LOG_ERR, "hddtemp: socket: %s",
148 strerror (errno));
149 continue;
150 }
152 /* connect to the hddtemp daemon */
153 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
154 {
155 DBG ("hddtemp: connect (%s, %s): %s", host, port,
156 strerror (errno));
157 close (fd);
158 fd = -1;
159 continue;
160 }
161 }
163 freeaddrinfo (ai_list);
165 if (fd < 0)
166 {
167 syslog (LOG_ERR, "hddtemp: Could not connect to daemon.");
168 return (-1);
169 }
171 /* receive data from the hddtemp daemon */
172 memset (buffer, '\0', buffer_size);
174 buffer_fill = 0;
175 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
176 {
177 if (status == -1)
178 {
179 if ((errno == EAGAIN) || (errno == EINTR))
180 continue;
182 syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
183 strerror (errno));
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 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
196 }
197 else if (buffer_fill == 0)
198 {
199 syslog (LOG_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 (char *key, 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 if (hddtemp_port != NULL)
220 free (hddtemp_port);
221 hddtemp_port = strdup (value);
222 }
223 else
224 {
225 return (-1);
226 }
228 return (0);
229 }
231 /* In the init-function we initialize the `hddname_t' list used to translate
232 * disk-names. Under Linux that's done using `/proc/partitions'. Under other
233 * operating-systems, it's not done at all. */
234 static void hddtemp_init (void)
235 {
236 #if KERNEL_LINUX
237 FILE *fh;
238 char buf[BUFFER_SIZE];
239 int buflen;
241 char *fields[16];
242 int num_fields;
244 int major;
245 int minor;
246 char *name;
247 hddname_t *next;
248 hddname_t *entry;
250 next = first_hddname;
251 while (next != NULL)
252 {
253 entry = next;
254 next = entry->next;
256 free (entry->name);
257 free (entry);
258 }
259 first_hddname = NULL;
261 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
262 {
263 DBG ("Looking at /proc/partitions...");
265 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
266 {
267 /* Delete trailing newlines */
268 buflen = strlen (buf);
270 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
271 buf[--buflen] = '\0';
273 /* We want lines of the form:
274 *
275 * 3 1 77842926 hda1
276 *
277 * ...so, skip everything else. */
278 if (buflen == 0)
279 continue;
281 num_fields = strsplit (buf, fields, 16);
283 if (num_fields != 4)
284 continue;
286 major = atoi (fields[0]);
287 minor = atoi (fields[1]);
289 /* We try to keep only entries, which may correspond to
290 * physical disks and that may have a corresponding
291 * entry in the hddtemp daemon. Basically, this means
292 * IDE and SCSI. */
293 switch (major)
294 {
295 /* SCSI. */
296 case SCSI_DISK0_MAJOR:
297 case SCSI_DISK1_MAJOR:
298 case SCSI_DISK2_MAJOR:
299 case SCSI_DISK3_MAJOR:
300 case SCSI_DISK4_MAJOR:
301 case SCSI_DISK5_MAJOR:
302 case SCSI_DISK6_MAJOR:
303 case SCSI_DISK7_MAJOR:
304 case SCSI_DISK8_MAJOR:
305 case SCSI_DISK9_MAJOR:
306 case SCSI_DISK10_MAJOR:
307 case SCSI_DISK11_MAJOR:
308 case SCSI_DISK12_MAJOR:
309 case SCSI_DISK13_MAJOR:
310 case SCSI_DISK14_MAJOR:
311 case SCSI_DISK15_MAJOR:
312 /* SCSI disks minors are multiples of 16.
313 * Keep only those. */
314 if (minor % 16)
315 continue;
316 break;
318 /* IDE. */
319 case IDE0_MAJOR:
320 case IDE1_MAJOR:
321 case IDE2_MAJOR:
322 case IDE3_MAJOR:
323 case IDE4_MAJOR:
324 case IDE5_MAJOR:
325 case IDE6_MAJOR:
326 case IDE7_MAJOR:
327 case IDE8_MAJOR:
328 case IDE9_MAJOR:
329 /* IDE disks minors can only be 0 or 64.
330 * Keep only those. */
331 if(minor != 0 && minor != 64)
332 continue;
333 break;
335 /* Skip all other majors. */
336 default:
337 DBG ("Skipping unknown major %i", major);
338 continue;
339 } /* switch (major) */
341 if ((name = strdup (fields[3])) == NULL)
342 {
343 syslog (LOG_ERR, "hddtemp: strdup(%s) == NULL", fields[3]);
344 continue;
345 }
347 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
348 {
349 syslog (LOG_ERR, "hddtemp: malloc (%u) == NULL", sizeof (hddname_t));
350 free (name);
351 continue;
352 }
354 DBG ("Found disk: %s (%u:%u).", name, major, minor);
356 entry->major = major;
357 entry->minor = minor;
358 entry->name = name;
359 entry->next = NULL;
361 if (first_hddname == NULL)
362 {
363 first_hddname = entry;
364 }
365 else
366 {
367 entry->next = first_hddname;
368 first_hddname = entry;
369 }
370 }
371 }
372 else
373 DBG ("Could not open /proc/partitions: %s",
374 strerror (errno));
375 #endif /* KERNEL_LINUX */
376 }
378 static void hddtemp_write (char *host, char *inst, char *val)
379 {
380 char filename[BUFFER_SIZE];
381 int status;
383 /* construct filename */
384 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
385 if (status < 1)
386 return;
387 else if (status >= BUFFER_SIZE)
388 return;
390 rrd_update_file (host, filename, val, ds_def, ds_num);
391 }
393 /*
394 * hddtemp_get_name
395 *
396 * Description:
397 * Try to "cook" a bit the drive name as returned
398 * by the hddtemp daemon. The intend is to transform disk
399 * names into <major>-<minor> when possible.
400 */
401 static char *hddtemp_get_name (char *drive)
402 {
403 hddname_t *list;
404 char *ret;
406 for (list = first_hddname; list != NULL; list = list->next)
407 if (strcmp (drive, list->name) == 0)
408 break;
410 if (list == NULL)
411 {
412 DBG ("Don't know %s, keeping name as-is.", drive);
413 return (strdup (drive));
414 }
416 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
417 return (NULL);
419 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
420 {
421 free (ret);
422 return (NULL);
423 }
425 return (ret);
426 }
428 static void hddtemp_submit (char *inst, double temperature)
429 {
430 char buf[BUFFER_SIZE];
432 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature)
433 >= BUFFER_SIZE)
434 return;
436 plugin_submit (MODULE_NAME, inst, buf);
437 }
439 static void hddtemp_read (void)
440 {
441 char buf[BUFFER_SIZE];
442 char *fields[128];
443 char *ptr;
444 int num_fields;
445 int num_disks;
446 int i;
448 static int wait_time = 1;
449 static int wait_left = 0;
451 if (wait_left >= 10)
452 {
453 wait_left -= 10;
454 return;
455 }
457 /* get data from daemon */
458 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
459 {
460 /* This limit is reached in log2(86400) =~ 17 steps. Since
461 * there is a 2^n seconds wait between each step it will need
462 * roughly one day to reach this limit. -octo */
464 wait_time *= 2;
465 if (wait_time > 86400)
466 wait_time = 86400;
468 wait_left = wait_time;
470 return;
471 }
472 else
473 {
474 wait_time = 1;
475 wait_left = 0;
476 }
478 /* NB: strtok will eat up "||" and leading "|"'s */
479 num_fields = 0;
480 ptr = buf;
481 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
482 {
483 ptr = NULL;
484 num_fields++;
486 if (num_fields >= 128)
487 break;
488 }
490 num_disks = num_fields / 4;
492 for (i = 0; i < num_disks; i++)
493 {
494 char *name, *submit_name;
495 double temperature;
496 char *mode;
498 mode = fields[4*i + 3];
499 name = basename (fields[4*i + 0]);
501 /* Skip non-temperature information */
502 if (mode[0] != 'C' && mode[0] != 'F')
503 continue;
505 temperature = atof (fields[4*i + 2]);
507 /* Convert farenheit to celsius */
508 if (mode[0] == 'F')
509 temperature = (temperature - 32.0) * 5.0 / 9.0;
511 if ((submit_name = hddtemp_get_name (name)) != NULL)
512 {
513 hddtemp_submit (submit_name, temperature);
514 free (submit_name);
515 }
516 else
517 {
518 hddtemp_submit (name, temperature);
519 }
520 }
521 }
523 /* module_register
524 Register collectd plugin. */
525 void module_register (void)
526 {
527 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
528 cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);
529 }
531 #undef MODULE_NAME