bda41a192a895e51d7bcef600cc25c61c3bf0f49
[collectd.git] / src / hddtemp.c
1 /**
2  * collectd - src/hddtemp.c
3  * Copyright (C) 2005,2006  Vincent StehlĂ©
4  * Copyright (C) 2006-2010  Florian octo Forster
5  * Copyright (C) 2008       Sebastian Harl
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  * Authors:
22  *   Vincent StehlĂ© <vincent.stehle at free.fr>
23  *   Florian octo Forster <octo at collectd.org>
24  *   Sebastian Harl <sh at tokkee.org>
25  *
26  * TODO:
27  *   Do a pass, some day, and spare some memory. We consume too much for now
28  *   in string buffers and the like.
29  *
30  **/
31
32 #include "collectd.h"
33 #include "common.h"
34 #include "plugin.h"
35 #include "configfile.h"
36
37 # include <netdb.h>
38 # include <netinet/in.h>
39 # include <netinet/tcp.h>
40 # include <libgen.h> /* for basename */
41
42 #if HAVE_LINUX_MAJOR_H
43 # include <linux/major.h>
44 #endif
45
46 #define HDDTEMP_DEF_HOST "127.0.0.1"
47 #define HDDTEMP_DEF_PORT "7634"
48
49 static const char *config_keys[] =
50 {
51         "Host",
52         "Port"
53 };
54 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
55
56 static char *hddtemp_host = NULL;
57 static char hddtemp_port[16];
58
59 /*
60  * NAME
61  *  hddtemp_query_daemon
62  *
63  * DESCRIPTION
64  * Connect to the hddtemp daemon and receive data.
65  *
66  * ARGUMENTS:
67  *  `buffer'            The buffer where we put the received ascii string.
68  *  `buffer_size'       Size of the buffer
69  *
70  * RETURN VALUE:
71  *   >= 0 if ok, < 0 otherwise.
72  *
73  * NOTES:
74  *  Example of possible strings, as received from daemon:
75  *    |/dev/hda|ST340014A|36|C|
76  *    |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
77  *
78  * FIXME:
79  *  we need to create a new socket each time. Is there another way?
80  *  Hm, maybe we can re-use the `sockaddr' structure? -octo
81  */
82 static int hddtemp_query_daemon (char *buffer, int buffer_size)
83 {
84         int fd;
85         ssize_t status;
86         int buffer_fill;
87
88         const char *host;
89         const char *port;
90
91         struct addrinfo  ai_hints = { 0 };
92         struct addrinfo *ai_list, *ai_ptr;
93         int              ai_return;
94
95         ai_hints.ai_flags    = AI_ADDRCONFIG;
96         ai_hints.ai_family   = PF_UNSPEC;
97         ai_hints.ai_socktype = SOCK_STREAM;
98         ai_hints.ai_protocol = IPPROTO_TCP;
99
100         host = hddtemp_host;
101         if (host == NULL)
102                 host = HDDTEMP_DEF_HOST;
103
104         port = hddtemp_port;
105         if (strlen (port) == 0)
106                 port = HDDTEMP_DEF_PORT;
107
108         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
109         {
110                 char errbuf[1024];
111                 ERROR ("hddtemp plugin: getaddrinfo (%s, %s): %s",
112                                 host, port,
113                                 (ai_return == EAI_SYSTEM)
114                                 ? sstrerror (errno, errbuf, sizeof (errbuf))
115                                 : gai_strerror (ai_return));
116                 return (-1);
117         }
118
119         fd = -1;
120         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
121         {
122                 /* create our socket descriptor */
123                 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
124                                 ai_ptr->ai_protocol);
125                 if (fd < 0)
126                 {
127                         char errbuf[1024];
128                         ERROR ("hddtemp plugin: socket: %s",
129                                         sstrerror (errno, errbuf, sizeof (errbuf)));
130                         continue;
131                 }
132
133                 /* connect to the hddtemp daemon */
134                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr,
135                                         ai_ptr->ai_addrlen))
136                 {
137                         char errbuf[1024];
138                         INFO ("hddtemp plugin: connect (%s, %s) failed: %s",
139                                         host, port,
140                                         sstrerror (errno, errbuf, sizeof (errbuf)));
141                         close (fd);
142                         fd = -1;
143                         continue;
144                 }
145
146                 /* A socket could be opened and connecting succeeded. We're
147                  * done. */
148                 break;
149         }
150
151         freeaddrinfo (ai_list);
152
153         if (fd < 0)
154         {
155                 ERROR ("hddtemp plugin: Could not connect to daemon.");
156                 return (-1);
157         }
158
159         /* receive data from the hddtemp daemon */
160         memset (buffer, '\0', buffer_size);
161
162         buffer_fill = 0;
163         while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
164         {
165                 if (status == -1)
166                 {
167                         char errbuf[1024];
168
169                         if ((errno == EAGAIN) || (errno == EINTR))
170                                 continue;
171
172                         ERROR ("hddtemp plugin: Error reading from socket: %s",
173                                         sstrerror (errno, errbuf, sizeof (errbuf)));
174                         close (fd);
175                         return (-1);
176                 }
177                 buffer_fill += status;
178
179                 if (buffer_fill >= buffer_size)
180                         break;
181         }
182
183         if (buffer_fill >= buffer_size)
184         {
185                 buffer[buffer_size - 1] = '\0';
186                 WARNING ("hddtemp plugin: Message from hddtemp has been "
187                                 "truncated.");
188         }
189         else if (buffer_fill == 0)
190         {
191                 WARNING ("hddtemp plugin: Peer has unexpectedly shut down "
192                                 "the socket. Buffer: `%s'", buffer);
193                 close (fd);
194                 return (-1);
195         }
196
197         close (fd);
198         return (0);
199 }
200
201 static int hddtemp_config (const char *key, const 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                 int port = (int) (atof (value));
212                 if ((port > 0) && (port <= 65535))
213                         ssnprintf (hddtemp_port, sizeof (hddtemp_port),
214                                         "%i", port);
215                 else
216                         sstrncpy (hddtemp_port, value, sizeof (hddtemp_port));
217         }
218         else
219         {
220                 return (-1);
221         }
222
223         return (0);
224 }
225
226 static void hddtemp_submit (char *type_instance, double value)
227 {
228         value_t values[1];
229         value_list_t vl = VALUE_LIST_INIT;
230
231         values[0].gauge = value;
232
233         vl.values = values;
234         vl.values_len = 1;
235         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
236         sstrncpy (vl.plugin, "hddtemp", sizeof (vl.plugin));
237         sstrncpy (vl.type, "temperature", sizeof (vl.type));
238         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
239
240         plugin_dispatch_values (&vl);
241 }
242
243 static int hddtemp_read (void)
244 {
245         char buf[1024];
246         char *fields[128];
247         char *ptr;
248         char *saveptr;
249         int num_fields;
250         int num_disks;
251         int i;
252
253         /* get data from daemon */
254         if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
255                 return (-1);
256
257         /* NB: strtok_r will eat up "||" and leading "|"'s */
258         num_fields = 0;
259         ptr = buf;
260         saveptr = NULL;
261         while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
262         {
263                 ptr = NULL;
264                 num_fields++;
265
266                 if (num_fields >= 128)
267                         break;
268         }
269
270         num_disks = num_fields / 4;
271
272         for (i = 0; i < num_disks; i++)
273         {
274                 char *name;
275                 double temperature;
276                 char *mode;
277
278                 mode = fields[4*i + 3];
279                 name = basename (fields[4*i + 0]);
280
281                 /* Skip non-temperature information */
282                 if (mode[0] != 'C' && mode[0] != 'F')
283                         continue;
284
285                 temperature = atof (fields[4*i + 2]);
286
287                 /* Convert farenheit to celsius */
288                 if (mode[0] == 'F')
289                         temperature = (temperature - 32.0) * 5.0 / 9.0;
290
291                 hddtemp_submit (name, temperature);
292         }
293         
294         return (0);
295 } /* int hddtemp_read */
296
297 /* module_register
298    Register collectd plugin. */
299 void module_register (void)
300 {
301         plugin_register_config ("hddtemp", hddtemp_config,
302                         config_keys, config_keys_num);
303         plugin_register_read ("hddtemp", hddtemp_read);
304 }