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