{hddtemp,mbmon,ntpd} plugins: Only use the `AI_ADDRCONFIG' if it's defined.
[collectd.git] / src / mbmon.c
1 /**
2  * collectd - src/mbmon.c
3  * Copyright (C) 2006 Flavio Stanchina
4  * Based on the hddtemp plugin.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Flavio Stanchina <flavio at stanchina.net>
22  **/
23
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
28
29 #if HAVE_NETDB_H && HAVE_SYS_SOCKET_H && HAVE_NETINET_IN_H && HAVE_NETINET_TCP_H
30 # include <netdb.h>
31 # include <sys/socket.h>
32 # include <netinet/in.h>
33 # include <netinet/tcp.h>
34 # define MBMON_HAVE_READ 1
35 #else
36 # define MBMON_HAVE_READ 0
37 #endif
38
39 #define MBMON_DEF_HOST "127.0.0.1"
40 #define MBMON_DEF_PORT "411" /* the default for Debian */
41
42 static const char *config_keys[] =
43 {
44         "Host",
45         "Port",
46         NULL
47 };
48 static int config_keys_num = 2;
49
50 #if MBMON_HAVE_READ
51 static char *mbmon_host = NULL;
52 static char *mbmon_port = NULL;
53
54 /*
55  * NAME
56  *  mbmon_query_daemon
57  *
58  * DESCRIPTION
59  * Connect to the mbmon daemon and receive data.
60  *
61  * ARGUMENTS:
62  *  `buffer'            The buffer where we put the received ascii string.
63  *  `buffer_size'       Size of the buffer
64  *
65  * RETURN VALUE:
66  *   >= 0 if ok, < 0 otherwise.
67  *
68  * NOTES:
69  *  Example of possible strings, as received from daemon:
70  *    TEMP0 : 27.0
71  *    TEMP1 : 31.0
72  *    TEMP2 : 29.5
73  *    FAN0  : 4411
74  *    FAN1  : 4470
75  *    FAN2  : 4963
76  *    VC0   :  +1.68
77  *    VC1   :  +1.73
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 mbmon_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_hints;
93         struct addrinfo *ai_list, *ai_ptr;
94         int              ai_return;
95
96         memset (&ai_hints, '\0', sizeof (ai_hints));
97         ai_hints.ai_flags    = 0;
98 #ifdef AI_ADDRCONFIG
99         ai_hints.ai_flags   |= AI_ADDRCONFIG;
100 #endif
101         ai_hints.ai_family   = PF_UNSPEC;
102         ai_hints.ai_socktype = SOCK_STREAM;
103         ai_hints.ai_protocol = IPPROTO_TCP;
104
105         host = mbmon_host;
106         if (host == NULL)
107                 host = MBMON_DEF_HOST;
108
109         port = mbmon_port;
110         if (port == NULL)
111                 port = MBMON_DEF_PORT;
112
113         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
114         {
115                 char errbuf[1024];
116                 ERROR ("mbmon: getaddrinfo (%s, %s): %s",
117                                 host, port,
118                                 (ai_return == EAI_SYSTEM)
119                                 ? sstrerror (errno, errbuf, sizeof (errbuf))
120                                 : gai_strerror (ai_return));
121                 return (-1);
122         }
123
124         fd = -1;
125         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
126         {
127                 /* create our socket descriptor */
128                 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
129                 {
130                         char errbuf[1024];
131                         ERROR ("mbmon: socket: %s",
132                                         sstrerror (errno, errbuf,
133                                                 sizeof (errbuf)));
134                         continue;
135                 }
136
137                 /* connect to the mbmon daemon */
138                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
139                 {
140                         char errbuf[1024];
141                         DEBUG ("mbmon: connect (%s, %s): %s", host, port,
142                                         sstrerror (errno, errbuf,
143                                                 sizeof (errbuf)));
144                         close (fd);
145                         fd = -1;
146                         continue;
147                 }
148
149                 /* A socket could be opened and connecting succeeded. We're
150                  * done. */
151                 break;
152         }
153
154         freeaddrinfo (ai_list);
155
156         if (fd < 0)
157         {
158                 ERROR ("mbmon: Could not connect to daemon.");
159                 return (-1);
160         }
161
162         /* receive data from the mbmon daemon */
163         memset (buffer, '\0', buffer_size);
164
165         buffer_fill = 0;
166         while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
167         {
168                 if (status == -1)
169                 {
170                         char errbuf[1024];
171
172                         if ((errno == EAGAIN) || (errno == EINTR))
173                                 continue;
174
175                         ERROR ("mbmon: Error reading from socket: %s",
176                                         sstrerror (errno, errbuf,
177                                                 sizeof (errbuf)));
178                         close (fd);
179                         return (-1);
180                 }
181                 buffer_fill += status;
182
183                 if (buffer_fill >= buffer_size)
184                         break;
185         }
186
187         if (buffer_fill >= buffer_size)
188         {
189                 buffer[buffer_size - 1] = '\0';
190                 WARNING ("mbmon: Message from mbmon has been truncated.");
191         }
192         else if (buffer_fill == 0)
193         {
194                 WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
195                                 "Buffer: `%s'", buffer);
196                 close (fd);
197                 return (-1);
198         }
199
200         close (fd);
201         return (0);
202 }
203
204 static int mbmon_config (const char *key, const char *value)
205 {
206         if (strcasecmp (key, "host") == 0)
207         {
208                 if (mbmon_host != NULL)
209                         free (mbmon_host);
210                 mbmon_host = strdup (value);
211         }
212         else if (strcasecmp (key, "port") == 0)
213         {
214                 if (mbmon_port != NULL)
215                         free (mbmon_port);
216                 mbmon_port = strdup (value);
217         }
218         else
219         {
220                 return (-1);
221         }
222
223         return (0);
224 }
225
226 static void mbmon_submit (const char *type, const char *type_instance,
227                 double value)
228 {
229         value_t values[1];
230         value_list_t vl = VALUE_LIST_INIT;
231
232         values[0].gauge = value;
233
234         vl.values = values;
235         vl.values_len = 1;
236         vl.time = time (NULL);
237         strcpy (vl.host, hostname_g);
238         strcpy (vl.plugin, "mbmon");
239         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
240
241         plugin_dispatch_values (type, &vl);
242 } /* void mbmon_submit */
243
244 /* Trim trailing whitespace from a string. */
245 static void trim_spaces (char *s)
246 {
247         size_t l;
248
249         for (l = strlen (s) - 1; (l > 0) && isspace (s[l]); l--)
250                 s[l] = '\0';
251 }
252
253 static int mbmon_read (void)
254 {
255         char buf[1024];
256         char *s, *t;
257
258         /* get data from daemon */
259         if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
260                 return (-1);
261
262         s = buf;
263         while ((t = strchr (s, ':')) != NULL)
264         {
265                 double value;
266                 char *nextc;
267
268                 char *type;
269                 char *inst;
270
271                 *t++ = '\0';
272                 trim_spaces (s);
273
274                 value = strtod (t, &nextc);
275                 if ((*nextc != '\n') && (*nextc != '\0'))
276                 {
277                         ERROR ("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
278                         break;
279                 }
280
281                 if (strncmp (s, "TEMP", 4) == 0)
282                 {
283                         inst = s + 4;
284                         type = "temperature";
285                 }
286                 else if (strncmp (s, "FAN", 3) == 0)
287                 {
288                         inst = s + 3;
289                         type = "fanspeed";
290                 }
291                 else if (strncmp (s, "V", 1) == 0)
292                 {
293                         inst = s + 1;
294                         type = "voltage";
295                 }
296                 else
297                 {
298                         continue;
299                 }
300
301                 mbmon_submit (type, inst, value);
302
303                 if (*nextc == '\0')
304                         break;
305
306                 s = nextc + 1;
307         }
308
309         return (0);
310 } /* void mbmon_read */
311 #endif /* MBMON_HAVE_READ */
312
313 /* module_register
314    Register collectd plugin. */
315 void module_register (void)
316 {
317 #if MBMON_HAVE_READ
318         plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
319         plugin_register_read ("mbmon", mbmon_read);
320 #endif /* MBMON_HAVE_READ */
321 } /* void module_register */