Fix compile time issues
[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 "plugin.h"
37 #include "utils/common/common.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;
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     ERROR("hddtemp plugin: getaddrinfo (%s, %s): %s", host, port,
112           (ai_return == EAI_SYSTEM) ? STRERRNO : gai_strerror(ai_return));
113     return NULL;
114   }
115
116   fd = -1;
117   for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
118        ai_ptr = ai_ptr->ai_next) {
119     /* create our socket descriptor */
120     fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
121     if (fd < 0) {
122       ERROR("hddtemp plugin: socket: %s", STRERRNO);
123       continue;
124     }
125
126     /* connect to the hddtemp daemon */
127     if (connect(fd, (struct sockaddr *)ai_ptr->ai_addr, ai_ptr->ai_addrlen)) {
128       INFO("hddtemp plugin: connect (%s, %s) failed: %s", host, port, STRERRNO);
129       close(fd);
130       fd = -1;
131       continue;
132     }
133
134     /* A socket could be opened and connecting succeeded. We're
135      * done. */
136     break;
137   }
138
139   freeaddrinfo(ai_list);
140
141   if (fd < 0) {
142     ERROR("hddtemp plugin: Could not connect to daemon.");
143     return NULL;
144   }
145
146   /* receive data from the hddtemp daemon */
147   buffer = NULL;
148   buffer_size = 0;
149   buffer_fill = 0;
150   while (1) {
151     if ((buffer_size == 0) || (buffer_fill >= buffer_size - 1)) {
152       if (buffer_size == 0)
153         buffer_size = 1024;
154       else
155         buffer_size *= 2;
156       if (buffer_size > HDDTEMP_MAX_RECV_BUF) {
157         WARNING("hddtemp plugin: Message from hddtemp has been "
158                 "truncated.");
159         break;
160       }
161       new_buffer = realloc(buffer, buffer_size);
162       if (new_buffer == NULL) {
163         close(fd);
164         free(buffer);
165         ERROR("hddtemp plugin: Allocation failed.");
166         return NULL;
167       }
168       buffer = new_buffer;
169     }
170     status = read(fd, buffer + buffer_fill, buffer_size - buffer_fill - 1);
171     if (status == 0) {
172       break;
173     } else if (status == -1) {
174
175       if ((errno == EAGAIN) || (errno == EINTR))
176         continue;
177
178       ERROR("hddtemp plugin: Error reading from socket: %s", STRERRNO);
179       close(fd);
180       free(buffer);
181       return NULL;
182     }
183     buffer_fill += status;
184   }
185
186   if (buffer_fill == 0) {
187     WARNING("hddtemp plugin: Peer has unexpectedly shut down "
188             "the socket. Buffer: `%s'",
189             buffer);
190     close(fd);
191     free(buffer);
192     return NULL;
193   }
194
195   assert(buffer_fill < buffer_size);
196   buffer[buffer_fill] = '\0';
197   close(fd);
198   return buffer;
199 }
200
201 static int hddtemp_config(const char *key, const char *value) {
202   if (strcasecmp(key, "Host") == 0) {
203     if (hddtemp_host != NULL)
204       free(hddtemp_host);
205     hddtemp_host = strdup(value);
206   } else if (strcasecmp(key, "Port") == 0) {
207     int port = (int)(atof(value));
208     if ((port > 0) && (port <= 65535))
209       snprintf(hddtemp_port, sizeof(hddtemp_port), "%i", port);
210     else
211       sstrncpy(hddtemp_port, value, sizeof(hddtemp_port));
212   } else {
213     return -1;
214   }
215
216   return 0;
217 }
218
219 static void hddtemp_submit(char *type_instance, double value) {
220   value_list_t vl = VALUE_LIST_INIT;
221
222   vl.values = &(value_t){.gauge = value};
223   vl.values_len = 1;
224   sstrncpy(vl.plugin, "hddtemp", sizeof(vl.plugin));
225   sstrncpy(vl.type, "temperature", sizeof(vl.type));
226   sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
227
228   plugin_dispatch_values(&vl);
229 }
230
231 static int hddtemp_read(void) {
232   char *buf;
233   char *ptr;
234   char *saveptr;
235   char *name;
236   char *model;
237   char *temperature;
238   char *mode;
239
240   /* get data from daemon */
241   buf = hddtemp_query_daemon();
242   if (buf == NULL)
243     return -1;
244
245   /* NB: strtok_r will eat up "||" and leading "|"'s */
246   ptr = buf;
247   saveptr = NULL;
248   while ((name = strtok_r(ptr, "|", &saveptr)) != NULL &&
249          (model = strtok_r(NULL, "|", &saveptr)) != NULL &&
250          (temperature = strtok_r(NULL, "|", &saveptr)) != NULL &&
251          (mode = strtok_r(NULL, "|", &saveptr)) != NULL) {
252     double temperature_value;
253
254     ptr = NULL;
255
256     /* Skip non-temperature information */
257     if (mode[0] != 'C' && mode[0] != 'F')
258       continue;
259
260     name = basename(name);
261     temperature_value = atof(temperature);
262
263     /* Convert farenheit to celsius */
264     if (mode[0] == 'F')
265       temperature_value = (temperature_value - 32.0) * 5.0 / 9.0;
266
267     hddtemp_submit(name, temperature_value);
268   }
269
270   free(buf);
271   return 0;
272 } /* int hddtemp_read */
273
274 /* module_register
275    Register collectd plugin. */
276 void module_register(void) {
277   plugin_register_config("hddtemp", hddtemp_config, config_keys,
278                          config_keys_num);
279   plugin_register_read("hddtemp", hddtemp_read);
280 }