Removed the `XXX_HAVE_READ' defines from all plugins.
[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 #include <netdb.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
33
34 #define MBMON_DEF_HOST "127.0.0.1"
35 #define MBMON_DEF_PORT "411" /* the default for Debian */
36
37 static const char *config_keys[] =
38 {
39         "Host",
40         "Port",
41         NULL
42 };
43 static int config_keys_num = 2;
44
45 static char *mbmon_host = NULL;
46 static char *mbmon_port = NULL;
47
48 /*
49  * NAME
50  *  mbmon_query_daemon
51  *
52  * DESCRIPTION
53  * Connect to the mbmon daemon and receive data.
54  *
55  * ARGUMENTS:
56  *  `buffer'            The buffer where we put the received ascii string.
57  *  `buffer_size'       Size of the buffer
58  *
59  * RETURN VALUE:
60  *   >= 0 if ok, < 0 otherwise.
61  *
62  * NOTES:
63  *  Example of possible strings, as received from daemon:
64  *    TEMP0 : 27.0
65  *    TEMP1 : 31.0
66  *    TEMP2 : 29.5
67  *    FAN0  : 4411
68  *    FAN1  : 4470
69  *    FAN2  : 4963
70  *    VC0   :  +1.68
71  *    VC1   :  +1.73
72  *
73  * FIXME:
74  *  we need to create a new socket each time. Is there another way?
75  *  Hm, maybe we can re-use the `sockaddr' structure? -octo
76  */
77 static int mbmon_query_daemon (char *buffer, int buffer_size)
78 {
79         int fd;
80         ssize_t status;
81         int buffer_fill;
82
83         const char *host;
84         const char *port;
85
86         struct addrinfo  ai_hints;
87         struct addrinfo *ai_list, *ai_ptr;
88         int              ai_return;
89
90         memset (&ai_hints, '\0', sizeof (ai_hints));
91         ai_hints.ai_flags    = AI_ADDRCONFIG;
92         ai_hints.ai_family   = PF_UNSPEC;
93         ai_hints.ai_socktype = SOCK_STREAM;
94         ai_hints.ai_protocol = IPPROTO_TCP;
95
96         host = mbmon_host;
97         if (host == NULL)
98                 host = MBMON_DEF_HOST;
99
100         port = mbmon_port;
101         if (port == NULL)
102                 port = MBMON_DEF_PORT;
103
104         if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
105         {
106                 char errbuf[1024];
107                 ERROR ("mbmon: getaddrinfo (%s, %s): %s",
108                                 host, port,
109                                 (ai_return == EAI_SYSTEM)
110                                 ? sstrerror (errno, errbuf, sizeof (errbuf))
111                                 : gai_strerror (ai_return));
112                 return (-1);
113         }
114
115         fd = -1;
116         for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
117         {
118                 /* create our socket descriptor */
119                 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
120                 {
121                         char errbuf[1024];
122                         ERROR ("mbmon: socket: %s",
123                                         sstrerror (errno, errbuf,
124                                                 sizeof (errbuf)));
125                         continue;
126                 }
127
128                 /* connect to the mbmon daemon */
129                 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
130                 {
131                         char errbuf[1024];
132                         DEBUG ("mbmon: connect (%s, %s): %s", host, port,
133                                         sstrerror (errno, errbuf,
134                                                 sizeof (errbuf)));
135                         close (fd);
136                         fd = -1;
137                         continue;
138                 }
139
140                 /* A socket could be opened and connecting succeeded. We're
141                  * done. */
142                 break;
143         }
144
145         freeaddrinfo (ai_list);
146
147         if (fd < 0)
148         {
149                 ERROR ("mbmon: Could not connect to daemon.");
150                 return (-1);
151         }
152
153         /* receive data from the mbmon daemon */
154         memset (buffer, '\0', buffer_size);
155
156         buffer_fill = 0;
157         while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
158         {
159                 if (status == -1)
160                 {
161                         char errbuf[1024];
162
163                         if ((errno == EAGAIN) || (errno == EINTR))
164                                 continue;
165
166                         ERROR ("mbmon: Error reading from socket: %s",
167                                         sstrerror (errno, errbuf,
168                                                 sizeof (errbuf)));
169                         close (fd);
170                         return (-1);
171                 }
172                 buffer_fill += status;
173
174                 if (buffer_fill >= buffer_size)
175                         break;
176         }
177
178         if (buffer_fill >= buffer_size)
179         {
180                 buffer[buffer_size - 1] = '\0';
181                 WARNING ("mbmon: Message from mbmon has been truncated.");
182         }
183         else if (buffer_fill == 0)
184         {
185                 WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
186                                 "Buffer: `%s'", buffer);
187                 close (fd);
188                 return (-1);
189         }
190
191         close (fd);
192         return (0);
193 }
194
195 static int mbmon_config (const char *key, const char *value)
196 {
197         if (strcasecmp (key, "host") == 0)
198         {
199                 if (mbmon_host != NULL)
200                         free (mbmon_host);
201                 mbmon_host = strdup (value);
202         }
203         else if (strcasecmp (key, "port") == 0)
204         {
205                 if (mbmon_port != NULL)
206                         free (mbmon_port);
207                 mbmon_port = strdup (value);
208         }
209         else
210         {
211                 return (-1);
212         }
213
214         return (0);
215 }
216
217 static void mbmon_submit (const char *type, const char *type_instance,
218                 double value)
219 {
220         value_t values[1];
221         value_list_t vl = VALUE_LIST_INIT;
222
223         values[0].gauge = value;
224
225         vl.values = values;
226         vl.values_len = 1;
227         vl.time = time (NULL);
228         strcpy (vl.host, hostname_g);
229         strcpy (vl.plugin, "mbmon");
230         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
231
232         plugin_dispatch_values (type, &vl);
233 } /* void mbmon_submit */
234
235 /* Trim trailing whitespace from a string. */
236 static void trim_spaces (char *s)
237 {
238         size_t l;
239
240         for (l = strlen (s) - 1; (l > 0) && isspace (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                 char *type;
260                 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 */