-
Notifications
You must be signed in to change notification settings - Fork 6
/
metadata_input_munin.c
341 lines (288 loc) · 9.99 KB
/
metadata_input_munin.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/time.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include "metadata_exporter.h"
#include "metadata_input_munin.h"
#include "backend_event_loop.h"
/* polling interval in seconds */
#define MUNIN_POLLING_INTERVAL 60
/* TODO
- handle socket disconnect/reconnect
*/
ssize_t md_munin_readLine(int fd, void *buffer, size_t n)
{
ssize_t numRead;
size_t totRead;
char *buf;
char ch;
if (n <= 0 || buffer == NULL) {
errno = EINVAL;
return -1;
}
buf = buffer;
totRead = 0;
for (;;) {
numRead = read(fd, &ch, 1);
if (numRead == -1) {
if (errno == EINTR)
continue;
else
return -1;
} else if (numRead == 0) {
if (totRead == 0)
return 0;
else
break;
} else {
if (totRead < n - 1) {
totRead++;
*buf++ = ch;
}
if (ch == '\n')
break;
}
}
*buf = '\0';
return totRead;
}
uint8_t md_munin_reconnect (struct md_input_munin *mim, const char *address, const char *port) {
int n = -1;
char buffer[256] = "\n";
struct hostent* server;
struct sockaddr_in serv_addr;
if ((mim->munin_socket_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "Error opening socket.\n");
return RETVAL_FAILURE;
}
if ((server = gethostbyname(address)) == NULL) {
fprintf(stderr, "No such host: %s\n", address);
return RETVAL_FAILURE;
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(atoi(port));
if (connect(mim->munin_socket_fd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) {
fprintf(stderr, "Could not connect to munin.\n");
return RETVAL_FAILURE;
}
if ((n = md_munin_readLine(mim->munin_socket_fd, buffer, 255))<0) {
fprintf(stderr, "Could not read munin welcome string.\n");
close(mim->munin_socket_fd);
return RETVAL_FAILURE;
}
printf("%s", buffer);
return RETVAL_SUCCESS;
}
void md_munin_json_add_key_value(char* kv, json_object* blob) {
char *running = kv;
// a munin string is in the form uptime.value 1.23
char *key = strsep(&running, ".");
if (running == NULL) {
fprintf(stderr, "Munin return line did not match format key.value value\n");
return;
}
char *svalue = strsep(&running, " "); //ignore .value
if (running == NULL || (strncmp(svalue, "value", 5)!=0) ) {
fprintf(stderr, "Munin return line did not match format key.value value\n");
return;
}
char *value = strsep(&running, "\n");
struct json_object *obj_add = NULL;
if ((obj_add = json_object_new_string(value))==NULL)
return;
json_object_object_add(blob, key, obj_add);
}
static void md_input_munin_handle_event(void *ptr, int32_t fd, uint32_t events)
{
struct md_input_munin *mim = ptr;
int i=0,n;
uint64_t exp;
struct md_gps_event gps_event;
if ((n=read(fd, &exp, sizeof(uint64_t))) < sizeof(uint64_t)) {
fprintf(stderr, "Munin timer failed.\n");
return;
}
setsockopt( mim->munin_socket_fd, IPPROTO_TCP, TCP_NODELAY, (void *)&i, sizeof(i));
struct json_object *blob = NULL;
struct timeval tv;
if (!(blob = json_object_new_object()))
return;
for (i=0; i < mim->module_count; i++) {
char cmd[256];
char buffer[256];
snprintf(cmd, 255, "fetch %s\n", mim->modules[i]);
if ((n=write(mim->munin_socket_fd, cmd, strlen(cmd)))<0) {
fprintf(stderr, "Writing to munin failed.\n");
return;
}
struct json_object *obj_mod = NULL;
if (!(obj_mod = json_object_new_object()))
break;
json_object_object_add(blob, mim->modules[i], obj_mod);
while (1) {
if ((n=md_munin_readLine(mim->munin_socket_fd, buffer, 255))<0) {
fprintf(stderr, "Reading from munin failed.\n");
return;
}
if ((buffer[0] == '#') || (buffer[0] == '.'))
break;
md_munin_json_add_key_value(buffer, obj_mod);
}
}
struct md_munin_event munin_event;
memset(&munin_event, 0, sizeof(struct md_munin_event));
gettimeofday(&tv, NULL);
munin_event.md_type = META_TYPE_MUNIN;
munin_event.tstamp = tv.tv_sec;
munin_event.sequence = mde_inc_seq(mim->parent);
munin_event.json_blob = blob;
//fprintf(stderr, "JSON dump: %s\n", json_object_to_json_string(blob));
mde_publish_event_obj(mim->parent, (struct md_event *) &munin_event);
json_object_put(blob);
if (mim->nsb_gps == 1) {
char cmd[256];
char buffer[256];
memset(&gps_event, 0, sizeof(struct md_gps_event));
gps_event.md_type = META_TYPE_POS;
gps_event.minmea_id = MINMEA_UNKNOWN;
snprintf(cmd, 255, "fetch nsb\n");
if ((n=write(mim->munin_socket_fd, cmd, strlen(cmd)))<0) {
fprintf(stderr, "Writing to munin failed.\n");
return;
}
while (1) {
if ((n=md_munin_readLine(mim->munin_socket_fd, buffer, 255))<0) {
fprintf(stderr, "Reading from munin failed.\n");
return;
}
if ((buffer[0] == '#') || (buffer[0] == '.'))
break;
if (strncmp(buffer, "lat.value", 9)==0)
gps_event.latitude = atof(buffer + 10);
if (strncmp(buffer, "lon.value", 9)==0)
gps_event.longitude = atof(buffer + 10);
if (strncmp(buffer, "sats.value", 10)==0)
gps_event.satellites_tracked = atoi(buffer + 11);
if (strncmp(buffer, "speed.value", 9)==0)
gps_event.speed = atof(buffer + 12);
}
if (gps_event.latitude != 0.0) {
gps_event.tstamp = tv.tv_sec;
gps_event.sequence = mde_inc_seq(mim->parent);
mde_publish_event_obj(mim->parent, (struct md_event *) &gps_event);
}
}
}
static uint8_t md_munin_config(struct md_input_munin *mim,
const char *address,
const char *port,
const char *modules,
int nsb_gps)
{
int timer;
struct itimerspec delay;
if (md_munin_reconnect(mim, address, port) == RETVAL_SUCCESS) {
char *running;
if ((running = strdup(modules)) == NULL) {
fprintf(stderr, "Memory allocation failed.");
return RETVAL_FAILURE;
}
char **tokens = NULL;
char * token = strsep(&running, ",");
int n_commas = 0,
n_tokens = 0;
while(token) {
if ((tokens = realloc(tokens, sizeof(char*)* ++n_commas))==NULL) {
fprintf(stderr, "Memory allocation failed.");
return RETVAL_FAILURE;
}
tokens[n_commas-1] = token;
token = strsep(&running, ",");
n_tokens++;
}
mim->module_count = n_tokens;
mim->modules = tokens;
mim->nsb_gps = nsb_gps;
if ((timer = timerfd_create(CLOCK_REALTIME, 0)) < 0) {
fprintf(stderr, "Could not create munin polling timer.");
return RETVAL_FAILURE;
}
delay.it_value.tv_sec = MUNIN_POLLING_INTERVAL;
delay.it_value.tv_nsec = 0;
delay.it_interval.tv_sec = MUNIN_POLLING_INTERVAL;
delay.it_interval.tv_nsec = 0;
if (timerfd_settime(timer, TFD_TIMER_ABSTIME, &delay, NULL) < 0) {
fprintf(stderr, "Could not initialize munin polling timer.");
return RETVAL_FAILURE;
}
if(!(mim->event_handle = backend_create_epoll_handle(
mim, timer, md_input_munin_handle_event)))
return RETVAL_FAILURE;
backend_event_loop_update(
mim->parent->event_loop, EPOLLIN, EPOLL_CTL_ADD, timer, mim->event_handle);
return RETVAL_SUCCESS;
} else {
return RETVAL_FAILURE;
}
}
void md_input_munin_destroy(void *ptr)
{
struct md_input_munin *mim = ptr;
if ((mim != NULL) && (mim->module_count > 0)) {
free(mim->modules);
mim->module_count = 0;
}
}
static uint8_t md_input_munin_init(void *ptr, json_object* config)
{
struct md_input_munin *mim = ptr;
const char *address = NULL, *port = NULL;
const char *modules = "memory,cpu";
int nsb_gps = 0;
json_object* subconfig;
if (json_object_object_get_ex(config, "munin", &subconfig)) {
json_object_object_foreach(subconfig, key, val) {
if (!strcmp(key, "address"))
address = json_object_get_string(val);
else if (!strcmp(key, "port"))
port = json_object_get_string(val);
else if (!strcmp(key, "modules"))
modules = json_object_get_string(val);
else if (!strcmp(key, "nsb_gps"))
nsb_gps = json_object_get_boolean(val);
}
}
if (address == NULL || port == NULL) {
fprintf(stderr, "Missing required Munin argument\n");
return RETVAL_FAILURE;
}
return md_munin_config(mim, address, port, modules, nsb_gps);
}
void md_munin_usage()
{
fprintf(stderr, "\"munin\": {\t\tMunin input\n");
// TODO: --munin-binary (no need to run systemd, nginx or inetd)
fprintf(stderr, " \"address\":\t\tmunin address\n");
fprintf(stderr, " \"port\":\t\tmunin port\n");
// TODO: change comma separated modules into a JSON array
fprintf(stderr, " \"modules\":\t\tcomma separated, modules to export\n");
fprintf(stderr, " \"nsb_gps\":\t\tif 1, generate gps events from nsb sensor\n");
fprintf(stderr, "},\n");
}
void md_munin_setup(struct md_exporter *mde, struct md_input_munin *mim)
{
mim->parent = mde;
mim->init = md_input_munin_init;
mim->destroy = md_input_munin_destroy;
}