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>
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_list;
92 host = MBMON_DEF_HOST;
96 port = MBMON_DEF_PORT;
98 struct addrinfo ai_hints = {
99 .ai_family = AF_UNSPEC,
100 .ai_flags = AI_ADDRCONFIG,
101 .ai_protocol = IPPROTO_TCP,
102 .ai_socktype = SOCK_STREAM
105 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
108 ERROR ("mbmon: getaddrinfo (%s, %s): %s",
110 (ai_return == EAI_SYSTEM)
111 ? sstrerror (errno, errbuf, sizeof (errbuf))
112 : gai_strerror (ai_return));
117 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
119 /* create our socket descriptor */
120 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
123 ERROR ("mbmon: socket: %s",
124 sstrerror (errno, errbuf,
129 /* connect to the mbmon daemon */
130 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
133 INFO ("mbmon: connect (%s, %s): %s", host, port,
134 sstrerror (errno, errbuf,
141 /* A socket could be opened and connecting succeeded. We're
146 freeaddrinfo (ai_list);
150 ERROR ("mbmon: Could not connect to daemon.");
154 /* receive data from the mbmon daemon */
155 memset (buffer, '\0', buffer_size);
158 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
164 if ((errno == EAGAIN) || (errno == EINTR))
167 ERROR ("mbmon: Error reading from socket: %s",
168 sstrerror (errno, errbuf,
173 buffer_fill += status;
175 if (buffer_fill >= buffer_size)
179 if (buffer_fill >= buffer_size)
181 buffer[buffer_size - 1] = '\0';
182 WARNING ("mbmon: Message from mbmon has been truncated.");
184 else if (buffer_fill == 0)
186 WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
187 "Buffer: `%s'", buffer);
196 static int mbmon_config (const char *key, const char *value)
198 if (strcasecmp (key, "host") == 0)
200 if (mbmon_host != NULL)
202 mbmon_host = strdup (value);
204 else if (strcasecmp (key, "port") == 0)
206 if (mbmon_port != NULL)
208 mbmon_port = strdup (value);
218 static void mbmon_submit (const char *type, const char *type_instance,
221 value_list_t vl = VALUE_LIST_INIT;
223 vl.values = &(value_t) { .gauge = value };
225 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
226 sstrncpy (vl.plugin, "mbmon", sizeof (vl.plugin));
227 sstrncpy (vl.type, type, sizeof (vl.type));
228 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
230 plugin_dispatch_values (&vl);
231 } /* void mbmon_submit */
233 /* Trim trailing whitespace from a string. */
234 static void trim_spaces (char *s)
236 for (size_t l = strlen (s) - 1; (l > 0) && isspace ((int) s[l]); l--)
240 static int mbmon_read (void)
245 /* get data from daemon */
246 if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
250 while ((t = strchr (s, ':')) != NULL)
261 value = strtod (t, &nextc);
262 if ((*nextc != '\n') && (*nextc != '\0'))
264 ERROR ("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
268 if (strncmp (s, "TEMP", 4) == 0)
271 type = "temperature";
273 else if (strncmp (s, "FAN", 3) == 0)
278 else if (strncmp (s, "V", 1) == 0)
288 mbmon_submit (type, inst, value);
297 } /* void mbmon_read */
300 Register collectd plugin. */
301 void module_register (void)
303 plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
304 plugin_register_read ("mbmon", mbmon_read);
305 } /* void module_register */