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