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>
27 #include "configfile.h"
28 #include "utils_debug.h"
30 #define MODULE_NAME "mbmon"
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
37 #define MBMON_DEF_HOST "127.0.0.1"
38 #define MBMON_DEF_PORT "411" /* the default for Debian */
41 Size of the buffer we use to receive from the mbmon daemon. */
42 #define BUFFER_SIZE 1024
44 static char *filename_temperature = "mbmon/temperature-%s.rrd";
45 static char *filename_fanspeed = "mbmon/fanspeed-%s.rrd";
46 static char *filename_voltage = "mbmon/voltage-%s.rrd";
48 /* temperature and fan sensors */
49 static char *ds_def[] =
51 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
54 static int ds_num = 1;
57 static char *voltage_ds_def[] =
59 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":U:U",
62 static int voltage_ds_num = 1;
64 static char *config_keys[] =
70 static int config_keys_num = 2;
72 static char *mbmon_host = NULL;
73 static char *mbmon_port = NULL;
80 * Connect to the mbmon daemon and receive data.
83 * `buffer' The buffer where we put the received ascii string.
84 * `buffer_size' Size of the buffer
87 * >= 0 if ok, < 0 otherwise.
90 * Example of possible strings, as received from daemon:
101 * we need to create a new socket each time. Is there another way?
102 * Hm, maybe we can re-use the `sockaddr' structure? -octo
104 static int mbmon_query_daemon (char *buffer, int buffer_size)
113 struct addrinfo ai_hints;
114 struct addrinfo *ai_list, *ai_ptr;
117 memset (&ai_hints, '\0', sizeof (ai_hints));
118 ai_hints.ai_flags = AI_ADDRCONFIG;
119 ai_hints.ai_family = PF_UNSPEC;
120 ai_hints.ai_socktype = SOCK_STREAM;
121 ai_hints.ai_protocol = IPPROTO_TCP;
125 host = MBMON_DEF_HOST;
129 port = MBMON_DEF_PORT;
131 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
133 syslog (LOG_ERR, "mbmon: getaddrinfo (%s, %s): %s",
135 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
140 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
142 /* create our socket descriptor */
143 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
145 syslog (LOG_ERR, "mbmon: socket: %s",
150 /* connect to the mbmon daemon */
151 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
153 DBG ("mbmon: connect (%s, %s): %s", host, port,
160 /* A socket could be opened and connecting succeeded. We're
165 freeaddrinfo (ai_list);
169 syslog (LOG_ERR, "mbmon: Could not connect to daemon.");
173 /* receive data from the mbmon daemon */
174 memset (buffer, '\0', buffer_size);
177 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
181 if ((errno == EAGAIN) || (errno == EINTR))
184 syslog (LOG_ERR, "mbmon: Error reading from socket: %s",
189 buffer_fill += status;
191 if (buffer_fill >= buffer_size)
195 if (buffer_fill >= buffer_size)
197 buffer[buffer_size - 1] = '\0';
198 syslog (LOG_WARNING, "mbmon: Message from mbmon has been truncated.");
200 else if (buffer_fill == 0)
202 syslog (LOG_WARNING, "mbmon: Peer has unexpectedly shut down the socket. "
203 "Buffer: `%s'", buffer);
212 static int mbmon_config (char *key, char *value)
214 if (strcasecmp (key, "host") == 0)
216 if (mbmon_host != NULL)
218 mbmon_host = strdup (value);
220 else if (strcasecmp (key, "port") == 0)
222 if (mbmon_port != NULL)
224 mbmon_port = strdup (value);
234 static void mbmon_init (void)
239 static void mbmon_write_temperature (char *host, char *inst, char *val)
241 char filename[BUFFER_SIZE];
244 /* construct filename */
245 status = snprintf (filename, BUFFER_SIZE, filename_temperature, inst);
246 if ((status < 1) || (status >= BUFFER_SIZE))
249 rrd_update_file (host, filename, val, ds_def, ds_num);
252 static void mbmon_write_fanspeed (char *host, char *inst, char *val)
254 char filename[BUFFER_SIZE];
257 /* construct filename */
258 status = snprintf (filename, BUFFER_SIZE, filename_fanspeed, inst);
259 if ((status < 1) || (status >= BUFFER_SIZE))
262 rrd_update_file (host, filename, val, ds_def, ds_num);
265 static void mbmon_write_voltage (char *host, char *inst, char *val)
267 char filename[BUFFER_SIZE];
270 /* construct filename */
271 status = snprintf (filename, BUFFER_SIZE, filename_voltage, inst);
272 if ((status < 1) || (status >= BUFFER_SIZE))
275 rrd_update_file (host, filename, val, voltage_ds_def, voltage_ds_num);
278 static void mbmon_submit (char *type, char *inst, double value)
280 char buf[BUFFER_SIZE];
282 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, value)
286 plugin_submit (type, inst, buf);
289 /* Trim trailing whitespace from a string. */
290 static void trim_spaces (char *s)
294 for (l = strlen (s) - 1; (l > 0) && isspace (s[l]); l--)
298 static void mbmon_read (void)
300 char buf[BUFFER_SIZE];
303 static int wait_time = 1;
304 static int wait_left = 0;
312 /* get data from daemon */
313 if (mbmon_query_daemon (buf, BUFFER_SIZE) < 0)
315 /* This limit is reached in log2(86400) =~ 17 steps. Since
316 * there is a 2^n seconds wait between each step it will need
317 * roughly one day to reach this limit. -octo */
320 if (wait_time > 86400)
323 wait_left = wait_time;
334 while ((t = strchr (s, ':')) != NULL)
345 value = strtod (t, &nextc);
346 if ((*nextc != '\n') && (*nextc != '\0'))
348 syslog (LOG_ERR, "mbmon: value for `%s' contains invalid characters: `%s'", s, t);
352 if (strncmp (s, "TEMP", 4) == 0)
355 type = "mbmon_temperature";
357 else if (strncmp (s, "FAN", 3) == 0)
360 type = "mbmon_fanspeed";
362 else if (strncmp (s, "V", 1) == 0)
365 type = "mbmon_voltage";
372 mbmon_submit (type, inst, value);
379 } /* void mbmon_read */
382 Register collectd plugin. */
383 void module_register (void)
385 plugin_register (MODULE_NAME, mbmon_init, mbmon_read, NULL);
386 plugin_register ("mbmon_temperature", NULL, NULL, mbmon_write_temperature);
387 plugin_register ("mbmon_fanspeed", NULL, NULL, mbmon_write_fanspeed);
388 plugin_register ("mbmon_voltage", NULL, NULL, mbmon_write_voltage);
389 cf_register (MODULE_NAME, mbmon_config, config_keys, config_keys_num);