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