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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
#define _GNU_SOURCE /* forgive me */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <curl/curl.h>
#include "monitor.h"
#define BUFF_SIZE 65535
#define INIT_VEC_CAPACITY 256
typedef enum {
TYPE_REACH,
TYPE_DNS,
TYPE_WEB
} type_t;
const char *type_str[] = { "reach", "dns", "web" };
typedef enum {
STATUS_DOWN,
STATUS_UP
} status_t;
typedef struct {
time_t time;
status_t status;
} event_t;
typedef struct {
type_t type;
char *name;
char *target;
status_t status;
event_t *events;
size_t events_size, events_capacity;
} target_t;
/* baked */
typedef struct {
time_t started_time;
time_t duration_time;
int resolved;
char *service;
char *started;
char *duration;
} incident_t;
static target_t targets[INIT_VEC_CAPACITY];
static size_t targets_n = 0;
/* ordered*/
static incident_t *incidents = NULL;
static size_t incidents_size = 0, incidents_capacity = 0;
static char timestr[256];
static void
target_events_push(target_t *target, event_t event)
{
if (target->events_size + 1 > target->events_capacity)
target->events = realloc(target->events,
2 * sizeof(event_t) * target->events_capacity);
target->events[target->events_size++] = event;
}
static void
incidents_push_ordered(const incident_t *incident)
{
if (incidents_size + 1 > incidents_capacity)
incidents = realloc(incidents,
2 * sizeof(incident_t) * incidents_capacity);
size_t i = 0;
while (incidents[i].started_time < incident->started_time
&& i < incidents_size)
i++;
/* incidents[i].started_time >= incident.started_time */
memmove(&incidents[i + 1], &incidents[i],
(incidents_size - i) * sizeof(incident_t));
incidents[i] = *incident;
incidents_size++;
}
static size_t
target_events_load(target_t *target, const char *logbuff)
{
char line[256];
size_t n = 0;
while (*logbuff) {
char *nlpos = strchr(logbuff, '\n');
if (!nlpos)
return n;
size_t linelen = nlpos - logbuff;
strncpy(line, logbuff, linelen);
line[linelen] = '\0';
logbuff += linelen + 1;
/* process line */
if (line[0] == '\0' || line[0] == '#')
continue;
char *name = strtok(line, ",");
char *time = strtok(NULL, ",");
char *status = strtok(NULL, ",");
if (!name || !time || !status) {
fprintf(stderr, "malformed log line: %s\n", line);
continue;
}
if (strcmp(name, target->name) != 0)
continue;
struct tm event_time = { 0 };
strptime(time, "%FT%T%z", &event_time);
event_t event = {
mktime(&event_time) - timezone,
strcmp(status, "up") == 0 ? STATUS_UP : STATUS_DOWN
};
target_events_push(target, event);
n++;
}
return n;
}
/* assume events start with down */
void
incidents_render()
{
char buff[256];
for (size_t i = 0; i < targets_n; i++) {
/* iterate through downs */
for (size_t j = 0; j < targets[i].events_size; j++) {
if (targets[i].events[j].status != STATUS_DOWN)
continue;
/* next must be up */
int resolved = 1;
if (targets[i].events_size == j + 1 ||
targets[i].events[j + 1].status != STATUS_UP)
resolved = 0;
time_t start = targets[i].events[j].time;
time_t duration = targets[i].events[j + 1].time - start;
snprintf(buff, 256, "%ldh %ldm %lds", duration / 3600,
(duration / 60) % 60, duration % 60);
char *durationstr = strdup(buff);
struct tm *tm_start = gmtime(&start);
strftime(buff, 256, "%Y-%m-%d %H:%M:%S", tm_start);
char *startstr = strdup(buff);
incident_t incident = {
targets[i].events[j].time,
duration,
resolved,
targets[i].name,
startstr,
durationstr
};
incidents_push_ordered(&incident);
}
}
}
int
monitor_init(const char *cfg_path, const char *log_path)
{
/* read monitor log */
FILE *logf = fopen(log_path, "r");
if (!logf) {
fprintf(stderr, "Error opening log: %s\n", strerror(errno));
return -1;
}
fseek(logf, 0, SEEK_END);
size_t logsize = ftell(logf);
rewind(logf);
char *logbuff = malloc(logsize + 1);
size_t logread = fread(logbuff, 1, logsize, logf);
fclose(logf);
logbuff[logread] = '\0';
/* read monitoring configuration */
FILE *cfgf = fopen(cfg_path, "r");
if (!cfgf) {
fprintf(stderr, "Error opening config: %s\n", strerror(errno));
return -1;
}
printf("monitor targets:\n");
tzset(); /* initialize tz conversion */
char line[256];
while (fgets(line, sizeof(line), cfgf)) {
if (*line == '#' || *line == '\n')
continue;
line[strlen(line) - 1] = '\0'; /* strip \n */
char *type = strtok(line, ",");
char *name = strtok(NULL, ",");
char *target = strtok(NULL, ",");
if (!target || !name || !target) {
fprintf(stderr, "malformed config line: %s\n", line);
continue;
}
if (strcmp(type, "reach") == 0)
targets[targets_n].type = TYPE_REACH;
else if (strcmp(type, "dns") == 0)
targets[targets_n].type = TYPE_DNS;
else if (strcmp(type, "web") == 0)
targets[targets_n].type = TYPE_WEB;
targets[targets_n].name = strdup(name);
targets[targets_n].target = strdup(target);
targets[targets_n].status = STATUS_DOWN;
/* read monitor logs */
targets[targets_n].events_capacity = INIT_VEC_CAPACITY;
targets[targets_n].events_size = 0;
targets[targets_n].events = malloc(INIT_VEC_CAPACITY * sizeof(event_t));
size_t event_n = target_events_load(&targets[targets_n], logbuff);
printf("\t%s: %s,%s %ld events\n",
targets[targets_n].name,
type_str[targets[targets_n].type],
targets[targets_n].target,
event_n
);
targets_n++;
}
fclose(cfgf);
incidents = malloc(sizeof(incident_t) * INIT_VEC_CAPACITY);
incidents_capacity = INIT_VEC_CAPACITY;
incidents_size = 0;
incidents_render();
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
if (res) {
fprintf(stderr, "Error initializing cURL: %s\n",
curl_easy_strerror(res));
return -1;
}
return 0;
}
const char *
target_uptime(const target_t *target)
{
static char buff[256];
if (target->events_size == 0) {
snprintf(buff, 256, "-");
return buff;
}
event_t last_event = target->events[target->events_size - 1];
if (last_event.status == STATUS_DOWN) {
snprintf(buff, 256, "-");
return buff;
}
time_t uptime = time(NULL) - last_event.time;
snprintf(buff, 256, "%ldd %ldh %ldm %lds",
uptime / (3600*24), (uptime / 3600) % 24, (uptime / 60) % 60, uptime % 60);
return buff;
}
const char *
monitor_generate_status_html()
{
static char buff[BUFF_SIZE];
static char *status_html[] = {
"<td class=\"down\">down</td>",
"<td class=\"up\">up</td>"
};
char *pos = buff;
for (size_t i = 0; i < targets_n; i++) {
pos += snprintf(pos, BUFF_SIZE - (pos - buff),
"<tr><td>%s</td><td>%s</td>%s<td>%s</td><td>%s</td><td>%s</td></tr>\n",
type_str[targets[i].type], /* type */
targets[i].target, /* target */
status_html[targets[i].status], /* status */
target_uptime(&targets[i]), /* uptime */
"", /* %up month */
"" /* %up total */
);
}
return buff;
}
const char *
monitor_generate_incidents_html()
{
static char buff[BUFF_SIZE];
static const char *resolvedstr[] = {
"unresolved",
"resolved"
};
char *pos = buff;
for (int i = incidents_size - 1; i >= 0; i--) {
pos += snprintf(pos, BUFF_SIZE - (pos - buff),
"<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n",
incidents[i].service,
resolvedstr[incidents[i].resolved],
incidents[i].started,
incidents[i].duration
);
}
return buff;
}
static int
check_reach(const char *target)
{
static char ping_cmd[256];
snprintf(ping_cmd, 256, "ping -W1 -c1 %s > /dev/null", target);
/* i know */
if (system(ping_cmd) == 0) {
printf("reachable\n");
return STATUS_UP;
} else {
printf("unreachable\n");
return STATUS_DOWN;
}
}
static int
check_dns(const char *name)
{
static char dig_cmd[512];
static char cmd_out[256];
snprintf(dig_cmd, 512, "dig +nocookie +short %s NS", name);
FILE *pf = popen(dig_cmd, "r");
fread(cmd_out, 256, 1, pf);
pclose(pf);
if (*cmd_out == '\0') {
printf("no ns\n");
return STATUS_DOWN;
}
*strchr(cmd_out, '\n') = '\0';
snprintf(dig_cmd, 512, "dig +nocookie +short @%s %s A", cmd_out, name);
pf = popen(dig_cmd, "r");
fread(cmd_out, 256, 1, pf);
pclose(pf);
if (*cmd_out == '\0') {
printf("no a\n");
return STATUS_DOWN;
}
*strchr(cmd_out, '\n') = '\0';
printf("%s\n", cmd_out);
return STATUS_UP;
}
static size_t
write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
return size * nmemb;
}
static int
check_http(const char *endpoint)
{
CURL *curl = curl_easy_init();
if (!curl) {
fprintf(stderr, "Error allocating cURL handle\n");
return -1;
}
//curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
curl_easy_setopt(curl, CURLOPT_URL, endpoint);
CURLcode curl_code = curl_easy_perform(curl);
if (curl_code != CURLE_OK) {
printf("curl_easy_perform() failed: %s\n",
curl_easy_strerror(curl_code));
return STATUS_DOWN;
}
long http_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
curl_easy_cleanup(curl);
printf("%ld\n", http_code);
return http_code == 200 ? STATUS_UP : STATUS_DOWN;
}
void
monitor_check()
{
static size_t check_num = 0;
time_t time_now = time(NULL);
struct tm *tm_now = gmtime(&time_now);
strftime(timestr, 256, "%Y-%m-%d %H:%M:%S", tm_now);
static const int (*check_funcs[])(const char *) = {
check_reach,
check_dns,
check_http
};
for (size_t i = 0; i < targets_n; i++) {
printf("[%s] [monitor] check #%ld %s: ",
timestr, check_num, targets[i].name);
targets[i].status = check_funcs[targets[i].type](targets[i].target);
}
check_num++;
}
|