0df65ccaf4af2f5e9d831d3c4b759d626805e5cf
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 **/
25 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29 #include "utils_debug.h"
31 #define MODULE_NAME "mbmon"
33 #include <netdb.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <netinet/tcp.h>
38 #define MBMON_DEF_HOST "127.0.0.1"
39 #define MBMON_DEF_PORT "411" /* the default for Debian */
41 /* BUFFER_SIZE
42 Size of the buffer we use to receive from the mbmon daemon. */
43 #define BUFFER_SIZE 1024
45 static char *filename_format = "mbmon-%s.rrd";
47 static char *ds_def[] =
48 {
49 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
50 NULL
51 };
52 static int ds_num = 1;
54 static char *config_keys[] =
55 {
56 "Host",
57 "Port",
58 NULL
59 };
60 static int config_keys_num = 2;
62 static char *mbmon_host = NULL;
63 static char *mbmon_port = NULL;
65 /*
66 * NAME
67 * mbmon_query_daemon
68 *
69 * DESCRIPTION
70 * Connect to the mbmon daemon and receive data.
71 *
72 * ARGUMENTS:
73 * `buffer' The buffer where we put the received ascii string.
74 * `buffer_size' Size of the buffer
75 *
76 * RETURN VALUE:
77 * >= 0 if ok, < 0 otherwise.
78 *
79 * NOTES:
80 * Example of possible strings, as received from daemon:
81 * TEMP0 : 27.0
82 * TEMP1 : 31.0
83 * TEMP2 : 29.5
84 * FAN0 : 4411
85 * FAN1 : 4470
86 * FAN2 : 4963
87 * VC0 : +1.68
88 * VC1 : +1.73
89 *
90 * FIXME:
91 * we need to create a new socket each time. Is there another way?
92 * Hm, maybe we can re-use the `sockaddr' structure? -octo
93 */
94 static int mbmon_query_daemon (char *buffer, int buffer_size)
95 {
96 int fd;
97 ssize_t status;
98 int buffer_fill;
100 const char *host;
101 const char *port;
103 struct addrinfo ai_hints;
104 struct addrinfo *ai_list, *ai_ptr;
105 int ai_return;
107 memset (&ai_hints, '\0', sizeof (ai_hints));
108 ai_hints.ai_flags = AI_ADDRCONFIG;
109 ai_hints.ai_family = PF_UNSPEC;
110 ai_hints.ai_socktype = SOCK_STREAM;
111 ai_hints.ai_protocol = IPPROTO_TCP;
113 host = mbmon_host;
114 if (host == NULL)
115 host = MBMON_DEF_HOST;
117 port = mbmon_port;
118 if (port == NULL)
119 port = MBMON_DEF_PORT;
121 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
122 {
123 syslog (LOG_ERR, "mbmon: getaddrinfo (%s, %s): %s",
124 host, port,
125 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
126 return (-1);
127 }
129 fd = -1;
130 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
131 {
132 /* create our socket descriptor */
133 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
134 {
135 syslog (LOG_ERR, "mbmon: socket: %s",
136 strerror (errno));
137 continue;
138 }
140 /* connect to the mbmon daemon */
141 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
142 {
143 DBG ("mbmon: connect (%s, %s): %s", host, port,
144 strerror (errno));
145 close (fd);
146 fd = -1;
147 continue;
148 }
150 /* A socket could be opened and connecting succeeded. We're
151 * done. */
152 break;
153 }
155 freeaddrinfo (ai_list);
157 if (fd < 0)
158 {
159 syslog (LOG_ERR, "mbmon: Could not connect to daemon.");
160 return (-1);
161 }
163 /* receive data from the mbmon daemon */
164 memset (buffer, '\0', buffer_size);
166 buffer_fill = 0;
167 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
168 {
169 if (status == -1)
170 {
171 if ((errno == EAGAIN) || (errno == EINTR))
172 continue;
174 syslog (LOG_ERR, "mbmon: Error reading from socket: %s",
175 strerror (errno));
176 close (fd);
177 return (-1);
178 }
179 buffer_fill += status;
181 if (buffer_fill >= buffer_size)
182 break;
183 }
185 if (buffer_fill >= buffer_size)
186 {
187 buffer[buffer_size - 1] = '\0';
188 syslog (LOG_WARNING, "mbmon: Message from mbmon has been truncated.");
189 }
190 else if (buffer_fill == 0)
191 {
192 syslog (LOG_WARNING, "mbmon: Peer has unexpectedly shut down the socket. "
193 "Buffer: `%s'", buffer);
194 close (fd);
195 return (-1);
196 }
198 close (fd);
199 return (0);
200 }
202 static int mbmon_config (char *key, char *value)
203 {
204 if (strcasecmp (key, "host") == 0)
205 {
206 if (mbmon_host != NULL)
207 free (mbmon_host);
208 mbmon_host = strdup (value);
209 }
210 else if (strcasecmp (key, "port") == 0)
211 {
212 if (mbmon_port != NULL)
213 free (mbmon_port);
214 mbmon_port = strdup (value);
215 }
216 else
217 {
218 return (-1);
219 }
221 return (0);
222 }
224 static void mbmon_init (void)
225 {
226 return;
227 }
229 static void mbmon_write (char *host, char *inst, char *val)
230 {
231 char filename[BUFFER_SIZE];
232 int status;
234 /* construct filename */
235 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
236 if (status < 1)
237 return;
238 else if (status >= BUFFER_SIZE)
239 return;
241 rrd_update_file (host, filename, val, ds_def, ds_num);
242 }
244 static void mbmon_submit (char *inst, double value)
245 {
246 char buf[BUFFER_SIZE];
248 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, value)
249 >= BUFFER_SIZE)
250 return;
252 plugin_submit (MODULE_NAME, inst, buf);
253 }
255 /* Trim trailing whitespace from a string. */
256 static void trim_spaces (char *s)
257 {
258 size_t l;
260 for (l = strlen (s) - 1; (l > 0) && isspace (s[l]); l--)
261 s[l] = '\0';
262 }
264 static void mbmon_read (void)
265 {
266 char buf[BUFFER_SIZE];
267 char *s, *t;
269 static int wait_time = 1;
270 static int wait_left = 0;
272 if (wait_left >= 10)
273 {
274 wait_left -= 10;
275 return;
276 }
278 /* get data from daemon */
279 if (mbmon_query_daemon (buf, BUFFER_SIZE) < 0)
280 {
281 /* This limit is reached in log2(86400) =~ 17 steps. Since
282 * there is a 2^n seconds wait between each step it will need
283 * roughly one day to reach this limit. -octo */
285 wait_time *= 2;
286 if (wait_time > 86400)
287 wait_time = 86400;
289 wait_left = wait_time;
291 return;
292 }
293 else
294 {
295 wait_time = 1;
296 wait_left = 0;
297 }
299 s = buf;
300 while ((t = strchr (s, ':')) != NULL)
301 {
302 double value;
303 char *nextc;
305 *t++ = '\0';
306 trim_spaces (s);
308 value = strtod (t, &nextc);
309 if ((*nextc != '\n') && (*nextc != '\0'))
310 {
311 syslog (LOG_ERR, "mbmon: value for `%s' contains invalid characters: `%s'", s, t);
312 break;
313 }
315 mbmon_submit (s, value);
317 if (*nextc == '\0')
318 break;
320 s = nextc + 1;
321 }
322 }
324 /* module_register
325 Register collectd plugin. */
326 void module_register (void)
327 {
328 plugin_register (MODULE_NAME, mbmon_init, mbmon_read, mbmon_write);
329 cf_register (MODULE_NAME, mbmon_config, config_keys, config_keys_num);
330 }
332 #undef MODULE_NAME