1 /**
2 * collectd - src/mbmon.c
3 * Copyright (C) 2006 Flavio Stanchina
4 * Based on the hddtemp plugin.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Flavio Stanchina <flavio at stanchina.net>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
28 #include "utils_debug.h"
30 #define MODULE_NAME "mbmon"
32 #include <netdb.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
37 #define MBMON_DEF_HOST "127.0.0.1"
38 #define MBMON_DEF_PORT "411" /* the default for Debian */
40 /* BUFFER_SIZE
41 Size of the buffer we use to receive from the mbmon daemon. */
42 #define BUFFER_SIZE 1024
44 static char *filename_temperature = "mbmon/temperature-%s.rrd";
45 static char *filename_fanspeed = "mbmon/fanspeed-%s.rrd";
46 static char *filename_voltage = "mbmon/voltage-%s.rrd";
48 /* temperature and fan sensors */
49 static char *ds_def[] =
50 {
51 "DS:value:GAUGE:"COLLECTD_HEARTBEAT":U:U",
52 NULL
53 };
54 static int ds_num = 1;
56 /* voltage sensors */
57 static char *voltage_ds_def[] =
58 {
59 "DS:voltage:GAUGE:"COLLECTD_HEARTBEAT":U:U",
60 NULL
61 };
62 static int voltage_ds_num = 1;
64 static char *config_keys[] =
65 {
66 "Host",
67 "Port",
68 NULL
69 };
70 static int config_keys_num = 2;
72 static char *mbmon_host = NULL;
73 static char *mbmon_port = NULL;
75 /*
76 * NAME
77 * mbmon_query_daemon
78 *
79 * DESCRIPTION
80 * Connect to the mbmon daemon and receive data.
81 *
82 * ARGUMENTS:
83 * `buffer' The buffer where we put the received ascii string.
84 * `buffer_size' Size of the buffer
85 *
86 * RETURN VALUE:
87 * >= 0 if ok, < 0 otherwise.
88 *
89 * NOTES:
90 * Example of possible strings, as received from daemon:
91 * TEMP0 : 27.0
92 * TEMP1 : 31.0
93 * TEMP2 : 29.5
94 * FAN0 : 4411
95 * FAN1 : 4470
96 * FAN2 : 4963
97 * VC0 : +1.68
98 * VC1 : +1.73
99 *
100 * FIXME:
101 * we need to create a new socket each time. Is there another way?
102 * Hm, maybe we can re-use the `sockaddr' structure? -octo
103 */
104 static int mbmon_query_daemon (char *buffer, int buffer_size)
105 {
106 int fd;
107 ssize_t status;
108 int buffer_fill;
110 const char *host;
111 const char *port;
113 struct addrinfo ai_hints;
114 struct addrinfo *ai_list, *ai_ptr;
115 int ai_return;
117 memset (&ai_hints, '\0', sizeof (ai_hints));
118 ai_hints.ai_flags = AI_ADDRCONFIG;
119 ai_hints.ai_family = PF_UNSPEC;
120 ai_hints.ai_socktype = SOCK_STREAM;
121 ai_hints.ai_protocol = IPPROTO_TCP;
123 host = mbmon_host;
124 if (host == NULL)
125 host = MBMON_DEF_HOST;
127 port = mbmon_port;
128 if (port == NULL)
129 port = MBMON_DEF_PORT;
131 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
132 {
133 syslog (LOG_ERR, "mbmon: getaddrinfo (%s, %s): %s",
134 host, port,
135 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
136 return (-1);
137 }
139 fd = -1;
140 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
141 {
142 /* create our socket descriptor */
143 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
144 {
145 syslog (LOG_ERR, "mbmon: socket: %s",
146 strerror (errno));
147 continue;
148 }
150 /* connect to the mbmon daemon */
151 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
152 {
153 DBG ("mbmon: connect (%s, %s): %s", host, port,
154 strerror (errno));
155 close (fd);
156 fd = -1;
157 continue;
158 }
160 /* A socket could be opened and connecting succeeded. We're
161 * done. */
162 break;
163 }
165 freeaddrinfo (ai_list);
167 if (fd < 0)
168 {
169 syslog (LOG_ERR, "mbmon: Could not connect to daemon.");
170 return (-1);
171 }
173 /* receive data from the mbmon daemon */
174 memset (buffer, '\0', buffer_size);
176 buffer_fill = 0;
177 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
178 {
179 if (status == -1)
180 {
181 if ((errno == EAGAIN) || (errno == EINTR))
182 continue;
184 syslog (LOG_ERR, "mbmon: Error reading from socket: %s",
185 strerror (errno));
186 close (fd);
187 return (-1);
188 }
189 buffer_fill += status;
191 if (buffer_fill >= buffer_size)
192 break;
193 }
195 if (buffer_fill >= buffer_size)
196 {
197 buffer[buffer_size - 1] = '\0';
198 syslog (LOG_WARNING, "mbmon: Message from mbmon has been truncated.");
199 }
200 else if (buffer_fill == 0)
201 {
202 syslog (LOG_WARNING, "mbmon: Peer has unexpectedly shut down the socket. "
203 "Buffer: `%s'", buffer);
204 close (fd);
205 return (-1);
206 }
208 close (fd);
209 return (0);
210 }
212 static int mbmon_config (char *key, char *value)
213 {
214 if (strcasecmp (key, "host") == 0)
215 {
216 if (mbmon_host != NULL)
217 free (mbmon_host);
218 mbmon_host = strdup (value);
219 }
220 else if (strcasecmp (key, "port") == 0)
221 {
222 if (mbmon_port != NULL)
223 free (mbmon_port);
224 mbmon_port = strdup (value);
225 }
226 else
227 {
228 return (-1);
229 }
231 return (0);
232 }
234 static void mbmon_init (void)
235 {
236 return;
237 }
239 static void mbmon_write_temperature (char *host, char *inst, char *val)
240 {
241 char filename[BUFFER_SIZE];
242 int status;
244 /* construct filename */
245 status = snprintf (filename, BUFFER_SIZE, filename_temperature, inst);
246 if ((status < 1) || (status >= BUFFER_SIZE))
247 return;
249 rrd_update_file (host, filename, val, ds_def, ds_num);
250 }
252 static void mbmon_write_fanspeed (char *host, char *inst, char *val)
253 {
254 char filename[BUFFER_SIZE];
255 int status;
257 /* construct filename */
258 status = snprintf (filename, BUFFER_SIZE, filename_fanspeed, inst);
259 if ((status < 1) || (status >= BUFFER_SIZE))
260 return;
262 rrd_update_file (host, filename, val, ds_def, ds_num);
263 }
265 static void mbmon_write_voltage (char *host, char *inst, char *val)
266 {
267 char filename[BUFFER_SIZE];
268 int status;
270 /* construct filename */
271 status = snprintf (filename, BUFFER_SIZE, filename_voltage, inst);
272 if ((status < 1) || (status >= BUFFER_SIZE))
273 return;
275 rrd_update_file (host, filename, val, voltage_ds_def, voltage_ds_num);
276 }
278 static void mbmon_submit (char *type, char *inst, double value)
279 {
280 char buf[BUFFER_SIZE];
282 if (snprintf (buf, BUFFER_SIZE, "%u:%.3f", (unsigned int) curtime, value)
283 >= BUFFER_SIZE)
284 return;
286 plugin_submit (type, inst, buf);
287 }
289 /* Trim trailing whitespace from a string. */
290 static void trim_spaces (char *s)
291 {
292 size_t l;
294 for (l = strlen (s) - 1; (l > 0) && isspace (s[l]); l--)
295 s[l] = '\0';
296 }
298 static void mbmon_read (void)
299 {
300 char buf[BUFFER_SIZE];
301 char *s, *t;
303 static int wait_time = 1;
304 static int wait_left = 0;
306 if (wait_left >= 10)
307 {
308 wait_left -= 10;
309 return;
310 }
312 /* get data from daemon */
313 if (mbmon_query_daemon (buf, BUFFER_SIZE) < 0)
314 {
315 /* This limit is reached in log2(86400) =~ 17 steps. Since
316 * there is a 2^n seconds wait between each step it will need
317 * roughly one day to reach this limit. -octo */
319 wait_time *= 2;
320 if (wait_time > 86400)
321 wait_time = 86400;
323 wait_left = wait_time;
325 return;
326 }
327 else
328 {
329 wait_time = 1;
330 wait_left = 0;
331 }
333 s = buf;
334 while ((t = strchr (s, ':')) != NULL)
335 {
336 double value;
337 char *nextc;
339 char *type;
340 char *inst;
342 *t++ = '\0';
343 trim_spaces (s);
345 value = strtod (t, &nextc);
346 if ((*nextc != '\n') && (*nextc != '\0'))
347 {
348 syslog (LOG_ERR, "mbmon: value for `%s' contains invalid characters: `%s'", s, t);
349 break;
350 }
352 if (strncmp (s, "TEMP", 4) == 0)
353 {
354 inst = s + 4;
355 type = "mbmon_temperature";
356 }
357 else if (strncmp (s, "FAN", 3) == 0)
358 {
359 inst = s + 3;
360 type = "mbmon_fanspeed";
361 }
362 else if (strncmp (s, "V", 1) == 0)
363 {
364 inst = s + 1;
365 type = "mbmon_voltage";
366 }
367 else
368 {
369 continue;
370 }
372 mbmon_submit (type, inst, value);
374 if (*nextc == '\0')
375 break;
377 s = nextc + 1;
378 }
379 } /* void mbmon_read */
381 /* module_register
382 Register collectd plugin. */
383 void module_register (void)
384 {
385 plugin_register (MODULE_NAME, mbmon_init, mbmon_read, NULL);
386 plugin_register ("mbmon_temperature", NULL, NULL, mbmon_write_temperature);
387 plugin_register ("mbmon_fanspeed", NULL, NULL, mbmon_write_fanspeed);
388 plugin_register ("mbmon_voltage", NULL, NULL, mbmon_write_voltage);
389 cf_register (MODULE_NAME, mbmon_config, config_keys, config_keys_num);
390 }
392 #undef MODULE_NAME