Merge branch 'pr/1826'
[collectd.git] / src / mbmon.c
1 /**
2  * collectd - src/mbmon.c
3  * Copyright (C) 2006       Flavio Stanchina
4  * Copyright (C) 2006-2007  Florian octo Forster
5  * Based on the hddtemp plugin.
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  *   Flavio Stanchina <flavio at stanchina.net>
23  *   Florian Forster <octo at collectd.org>
24  **/
25
26 #include "collectd.h"
27
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31
32 #include <netdb.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35
36 #define MBMON_DEF_HOST "127.0.0.1"
37 #define MBMON_DEF_PORT "411" /* the default for Debian */
38
39 static const char *config_keys[] =
40 {
41         "Host",
42         "Port",
43         NULL
44 };
45 static int config_keys_num = 2;
46
47 static char *mbmon_host = NULL;
48 static char *mbmon_port = NULL;
49
50 /*
51  * NAME
52  *  mbmon_query_daemon
53  *
54  * DESCRIPTION
55  * Connect to the mbmon daemon and receive data.
56  *
57  * ARGUMENTS:
58  *  `buffer'            The buffer where we put the received ascii string.
59  *  `buffer_size'       Size of the buffer
60  *
61  * RETURN VALUE:
62  *   >= 0 if ok, < 0 otherwise.
63  *
64  * NOTES:
65  *  Example of possible strings, as received from daemon:
66  *    TEMP0 : 27.0
67  *    TEMP1 : 31.0
68  *    TEMP2 : 29.5
69  *    FAN0  : 4411
70  *    FAN1  : 4470
71  *    FAN2  : 4963
72  *    VC0   :  +1.68
73  *    VC1   :  +1.73
74  *
75  * FIXME:
76  *  we need to create a new socket each time. Is there another way?
77  *  Hm, maybe we can re-use the `sockaddr' structure? -octo
78  */
79 static int mbmon_query_daemon (char *buffer, int buffer_size)
80 {
81         int fd;
82         ssize_t status;
83         int buffer_fill;
84
85         const char *host;
86         const char *port;
87
88         struct addrinfo *ai_list;
89         int              ai_return;
90
91         host = mbmon_host;
92         if (host == NULL)
93                 host = MBMON_DEF_HOST;
94
95         port = mbmon_port;
96         if (port == NULL)
97                 port = MBMON_DEF_PORT;
98
99         struct addrinfo ai_hints = {
100                 .ai_family = AF_UNSPEC,
101                 .ai_flags = AI_ADDRCONFIG,
102                 .ai_protocol = IPPROTO_TCP,
103                 .ai_socktype = SOCK_STREAM
104         };
105
106         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
107         {
108                 char errbuf[1024];
109                 ERROR ("mbmon: getaddrinfo (%s, %s): %s",
110                                 host, port,
111                                 (ai_return == EAI_SYSTEM)
112                                 ? sstrerror (errno, errbuf, sizeof (errbuf))
113                                 : gai_strerror (ai_return));
114                 return (-1);
115         }
116
117         fd = -1;
118         for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
119         {
120                 /* create our socket descriptor */
121                 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
122                 {
123                         char errbuf[1024];
124                         ERROR ("mbmon: socket: %s",
125                                         sstrerror (errno, errbuf,
126                                                 sizeof (errbuf)));
127                         continue;
128                 }
129
130                 /* connect to the mbmon daemon */
131                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
132                 {
133                         char errbuf[1024];
134                         INFO ("mbmon: connect (%s, %s): %s", host, port,
135                                         sstrerror (errno, errbuf,
136                                                 sizeof (errbuf)));
137                         close (fd);
138                         fd = -1;
139                         continue;
140                 }
141
142                 /* A socket could be opened and connecting succeeded. We're
143                  * done. */
144                 break;
145         }
146
147         freeaddrinfo (ai_list);
148
149         if (fd < 0)
150         {
151                 ERROR ("mbmon: Could not connect to daemon.");
152                 return (-1);
153         }
154
155         /* receive data from the mbmon daemon */
156         memset (buffer, '\0', buffer_size);
157
158         buffer_fill = 0;
159         while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
160         {
161                 if (status == -1)
162                 {
163                         char errbuf[1024];
164
165                         if ((errno == EAGAIN) || (errno == EINTR))
166                                 continue;
167
168                         ERROR ("mbmon: Error reading from socket: %s",
169                                         sstrerror (errno, errbuf,
170                                                 sizeof (errbuf)));
171                         close (fd);
172                         return (-1);
173                 }
174                 buffer_fill += status;
175
176                 if (buffer_fill >= buffer_size)
177                         break;
178         }
179
180         if (buffer_fill >= buffer_size)
181         {
182                 buffer[buffer_size - 1] = '\0';
183                 WARNING ("mbmon: Message from mbmon has been truncated.");
184         }
185         else if (buffer_fill == 0)
186         {
187                 WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
188                                 "Buffer: `%s'", buffer);
189                 close (fd);
190                 return (-1);
191         }
192
193         close (fd);
194         return (0);
195 }
196
197 static int mbmon_config (const char *key, const char *value)
198 {
199         if (strcasecmp (key, "host") == 0)
200         {
201                 if (mbmon_host != NULL)
202                         free (mbmon_host);
203                 mbmon_host = strdup (value);
204         }
205         else if (strcasecmp (key, "port") == 0)
206         {
207                 if (mbmon_port != NULL)
208                         free (mbmon_port);
209                 mbmon_port = strdup (value);
210         }
211         else
212         {
213                 return (-1);
214         }
215
216         return (0);
217 }
218
219 static void mbmon_submit (const char *type, const char *type_instance,
220                 double value)
221 {
222         value_t values[1];
223         value_list_t vl = VALUE_LIST_INIT;
224
225         values[0].gauge = value;
226
227         vl.values = values;
228         vl.values_len = 1;
229         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
230         sstrncpy (vl.plugin, "mbmon", sizeof (vl.plugin));
231         sstrncpy (vl.type, type, sizeof (vl.type));
232         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
233
234         plugin_dispatch_values (&vl);
235 } /* void mbmon_submit */
236
237 /* Trim trailing whitespace from a string. */
238 static void trim_spaces (char *s)
239 {
240         for (size_t l = strlen (s) - 1; (l > 0) && isspace ((int) s[l]); l--)
241                 s[l] = '\0';
242 }
243
244 static int mbmon_read (void)
245 {
246         char buf[1024];
247         char *s, *t;
248
249         /* get data from daemon */
250         if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
251                 return (-1);
252
253         s = buf;
254         while ((t = strchr (s, ':')) != NULL)
255         {
256                 double value;
257                 char *nextc;
258
259                 const char *type;
260                 const char *inst;
261
262                 *t++ = '\0';
263                 trim_spaces (s);
264
265                 value = strtod (t, &nextc);
266                 if ((*nextc != '\n') && (*nextc != '\0'))
267                 {
268                         ERROR ("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
269                         break;
270                 }
271
272                 if (strncmp (s, "TEMP", 4) == 0)
273                 {
274                         inst = s + 4;
275                         type = "temperature";
276                 }
277                 else if (strncmp (s, "FAN", 3) == 0)
278                 {
279                         inst = s + 3;
280                         type = "fanspeed";
281                 }
282                 else if (strncmp (s, "V", 1) == 0)
283                 {
284                         inst = s + 1;
285                         type = "voltage";
286                 }
287                 else
288                 {
289                         continue;
290                 }
291
292                 mbmon_submit (type, inst, value);
293
294                 if (*nextc == '\0')
295                         break;
296
297                 s = nextc + 1;
298         }
299
300         return (0);
301 } /* void mbmon_read */
302
303 /* module_register
304    Register collectd plugin. */
305 void module_register (void)
306 {
307         plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
308         plugin_register_read ("mbmon", mbmon_read);
309 } /* void module_register */