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 #if HAVE_NETDB_H && HAVE_SYS_SOCKET_H && HAVE_NETINET_IN_H && HAVE_NETINET_TCP_H
32 # include <sys/socket.h>
33 # include <netinet/in.h>
34 # include <netinet/tcp.h>
35 # define MBMON_HAVE_READ 1
37 # define MBMON_HAVE_READ 0
40 #define MBMON_DEF_HOST "127.0.0.1"
41 #define MBMON_DEF_PORT "411" /* the default for Debian */
43 static data_source_t data_source_fanspeed[1] =
45 {"value", DS_TYPE_GAUGE, 0, NAN}
48 static data_set_t fanspeed_ds =
50 "fanspeed", 1, data_source_fanspeed
53 static data_source_t data_source_temperature[1] =
55 {"value", DS_TYPE_GAUGE, -273.15, NAN}
58 static data_set_t temperature_ds =
60 "temperature", 1, data_source_temperature
63 static data_source_t data_source_voltage[1] =
65 {"voltage", DS_TYPE_GAUGE, NAN, NAN}
68 static data_set_t voltage_ds =
70 "voltage", 1, data_source_voltage
73 static const char *config_keys[] =
79 static int config_keys_num = 2;
82 static char *mbmon_host = NULL;
83 static char *mbmon_port = NULL;
90 * Connect to the mbmon daemon and receive data.
93 * `buffer' The buffer where we put the received ascii string.
94 * `buffer_size' Size of the buffer
97 * >= 0 if ok, < 0 otherwise.
100 * Example of possible strings, as received from daemon:
111 * we need to create a new socket each time. Is there another way?
112 * Hm, maybe we can re-use the `sockaddr' structure? -octo
114 static int mbmon_query_daemon (char *buffer, int buffer_size)
123 struct addrinfo ai_hints;
124 struct addrinfo *ai_list, *ai_ptr;
127 memset (&ai_hints, '\0', sizeof (ai_hints));
128 ai_hints.ai_flags = AI_ADDRCONFIG;
129 ai_hints.ai_family = PF_UNSPEC;
130 ai_hints.ai_socktype = SOCK_STREAM;
131 ai_hints.ai_protocol = IPPROTO_TCP;
135 host = MBMON_DEF_HOST;
139 port = MBMON_DEF_PORT;
141 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
143 syslog (LOG_ERR, "mbmon: getaddrinfo (%s, %s): %s",
145 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
150 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
152 /* create our socket descriptor */
153 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
155 syslog (LOG_ERR, "mbmon: socket: %s",
160 /* connect to the mbmon daemon */
161 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
163 DBG ("mbmon: connect (%s, %s): %s", host, port,
170 /* A socket could be opened and connecting succeeded. We're
175 freeaddrinfo (ai_list);
179 syslog (LOG_ERR, "mbmon: Could not connect to daemon.");
183 /* receive data from the mbmon daemon */
184 memset (buffer, '\0', buffer_size);
187 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
191 if ((errno == EAGAIN) || (errno == EINTR))
194 syslog (LOG_ERR, "mbmon: Error reading from socket: %s",
199 buffer_fill += status;
201 if (buffer_fill >= buffer_size)
205 if (buffer_fill >= buffer_size)
207 buffer[buffer_size - 1] = '\0';
208 syslog (LOG_WARNING, "mbmon: Message from mbmon has been truncated.");
210 else if (buffer_fill == 0)
212 syslog (LOG_WARNING, "mbmon: Peer has unexpectedly shut down the socket. "
213 "Buffer: `%s'", buffer);
222 static int mbmon_config (const char *key, const char *value)
224 if (strcasecmp (key, "host") == 0)
226 if (mbmon_host != NULL)
228 mbmon_host = strdup (value);
230 else if (strcasecmp (key, "port") == 0)
232 if (mbmon_port != NULL)
234 mbmon_port = strdup (value);
244 static void mbmon_submit (const char *type, const char *type_instance,
248 value_list_t vl = VALUE_LIST_INIT;
250 values[0].gauge = value;
254 vl.time = time (NULL);
255 strcpy (vl.host, hostname);
256 strcpy (vl.plugin, "mbmon");
257 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
259 plugin_dispatch_values (type, &vl);
260 } /* void mbmon_submit */
262 /* Trim trailing whitespace from a string. */
263 static void trim_spaces (char *s)
267 for (l = strlen (s) - 1; (l > 0) && isspace (s[l]); l--)
271 static int mbmon_read (void)
276 /* get data from daemon */
277 if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
281 while ((t = strchr (s, ':')) != NULL)
292 value = strtod (t, &nextc);
293 if ((*nextc != '\n') && (*nextc != '\0'))
295 syslog (LOG_ERR, "mbmon: value for `%s' contains invalid characters: `%s'", s, t);
299 if (strncmp (s, "TEMP", 4) == 0)
302 type = "temperature";
304 else if (strncmp (s, "FAN", 3) == 0)
309 else if (strncmp (s, "V", 1) == 0)
319 mbmon_submit (type, inst, value);
328 } /* void mbmon_read */
329 #endif /* MBMON_HAVE_READ */
332 Register collectd plugin. */
333 void module_register (void)
335 plugin_register_data_set (&fanspeed_ds);
336 plugin_register_data_set (&temperature_ds);
337 plugin_register_data_set (&voltage_ds);
340 plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
341 plugin_register_read ("mbmon", mbmon_read);
342 #endif /* MBMON_HAVE_READ */