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"
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
34 #define MBMON_DEF_HOST "127.0.0.1"
35 #define MBMON_DEF_PORT "411" /* the default for Debian */
37 static const char *config_keys[] =
43 static int config_keys_num = 2;
45 static char *mbmon_host = NULL;
46 static char *mbmon_port = NULL;
53 * Connect to the mbmon daemon and receive data.
56 * `buffer' The buffer where we put the received ascii string.
57 * `buffer_size' Size of the buffer
60 * >= 0 if ok, < 0 otherwise.
63 * Example of possible strings, as received from daemon:
74 * we need to create a new socket each time. Is there another way?
75 * Hm, maybe we can re-use the `sockaddr' structure? -octo
77 static int mbmon_query_daemon (char *buffer, int buffer_size)
86 struct addrinfo ai_hints;
87 struct addrinfo *ai_list, *ai_ptr;
90 memset (&ai_hints, '\0', sizeof (ai_hints));
91 ai_hints.ai_flags = AI_ADDRCONFIG;
92 ai_hints.ai_family = PF_UNSPEC;
93 ai_hints.ai_socktype = SOCK_STREAM;
94 ai_hints.ai_protocol = IPPROTO_TCP;
98 host = MBMON_DEF_HOST;
102 port = MBMON_DEF_PORT;
104 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
107 ERROR ("mbmon: getaddrinfo (%s, %s): %s",
109 (ai_return == EAI_SYSTEM)
110 ? sstrerror (errno, errbuf, sizeof (errbuf))
111 : gai_strerror (ai_return));
116 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
118 /* create our socket descriptor */
119 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
122 ERROR ("mbmon: socket: %s",
123 sstrerror (errno, errbuf,
128 /* connect to the mbmon daemon */
129 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
132 DEBUG ("mbmon: connect (%s, %s): %s", host, port,
133 sstrerror (errno, errbuf,
140 /* A socket could be opened and connecting succeeded. We're
145 freeaddrinfo (ai_list);
149 ERROR ("mbmon: Could not connect to daemon.");
153 /* receive data from the mbmon daemon */
154 memset (buffer, '\0', buffer_size);
157 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
163 if ((errno == EAGAIN) || (errno == EINTR))
166 ERROR ("mbmon: Error reading from socket: %s",
167 sstrerror (errno, errbuf,
172 buffer_fill += status;
174 if (buffer_fill >= buffer_size)
178 if (buffer_fill >= buffer_size)
180 buffer[buffer_size - 1] = '\0';
181 WARNING ("mbmon: Message from mbmon has been truncated.");
183 else if (buffer_fill == 0)
185 WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
186 "Buffer: `%s'", buffer);
195 static int mbmon_config (const char *key, const char *value)
197 if (strcasecmp (key, "host") == 0)
199 if (mbmon_host != NULL)
201 mbmon_host = strdup (value);
203 else if (strcasecmp (key, "port") == 0)
205 if (mbmon_port != NULL)
207 mbmon_port = strdup (value);
217 static void mbmon_submit (const char *type, const char *type_instance,
221 value_list_t vl = VALUE_LIST_INIT;
223 values[0].gauge = value;
227 vl.time = time (NULL);
228 strcpy (vl.host, hostname_g);
229 strcpy (vl.plugin, "mbmon");
230 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
232 plugin_dispatch_values (type, &vl);
233 } /* void mbmon_submit */
235 /* Trim trailing whitespace from a string. */
236 static void trim_spaces (char *s)
240 for (l = strlen (s) - 1; (l > 0) && isspace (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 */