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) {
98 ERROR("mbmon: getaddrinfo (%s, %s): %s", host, port,
99 (ai_return == EAI_SYSTEM) ? STRERRNO : gai_strerror(ai_return));
104 for (struct addrinfo *ai_ptr = ai_list; ai_ptr != NULL;
105 ai_ptr = ai_ptr->ai_next) {
106 /* create our socket descriptor */
107 if ((fd = socket(ai_ptr->ai_family, ai_ptr->ai_socktype,
108 ai_ptr->ai_protocol)) < 0) {
109 ERROR("mbmon: socket: %s", STRERRNO);
113 /* connect to the mbmon daemon */
114 if (connect(fd, (struct sockaddr *)ai_ptr->ai_addr, ai_ptr->ai_addrlen)) {
115 INFO("mbmon: connect (%s, %s): %s", host, port, STRERRNO);
121 /* A socket could be opened and connecting succeeded. We're
126 freeaddrinfo(ai_list);
129 ERROR("mbmon: Could not connect to daemon.");
133 /* receive data from the mbmon daemon */
134 memset(buffer, '\0', buffer_size);
137 while ((status = read(fd, buffer + buffer_fill, buffer_size - buffer_fill)) !=
141 if ((errno == EAGAIN) || (errno == EINTR))
144 ERROR("mbmon: Error reading from socket: %s", STRERRNO);
148 buffer_fill += status;
150 if (buffer_fill >= buffer_size)
154 if (buffer_fill >= buffer_size) {
155 buffer[buffer_size - 1] = '\0';
156 WARNING("mbmon: Message from mbmon has been truncated.");
157 } else if (buffer_fill == 0) {
158 WARNING("mbmon: Peer has unexpectedly shut down the socket. "
169 static int mbmon_config(const char *key, const char *value) {
170 if (strcasecmp(key, "host") == 0) {
171 if (mbmon_host != NULL)
173 mbmon_host = strdup(value);
174 } else if (strcasecmp(key, "port") == 0) {
175 if (mbmon_port != NULL)
177 mbmon_port = strdup(value);
185 static void mbmon_submit(const char *type, const char *type_instance,
187 value_list_t vl = VALUE_LIST_INIT;
189 vl.values = &(value_t){.gauge = value};
191 sstrncpy(vl.plugin, "mbmon", sizeof(vl.plugin));
192 sstrncpy(vl.type, type, sizeof(vl.type));
193 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
195 plugin_dispatch_values(&vl);
196 } /* void mbmon_submit */
198 /* Trim trailing whitespace from a string. */
199 static void trim_spaces(char *s) {
200 for (size_t l = strlen(s) - 1; (l > 0) && isspace((int)s[l]); l--)
204 static int mbmon_read(void) {
208 /* get data from daemon */
209 if (mbmon_query_daemon(buf, sizeof(buf)) < 0)
213 while ((t = strchr(s, ':')) != NULL) {
223 value = strtod(t, &nextc);
224 if ((*nextc != '\n') && (*nextc != '\0')) {
225 ERROR("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
229 if (strncmp(s, "TEMP", 4) == 0) {
231 type = "temperature";
232 } else if (strncmp(s, "FAN", 3) == 0) {
235 } else if (strncmp(s, "V", 1) == 0) {
242 mbmon_submit(type, inst, value);
251 } /* void mbmon_read */
254 Register collectd plugin. */
255 void module_register(void) {
256 plugin_register_config("mbmon", mbmon_config, config_keys, config_keys_num);
257 plugin_register_read("mbmon", mbmon_read);
258 } /* void module_register */