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