Merge branch 'pr/1918'
[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
34 #include "common.h"
35 #include "plugin.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_list;
92         int              ai_return;
93
94         host = hddtemp_host;
95         if (host == NULL)
96                 host = HDDTEMP_DEF_HOST;
97
98         port = hddtemp_port;
99         if (strlen (port) == 0)
100                 port = HDDTEMP_DEF_PORT;
101
102         struct addrinfo ai_hints = {
103                 .ai_flags = AI_ADDRCONFIG,
104                 .ai_family = AF_UNSPEC,
105                 .ai_protocol = IPPROTO_TCP,
106                 .ai_socktype = SOCK_STREAM
107         };
108
109         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
110         {
111                 char errbuf[1024];
112                 ERROR ("hddtemp plugin: getaddrinfo (%s, %s): %s",
113                                 host, port,
114                                 (ai_return == EAI_SYSTEM)
115                                 ? sstrerror (errno, errbuf, sizeof (errbuf))
116                                 : gai_strerror (ai_return));
117                 return (-1);
118         }
119
120         fd = -1;
121         for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
122         {
123                 /* create our socket descriptor */
124                 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
125                                 ai_ptr->ai_protocol);
126                 if (fd < 0)
127                 {
128                         char errbuf[1024];
129                         ERROR ("hddtemp plugin: socket: %s",
130                                         sstrerror (errno, errbuf, sizeof (errbuf)));
131                         continue;
132                 }
133
134                 /* connect to the hddtemp daemon */
135                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr,
136                                         ai_ptr->ai_addrlen))
137                 {
138                         char errbuf[1024];
139                         INFO ("hddtemp plugin: connect (%s, %s) failed: %s",
140                                         host, port,
141                                         sstrerror (errno, errbuf, sizeof (errbuf)));
142                         close (fd);
143                         fd = -1;
144                         continue;
145                 }
146
147                 /* A socket could be opened and connecting succeeded. We're
148                  * done. */
149                 break;
150         }
151
152         freeaddrinfo (ai_list);
153
154         if (fd < 0)
155         {
156                 ERROR ("hddtemp plugin: Could not connect to daemon.");
157                 return (-1);
158         }
159
160         /* receive data from the hddtemp daemon */
161         memset (buffer, '\0', buffer_size);
162
163         buffer_fill = 0;
164         while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
165         {
166                 if (status == -1)
167                 {
168                         char errbuf[1024];
169
170                         if ((errno == EAGAIN) || (errno == EINTR))
171                                 continue;
172
173                         ERROR ("hddtemp plugin: Error reading from socket: %s",
174                                         sstrerror (errno, errbuf, sizeof (errbuf)));
175                         close (fd);
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                 WARNING ("hddtemp plugin: Message from hddtemp has been "
188                                 "truncated.");
189         }
190         else if (buffer_fill == 0)
191         {
192                 WARNING ("hddtemp plugin: Peer has unexpectedly shut down "
193                                 "the socket. Buffer: `%s'", buffer);
194                 close (fd);
195                 return (-1);
196         }
197
198         close (fd);
199         return (0);
200 }
201
202 static int hddtemp_config (const char *key, const char *value)
203 {
204         if (strcasecmp (key, "Host") == 0)
205         {
206                 if (hddtemp_host != NULL)
207                         free (hddtemp_host);
208                 hddtemp_host = strdup (value);
209         }
210         else if (strcasecmp (key, "Port") == 0)
211         {
212                 int port = (int) (atof (value));
213                 if ((port > 0) && (port <= 65535))
214                         ssnprintf (hddtemp_port, sizeof (hddtemp_port),
215                                         "%i", port);
216                 else
217                         sstrncpy (hddtemp_port, value, sizeof (hddtemp_port));
218         }
219         else
220         {
221                 return (-1);
222         }
223
224         return (0);
225 }
226
227 static void hddtemp_submit (char *type_instance, double value)
228 {
229         value_list_t vl = VALUE_LIST_INIT;
230
231         vl.values = &(value_t) { .gauge = value };
232         vl.values_len = 1;
233         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
234         sstrncpy (vl.plugin, "hddtemp", sizeof (vl.plugin));
235         sstrncpy (vl.type, "temperature", sizeof (vl.type));
236         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
237
238         plugin_dispatch_values (&vl);
239 }
240
241 static int hddtemp_read (void)
242 {
243         char buf[1024];
244         char *fields[128];
245         char *ptr;
246         char *saveptr;
247         int num_fields;
248         int num_disks;
249
250         /* get data from daemon */
251         if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
252                 return (-1);
253
254         /* NB: strtok_r will eat up "||" and leading "|"'s */
255         num_fields = 0;
256         ptr = buf;
257         saveptr = NULL;
258         while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
259         {
260                 ptr = NULL;
261                 num_fields++;
262
263                 if (num_fields >= 128)
264                         break;
265         }
266
267         num_disks = num_fields / 4;
268
269         for (int i = 0; i < num_disks; i++)
270         {
271                 char *name;
272                 double temperature;
273                 char *mode;
274
275                 mode = fields[4*i + 3];
276                 name = basename (fields[4*i + 0]);
277
278                 /* Skip non-temperature information */
279                 if (mode[0] != 'C' && mode[0] != 'F')
280                         continue;
281
282                 temperature = atof (fields[4*i + 2]);
283
284                 /* Convert farenheit to celsius */
285                 if (mode[0] == 'F')
286                         temperature = (temperature - 32.0) * 5.0 / 9.0;
287
288                 hddtemp_submit (name, temperature);
289         }
290         
291         return (0);
292 } /* int hddtemp_read */
293
294 /* module_register
295    Register collectd plugin. */
296 void module_register (void)
297 {
298         plugin_register_config ("hddtemp", hddtemp_config,
299                         config_keys, config_keys_num);
300         plugin_register_read ("hddtemp", hddtemp_read);
301 }