d16845e3fb4ff070c5f8a7766c44a2737e846218
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"
30 #include "configfile.h"
32 #include <netdb.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
36 #define MBMON_DEF_HOST "127.0.0.1"
37 #define MBMON_DEF_PORT "411" /* the default for Debian */
39 static const char *config_keys[] =
40 {
41 "Host",
42 "Port",
43 NULL
44 };
45 static int config_keys_num = 2;
47 static char *mbmon_host = NULL;
48 static char *mbmon_port = NULL;
50 /*
51 * NAME
52 * mbmon_query_daemon
53 *
54 * DESCRIPTION
55 * Connect to the mbmon daemon and receive data.
56 *
57 * ARGUMENTS:
58 * `buffer' The buffer where we put the received ascii string.
59 * `buffer_size' Size of the buffer
60 *
61 * RETURN VALUE:
62 * >= 0 if ok, < 0 otherwise.
63 *
64 * NOTES:
65 * Example of possible strings, as received from daemon:
66 * TEMP0 : 27.0
67 * TEMP1 : 31.0
68 * TEMP2 : 29.5
69 * FAN0 : 4411
70 * FAN1 : 4470
71 * FAN2 : 4963
72 * VC0 : +1.68
73 * VC1 : +1.73
74 *
75 * FIXME:
76 * we need to create a new socket each time. Is there another way?
77 * Hm, maybe we can re-use the `sockaddr' structure? -octo
78 */
79 static int mbmon_query_daemon (char *buffer, int buffer_size)
80 {
81 int fd;
82 ssize_t status;
83 int buffer_fill;
85 const char *host;
86 const char *port;
88 struct addrinfo ai_hints = { 0 };
89 struct addrinfo *ai_list, *ai_ptr;
90 int ai_return;
92 #ifdef AI_ADDRCONFIG
93 ai_hints.ai_flags |= AI_ADDRCONFIG;
94 #endif
95 ai_hints.ai_family = PF_UNSPEC;
96 ai_hints.ai_socktype = SOCK_STREAM;
97 ai_hints.ai_protocol = IPPROTO_TCP;
99 host = mbmon_host;
100 if (host == NULL)
101 host = MBMON_DEF_HOST;
103 port = mbmon_port;
104 if (port == NULL)
105 port = MBMON_DEF_PORT;
107 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
108 {
109 char errbuf[1024];
110 ERROR ("mbmon: getaddrinfo (%s, %s): %s",
111 host, port,
112 (ai_return == EAI_SYSTEM)
113 ? sstrerror (errno, errbuf, sizeof (errbuf))
114 : gai_strerror (ai_return));
115 return (-1);
116 }
118 fd = -1;
119 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
120 {
121 /* create our socket descriptor */
122 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
123 {
124 char errbuf[1024];
125 ERROR ("mbmon: socket: %s",
126 sstrerror (errno, errbuf,
127 sizeof (errbuf)));
128 continue;
129 }
131 /* connect to the mbmon daemon */
132 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
133 {
134 char errbuf[1024];
135 INFO ("mbmon: connect (%s, %s): %s", host, port,
136 sstrerror (errno, errbuf,
137 sizeof (errbuf)));
138 close (fd);
139 fd = -1;
140 continue;
141 }
143 /* A socket could be opened and connecting succeeded. We're
144 * done. */
145 break;
146 }
148 freeaddrinfo (ai_list);
150 if (fd < 0)
151 {
152 ERROR ("mbmon: Could not connect to daemon.");
153 return (-1);
154 }
156 /* receive data from the mbmon daemon */
157 memset (buffer, '\0', buffer_size);
159 buffer_fill = 0;
160 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
161 {
162 if (status == -1)
163 {
164 char errbuf[1024];
166 if ((errno == EAGAIN) || (errno == EINTR))
167 continue;
169 ERROR ("mbmon: Error reading from socket: %s",
170 sstrerror (errno, errbuf,
171 sizeof (errbuf)));
172 close (fd);
173 return (-1);
174 }
175 buffer_fill += status;
177 if (buffer_fill >= buffer_size)
178 break;
179 }
181 if (buffer_fill >= buffer_size)
182 {
183 buffer[buffer_size - 1] = '\0';
184 WARNING ("mbmon: Message from mbmon has been truncated.");
185 }
186 else if (buffer_fill == 0)
187 {
188 WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
189 "Buffer: `%s'", buffer);
190 close (fd);
191 return (-1);
192 }
194 close (fd);
195 return (0);
196 }
198 static int mbmon_config (const char *key, const char *value)
199 {
200 if (strcasecmp (key, "host") == 0)
201 {
202 if (mbmon_host != NULL)
203 free (mbmon_host);
204 mbmon_host = strdup (value);
205 }
206 else if (strcasecmp (key, "port") == 0)
207 {
208 if (mbmon_port != NULL)
209 free (mbmon_port);
210 mbmon_port = strdup (value);
211 }
212 else
213 {
214 return (-1);
215 }
217 return (0);
218 }
220 static void mbmon_submit (const char *type, const char *type_instance,
221 double value)
222 {
223 value_t values[1];
224 value_list_t vl = VALUE_LIST_INIT;
226 values[0].gauge = value;
228 vl.values = values;
229 vl.values_len = 1;
230 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
231 sstrncpy (vl.plugin, "mbmon", sizeof (vl.plugin));
232 sstrncpy (vl.type, type, sizeof (vl.type));
233 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
235 plugin_dispatch_values (&vl);
236 } /* void mbmon_submit */
238 /* Trim trailing whitespace from a string. */
239 static void trim_spaces (char *s)
240 {
241 size_t l;
243 for (l = strlen (s) - 1; (l > 0) && isspace ((int) s[l]); l--)
244 s[l] = '\0';
245 }
247 static int mbmon_read (void)
248 {
249 char buf[1024];
250 char *s, *t;
252 /* get data from daemon */
253 if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
254 return (-1);
256 s = buf;
257 while ((t = strchr (s, ':')) != NULL)
258 {
259 double value;
260 char *nextc;
262 const char *type;
263 const char *inst;
265 *t++ = '\0';
266 trim_spaces (s);
268 value = strtod (t, &nextc);
269 if ((*nextc != '\n') && (*nextc != '\0'))
270 {
271 ERROR ("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
272 break;
273 }
275 if (strncmp (s, "TEMP", 4) == 0)
276 {
277 inst = s + 4;
278 type = "temperature";
279 }
280 else if (strncmp (s, "FAN", 3) == 0)
281 {
282 inst = s + 3;
283 type = "fanspeed";
284 }
285 else if (strncmp (s, "V", 1) == 0)
286 {
287 inst = s + 1;
288 type = "voltage";
289 }
290 else
291 {
292 continue;
293 }
295 mbmon_submit (type, inst, value);
297 if (*nextc == '\0')
298 break;
300 s = nextc + 1;
301 }
303 return (0);
304 } /* void mbmon_read */
306 /* module_register
307 Register collectd plugin. */
308 void module_register (void)
309 {
310 plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
311 plugin_register_read ("mbmon", mbmon_read);
312 } /* void module_register */