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[] = {"Host", "Port", NULL};
39 static int config_keys_num = 2;
41 static char *mbmon_host = NULL;
42 static char *mbmon_port = NULL;
49 * Connect to the mbmon daemon and receive data.
52 * `buffer' The buffer where we put the received ascii string.
53 * `buffer_size' Size of the buffer
56 * >= 0 if ok, < 0 otherwise.
59 * Example of possible strings, as received from daemon:
70 * we need to create a new socket each time. Is there another way?
71 * Hm, maybe we can re-use the `sockaddr' structure? -octo
73 static int mbmon_query_daemon(char *buffer, int buffer_size) {
81 struct addrinfo *ai_list;
86 host = MBMON_DEF_HOST;
90 port = MBMON_DEF_PORT;
92 struct addrinfo ai_hints = {.ai_family = AF_UNSPEC,
93 .ai_flags = AI_ADDRCONFIG,
94 .ai_protocol = IPPROTO_TCP,
95 .ai_socktype = SOCK_STREAM};
97 if ((ai_return = getaddrinfo(host, port, &ai_hints, &ai_list)) != 0) {
99 ERROR("mbmon: getaddrinfo (%s, %s): %s", host, port,
100 (ai_return == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
101 : gai_strerror(ai_return));
106 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
107 ai_ptr = ai_ptr->ai_next) {
108 /* create our socket descriptor */
109 if ((fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype,
110 ai_ptr->ai_protocol)) < 0) {
112 ERROR("mbmon: socket: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
116 /* connect to the mbmon daemon */
117 if (connect(fd, (struct sockaddr *)ai_ptr->ai_addr, ai_ptr->ai_addrlen)) {
119 INFO("mbmon: connect (%s, %s): %s", host, port,
120 sstrerror(errno, errbuf, sizeof(errbuf)));
126 /* A socket could be opened and connecting succeeded. We're
131 freeaddrinfo(ai_list);
134 ERROR("mbmon: Could not connect to daemon.");
138 /* receive data from the mbmon daemon */
139 memset(buffer, '\0', buffer_size);
142 while ((status = read(fd, buffer + buffer_fill, buffer_size - buffer_fill)) !=
147 if ((errno == EAGAIN) || (errno == EINTR))
150 ERROR("mbmon: Error reading from socket: %s",
151 sstrerror(errno, errbuf, sizeof(errbuf)));
155 buffer_fill += status;
157 if (buffer_fill >= buffer_size)
161 if (buffer_fill >= buffer_size) {
162 buffer[buffer_size - 1] = '\0';
163 WARNING("mbmon: Message from mbmon has been truncated.");
164 } else if (buffer_fill == 0) {
165 WARNING("mbmon: Peer has unexpectedly shut down the socket. "
176 static int mbmon_config(const char *key, const char *value) {
177 if (strcasecmp(key, "host") == 0) {
178 if (mbmon_host != NULL)
180 mbmon_host = strdup(value);
181 } else if (strcasecmp(key, "port") == 0) {
182 if (mbmon_port != NULL)
184 mbmon_port = strdup(value);
192 static void mbmon_submit(const char *type, const char *type_instance,
194 value_list_t vl = VALUE_LIST_INIT;
196 vl.values = &(value_t){.gauge = value};
198 sstrncpy(vl.plugin, "mbmon", sizeof(vl.plugin));
199 sstrncpy(vl.type, type, sizeof(vl.type));
200 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
202 plugin_dispatch_values(&vl);
203 } /* void mbmon_submit */
205 /* Trim trailing whitespace from a string. */
206 static void trim_spaces(char *s) {
207 for (size_t l = strlen(s) - 1; (l > 0) && isspace((int)s[l]); l--)
211 static int mbmon_read(void) {
215 /* get data from daemon */
216 if (mbmon_query_daemon(buf, sizeof(buf)) < 0)
220 while ((t = strchr(s, ':')) != NULL) {
230 value = strtod(t, &nextc);
231 if ((*nextc != '\n') && (*nextc != '\0')) {
232 ERROR("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
236 if (strncmp(s, "TEMP", 4) == 0) {
238 type = "temperature";
239 } else if (strncmp(s, "FAN", 3) == 0) {
242 } else if (strncmp(s, "V", 1) == 0) {
249 mbmon_submit(type, inst, value);
258 } /* void mbmon_read */
261 Register collectd plugin. */
262 void module_register(void) {
263 plugin_register_config("mbmon", mbmon_config, config_keys, config_keys_num);
264 plugin_register_read("mbmon", mbmon_read);
265 } /* void module_register */