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