Removed `gethostbyname' and replaced with protocol independend `getaddrinfo'
[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 char *hddtemp_port = NULL;
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 fd;
108         ssize_t status;
109         int buffer_fill;
110
111         char *host;
112         char *port;
113
114         struct addrinfo  ai_hints;
115         struct addrinfo *ai_list, *ai_ptr;
116         int              ai_return;
117
118         memset (&ai_hints, '\0', sizeof (ai_hints));
119         ai_hints.ai_flags    = AI_ADDRCONFIG;
120         ai_hints.ai_family   = PF_UNSPEC;
121         ai_hints.ai_socktype = SOCK_STREAM;
122         ai_hints.ai_protocol = IPPROTO_TCP;
123
124         host = hddtemp_host;
125         if (host == NULL)
126                 host = HDDTEMP_DEF_HOST;
127
128         port = hddtemp_port;
129         if (port == NULL)
130                 port = HDDTEMP_DEF_PORT;
131
132         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
133         {
134                 syslog (LOG_ERR, "hddtemp: getaddrinfo (%s, %s): %s",
135                                 host, port,
136                                 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
137                 return (-1);
138         }
139
140         fd = -1;
141         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
142         {
143                 /* create our socket descriptor */
144                 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
145                 {
146                         syslog (LOG_ERR, "hddtemp: socket: %s",
147                                         strerror (errno));
148                         continue;
149                 }
150
151                 /* connect to the hddtemp daemon */
152                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
153                 {
154                         syslog (LOG_ERR, "hddtemp: connect (%s, %s): %s",
155                                         host, port, strerror (errno));
156                         close (fd);
157                         fd = -1;
158                         continue;
159                 }
160         }
161
162         freeaddrinfo (ai_list);
163
164         if (fd < 0)
165                 return (-1);
166
167         /* receive data from the hddtemp daemon */
168         memset (buffer, '\0', buffer_size);
169
170         buffer_fill = 0;
171         while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
172         {
173                 if (status == -1)
174                 {
175                         if ((errno == EAGAIN) || (errno == EINTR))
176                                 continue;
177
178                         syslog (LOG_ERR, "hddtemp: Error reading from socket: %s",
179                                                 strerror (errno));
180                         return (-1);
181                 }
182                 buffer_fill += status;
183
184                 if (buffer_fill >= buffer_size)
185                         break;
186         }
187
188         if (buffer_fill >= buffer_size)
189         {
190                 buffer[buffer_size - 1] = '\0';
191                 syslog (LOG_WARNING, "hddtemp: Message from hddtemp has been truncated.");
192         }
193         else if (buffer_fill == 0)
194         {
195                 syslog (LOG_WARNING, "hddtemp: Peer has unexpectedly shut down the socket. "
196                                 "Buffer: `%s'", buffer);
197                 close (fd);
198                 return (-1);
199         }
200
201         close (fd);
202         return (0);
203 }
204
205 static int hddtemp_config (char *key, char *value)
206 {
207         if (strcasecmp (key, "host") == 0)
208         {
209                 if (hddtemp_host != NULL)
210                         free (hddtemp_host);
211                 hddtemp_host = strdup (value);
212         }
213         else if (strcasecmp (key, "port") == 0)
214         {
215                 if (hddtemp_port != NULL)
216                         free (hddtemp_port);
217                 hddtemp_port = strdup (value);
218         }
219         else
220         {
221                 return (-1);
222         }
223
224         return (0);
225 }
226
227 static void hddtemp_init (void)
228 {
229         FILE *fh;
230         char buf[BUFFER_SIZE];
231         int buflen;
232
233         char *fields[16];
234         int num_fields;
235
236         int major;
237         int minor;
238         char *name;
239         hddname_t *next;
240         hddname_t *entry;
241
242         next = first_hddname;
243         while (next != NULL)
244         {
245                 entry = next;
246                 next = entry->next;
247
248                 free (entry->name);
249                 free (entry);
250         }
251         first_hddname = NULL;
252
253         if ((fh = fopen ("/proc/partitions", "r")) != NULL)
254         {
255                 while (fgets (buf, BUFFER_SIZE, fh) != NULL)
256                 {
257                         /* Delete trailing newlines */
258                         buflen = strlen (buf);
259                         while ((buflen > 0) && ((buf[buflen-1] == '\n') || (buf[buflen-1] == '\r')))
260                                 buf[--buflen] = '\0';
261                         if (buflen == 0)
262                                 continue;
263                         
264                         num_fields = strsplit (buf, fields, 16);
265
266                         if (num_fields != 4)
267                                 continue;
268
269                         major = atoi (fields[0]);
270                         minor = atoi (fields[1]);
271
272                         /* I know that this makes `minor' redundant, but I want
273                          * to be able to change this beavior in the future..
274                          * And 4 or 8 bytes won't hurt anybody.. -octo */
275                         if (major == 0)
276                                 continue;
277                         if (minor != 0)
278                                 continue;
279
280                         if ((name = strdup (fields[3])) == NULL)
281                                 continue;
282
283                         if ((entry = (hddname_t *) malloc (sizeof (hddname_t))) == NULL)
284                         {
285                                 free (name);
286                                 continue;
287                         }
288
289                         entry->major = major;
290                         entry->minor = minor;
291                         entry->name  = name;
292                         entry->next  = NULL;
293
294                         if (first_hddname == NULL)
295                         {
296                                 first_hddname = entry;
297                         }
298                         else
299                         {
300                                 entry->next = first_hddname;
301                                 first_hddname = entry;
302                         }
303                 }
304         }
305
306         return;
307 }
308
309 static void hddtemp_write (char *host, char *inst, char *val)
310 {
311         char filename[BUFFER_SIZE];
312         int status;
313
314         /* construct filename */
315         status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
316         if (status < 1)
317                 return;
318         else if (status >= BUFFER_SIZE)
319                 return;
320
321         rrd_update_file (host, filename, val, ds_def, ds_num);
322 }
323
324 static char *hddtemp_get_name (char *drive)
325 {
326         hddname_t *list;
327         char *ret;
328
329         for (list = first_hddname; list != NULL; list = list->next)
330                 if (strcmp (drive, list->name) == 0)
331                         break;
332
333         if (list == NULL)
334                 return (strdup (drive));
335
336         if ((ret = (char *) malloc (128 * sizeof (char))) == NULL)
337                 return (NULL);
338
339         if (snprintf (ret, 128, "%i-%i", list->major, list->minor) >= 128)
340         {
341                 free (ret);
342                 return (NULL);
343         }
344
345         return (ret);
346 }
347
348 static void hddtemp_submit (char *inst, double temperature)
349 {
350         char buf[BUFFER_SIZE];
351
352         if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, temperature) >= BUFFER_SIZE)
353                 return;
354
355         plugin_submit (MODULE_NAME, inst, buf);
356 }
357
358 static void hddtemp_read (void)
359 {
360         char buf[BUFFER_SIZE];
361         char *fields[128];
362         char *ptr;
363         int num_fields;
364         int num_disks;
365         int i;
366
367         static int wait_time = 1;
368         static int wait_left = 0;
369
370         if (wait_left >= 10)
371         {
372                 wait_left -= 10;
373                 return;
374         }
375
376         /* get data from daemon */
377         if (hddtemp_query_daemon (buf, BUFFER_SIZE) < 0)
378         {
379                 /* This limit is reached in log2(86400) =~ 17 steps. Since
380                  * there is a 2^n seconds wait between each step it will need
381                  * roughly one day to reach this limit. -octo */
382                 
383                 wait_time *= 2;
384                 if (wait_time > 86400)
385                         wait_time = 86400;
386
387                 wait_left = wait_time;
388
389                 return;
390         }
391         else
392         {
393                 wait_time = 1;
394                 wait_left = 0;
395         }
396
397         /* NB: strtok will eat up "||" and leading "|"'s */
398         num_fields = 0;
399         ptr = buf;
400         while ((fields[num_fields] = strtok (ptr, "|")) != NULL)
401         {
402                 ptr = NULL;
403                 num_fields++;
404
405                 if (num_fields >= 128)
406                         break;
407         }
408
409         num_disks = num_fields / 4;
410
411         for (i = 0; i < num_disks; i++)
412         {
413                 char *name, *submit_name;
414                 double temperature;
415                 char *mode;
416
417                 mode = fields[4*i + 3];
418
419                 /* Skip non-temperature information */
420                 if (mode[0] != 'C' && mode[0] != 'F')
421                         continue;
422
423                 name = basename (fields[4*i + 0]);
424                 temperature = atof (fields[4*i + 2]);
425
426                 /* Convert farenheit to celsius */
427                 if (mode[0] == 'F')
428                         temperature = (temperature - 32.0) * 5.0 / 9.0;
429
430                 if ((submit_name = hddtemp_get_name (name)) != NULL)
431                 {
432                         hddtemp_submit (submit_name, temperature);
433                         free (submit_name);
434                 }
435                 else
436                 {
437                         hddtemp_submit (name, temperature);
438                 }
439         }
440 }
441
442 /* module_register
443    Register collectd plugin. */
444 void module_register (void)
445 {
446         plugin_register (MODULE_NAME, hddtemp_init, hddtemp_read, hddtemp_write);
447         cf_register (MODULE_NAME, hddtemp_config, config_keys, config_keys_num);
448 }
449
450 #undef MODULE_NAME