forked from collectd/collectd
/
mbmon.c
314 lines (270 loc) · 6.63 KB
/
mbmon.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* collectd - src/mbmon.c
* Copyright (C) 2006 Flavio Stanchina
* Copyright (C) 2006-2007 Florian octo Forster
* Based on the hddtemp plugin.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Flavio Stanchina <flavio at stanchina.net>
* Florian Forster <octo at verplant.org>
**/
#include "collectd.h"
#include "common.h"
#include "plugin.h"
#include "configfile.h"
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#define MBMON_DEF_HOST "127.0.0.1"
#define MBMON_DEF_PORT "411" /* the default for Debian */
static const char *config_keys[] =
{
"Host",
"Port",
NULL
};
static int config_keys_num = 2;
static char *mbmon_host = NULL;
static char *mbmon_port = NULL;
/*
* NAME
* mbmon_query_daemon
*
* DESCRIPTION
* Connect to the mbmon daemon and receive data.
*
* ARGUMENTS:
* `buffer' The buffer where we put the received ascii string.
* `buffer_size' Size of the buffer
*
* RETURN VALUE:
* >= 0 if ok, < 0 otherwise.
*
* NOTES:
* Example of possible strings, as received from daemon:
* TEMP0 : 27.0
* TEMP1 : 31.0
* TEMP2 : 29.5
* FAN0 : 4411
* FAN1 : 4470
* FAN2 : 4963
* VC0 : +1.68
* VC1 : +1.73
*
* FIXME:
* we need to create a new socket each time. Is there another way?
* Hm, maybe we can re-use the `sockaddr' structure? -octo
*/
static int mbmon_query_daemon (char *buffer, int buffer_size)
{
int fd;
ssize_t status;
int buffer_fill;
const char *host;
const char *port;
struct addrinfo ai_hints;
struct addrinfo *ai_list, *ai_ptr;
int ai_return;
memset (&ai_hints, '\0', sizeof (ai_hints));
ai_hints.ai_flags = 0;
#ifdef AI_ADDRCONFIG
ai_hints.ai_flags |= AI_ADDRCONFIG;
#endif
ai_hints.ai_family = PF_UNSPEC;
ai_hints.ai_socktype = SOCK_STREAM;
ai_hints.ai_protocol = IPPROTO_TCP;
host = mbmon_host;
if (host == NULL)
host = MBMON_DEF_HOST;
port = mbmon_port;
if (port == NULL)
port = MBMON_DEF_PORT;
if ((ai_return = getaddrinfo (host, port, &ai_hints, &ai_list)) != 0)
{
char errbuf[1024];
ERROR ("mbmon: getaddrinfo (%s, %s): %s",
host, port,
(ai_return == EAI_SYSTEM)
? sstrerror (errno, errbuf, sizeof (errbuf))
: gai_strerror (ai_return));
return (-1);
}
fd = -1;
for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
{
/* create our socket descriptor */
if ((fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol)) < 0)
{
char errbuf[1024];
ERROR ("mbmon: socket: %s",
sstrerror (errno, errbuf,
sizeof (errbuf)));
continue;
}
/* connect to the mbmon daemon */
if (connect (fd, (struct sockaddr *) ai_ptr->ai_addr, ai_ptr->ai_addrlen))
{
char errbuf[1024];
INFO ("mbmon: connect (%s, %s): %s", host, port,
sstrerror (errno, errbuf,
sizeof (errbuf)));
close (fd);
fd = -1;
continue;
}
/* A socket could be opened and connecting succeeded. We're
* done. */
break;
}
freeaddrinfo (ai_list);
if (fd < 0)
{
ERROR ("mbmon: Could not connect to daemon.");
return (-1);
}
/* receive data from the mbmon daemon */
memset (buffer, '\0', buffer_size);
buffer_fill = 0;
while ((status = read (fd, buffer + buffer_fill, buffer_size - buffer_fill)) != 0)
{
if (status == -1)
{
char errbuf[1024];
if ((errno == EAGAIN) || (errno == EINTR))
continue;
ERROR ("mbmon: Error reading from socket: %s",
sstrerror (errno, errbuf,
sizeof (errbuf)));
close (fd);
return (-1);
}
buffer_fill += status;
if (buffer_fill >= buffer_size)
break;
}
if (buffer_fill >= buffer_size)
{
buffer[buffer_size - 1] = '\0';
WARNING ("mbmon: Message from mbmon has been truncated.");
}
else if (buffer_fill == 0)
{
WARNING ("mbmon: Peer has unexpectedly shut down the socket. "
"Buffer: `%s'", buffer);
close (fd);
return (-1);
}
close (fd);
return (0);
}
static int mbmon_config (const char *key, const char *value)
{
if (strcasecmp (key, "host") == 0)
{
if (mbmon_host != NULL)
free (mbmon_host);
mbmon_host = strdup (value);
}
else if (strcasecmp (key, "port") == 0)
{
if (mbmon_port != NULL)
free (mbmon_port);
mbmon_port = strdup (value);
}
else
{
return (-1);
}
return (0);
}
static void mbmon_submit (const char *type, const char *type_instance,
double value)
{
value_t values[1];
value_list_t vl = VALUE_LIST_INIT;
values[0].gauge = value;
vl.values = values;
vl.values_len = 1;
sstrncpy (vl.host, hostname_g, sizeof (vl.host));
sstrncpy (vl.plugin, "mbmon", sizeof (vl.plugin));
sstrncpy (vl.type, type, sizeof (vl.type));
sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
plugin_dispatch_values (&vl);
} /* void mbmon_submit */
/* Trim trailing whitespace from a string. */
static void trim_spaces (char *s)
{
size_t l;
for (l = strlen (s) - 1; (l > 0) && isspace ((int) s[l]); l--)
s[l] = '\0';
}
static int mbmon_read (void)
{
char buf[1024];
char *s, *t;
/* get data from daemon */
if (mbmon_query_daemon (buf, sizeof (buf)) < 0)
return (-1);
s = buf;
while ((t = strchr (s, ':')) != NULL)
{
double value;
char *nextc;
char *type;
char *inst;
*t++ = '\0';
trim_spaces (s);
value = strtod (t, &nextc);
if ((*nextc != '\n') && (*nextc != '\0'))
{
ERROR ("mbmon: value for `%s' contains invalid characters: `%s'", s, t);
break;
}
if (strncmp (s, "TEMP", 4) == 0)
{
inst = s + 4;
type = "temperature";
}
else if (strncmp (s, "FAN", 3) == 0)
{
inst = s + 3;
type = "fanspeed";
}
else if (strncmp (s, "V", 1) == 0)
{
inst = s + 1;
type = "voltage";
}
else
{
continue;
}
mbmon_submit (type, inst, value);
if (*nextc == '\0')
break;
s = nextc + 1;
}
return (0);
} /* void mbmon_read */
/* module_register
Register collectd plugin. */
void module_register (void)
{
plugin_register_config ("mbmon", mbmon_config, config_keys, config_keys_num);
plugin_register_read ("mbmon", mbmon_read);
} /* void module_register */