Updated `configure.in'
[collectd.git] / src / hddtemp.c
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  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
28
29 #define MODULE_NAME "hddtemp"
30
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 */
36
37 #if 0
38 /* LOCALHOST_ADDR
39    The ip address 127.0.0.1, as a 32 bit. */
40 #define LOCALHOST_ADDR 0x7F000001
41
42 /* HDDTEMP_PORT
43    The tcp port the hddtemp daemon is listening on. */
44 #define HDDTEMP_PORT 7634
45 #endif
46
47 #define HDDTEMP_DEF_HOST "127.0.0.1"
48 #define HDDTEMP_DEF_PORT 7634
49
50 /* BUFFER_SIZE
51    Size of the buffer we use to receive from the hddtemp daemon. */
52 #define BUFFER_SIZE 1024
53
54 static char *filename_format = "hddtemp-%s.rrd";
55
56 static char *ds_def[] =
57 {
58         "DS:value:GAUGE:25:U:U",
59         NULL
60 };
61 static int ds_num = 1;
62
63 static char *config_keys[] =
64 {
65         "Host",
66         "Port",
67         NULL
68 };
69 static int config_keys_num = 2;
70
71 typedef struct hddname
72 {
73         int major;
74         int minor;
75         char *name;
76         struct hddname *next;
77 } hddname_t;
78
79 static hddname_t *first_hddname = NULL;
80 static char *hddtemp_host = NULL;
81 static int   hddtemp_port = 0;
82
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;
110
111         char *host;
112         int   port;
113
114         struct hostent     *srv_ent;
115         struct sockaddr_in  srv_addr;
116
117         host = hddtemp_host;
118         if (host == NULL)
119                 host = HDDTEMP_DEF_HOST;
120
121         port = hddtemp_port;
122         if (port == 0)
123                 port = HDDTEMP_DEF_PORT;
124
125         /*
126          * Resolve `host' address and set up `srv_addr'
127          */
128         memset (&srv_addr, '\0', sizeof (srv_addr));
129
130         /*
131          * FIXME
132          * use `getaddrinfo'
133          */
134         if ((srv_ent = gethostbyname (host)) == NULL)
135         {
136                 syslog (LOG_WARNING, "hddtemp: Could not resolve hostname `%s'",
137                                 host);
138                 return (-1);
139         }
140         
141         memcpy (&srv_addr.sin_addr.s_addr, srv_ent->h_addr, srv_ent->h_length);
142         srv_addr.sin_port   = htons (port);
143         srv_addr.sin_family = AF_INET;
144
145         /* create our socket descriptor */
146         if ((sock = socket (PF_INET, SOCK_STREAM, 0)) < 0)
147         {
148                 syslog (LOG_ERR, "hddtemp: could not create socket: %s",
149                                 strerror (errno));
150                 return (-1);
151         }
152
153         /* connect to the hddtemp daemon */
154         if (connect (sock, (struct sockaddr *) &srv_addr, sizeof (srv_addr)))
155         {
156                 syslog (LOG_ERR, "hddtemp: Could not connect to the hddtemp "
157                                 "daemon at %s:%i: %s",
158                                 host, port, strerror (errno));
159                 close (sock);
160                 return (-1);
161         }
162
163         /* receive data from the hddtemp daemon */
164         memset (buffer, '\0', buffer_size);
165
166         buffer_fill = 0;
167         while ((status = read (sock, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
168         {
169                 if (status == -1)
170                 {
171                         if ((errno == EAGAIN) || (errno == EINTR))
172                                 continue;
173
174                         syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
175                                                 strerror (errno));
176                         return (-1);
177                 }
178                 buffer_fill += status;
179
180                 if (buffer_fill >= buffer_size)
181                         break;
182         }
183
184         if (buffer_fill >= buffer_size)
185         {
186                 buffer[buffer_size - 1] = '\0';
187                 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
188         }
189         else if (buffer_fill == 0)
190         {
191                 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
192                                 "Buffer: `%s'", buffer);
193                 close (sock);
194                 return (-1);
195         }
196
197         close (sock);
198         return (0);
199 }
200
201 static int hddtemp_config (char *key, char *value)
202 {
203         if (strcasecmp (key, "host") == 0)
204         {
205                 if (hddtemp_host != NULL)
206                         free (hddtemp_host);
207                 hddtemp_host = strdup (value);
208         }
209         else if (strcasecmp (key, "port") == 0)
210         {
211                 hddtemp_port = atoi (value);
212         }
213         else
214         {
215                 return (-1);
216         }
217
218         return (0);
219 }
220
221 static void hddtemp_init (void)
222 {
223         FILE *fh;
224         char buf[BUFFER_SIZE];
225         int buflen;
226
227         char *fields[16];
228         int num_fields;
229
230         int major;
231         int minor;
232         char *name;
233         hddname_t *next;
234         hddname_t *entry;
235
236         next = first_hddname;
237         while (next != NULL)
238         {
239                 entry = next;
240                 next = entry->next;
241
242                 free (entry->name);
243                 free (entry);
244         }
245         first_hddname = NULL;
246
247         if ((fh = fopen ("/proc/partitions", "r")) != NULL)
248         {
249                 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
250                 {
251                         /* Delete trailing newlines */
252                         buflen = strlen (buf);
253                         while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
254                                 buf[--buflen] = '\0';
255                         if (buflen == 0)
256                                 continue;
257                         
258                         num_fields = strsplit (buf, fields, 16);
259
260                         if (num_fields != 4)
261                                 continue;
262
263                         major = atoi (fields[0]);
264                         minor = atoi (fields[1]);
265
266                         /* I know that this makes `minor' redundant, but I want
267                          * to be able to change this beavior in the future..
268                          * And 4 or 8 bytes won't hurt anybody.. -octo */
269                         if (major == 0)
270                                 continue;
271                         if (minor != 0)
272                                 continue;
273
274                         if ((name = strdup (fields[3])) == NULL)
275                                 continue;
276
277                         if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
278                         {
279                                 free (name);
280                                 continue;
281                         }
282
283                         entry->major = major;
284                         entry->minor = minor;
285                         entry->name  = name;
286                         entry->next  = NULL;
287
288                         if (first_hddname == NULL)
289                         {
290                                 first_hddname = entry;
291                         }
292                         else
293                         {
294                                 entry->next = first_hddname;
295                                 first_hddname = entry;
296                         }
297                 }
298         }
299
300         return;
301 }
302
303 static void hddtemp_write (char *host, char *inst, char *val)
304 {
305         char filename[BUFFER_SIZE];
306         int status;
307
308         /* construct filename */
309         status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
310         if (status < 1)
311                 return;
312         else if (status >= BUFFER_SIZE)
313                 return;
314
315         rrd_update_file (host, filename, val, ds_def, ds_num);
316 }
317
318 static char *hddtemp_get_name (char *drive)
319 {
320         hddname_t *list;
321         char *ret;
322
323         for (list = first_hddname; list != NULL; list = list->next)
324                 if (strcmp (drive, list->name) == 0)
325                         break;
326
327         if (list == NULL)
328                 return (strdup (drive));
329
330         if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
331                 return (NULL);
332
333         if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
334         {
335                 free (ret);
336                 return (NULL);
337         }
338
339         return (ret);
340 }
341
342 static void hddtemp_submit (char *inst, double temperature)
343 {
344         char buf[BUFFER_SIZE];
345
346         if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
347                 return;
348
349         plugin_submit (MODULE_NAME, inst, buf);
350 }
351
352 static void hddtemp_read (void)
353 {
354         char buf[BUFFER_SIZE];
355         char *fields[128];
356         char *ptr;
357         int num_fields;
358         int num_disks;
359         int i;
360
361         static int wait_time = 1;
362         static int wait_left = 0;
363
364         if (wait_left >= 10)
365         {
366                 wait_left -= 10;
367                 return;
368         }
369
370         /* get data from daemon */
371         if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
372         {
373                 /* This limit is reached in log2(86400) =~ 17 steps. Since
374                  * there is a 2^n seconds wait between each step it will need
375                  * roughly one day to reach this limit. -octo */
376                 
377                 wait_time *= 2;
378                 if (wait_time > 86400)
379                         wait_time = 86400;
380
381                 wait_left = wait_time;
382
383                 return;
384         }
385         else
386         {
387                 wait_time = 1;
388                 wait_left = 0;
389         }
390
391         /* NB: strtok will eat up "||" and leading "|"'s */
392         num_fields = 0;
393         ptr = buf;
394         while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
395         {
396                 ptr = NULL;
397                 num_fields++;
398
399                 if (num_fields >= 128)
400                         break;
401         }
402
403         num_disks = num_fields / 4;
404
405         for (i = 0; i < num_disks; i++)
406         {
407                 char *name, *submit_name;
408                 double temperature;
409                 char *mode;
410
411                 mode = fields[4*i + 3];
412
413                 /* Skip non-temperature information */
414                 if (mode[0] != 'C' && mode[0] != 'F')
415                         continue;
416
417                 name = basename (fields[4*i + 0]);
418                 temperature = atof (fields[4*i + 2]);
419
420                 /* Convert farenheit to celsius */
421                 if (mode[0] == 'F')
422                         temperature = (temperature - 32.0) * 5.0 / 9.0;
423
424                 if ((submit_name = hddtemp_get_name (name)) != NULL)
425                 {
426                         hddtemp_submit (submit_name, temperature);
427                         free (submit_name);
428                 }
429                 else
430                 {
431                         hddtemp_submit (name, temperature);
432                 }
433         }
434 }
435
436 /* module_register
437    Register collectd plugin. */
438 void module_register (void)
439 {
440         plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
441         cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);
442 }
443
444 #undef MODULE_NAME