2 * collectd - src/mbmon.c
3 * Copyright (C) 2006 Flavio Stanchina
4 * Copyright (C) 2006-2007 Florian octo Forster
5 * Based on the hddtemp plugin.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 * Flavio Stanchina <flavio at stanchina.net>
23 * Florian Forster <octo at collectd.org>
30 #include "configfile.h"
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
36 #define MBMON_DEF_HOST "127.0.0.1"
37 #define MBMON_DEF_PORT "411" /* the default for Debian */
39 static const char *config_keys[] =
45 static int config_keys_num = 2;
47 static char *mbmon_host = NULL;
48 static char *mbmon_port = NULL;
55 * Connect to the mbmon daemon and receive data.
58 * `buffer' The buffer where we put the received ascii string.
59 * `buffer_size' Size of the buffer
62 * >= 0 if ok, < 0 otherwise.
65 * Example of possible strings, as received from daemon:
76 * we need to create a new socket each time. Is there another way?
77 * Hm, maybe we can re-use the `sockaddr' structure? -octo
79 static int mbmon_query_daemon (char *buffer, int buffer_size)
88 struct addrinfo *ai_list;
93 host = MBMON_DEF_HOST;
97 port = MBMON_DEF_PORT;
99 struct addrinfo ai_hints = {
100 .ai_family = AF_UNSPEC,
101 .ai_flags = AI_ADDRCONFIG,
102 .ai_protocol = IPPROTO_TCP,
103 .ai_socktype = SOCK_STREAM
106 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
109 ERROR ("mbmon: getaddrinfo (%s, %s): %s",
111 (ai_return == EAI_SYSTEM)
112 ? sstrerror (errno, errbuf, sizeof (errbuf))
113 : gai_strerror (ai_return));
118 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
120 /* create our socket descriptor */
121 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
124 ERROR ("mbmon: socket: %s",
125 sstrerror (errno, errbuf,
130 /* connect to the mbmon daemon */
131 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
134 INFO ("mbmon: connect (%s, %s): %s", host, port,
135 sstrerror (errno, errbuf,
142 /* A socket could be opened and connecting succeeded. We're
147 freeaddrinfo (ai_list);
151 ERROR ("mbmon: Could not connect to daemon.");
155 /* receive data from the mbmon daemon */
156 memset (buffer, '\0', buffer_size);
159 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
165 if ((errno == EAGAIN) || (errno == EINTR))
168 ERROR ("mbmon: Error reading from socket: %s",
169 sstrerror (errno, errbuf,
174 buffer_fill += status;
176 if (buffer_fill >= buffer_size)
180 if (buffer_fill >= buffer_size)
182 buffer[buffer_size - 1] = '\0';
183 WARNING ("mbmon: Message from mbmon has been truncated.");
185 else if (buffer_fill == 0)
187 WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
188 "Buffer: `%s'", buffer);
197 static int mbmon_config (const char *key, const char *value)
199 if (strcasecmp (key, "host") == 0)
201 if (mbmon_host != NULL)
203 mbmon_host = strdup (value);
205 else if (strcasecmp (key, "port") == 0)
207 if (mbmon_port != NULL)
209 mbmon_port = strdup (value);
219 static void mbmon_submit (const char *type, const char *type_instance,
223 value_list_t vl = VALUE_LIST_INIT;
225 values[0].gauge = value;
229 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
230 sstrncpy (vl.plugin, "mbmon", sizeof (vl.plugin));
231 sstrncpy (vl.type, type, sizeof (vl.type));
232 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
234 plugin_dispatch_values (&vl);
235 } /* void mbmon_submit */
237 /* Trim trailing whitespace from a string. */
238 static void trim_spaces (char *s)
240 for (size_t l = strlen (s) - 1; (l > 0) && isspace ((int) s[l]); l--)
244 static int mbmon_read (void)
249 /* get data from daemon */
250 if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
254 while ((t = strchr (s, ':')) != NULL)
265 value = strtod (t, &nextc);
266 if ((*nextc != '\n') && (*nextc != '\0'))
268 ERROR ("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
272 if (strncmp (s, "TEMP", 4) == 0)
275 type = "temperature";
277 else if (strncmp (s, "FAN", 3) == 0)
282 else if (strncmp (s, "V", 1) == 0)
292 mbmon_submit (type, inst, value);
301 } /* void mbmon_read */
304 Register collectd plugin. */
305 void module_register (void)
307 plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
308 plugin_register_read ("mbmon", mbmon_read);
309 } /* void module_register */