2 * collectd - src/mbmon.c
3 * Copyright (C) 2006 Flavio Stanchina
4 * Based on the hddtemp plugin.
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.
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.
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
21 * Flavio Stanchina <flavio at stanchina.net>
28 #include "configfile.h"
29 #include "utils_debug.h"
31 #define MODULE_NAME "mbmon"
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 */
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[] =
49 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
52 static int ds_num = 1;
54 static char *config_keys[] =
60 static int config_keys_num = 2;
62 static char *mbmon_host = NULL;
63 static char *mbmon_port = NULL;
70 * Connect to the mbmon daemon and receive data.
73 * `buffer' The buffer where we put the received ascii string.
74 * `buffer_size' Size of the buffer
77 * >= 0 if ok, < 0 otherwise.
80 * Example of possible strings, as received from daemon:
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
94 static int mbmon_query_daemon (char *buffer, int buffer_size)
103 struct addrinfo ai_hints;
104 struct addrinfo *ai_list, *ai_ptr;
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;
115 host = MBMON_DEF_HOST;
119 port = MBMON_DEF_PORT;
121 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
123 syslog (LOG_ERR, "mbmon: getaddrinfo (%s, %s): %s",
125 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
130 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
132 /* create our socket descriptor */
133 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
135 syslog (LOG_ERR, "mbmon: socket: %s",
140 /* connect to the mbmon daemon */
141 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
143 DBG ("mbmon: connect (%s, %s): %s", host, port,
150 /* A socket could be opened and connecting succeeded. We're
155 freeaddrinfo (ai_list);
159 syslog (LOG_ERR, "mbmon: Could not connect to daemon.");
163 /* receive data from the mbmon daemon */
164 memset (buffer, '\0', buffer_size);
167 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
171 if ((errno == EAGAIN) || (errno == EINTR))
174 syslog (LOG_ERR, "mbmon: Error reading from socket: %s",
179 buffer_fill += status;
181 if (buffer_fill >= buffer_size)
185 if (buffer_fill >= buffer_size)
187 buffer[buffer_size - 1] = '\0';
188 syslog (LOG_WARNING, "mbmon: Message from mbmon has been truncated.");
190 else if (buffer_fill == 0)
192 syslog (LOG_WARNING, "mbmon: Peer has unexpectedly shut down the socket. "
193 "Buffer: `%s'", buffer);
202 static int mbmon_config (char *key, char *value)
204 if (strcasecmp (key, "host") == 0)
206 if (mbmon_host != NULL)
208 mbmon_host = strdup (value);
210 else if (strcasecmp (key, "port") == 0)
212 if (mbmon_port != NULL)
214 mbmon_port = strdup (value);
224 static void mbmon_init (void)
229 static void mbmon_write (char *host, char *inst, char *val)
231 char filename[BUFFER_SIZE];
234 /* construct filename */
235 status = snprintf (filename, BUFFER_SIZE, filename_format, inst);
238 else if (status >= BUFFER_SIZE)
241 rrd_update_file (host, filename, val, ds_def, ds_num);
244 static void mbmon_submit (char *inst, double value)
246 char buf[BUFFER_SIZE];
248 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, value)
252 plugin_submit (MODULE_NAME, inst, buf);
255 /* Trim trailing whitespace from a string. */
256 static void trim_spaces (char *s)
260 for (l = strlen (s) - 1; (l > 0) && isspace (s[l]); l--)
264 static void mbmon_read (void)
266 char buf[BUFFER_SIZE];
269 static int wait_time = 1;
270 static int wait_left = 0;
278 /* get data from daemon */
279 if (mbmon_query_daemon (buf, BUFFER_SIZE) < 0)
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 */
286 if (wait_time > 86400)
289 wait_left = wait_time;
300 while ((t = strchr (s, ':')) != NULL)
308 value = strtod (t, &nextc);
309 if ((*nextc != '\n') && (*nextc != '\0'))
311 syslog (LOG_ERR, "mbmon: value for `%s' contains invalid characters: `%s'", s, t);
315 mbmon_submit (s, value);
325 Register collectd plugin. */
326 void module_register (void)
328 plugin_register (MODULE_NAME, mbmon_init, mbmon_read, mbmon_write);
329 cf_register (MODULE_NAME, mbmon_config, config_keys, config_keys_num);