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 #if 0
38 /* LOCALHOST_ADDR
39 The ip address 127.0.0.1, as a 32 bit. */
40 #define LOCALHOST_ADDR 0x7F000001
42 /* HDDTEMP_PORT
43 The tcp port the hddtemp daemon is listening on. */
44 #define HDDTEMP_PORT 7634
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:25: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 int hddtemp_port = 0;
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 */
105 static int hddtemp_query_daemon (char *buffer, int buffer_size)
106 {
107 int sock;
108 ssize_t status;
109 int buffer_fill;
111 char *host;
112 int port;
114 struct hostent *srv_ent;
115 struct sockaddr_in srv_addr;
117 host = hddtemp_host;
118 if (host == NULL)
119 host = HDDTEMP_DEF_HOST;
121 port = hddtemp_port;
122 if (port == 0)
123 port = HDDTEMP_DEF_PORT;
125 /*
126 * Resolve `host' address and set up `srv_addr'
127 */
128 memset (&srv_addr, '\0', sizeof (srv_addr));
130 if ((srv_ent = gethostbyname (host)) == NULL)
131 {
132 syslog (LOG_WARNING, "hddtemp: Could not resolve hostname `%s'",
133 host);
134 return (-1);
135 }
137 memcpy (&srv_addr.sin_addr.s_addr, srv_ent->h_addr, srv_ent->h_length);
138 srv_addr.sin_port = htons (port);
139 srv_addr.sin_family = AF_INET;
141 /* create our socket descriptor */
142 if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
143 {
144 syslog (LOG_ERR, "hddtemp: could not create socket: %s",
145 strerror (errno));
146 return (-1);
147 }
149 /* connect to the hddtemp daemon */
150 if (connect (sock, (struct sockaddr *) &srv_addr, sizeof (srv_addr)))
151 {
152 syslog (LOG_ERR, "hddtemp: Could not connect to the hddtemp "
153 "daemon at %s:%i: %s",
154 host, port, strerror (errno));
155 close (sock);
156 return (-1);
157 }
159 /* receive data from the hddtemp daemon */
160 memset (buffer, '\0', buffer_size);
162 buffer_fill = 0;
163 while ((status = read (sock, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
164 {
165 if (status == -1)
166 {
167 if ((errno == EAGAIN) || (errno == EINTR))
168 continue;
170 syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
171 strerror (errno));
172 return (-1);
173 }
174 buffer_fill += status;
176 if (buffer_fill >= buffer_size)
177 break;
178 }
180 if (buffer_fill >= buffer_size)
181 {
182 buffer[buffer_size - 1] = '\0';
183 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
184 }
185 else if (buffer_fill == 0)
186 {
187 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
188 "Buffer: `%s'", buffer);
189 close (sock);
190 return (-1);
191 }
193 close (sock);
194 return (0);
195 }
197 static int hddtemp_config (char *key, char *value)
198 {
199 if (strcasecmp (key, "host") == 0)
200 {
201 if (hddtemp_host != NULL)
202 free (hddtemp_host);
203 hddtemp_host = strdup (value);
204 }
205 else if (strcasecmp (key, "port") == 0)
206 {
207 hddtemp_port = atoi (value);
208 }
209 else
210 {
211 return (-1);
212 }
214 return (0);
215 }
217 static void hddtemp_init (void)
218 {
219 FILE *fh;
220 char buf[BUFFER_SIZE];
221 int buflen;
223 char *fields[16];
224 int num_fields;
226 int major;
227 int minor;
228 char *name;
229 hddname_t *next;
230 hddname_t *entry;
232 next = first_hddname;
233 while (next != NULL)
234 {
235 entry = next;
236 next = entry->next;
238 free (entry->name);
239 free (entry);
240 }
241 first_hddname = NULL;
243 if ((fh = fopen ("/proc/partitions", "r")) != NULL)
244 {
245 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
246 {
247 /* Delete trailing newlines */
248 buflen = strlen (buf);
249 while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
250 buf[--buflen] = '\0';
251 if (buflen == 0)
252 continue;
254 num_fields = strsplit (buf, fields, 16);
256 if (num_fields != 4)
257 continue;
259 major = atoi (fields[0]);
260 minor = atoi (fields[1]);
262 /* I know that this makes `minor' redundant, but I want
263 * to be able to change this beavior in the future..
264 * And 4 or 8 bytes won't hurt anybody.. -octo */
265 if (major == 0)
266 continue;
267 if (minor != 0)
268 continue;
270 if ((name = strdup (fields[3])) == NULL)
271 continue;
273 if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
274 {
275 free (name);
276 continue;
277 }
279 entry->major = major;
280 entry->minor = minor;
281 entry->name = name;
282 entry->next = NULL;
284 if (first_hddname == NULL)
285 {
286 first_hddname = entry;
287 }
288 else
289 {
290 entry->next = first_hddname;
291 first_hddname = entry;
292 }
293 }
294 }
296 return;
297 }
299 static void hddtemp_write (char *host, char *inst, char *val)
300 {
301 char filename[BUFFER_SIZE];
302 int status;
304 /* construct filename */
305 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
306 if (status < 1)
307 return;
308 else if (status >= BUFFER_SIZE)
309 return;
311 rrd_update_file (host, filename, val, ds_def, ds_num);
312 }
314 static char *hddtemp_get_name (char *drive)
315 {
316 hddname_t *list;
317 char *ret;
319 for (list = first_hddname; list != NULL; list = list->next)
320 if (strcmp (drive, list->name) == 0)
321 break;
323 if (list == NULL)
324 return (strdup (drive));
326 if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
327 return (NULL);
329 if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
330 {
331 free (ret);
332 return (NULL);
333 }
335 return (ret);
336 }
338 static void hddtemp_submit (char *inst, double temperature)
339 {
340 char buf[BUFFER_SIZE];
342 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
343 return;
345 plugin_submit (MODULE_NAME, inst, buf);
346 }
348 static void hddtemp_read (void)
349 {
350 char buf[BUFFER_SIZE];
351 char *fields[128];
352 char *ptr;
353 int num_fields;
354 int num_disks;
355 int i;
357 static int wait_time = 1;
358 static int wait_left = 0;
360 if (wait_left >= 10)
361 {
362 wait_left -= 10;
363 return;
364 }
366 /* get data from daemon */
367 if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
368 {
369 /* This limit is reached in log2(86400) =~ 17 steps. Since
370 * there is a 2^n seconds wait between each step it will need
371 * roughly one day to reach this limit. -octo */
373 wait_time *= 2;
374 if (wait_time > 86400)
375 wait_time = 86400;
377 wait_left = wait_time;
379 return;
380 }
381 else
382 {
383 wait_time = 1;
384 wait_left = 0;
385 }
387 /* NB: strtok will eat up "||" and leading "|"'s */
388 num_fields = 0;
389 ptr = buf;
390 while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
391 {
392 ptr = NULL;
393 num_fields++;
395 if (num_fields >= 128)
396 break;
397 }
399 num_disks = num_fields / 4;
401 for (i = 0; i < num_disks; i++)
402 {
403 char *name, *submit_name;
404 double temperature;
405 char *mode;
407 mode = fields[4*i + 3];
409 /* Skip non-temperature information */
410 if (mode[0] != 'C' && mode[0] != 'F')
411 continue;
413 name = basename (fields[4*i + 0]);
414 temperature = atof (fields[4*i + 2]);
416 /* Convert farenheit to celsius */
417 if (mode[0] == 'F')
418 temperature = (temperature - 32.0) * 5.0 / 9.0;
420 if ((submit_name = hddtemp_get_name (name)) != NULL)
421 {
422 hddtemp_submit (submit_name, temperature);
423 free (submit_name);
424 }
425 else
426 {
427 hddtemp_submit (name, temperature);
428 }
429 }
430 }
432 /* module_register
433 Register collectd plugin. */
434 void module_register (void)
435 {
436 plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
437 cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);
438 }
440 #undef MODULE_NAME