Merge remote-tracking branch 'origin/pr/1346'
[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 #include "configfile.h"
37
38 # include <netdb.h>
39 # include <netinet/in.h>
40 # include <netinet/tcp.h>
41 # include <libgen.h> /* for basename */
42
43 #if HAVE_LINUX_MAJOR_H
44 # include <linux/major.h>
45 #endif
46
47 #define HDDTEMP_DEF_HOST "127.0.0.1"
48 #define HDDTEMP_DEF_PORT "7634"
49
50 static const char *config_keys[] =
51 {
52         "Host",
53         "Port"
54 };
55 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
56
57 static char *hddtemp_host = NULL;
58 static char hddtemp_port[16];
59
60 /*
61  * NAME
62  *  hddtemp_query_daemon
63  *
64  * DESCRIPTION
65  * Connect to the hddtemp daemon and receive data.
66  *
67  * ARGUMENTS:
68  *  `buffer'            The buffer where we put the received ascii string.
69  *  `buffer_size'       Size of the buffer
70  *
71  * RETURN VALUE:
72  *   >= 0 if ok, < 0 otherwise.
73  *
74  * NOTES:
75  *  Example of possible strings, as received from daemon:
76  *    |/dev/hda|ST340014A|36|C|
77  *    |/dev/hda|ST380011A|46|C||/dev/hdd|ST340016A|SLP|*|
78  *
79  * FIXME:
80  *  we need to create a new socket each time. Is there another way?
81  *  Hm, maybe we can re-use the `sockaddr' structure? -octo
82  */
83 static int hddtemp_query_daemon (char *buffer, int buffer_size)
84 {
85         int fd;
86         ssize_t status;
87         int buffer_fill;
88
89         const char *host;
90         const char *port;
91
92         struct addrinfo *ai_list;
93         int              ai_return;
94
95         host = hddtemp_host;
96         if (host == NULL)
97                 host = HDDTEMP_DEF_HOST;
98
99         port = hddtemp_port;
100         if (strlen (port) == 0)
101                 port = HDDTEMP_DEF_PORT;
102
103         struct addrinfo ai_hints = {
104                 .ai_flags = AI_ADDRCONFIG,
105                 .ai_family = AF_UNSPEC,
106                 .ai_protocol = IPPROTO_TCP,
107                 .ai_socktype = SOCK_STREAM
108         };
109
110         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
111         {
112                 char errbuf[1024];
113                 ERROR ("hddtemp plugin: getaddrinfo (%s, %s): %s",
114                                 host, port,
115                                 (ai_return == EAI_SYSTEM)
116                                 ? sstrerror (errno, errbuf, sizeof (errbuf))
117                                 : gai_strerror (ai_return));
118                 return (-1);
119         }
120
121         fd = -1;
122         for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
123         {
124                 /* create our socket descriptor */
125                 fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
126                                 ai_ptr->ai_protocol);
127                 if (fd < 0)
128                 {
129                         char errbuf[1024];
130                         ERROR ("hddtemp plugin: socket: %s",
131                                         sstrerror (errno, errbuf, sizeof (errbuf)));
132                         continue;
133                 }
134
135                 /* connect to the hddtemp daemon */
136                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr,
137                                         ai_ptr->ai_addrlen))
138                 {
139                         char errbuf[1024];
140                         INFO ("hddtemp plugin: connect (%s, %s) failed: %s",
141                                         host, port,
142                                         sstrerror (errno, errbuf, sizeof (errbuf)));
143                         close (fd);
144                         fd = -1;
145                         continue;
146                 }
147
148                 /* A socket could be opened and connecting succeeded. We're
149                  * done. */
150                 break;
151         }
152
153         freeaddrinfo (ai_list);
154
155         if (fd < 0)
156         {
157                 ERROR ("hddtemp plugin: Could not connect to daemon.");
158                 return (-1);
159         }
160
161         /* receive data from the hddtemp daemon */
162         memset (buffer, '\0', buffer_size);
163
164         buffer_fill = 0;
165         while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
166         {
167                 if (status == -1)
168                 {
169                         char errbuf[1024];
170
171                         if ((errno == EAGAIN) || (errno == EINTR))
172                                 continue;
173
174                         ERROR ("hddtemp plugin: Error reading from socket: %s",
175                                         sstrerror (errno, errbuf, sizeof (errbuf)));
176                         close (fd);
177                         return (-1);
178                 }
179                 buffer_fill += status;
180
181                 if (buffer_fill >= buffer_size)
182                         break;
183         }
184
185         if (buffer_fill >= buffer_size)
186         {
187                 buffer[buffer_size - 1] = '\0';
188                 WARNING ("hddtemp plugin: Message from hddtemp has been "
189                                 "truncated.");
190         }
191         else if (buffer_fill == 0)
192         {
193                 WARNING ("hddtemp plugin: Peer has unexpectedly shut down "
194                                 "the socket. Buffer: `%s'", buffer);
195                 close (fd);
196                 return (-1);
197         }
198
199         close (fd);
200         return (0);
201 }
202
203 static int hddtemp_config (const char *key, const char *value)
204 {
205         if (strcasecmp (key, "Host") == 0)
206         {
207                 if (hddtemp_host != NULL)
208                         free (hddtemp_host);
209                 hddtemp_host = strdup (value);
210         }
211         else if (strcasecmp (key, "Port") == 0)
212         {
213                 int port = (int) (atof (value));
214                 if ((port > 0) && (port <= 65535))
215                         ssnprintf (hddtemp_port, sizeof (hddtemp_port),
216                                         "%i", port);
217                 else
218                         sstrncpy (hddtemp_port, value, sizeof (hddtemp_port));
219         }
220         else
221         {
222                 return (-1);
223         }
224
225         return (0);
226 }
227
228 static void hddtemp_submit (char *type_instance, double value)
229 {
230         value_t values[1];
231         value_list_t vl = VALUE_LIST_INIT;
232
233         values[0].gauge = value;
234
235         vl.values = values;
236         vl.values_len = 1;
237         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
238         sstrncpy (vl.plugin, "hddtemp", sizeof (vl.plugin));
239         sstrncpy (vl.type, "temperature", sizeof (vl.type));
240         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
241
242         plugin_dispatch_values (&vl);
243 }
244
245 static int hddtemp_read (void)
246 {
247         char buf[1024];
248         char *fields[128];
249         char *ptr;
250         char *saveptr;
251         int num_fields;
252         int num_disks;
253
254         /* get data from daemon */
255         if (hddtemp_query_daemon (buf, sizeof (buf)) < 0)
256                 return (-1);
257
258         /* NB: strtok_r will eat up "||" and leading "|"'s */
259         num_fields = 0;
260         ptr = buf;
261         saveptr = NULL;
262         while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL)
263         {
264                 ptr = NULL;
265                 num_fields++;
266
267                 if (num_fields >= 128)
268                         break;
269         }
270
271         num_disks = num_fields / 4;
272
273         for (int i = 0; i < num_disks; i++)
274         {
275                 char *name;
276                 double temperature;
277                 char *mode;
278
279                 mode = fields[4*i + 3];
280                 name = basename (fields[4*i + 0]);
281
282                 /* Skip non-temperature information */
283                 if (mode[0] != 'C' && mode[0] != 'F')
284                         continue;
285
286                 temperature = atof (fields[4*i + 2]);
287
288                 /* Convert farenheit to celsius */
289                 if (mode[0] == 'F')
290                         temperature = (temperature - 32.0) * 5.0 / 9.0;
291
292                 hddtemp_submit (name, temperature);
293         }
294         
295         return (0);
296 } /* int hddtemp_read */
297
298 /* module_register
299    Register collectd plugin. */
300 void module_register (void)
301 {
302         plugin_register_config ("hddtemp", hddtemp_config,
303                         config_keys, config_keys_num);
304         plugin_register_read ("hddtemp", hddtemp_read);
305 }