treewide: replace memset to 0 with initializers
[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 #include "common.h"
28 #include "plugin.h"
29 #include "configfile.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_hints = { 0 };
88         struct addrinfo *ai_list, *ai_ptr;
89         int              ai_return;
90
91 #ifdef AI_ADDRCONFIG
92         ai_hints.ai_flags   |= AI_ADDRCONFIG;
93 #endif
94         ai_hints.ai_family   = PF_UNSPEC;
95         ai_hints.ai_socktype = SOCK_STREAM;
96         ai_hints.ai_protocol = IPPROTO_TCP;
97
98         host = mbmon_host;
99         if (host == NULL)
100                 host = MBMON_DEF_HOST;
101
102         port = mbmon_port;
103         if (port == NULL)
104                 port = MBMON_DEF_PORT;
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 (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         size_t l;
241
242         for (l = strlen (s) - 1; (l > 0) && isspace ((int) s[l]); l--)
243                 s[l] = '\0';
244 }
245
246 static int mbmon_read (void)
247 {
248         char buf[1024];
249         char *s, *t;
250
251         /* get data from daemon */
252         if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
253                 return (-1);
254
255         s = buf;
256         while ((t = strchr (s, ':')) != NULL)
257         {
258                 double value;
259                 char *nextc;
260
261                 const char *type;
262                 const char *inst;
263
264                 *t++ = '\0';
265                 trim_spaces (s);
266
267                 value = strtod (t, &nextc);
268                 if ((*nextc != '\n') && (*nextc != '\0'))
269                 {
270                         ERROR ("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
271                         break;
272                 }
273
274                 if (strncmp (s, "TEMP", 4) == 0)
275                 {
276                         inst = s + 4;
277                         type = "temperature";
278                 }
279                 else if (strncmp (s, "FAN", 3) == 0)
280                 {
281                         inst = s + 3;
282                         type = "fanspeed";
283                 }
284                 else if (strncmp (s, "V", 1) == 0)
285                 {
286                         inst = s + 1;
287                         type = "voltage";
288                 }
289                 else
290                 {
291                         continue;
292                 }
293
294                 mbmon_submit (type, inst, value);
295
296                 if (*nextc == '\0')
297                         break;
298
299                 s = nextc + 1;
300         }
301
302         return (0);
303 } /* void mbmon_read */
304
305 /* module_register
306    Register collectd plugin. */
307 void module_register (void)
308 {
309         plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
310         plugin_register_read ("mbmon", mbmon_read);
311 } /* void module_register */