63330524878de64eaac772cb718f458ea25b356b
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 #if HAVE_NETDB_H && HAVE_SYS_SOCKET_H && HAVE_NETINET_IN_H && HAVE_NETINET_TCP_H
31 # include <netdb.h>
32 # include <sys/socket.h>
33 # include <netinet/in.h>
34 # include <netinet/tcp.h>
35 # define MBMON_HAVE_READ 1
36 #else
37 # define MBMON_HAVE_READ 0
38 #endif
40 #define MBMON_DEF_HOST "127.0.0.1"
41 #define MBMON_DEF_PORT "411" /* the default for Debian */
43 static data_source_t data_source_fanspeed[1] =
44 {
45 {"value", DS_TYPE_GAUGE, 0, NAN}
46 };
48 static data_set_t fanspeed_ds =
49 {
50 "fanspeed", 1, data_source_fanspeed
51 };
53 static data_source_t data_source_temperature[1] =
54 {
55 {"value", DS_TYPE_GAUGE, -273.15, NAN}
56 };
58 static data_set_t temperature_ds =
59 {
60 "temperature", 1, data_source_temperature
61 };
63 static data_source_t data_source_voltage[1] =
64 {
65 {"voltage", DS_TYPE_GAUGE, NAN, NAN}
66 };
68 static data_set_t voltage_ds =
69 {
70 "voltage", 1, data_source_voltage
71 };
73 static const char *config_keys[] =
74 {
75 "Host",
76 "Port",
77 NULL
78 };
79 static int config_keys_num = 2;
81 #if MBMON_HAVE_READ
82 static char *mbmon_host = NULL;
83 static char *mbmon_port = NULL;
85 /*
86 * NAME
87 * mbmon_query_daemon
88 *
89 * DESCRIPTION
90 * Connect to the mbmon daemon and receive data.
91 *
92 * ARGUMENTS:
93 * `buffer' The buffer where we put the received ascii string.
94 * `buffer_size' Size of the buffer
95 *
96 * RETURN VALUE:
97 * >= 0 if ok, < 0 otherwise.
98 *
99 * NOTES:
100 * Example of possible strings, as received from daemon:
101 * TEMP0 : 27.0
102 * TEMP1 : 31.0
103 * TEMP2 : 29.5
104 * FAN0 : 4411
105 * FAN1 : 4470
106 * FAN2 : 4963
107 * VC0 : +1.68
108 * VC1 : +1.73
109 *
110 * FIXME:
111 * we need to create a new socket each time. Is there another way?
112 * Hm, maybe we can re-use the `sockaddr' structure? -octo
113 */
114 static int mbmon_query_daemon (char *buffer, int buffer_size)
115 {
116 int fd;
117 ssize_t status;
118 int buffer_fill;
120 const char *host;
121 const char *port;
123 struct addrinfo ai_hints;
124 struct addrinfo *ai_list, *ai_ptr;
125 int ai_return;
127 memset (&ai_hints, '\0', sizeof (ai_hints));
128 ai_hints.ai_flags = AI_ADDRCONFIG;
129 ai_hints.ai_family = PF_UNSPEC;
130 ai_hints.ai_socktype = SOCK_STREAM;
131 ai_hints.ai_protocol = IPPROTO_TCP;
133 host = mbmon_host;
134 if (host == NULL)
135 host = MBMON_DEF_HOST;
137 port = mbmon_port;
138 if (port == NULL)
139 port = MBMON_DEF_PORT;
141 if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
142 {
143 syslog (LOG_ERR, "mbmon: getaddrinfo (%s, %s): %s",
144 host, port,
145 ai_return == EAI_SYSTEM ? strerror (errno) : gai_strerror (ai_return));
146 return (-1);
147 }
149 fd = -1;
150 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
151 {
152 /* create our socket descriptor */
153 if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
154 {
155 syslog (LOG_ERR, "mbmon: socket: %s",
156 strerror (errno));
157 continue;
158 }
160 /* connect to the mbmon daemon */
161 if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
162 {
163 DBG ("mbmon: connect (%s, %s): %s", host, port,
164 strerror (errno));
165 close (fd);
166 fd = -1;
167 continue;
168 }
170 /* A socket could be opened and connecting succeeded. We're
171 * done. */
172 break;
173 }
175 freeaddrinfo (ai_list);
177 if (fd < 0)
178 {
179 syslog (LOG_ERR, "mbmon: Could not connect to daemon.");
180 return (-1);
181 }
183 /* receive data from the mbmon daemon */
184 memset (buffer, '\0', buffer_size);
186 buffer_fill = 0;
187 while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
188 {
189 if (status == -1)
190 {
191 if ((errno == EAGAIN) || (errno == EINTR))
192 continue;
194 syslog (LOG_ERR, "mbmon: Error reading from socket: %s",
195 strerror (errno));
196 close (fd);
197 return (-1);
198 }
199 buffer_fill += status;
201 if (buffer_fill >= buffer_size)
202 break;
203 }
205 if (buffer_fill >= buffer_size)
206 {
207 buffer[buffer_size - 1] = '\0';
208 syslog (LOG_WARNING, "mbmon: Message from mbmon has been truncated.");
209 }
210 else if (buffer_fill == 0)
211 {
212 syslog (LOG_WARNING, "mbmon: Peer has unexpectedly shut down the socket. "
213 "Buffer: `%s'", buffer);
214 close (fd);
215 return (-1);
216 }
218 close (fd);
219 return (0);
220 }
222 static int mbmon_config (const char *key, const char *value)
223 {
224 if (strcasecmp (key, "host") == 0)
225 {
226 if (mbmon_host != NULL)
227 free (mbmon_host);
228 mbmon_host = strdup (value);
229 }
230 else if (strcasecmp (key, "port") == 0)
231 {
232 if (mbmon_port != NULL)
233 free (mbmon_port);
234 mbmon_port = strdup (value);
235 }
236 else
237 {
238 return (-1);
239 }
241 return (0);
242 }
244 static void mbmon_submit (const char *type, const char *type_instance,
245 double value)
246 {
247 value_t values[1];
248 value_list_t vl = VALUE_LIST_INIT;
250 values[0].gauge = value;
252 vl.values = values;
253 vl.values_len = 1;
254 vl.time = time (NULL);
255 strcpy (vl.host, hostname);
256 strcpy (vl.plugin, "mbmon");
257 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
259 plugin_dispatch_values (type, &vl);
260 } /* void mbmon_submit */
262 /* Trim trailing whitespace from a string. */
263 static void trim_spaces (char *s)
264 {
265 size_t l;
267 for (l = strlen (s) - 1; (l > 0) && isspace (s[l]); l--)
268 s[l] = '\0';
269 }
271 static int mbmon_read (void)
272 {
273 char buf[1024];
274 char *s, *t;
276 /* get data from daemon */
277 if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
278 return (-1);
280 s = buf;
281 while ((t = strchr (s, ':')) != NULL)
282 {
283 double value;
284 char *nextc;
286 char *type;
287 char *inst;
289 *t++ = '\0';
290 trim_spaces (s);
292 value = strtod (t, &nextc);
293 if ((*nextc != '\n') && (*nextc != '\0'))
294 {
295 syslog (LOG_ERR, "mbmon: value for `%s' contains invalid characters: `%s'", s, t);
296 break;
297 }
299 if (strncmp (s, "TEMP", 4) == 0)
300 {
301 inst = s + 4;
302 type = "mbmon_temperature";
303 }
304 else if (strncmp (s, "FAN", 3) == 0)
305 {
306 inst = s + 3;
307 type = "mbmon_fanspeed";
308 }
309 else if (strncmp (s, "V", 1) == 0)
310 {
311 inst = s + 1;
312 type = "mbmon_voltage";
313 }
314 else
315 {
316 continue;
317 }
319 mbmon_submit (type, inst, value);
321 if (*nextc == '\0')
322 break;
324 s = nextc + 1;
325 }
327 return (0);
328 } /* void mbmon_read */
329 #endif /* MBMON_HAVE_READ */
331 /* module_register
332 Register collectd plugin. */
333 void module_register (void)
334 {
335 plugin_register_data_set (&fanspeed_ds);
336 plugin_register_data_set (&temperature_ds);
337 plugin_register_data_set (&voltage_ds);
339 #if MBMON_HAVE_READ
340 plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
341 plugin_register_read ("mbmon", mbmon_read);
342 #endif /* MBMON_HAVE_READ */
343 }