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 <sys/socket.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_hints;
89 struct addrinfo *ai_list, *ai_ptr;
92 memset (&ai_hints, '\0', sizeof (ai_hints));
93 ai_hints.ai_flags = 0;
95 ai_hints.ai_flags |= AI_ADDRCONFIG;
97 ai_hints.ai_family = PF_UNSPEC;
98 ai_hints.ai_socktype = SOCK_STREAM;
99 ai_hints.ai_protocol = IPPROTO_TCP;
103 host = MBMON_DEF_HOST;
107 port = MBMON_DEF_PORT;
109 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
112 ERROR ("mbmon: getaddrinfo (%s, %s): %s",
114 (ai_return == EAI_SYSTEM)
115 ? sstrerror (errno, errbuf, sizeof (errbuf))
116 : gai_strerror (ai_return));
121 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
123 /* create our socket descriptor */
124 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
127 ERROR ("mbmon: socket: %s",
128 sstrerror (errno, errbuf,
133 /* connect to the mbmon daemon */
134 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
137 INFO ("mbmon: connect (%s, %s): %s", host, port,
138 sstrerror (errno, errbuf,
145 /* A socket could be opened and connecting succeeded. We're
150 freeaddrinfo (ai_list);
154 ERROR ("mbmon: Could not connect to daemon.");
158 /* receive data from the mbmon daemon */
159 memset (buffer, '\0', buffer_size);
162 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
168 if ((errno == EAGAIN) || (errno == EINTR))
171 ERROR ("mbmon: Error reading from socket: %s",
172 sstrerror (errno, errbuf,
177 buffer_fill += status;
179 if (buffer_fill >= buffer_size)
183 if (buffer_fill >= buffer_size)
185 buffer[buffer_size - 1] = '\0';
186 WARNING ("mbmon: Message from mbmon has been truncated.");
188 else if (buffer_fill == 0)
190 WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
191 "Buffer: `%s'", buffer);
200 static int mbmon_config (const char *key, const char *value)
202 if (strcasecmp (key, "host") == 0)
204 if (mbmon_host != NULL)
206 mbmon_host = strdup (value);
208 else if (strcasecmp (key, "port") == 0)
210 if (mbmon_port != NULL)
212 mbmon_port = strdup (value);
222 static void mbmon_submit (const char *type, const char *type_instance,
226 value_list_t vl = VALUE_LIST_INIT;
228 values[0].gauge = value;
232 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
233 sstrncpy (vl.plugin, "mbmon", sizeof (vl.plugin));
234 sstrncpy (vl.type, type, sizeof (vl.type));
235 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
237 plugin_dispatch_values (&vl);
238 } /* void mbmon_submit */
240 /* Trim trailing whitespace from a string. */
241 static void trim_spaces (char *s)
245 for (l = strlen (s) - 1; (l > 0) && isspace ((int) s[l]); l--)
249 static int mbmon_read (void)
254 /* get data from daemon */
255 if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
259 while ((t = strchr (s, ':')) != NULL)
270 value = strtod (t, &nextc);
271 if ((*nextc != '\n') && (*nextc != '\0'))
273 ERROR ("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
277 if (strncmp (s, "TEMP", 4) == 0)
280 type = "temperature";
282 else if (strncmp (s, "FAN", 3) == 0)
287 else if (strncmp (s, "V", 1) == 0)
297 mbmon_submit (type, inst, value);
306 } /* void mbmon_read */
309 Register collectd plugin. */
310 void module_register (void)
312 plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
313 plugin_register_read ("mbmon", mbmon_read);
314 } /* void module_register */