1 /**
2 * collectd - src/hddtemp.c
3 * Copyright (C) 2005 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 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
29 #define MODULE_NAME "hddtemp"
31 #include <netdb.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35 #include <libgen.h> /* for basename */
37 #define HDDTEMP_DEF_HOST "127.0.0.1"
38 #define HDDTEMP_DEF_PORT "7634"
40 /* BUFFER_SIZE
41 Size of the buffer we use to receive from the hddtemp daemon. */
42 #define BUFFER_SIZE 1024
44 static char *filename_format = "hddtemp-%s.rrd";
46 static char *ds_def[] =
47 {
48 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
49 NULL
50 };
51 static int ds_num = 1;
53 static char *config_keys[] =
54 {
55 "Host",
56 "Port",
57 NULL
58 };
59 static int config_keys_num = 2;
61 typedef struct hddname
62 {
63 int major;
64 int minor;
65 char *name;
66 struct hddname *next;
67 } hddname_t;
69 static hddname_t *first_hddname = NULL;
70 static char *hddtemp_host = NULL;
71 static char *hddtemp_port = NULL;
73 /*
74 * NAME
75 * hddtemp_query_daemon
76 *
77 * DESCRIPTION
78 * Connect to the hddtemp daemon and receive data.
79 *
80 * ARGUMENTS:
81 * `buffer' The buffer where we put the received ascii string.
82 * `buffer_size' Size of the buffer
83 *
84 * RETURN VALUE:
85 * >= 0 if ok, < 0 otherwise.
86 *
87 * NOTES:
88 * Example of possible strings, as received from daemon:
89 * |/dev/hda|ST340014A|36|C|
90 * |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
91 *
92 * FIXME:
93 * we need to create a new socket each time. Is there another way?
94 * Hm, maybe we can re-use the `sockaddr' structure? -octo
95 */
96 static int hddtemp_query_daemon (char *buffer, int buffer_size)
97 {
98 int fd;
99 ssize_t status;
100 int buffer_fill;
102 char *host;
103 char *port;
105 struct addrinfo ai_hints;
106 struct addrinfo *ai_list, *ai_ptr;
107 int ai_return;
109 memset (&ai_hints, '\0', sizeof (ai_hints));
110 ai_hints.ai_flags = AI_ADDRCONFIG;
111 ai_hints.ai_family = PF_UNSPEC;
112 ai_hints.ai_socktype = SOCK_STREAM;
113 ai_hints.ai_protocol = IPPROTO_TCP;
115 host = hddtemp_host;
116 if (host == NULL)
117 host = HDDTEMP_DEF_HOST;
119 port = hddtemp_port;
120 if (port == NULL)
121 port = HDDTEMP_DEF_PORT;
123 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
124 {
125 syslog (LOG_ERR, "hddtemp: getaddrinfo (%s, %s): %s",
126 host, port,
127 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
128 return (-1);
129 }
131 fd = -1;
132 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
133 {
134 /* create our socket descriptor */
135 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
136 {
137 syslog (LOG_ERR, "hddtemp: socket: %s",
138 strerror (errno));
139 continue;
140 }
142 /* connect to the hddtemp daemon */
143 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
144 {
145 syslog (LOG_ERR, "hddtemp: connect (%s, %s): %s",
146 host, port, strerror (errno));
147 close (fd);
148 fd = -1;
149 continue;
150 }
151 }
153 freeaddrinfo (ai_list);
155 if (fd < 0)
156 return (-1);
158 /* receive data from the hddtemp daemon */
159 memset (buffer, '\0', buffer_size);
161 buffer_fill = 0;
162 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
163 {
164 if (status == -1)
165 {
166 if ((errno == EAGAIN) || (errno == EINTR))
167 continue;
169 syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
170 strerror (errno));
171 return (-1);
172 }
173 buffer_fill += status;
175 if (buffer_fill >= buffer_size)
176 break;
177 }
179 if (buffer_fill >= buffer_size)
180 {
181 buffer[buffer_size - 1] = '\0';
182 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
183 }
184 else if (buffer_fill == 0)
185 {
186 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
187 "Buffer: `%s'", buffer);
188 close (fd);
189 return (-1);
190 }
192 close (fd);
193 return (0);
194 }
196 static int hddtemp_config (char *key, char *value)
197 {
198 if (strcasecmp (key, "host") == 0)
199 {
200 if (hddtemp_host != NULL)
201 free (hddtemp_host);
202 hddtemp_host = strdup (value);
203 }
204 else if (strcasecmp (key, "port") == 0)
205 {
206 if (hddtemp_port != NULL)
207 free (hddtemp_port);
208 hddtemp_port = strdup (value);
209 }
210 else
211 {
212 return (-1);
213 }
215 return (0);
216 }
218 static void hddtemp_init (void)
219 {
220 FILE *fh;
221 char buf[BUFFER_SIZE];
222 int buflen;
224 char *fields[16];
225 int num_fields;
227 int major;
228 int minor;
229 char *name;
230 hddname_t *next;
231 hddname_t *entry;
233 next = first_hddname;
234 while (next != NULL)
235 {
236 entry = next;
237 next = entry->next;
239 free (entry->name);
240 free (entry);
241 }
242 first_hddname = NULL;
244 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
245 {
246 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
247 {
248 /* Delete trailing newlines */
249 buflen = strlen (buf);
250 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
251 buf[--buflen] = '\0';
252 if (buflen == 0)
253 continue;
255 num_fields = strsplit (buf, fields, 16);
257 if (num_fields != 4)
258 continue;
260 major = atoi (fields[0]);
261 minor = atoi (fields[1]);
263 /* I know that this makes `minor' redundant, but I want
264 * to be able to change this beavior in the future..
265 * And 4 or 8 bytes won't hurt anybody.. -octo */
266 if (major == 0)
267 continue;
268 if (minor != 0)
269 continue;
271 if ((name = strdup (fields[3])) == NULL)
272 continue;
274 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
275 {
276 free (name);
277 continue;
278 }
280 entry->major = major;
281 entry->minor = minor;
282 entry->name = name;
283 entry->next = NULL;
285 if (first_hddname == NULL)
286 {
287 first_hddname = entry;
288 }
289 else
290 {
291 entry->next = first_hddname;
292 first_hddname = entry;
293 }
294 }
295 }
297 return;
298 }
300 static void hddtemp_write (char *host, char *inst, char *val)
301 {
302 char filename[BUFFER_SIZE];
303 int status;
305 /* construct filename */
306 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
307 if (status < 1)
308 return;
309 else if (status >= BUFFER_SIZE)
310 return;
312 rrd_update_file (host, filename, val, ds_def, ds_num);
313 }
315 static char *hddtemp_get_name (char *drive)
316 {
317 hddname_t *list;
318 char *ret;
320 for (list = first_hddname; list != NULL; list = list->next)
321 if (strcmp (drive, list->name) == 0)
322 break;
324 if (list == NULL)
325 return (strdup (drive));
327 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
328 return (NULL);
330 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
331 {
332 free (ret);
333 return (NULL);
334 }
336 return (ret);
337 }
339 static void hddtemp_submit (char *inst, double temperature)
340 {
341 char buf[BUFFER_SIZE];
343 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
344 return;
346 plugin_submit (MODULE_NAME, inst, buf);
347 }
349 static void hddtemp_read (void)
350 {
351 char buf[BUFFER_SIZE];
352 char *fields[128];
353 char *ptr;
354 int num_fields;
355 int num_disks;
356 int i;
358 static int wait_time = 1;
359 static int wait_left = 0;
361 if (wait_left >= 10)
362 {
363 wait_left -= 10;
364 return;
365 }
367 /* get data from daemon */
368 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
369 {
370 /* This limit is reached in log2(86400) =~ 17 steps. Since
371 * there is a 2^n seconds wait between each step it will need
372 * roughly one day to reach this limit. -octo */
374 wait_time *= 2;
375 if (wait_time > 86400)
376 wait_time = 86400;
378 wait_left = wait_time;
380 return;
381 }
382 else
383 {
384 wait_time = 1;
385 wait_left = 0;
386 }
388 /* NB: strtok will eat up "||" and leading "|"'s */
389 num_fields = 0;
390 ptr = buf;
391 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
392 {
393 ptr = NULL;
394 num_fields++;
396 if (num_fields >= 128)
397 break;
398 }
400 num_disks = num_fields / 4;
402 for (i = 0; i < num_disks; i++)
403 {
404 char *name, *submit_name;
405 double temperature;
406 char *mode;
408 mode = fields[4*i + 3];
410 /* Skip non-temperature information */
411 if (mode[0] != 'C' && mode[0] != 'F')
412 continue;
414 name = basename (fields[4*i + 0]);
415 temperature = atof (fields[4*i + 2]);
417 /* Convert farenheit to celsius */
418 if (mode[0] == 'F')
419 temperature = (temperature - 32.0) * 5.0 / 9.0;
421 if ((submit_name = hddtemp_get_name (name)) != NULL)
422 {
423 hddtemp_submit (submit_name, temperature);
424 free (submit_name);
425 }
426 else
427 {
428 hddtemp_submit (name, temperature);
429 }
430 }
431 }
433 /* module_register
434 Register collectd plugin. */
435 void module_register (void)
436 {
437 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
438 cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);
439 }
441 #undef MODULE_NAME