1 /**
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.
6 *
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.
11 *
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.
16 *
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
20 *
21 * Authors:
22 * Flavio Stanchina <flavio at stanchina.net>
23 * Florian Forster <octo at collectd.org>
24 **/
26 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
31 #include <netdb.h>
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;
44 /*
45 * NAME
46 * mbmon_query_daemon
47 *
48 * DESCRIPTION
49 * Connect to the mbmon daemon and receive data.
50 *
51 * ARGUMENTS:
52 * `buffer' The buffer where we put the received ascii string.
53 * `buffer_size' Size of the buffer
54 *
55 * RETURN VALUE:
56 * >= 0 if ok, < 0 otherwise.
57 *
58 * NOTES:
59 * Example of possible strings, as received from daemon:
60 * TEMP0 : 27.0
61 * TEMP1 : 31.0
62 * TEMP2 : 29.5
63 * FAN0 : 4411
64 * FAN1 : 4470
65 * FAN2 : 4963
66 * VC0 : +1.68
67 * VC1 : +1.73
68 *
69 * FIXME:
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
72 */
73 static int mbmon_query_daemon(char *buffer, int buffer_size) {
74 int fd;
75 ssize_t status;
76 int buffer_fill;
78 const char *host;
79 const char *port;
81 struct addrinfo *ai_list;
82 int ai_return;
84 host = mbmon_host;
85 if (host == NULL)
86 host = MBMON_DEF_HOST;
88 port = mbmon_port;
89 if (port == NULL)
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 char errbuf[1024];
99 ERROR("mbmon: getaddrinfo (%s, %s): %s", host, port,
100 (ai_return == EAI_SYSTEM) ? sstrerror(errno, errbuf, sizeof(errbuf))
101 : gai_strerror(ai_return));
102 return (-1);
103 }
105 fd = -1;
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) {
111 char errbuf[1024];
112 ERROR("mbmon: socket: %s", sstrerror(errno, errbuf, sizeof(errbuf)));
113 continue;
114 }
116 /* connect to the mbmon daemon */
117 if (connect(fd, (struct sockaddr *)ai_ptr->ai_addr, ai_ptr->ai_addrlen)) {
118 char errbuf[1024];
119 INFO("mbmon: connect (%s, %s): %s", host, port,
120 sstrerror(errno, errbuf, sizeof(errbuf)));
121 close(fd);
122 fd = -1;
123 continue;
124 }
126 /* A socket could be opened and connecting succeeded. We're
127 * done. */
128 break;
129 }
131 freeaddrinfo(ai_list);
133 if (fd < 0) {
134 ERROR("mbmon: Could not connect to daemon.");
135 return (-1);
136 }
138 /* receive data from the mbmon daemon */
139 memset(buffer, '\0', buffer_size);
141 buffer_fill = 0;
142 while ((status = read(fd, buffer + buffer_fill, buffer_size - buffer_fill)) !=
143 0) {
144 if (status == -1) {
145 char errbuf[1024];
147 if ((errno == EAGAIN) || (errno == EINTR))
148 continue;
150 ERROR("mbmon: Error reading from socket: %s",
151 sstrerror(errno, errbuf, sizeof(errbuf)));
152 close(fd);
153 return (-1);
154 }
155 buffer_fill += status;
157 if (buffer_fill >= buffer_size)
158 break;
159 }
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. "
166 "Buffer: `%s'",
167 buffer);
168 close(fd);
169 return (-1);
170 }
172 close(fd);
173 return (0);
174 }
176 static int mbmon_config(const char *key, const char *value) {
177 if (strcasecmp(key, "host") == 0) {
178 if (mbmon_host != NULL)
179 free(mbmon_host);
180 mbmon_host = strdup(value);
181 } else if (strcasecmp(key, "port") == 0) {
182 if (mbmon_port != NULL)
183 free(mbmon_port);
184 mbmon_port = strdup(value);
185 } else {
186 return (-1);
187 }
189 return (0);
190 }
192 static void mbmon_submit(const char *type, const char *type_instance,
193 double value) {
194 value_t values[1];
195 value_list_t vl = VALUE_LIST_INIT;
197 values[0].gauge = value;
199 vl.values = values;
200 vl.values_len = 1;
201 sstrncpy(vl.host, hostname_g, sizeof(vl.host));
202 sstrncpy(vl.plugin, "mbmon", sizeof(vl.plugin));
203 sstrncpy(vl.type, type, sizeof(vl.type));
204 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
206 plugin_dispatch_values(&vl);
207 } /* void mbmon_submit */
209 /* Trim trailing whitespace from a string. */
210 static void trim_spaces(char *s) {
211 for (size_t l = strlen(s) - 1; (l > 0) && isspace((int)s[l]); l--)
212 s[l] = '\0';
213 }
215 static int mbmon_read(void) {
216 char buf[1024];
217 char *s, *t;
219 /* get data from daemon */
220 if (mbmon_query_daemon(buf, sizeof(buf)) < 0)
221 return (-1);
223 s = buf;
224 while ((t = strchr(s, ':')) != NULL) {
225 double value;
226 char *nextc;
228 const char *type;
229 const char *inst;
231 *t++ = '\0';
232 trim_spaces(s);
234 value = strtod(t, &nextc);
235 if ((*nextc != '\n') && (*nextc != '\0')) {
236 ERROR("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
237 break;
238 }
240 if (strncmp(s, "TEMP", 4) == 0) {
241 inst = s + 4;
242 type = "temperature";
243 } else if (strncmp(s, "FAN", 3) == 0) {
244 inst = s + 3;
245 type = "fanspeed";
246 } else if (strncmp(s, "V", 1) == 0) {
247 inst = s + 1;
248 type = "voltage";
249 } else {
250 continue;
251 }
253 mbmon_submit(type, inst, value);
255 if (*nextc == '\0')
256 break;
258 s = nextc + 1;
259 }
261 return (0);
262 } /* void mbmon_read */
264 /* module_register
265 Register collectd plugin. */
266 void module_register(void) {
267 plugin_register_config("mbmon", mbmon_config, config_keys, config_keys_num);
268 plugin_register_read("mbmon", mbmon_read);
269 } /* void module_register */