2e26aa1fc0083a004e7fe47e65461fd1b5d5cf0a
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_list, *ai_ptr;
89 int ai_return;
91 host = mbmon_host;
92 if (host == NULL)
93 host = MBMON_DEF_HOST;
95 port = mbmon_port;
96 if (port == NULL)
97 port = MBMON_DEF_PORT;
99 struct addrinfo ai_hints = {
100 .ai_family = AF_UNSPEC,
101 .ai_flags = AI_ADDRCONFIG,
102 .ai_protocol = IPPROTO_TCP,
103 .ai_socktype = SOCK_STREAM
104 };
106 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
107 {
108 char errbuf[1024];
109 ERROR ("mbmon: getaddrinfo (%s, %s): %s",
110 host, port,
111 (ai_return == EAI_SYSTEM)
112 ? sstrerror (errno, errbuf, sizeof (errbuf))
113 : gai_strerror (ai_return));
114 return (-1);
115 }
117 fd = -1;
118 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
119 {
120 /* create our socket descriptor */
121 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
122 {
123 char errbuf[1024];
124 ERROR ("mbmon: socket: %s",
125 sstrerror (errno, errbuf,
126 sizeof (errbuf)));
127 continue;
128 }
130 /* connect to the mbmon daemon */
131 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
132 {
133 char errbuf[1024];
134 INFO ("mbmon: connect (%s, %s): %s", host, port,
135 sstrerror (errno, errbuf,
136 sizeof (errbuf)));
137 close (fd);
138 fd = -1;
139 continue;
140 }
142 /* A socket could be opened and connecting succeeded. We're
143 * done. */
144 break;
145 }
147 freeaddrinfo (ai_list);
149 if (fd < 0)
150 {
151 ERROR ("mbmon: Could not connect to daemon.");
152 return (-1);
153 }
155 /* receive data from the mbmon daemon */
156 memset (buffer, '\0', buffer_size);
158 buffer_fill = 0;
159 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
160 {
161 if (status == -1)
162 {
163 char errbuf[1024];
165 if ((errno == EAGAIN) || (errno == EINTR))
166 continue;
168 ERROR ("mbmon: Error reading from socket: %s",
169 sstrerror (errno, errbuf,
170 sizeof (errbuf)));
171 close (fd);
172 return (-1);
173 }
174 buffer_fill += status;
176 if (buffer_fill >= buffer_size)
177 break;
178 }
180 if (buffer_fill >= buffer_size)
181 {
182 buffer[buffer_size - 1] = '\0';
183 WARNING ("mbmon: Message from mbmon has been truncated.");
184 }
185 else if (buffer_fill == 0)
186 {
187 WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
188 "Buffer: `%s'", buffer);
189 close (fd);
190 return (-1);
191 }
193 close (fd);
194 return (0);
195 }
197 static int mbmon_config (const char *key, const char *value)
198 {
199 if (strcasecmp (key, "host") == 0)
200 {
201 if (mbmon_host != NULL)
202 free (mbmon_host);
203 mbmon_host = strdup (value);
204 }
205 else if (strcasecmp (key, "port") == 0)
206 {
207 if (mbmon_port != NULL)
208 free (mbmon_port);
209 mbmon_port = strdup (value);
210 }
211 else
212 {
213 return (-1);
214 }
216 return (0);
217 }
219 static void mbmon_submit (const char *type, const char *type_instance,
220 double value)
221 {
222 value_t values[1];
223 value_list_t vl = VALUE_LIST_INIT;
225 values[0].gauge = value;
227 vl.values = values;
228 vl.values_len = 1;
229 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
230 sstrncpy (vl.plugin, "mbmon", sizeof (vl.plugin));
231 sstrncpy (vl.type, type, sizeof (vl.type));
232 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
234 plugin_dispatch_values (&vl);
235 } /* void mbmon_submit */
237 /* Trim trailing whitespace from a string. */
238 static void trim_spaces (char *s)
239 {
240 size_t l;
242 for (l = strlen (s) - 1; (l > 0) && isspace ((int) s[l]); l--)
243 s[l] = '\0';
244 }
246 static int mbmon_read (void)
247 {
248 char buf[1024];
249 char *s, *t;
251 /* get data from daemon */
252 if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
253 return (-1);
255 s = buf;
256 while ((t = strchr (s, ':')) != NULL)
257 {
258 double value;
259 char *nextc;
261 const char *type;
262 const char *inst;
264 *t++ = '\0';
265 trim_spaces (s);
267 value = strtod (t, &nextc);
268 if ((*nextc != '\n') && (*nextc != '\0'))
269 {
270 ERROR ("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
271 break;
272 }
274 if (strncmp (s, "TEMP", 4) == 0)
275 {
276 inst = s + 4;
277 type = "temperature";
278 }
279 else if (strncmp (s, "FAN", 3) == 0)
280 {
281 inst = s + 3;
282 type = "fanspeed";
283 }
284 else if (strncmp (s, "V", 1) == 0)
285 {
286 inst = s + 1;
287 type = "voltage";
288 }
289 else
290 {
291 continue;
292 }
294 mbmon_submit (type, inst, value);
296 if (*nextc == '\0')
297 break;
299 s = nextc + 1;
300 }
302 return (0);
303 } /* void mbmon_read */
305 /* module_register
306 Register collectd plugin. */
307 void module_register (void)
308 {
309 plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
310 plugin_register_read ("mbmon", mbmon_read);
311 } /* void module_register */