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>
29 #include "configfile.h"
32 #include <netinet/in.h>
33 #include <netinet/tcp.h>
35 #define MBMON_DEF_HOST "127.0.0.1"
36 #define MBMON_DEF_PORT "411" /* the default for Debian */
38 static const char *config_keys[] =
44 static int config_keys_num = 2;
46 static char *mbmon_host = NULL;
47 static char *mbmon_port = NULL;
54 * Connect to the mbmon daemon and receive data.
57 * `buffer' The buffer where we put the received ascii string.
58 * `buffer_size' Size of the buffer
61 * >= 0 if ok, < 0 otherwise.
64 * Example of possible strings, as received from daemon:
75 * we need to create a new socket each time. Is there another way?
76 * Hm, maybe we can re-use the `sockaddr' structure? -octo
78 static int mbmon_query_daemon (char *buffer, int buffer_size)
87 struct addrinfo ai_hints;
88 struct addrinfo *ai_list, *ai_ptr;
91 memset (&ai_hints, '\0', sizeof (ai_hints));
92 ai_hints.ai_flags = 0;
94 ai_hints.ai_flags |= AI_ADDRCONFIG;
96 ai_hints.ai_family = PF_UNSPEC;
97 ai_hints.ai_socktype = SOCK_STREAM;
98 ai_hints.ai_protocol = IPPROTO_TCP;
102 host = MBMON_DEF_HOST;
106 port = MBMON_DEF_PORT;
108 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
111 ERROR ("mbmon: getaddrinfo (%s, %s): %s",
113 (ai_return == EAI_SYSTEM)
114 ? sstrerror (errno, errbuf, sizeof (errbuf))
115 : gai_strerror (ai_return));
120 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
122 /* create our socket descriptor */
123 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
126 ERROR ("mbmon: socket: %s",
127 sstrerror (errno, errbuf,
132 /* connect to the mbmon daemon */
133 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
136 INFO ("mbmon: connect (%s, %s): %s", host, port,
137 sstrerror (errno, errbuf,
144 /* A socket could be opened and connecting succeeded. We're
149 freeaddrinfo (ai_list);
153 ERROR ("mbmon: Could not connect to daemon.");
157 /* receive data from the mbmon daemon */
158 memset (buffer, '\0', buffer_size);
161 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
167 if ((errno == EAGAIN) || (errno == EINTR))
170 ERROR ("mbmon: Error reading from socket: %s",
171 sstrerror (errno, errbuf,
176 buffer_fill += status;
178 if (buffer_fill >= buffer_size)
182 if (buffer_fill >= buffer_size)
184 buffer[buffer_size - 1] = '\0';
185 WARNING ("mbmon: Message from mbmon has been truncated.");
187 else if (buffer_fill == 0)
189 WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
190 "Buffer: `%s'", buffer);
199 static int mbmon_config (const char *key, const char *value)
201 if (strcasecmp (key, "host") == 0)
203 if (mbmon_host != NULL)
205 mbmon_host = strdup (value);
207 else if (strcasecmp (key, "port") == 0)
209 if (mbmon_port != NULL)
211 mbmon_port = strdup (value);
221 static void mbmon_submit (const char *type, const char *type_instance,
225 value_list_t vl = VALUE_LIST_INIT;
227 values[0].gauge = value;
231 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
232 sstrncpy (vl.plugin, "mbmon", sizeof (vl.plugin));
233 sstrncpy (vl.type, type, sizeof (vl.type));
234 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
236 plugin_dispatch_values (&vl);
237 } /* void mbmon_submit */
239 /* Trim trailing whitespace from a string. */
240 static void trim_spaces (char *s)
244 for (l = strlen (s) - 1; (l > 0) && isspace ((int) s[l]); l--)
248 static int mbmon_read (void)
253 /* get data from daemon */
254 if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
258 while ((t = strchr (s, ':')) != NULL)
269 value = strtod (t, &nextc);
270 if ((*nextc != '\n') && (*nextc != '\0'))
272 ERROR ("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
276 if (strncmp (s, "TEMP", 4) == 0)
279 type = "temperature";
281 else if (strncmp (s, "FAN", 3) == 0)
286 else if (strncmp (s, "V", 1) == 0)
296 mbmon_submit (type, inst, value);
305 } /* void mbmon_read */
308 Register collectd plugin. */
309 void module_register (void)
311 plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
312 plugin_register_read ("mbmon", mbmon_read);
313 } /* void module_register */