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