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