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