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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
// SPDX-License-Identifier: GPL-3.0-or-later
#include "health.h"
// ----------------------------------------------------------------------------
// health alarm log load/save
// no need for locking - only one thread is reading / writing the alarms log
inline int health_alarm_log_open(RRDHOST *host) {
if(host->health_log_fp)
fclose(host->health_log_fp);
host->health_log_fp = fopen(host->health_log_filename, "a");
if(host->health_log_fp) {
if (setvbuf(host->health_log_fp, NULL, _IOLBF, 0) != 0)
error("HEALTH [%s]: cannot set line buffering on health log file '%s'.", host->hostname, host->health_log_filename);
return 0;
}
error("HEALTH [%s]: cannot open health log file '%s'. Health data will be lost in case of netdata or server crash.", host->hostname, host->health_log_filename);
return -1;
}
static inline void health_alarm_log_close(RRDHOST *host) {
if(host->health_log_fp) {
fclose(host->health_log_fp);
host->health_log_fp = NULL;
}
}
static inline void health_log_rotate(RRDHOST *host) {
static size_t rotate_every = 0;
if(unlikely(rotate_every == 0)) {
rotate_every = (size_t)config_get_number(CONFIG_SECTION_HEALTH, "rotate log every lines", 2000);
if(rotate_every < 100) rotate_every = 100;
}
if(unlikely(host->health_log_entries_written > rotate_every)) {
if(unlikely(host->health_log_fp)) {
health_alarm_log_close(host);
char old_filename[FILENAME_MAX + 1];
snprintfz(old_filename, FILENAME_MAX, "%s.old", host->health_log_filename);
if(unlink(old_filename) == -1 && errno != ENOENT)
error("HEALTH [%s]: cannot remove old alarms log file '%s'", host->hostname, old_filename);
if(link(host->health_log_filename, old_filename) == -1 && errno != ENOENT)
error("HEALTH [%s]: cannot move file '%s' to '%s'.", host->hostname, host->health_log_filename, old_filename);
if(unlink(host->health_log_filename) == -1 && errno != ENOENT)
error("HEALTH [%s]: cannot remove old alarms log file '%s'", host->hostname, host->health_log_filename);
// open it with truncate
host->health_log_fp = fopen(host->health_log_filename, "w");
if(host->health_log_fp)
fclose(host->health_log_fp);
else
error("HEALTH [%s]: cannot truncate health log '%s'", host->hostname, host->health_log_filename);
host->health_log_fp = NULL;
host->health_log_entries_written = 0;
health_alarm_log_open(host);
}
}
}
inline void health_label_log_save(RRDHOST *host) {
health_log_rotate(host);
if(unlikely(host->health_log_fp)) {
BUFFER *wb = buffer_create(1024);
rrdlabels_to_buffer(localhost->host_labels, wb, "", "=", "", "\t ", NULL, NULL, NULL, NULL);
char *write = (char *) buffer_tostring(wb);
if (unlikely(fprintf(host->health_log_fp, "L\t%s", write) < 0))
error("HEALTH [%s]: failed to save alarm log entry to '%s'. Health data may be lost in case of abnormal restart.",
host->hostname, host->health_log_filename);
else
host->health_log_entries_written++;
buffer_free(wb);
}
}
inline void health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae) {
health_log_rotate(host);
if(unlikely(host->health_log_fp)) {
if(unlikely(fprintf(host->health_log_fp
, "%c\t%s"
"\t%08x\t%08x\t%08x\t%08x\t%08x"
"\t%08x\t%08x\t%08x"
"\t%08x\t%08x\t%08x"
"\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s"
"\t%d\t%d\t%d\t%d"
"\t" NETDATA_DOUBLE_FORMAT_AUTO "\t" NETDATA_DOUBLE_FORMAT_AUTO
"\t%016"PRIx64""
"\t%s\t%s\t%s"
"\n"
, (ae->flags & HEALTH_ENTRY_FLAG_SAVED)?'U':'A'
, host->hostname
, ae->unique_id
, ae->alarm_id
, ae->alarm_event_id
, ae->updated_by_id
, ae->updates_id
, (uint32_t)ae->when
, (uint32_t)ae->duration
, (uint32_t)ae->non_clear_duration
, (uint32_t)ae->flags
, (uint32_t)ae->exec_run_timestamp
, (uint32_t)ae->delay_up_to_timestamp
, (ae->name)?ae->name:""
, (ae->chart)?ae->chart:""
, (ae->family)?ae->family:""
, (ae->exec)?ae->exec:""
, (ae->recipient)?ae->recipient:""
, (ae->source)?ae->source:""
, (ae->units)?ae->units:""
, (ae->info)?ae->info:""
, ae->exec_code
, ae->new_status
, ae->old_status
, ae->delay
, ae->new_value
, ae->old_value
, (uint64_t)ae->last_repeat
, (ae->classification)?ae->classification:"Unknown"
, (ae->component)?ae->component:"Unknown"
, (ae->type)?ae->type:"Unknown"
) < 0))
error("HEALTH [%s]: failed to save alarm log entry to '%s'. Health data may be lost in case of abnormal restart.", host->hostname, host->health_log_filename);
else {
ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
host->health_log_entries_written++;
}
}else
sql_health_alarm_log_save(host, ae);
#ifdef ENABLE_ACLK
if (netdata_cloud_setting) {
sql_queue_alarm_to_aclk(host, ae, 0);
}
#endif
}
static uint32_t is_valid_alarm_id(RRDHOST *host, const char *chart, const char *name, uint32_t alarm_id)
{
uint32_t hash_chart = simple_hash(chart);
uint32_t hash_name = simple_hash(name);
ALARM_ENTRY *ae;
for(ae = host->health_log.alarms; ae ;ae = ae->next) {
if (unlikely(
ae->alarm_id == alarm_id && (!(ae->hash_name == hash_name && ae->hash_chart == hash_chart &&
!strcmp(name, ae->name) && !strcmp(chart, ae->chart))))) {
return 0;
}
}
return 1;
}
static inline ssize_t health_alarm_log_read(RRDHOST *host, FILE *fp, const char *filename) {
errno = 0;
char *s, *buf = mallocz(65536 + 1);
size_t line = 0, len = 0;
ssize_t loaded = 0, updated = 0, errored = 0, duplicate = 0;
netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
while((s = fgets_trim_len(buf, 65536, fp, &len))) {
host->health_log_entries_written++;
line++;
int max_entries = 33, entries = 0;
char *pointers[max_entries];
pointers[entries++] = s++;
while(*s) {
if(unlikely(*s == '\t')) {
*s = '\0';
pointers[entries++] = ++s;
if(entries >= max_entries) {
error("HEALTH [%s]: line %zu of file '%s' has more than %d entries. Ignoring excessive entries.", host->hostname, line, filename, max_entries);
break;
}
}
else s++;
}
if(likely(*pointers[0] == 'L'))
continue;
if(likely(*pointers[0] == 'U' || *pointers[0] == 'A')) {
ALARM_ENTRY *ae = NULL;
if(entries < 27) {
error("HEALTH [%s]: line %zu of file '%s' should have at least 27 entries, but it has %d. Ignoring it.", host->hostname, line, filename, entries);
errored++;
continue;
}
// check that we have valid ids
uint32_t unique_id = (uint32_t)strtoul(pointers[2], NULL, 16);
if(!unique_id) {
error("HEALTH [%s]: line %zu of file '%s' states alarm entry with invalid unique id %u (%s). Ignoring it.", host->hostname, line, filename, unique_id, pointers[2]);
errored++;
continue;
}
uint32_t alarm_id = (uint32_t)strtoul(pointers[3], NULL, 16);
if(!alarm_id) {
error("HEALTH [%s]: line %zu of file '%s' states alarm entry for invalid alarm id %u (%s). Ignoring it.", host->hostname, line, filename, alarm_id, pointers[3]);
errored++;
continue;
}
// Check if we got last_repeat field
time_t last_repeat = 0;
if(entries > 27) {
char* alarm_name = pointers[13];
last_repeat = (time_t)strtoul(pointers[27], NULL, 16);
RRDCALC *rc = alarm_max_last_repeat(host, alarm_name,simple_hash(alarm_name));
if (!rc) {
for(rc = host->alarms; rc ; rc = rc->next) {
RRDCALC *rdcmp = (RRDCALC *) avl_insert_lock(&(host)->alarms_idx_name, (avl_t *)rc);
if(rdcmp != rc) {
error("Cannot insert the alarm index ID using log %s", rc->name);
}
}
rc = alarm_max_last_repeat(host, alarm_name,simple_hash(alarm_name));
}
if(unlikely(rc)) {
if (rrdcalc_isrepeating(rc)) {
rc->last_repeat = last_repeat;
// We iterate through repeating alarm entries only to
// find the latest last_repeat timestamp. Otherwise,
// there is no need to keep them in memory.
continue;
}
}
}
if(unlikely(*pointers[0] == 'A')) {
// make sure it is properly numbered
if(unlikely(host->health_log.alarms && unique_id < host->health_log.alarms->unique_id)) {
error( "HEALTH [%s]: line %zu of file '%s' has alarm log entry %u in wrong order. Ignoring it."
, host->hostname, line, filename, unique_id);
errored++;
continue;
}
ae = callocz(1, sizeof(ALARM_ENTRY));
}
else if(unlikely(*pointers[0] == 'U')) {
// find the original
for(ae = host->health_log.alarms; ae ; ae = ae->next) {
if(unlikely(unique_id == ae->unique_id)) {
if(unlikely(*pointers[0] == 'A')) {
error("HEALTH [%s]: line %zu of file '%s' adds duplicate alarm log entry %u. Using the later."
, host->hostname, line, filename, unique_id);
*pointers[0] = 'U';
duplicate++;
}
break;
}
else if(unlikely(unique_id > ae->unique_id)) {
// no need to continue
// the linked list is sorted
ae = NULL;
break;
}
}
}
// if not found, skip this line
if(unlikely(!ae)) {
// error("HEALTH [%s]: line %zu of file '%s' updates alarm log entry with unique id %u, but it is not found.", host->hostname, line, filename, unique_id);
continue;
}
// check for a possible host mismatch
//if(strcmp(pointers[1], host->hostname))
// error("HEALTH [%s]: line %zu of file '%s' provides an alarm for host '%s' but this is named '%s'.", host->hostname, line, filename, pointers[1], host->hostname);
ae->unique_id = unique_id;
if (!is_valid_alarm_id(host, pointers[14], pointers[13], alarm_id))
alarm_id = rrdcalc_get_unique_id(host, pointers[14], pointers[13], NULL);
ae->alarm_id = alarm_id;
ae->alarm_event_id = (uint32_t)strtoul(pointers[4], NULL, 16);
ae->updated_by_id = (uint32_t)strtoul(pointers[5], NULL, 16);
ae->updates_id = (uint32_t)strtoul(pointers[6], NULL, 16);
ae->when = (uint32_t)strtoul(pointers[7], NULL, 16);
ae->duration = (uint32_t)strtoul(pointers[8], NULL, 16);
ae->non_clear_duration = (uint32_t)strtoul(pointers[9], NULL, 16);
ae->flags = (uint32_t)strtoul(pointers[10], NULL, 16);
ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
ae->exec_run_timestamp = (uint32_t)strtoul(pointers[11], NULL, 16);
ae->delay_up_to_timestamp = (uint32_t)strtoul(pointers[12], NULL, 16);
freez(ae->name);
ae->name = strdupz(pointers[13]);
ae->hash_name = simple_hash(ae->name);
freez(ae->chart);
ae->chart = strdupz(pointers[14]);
ae->hash_chart = simple_hash(ae->chart);
freez(ae->family);
ae->family = strdupz(pointers[15]);
freez(ae->exec);
ae->exec = strdupz(pointers[16]);
if(!*ae->exec) { freez(ae->exec); ae->exec = NULL; }
freez(ae->recipient);
ae->recipient = strdupz(pointers[17]);
if(!*ae->recipient) { freez(ae->recipient); ae->recipient = NULL; }
freez(ae->source);
ae->source = strdupz(pointers[18]);
if(!*ae->source) { freez(ae->source); ae->source = NULL; }
freez(ae->units);
ae->units = strdupz(pointers[19]);
if(!*ae->units) { freez(ae->units); ae->units = NULL; }
freez(ae->info);
ae->info = strdupz(pointers[20]);
if(!*ae->info) { freez(ae->info); ae->info = NULL; }
ae->exec_code = str2i(pointers[21]);
ae->new_status = str2i(pointers[22]);
ae->old_status = str2i(pointers[23]);
ae->delay = str2i(pointers[24]);
ae->new_value = str2l(pointers[25]);
ae->old_value = str2l(pointers[26]);
ae->last_repeat = last_repeat;
if (likely(entries > 30)) {
freez(ae->classification);
ae->classification = strdupz(pointers[28]);
if(!*ae->classification) { freez(ae->classification); ae->classification = NULL; }
freez(ae->component);
ae->component = strdupz(pointers[29]);
if(!*ae->component) { freez(ae->component); ae->component = NULL; }
freez(ae->type);
ae->type = strdupz(pointers[30]);
if(!*ae->type) { freez(ae->type); ae->type = NULL; }
}
char value_string[100 + 1];
freez(ae->old_value_string);
freez(ae->new_value_string);
ae->old_value_string = strdupz(format_value_and_unit(value_string, 100, ae->old_value, ae->units, -1));
ae->new_value_string = strdupz(format_value_and_unit(value_string, 100, ae->new_value, ae->units, -1));
// add it to host if not already there
if(unlikely(*pointers[0] == 'A')) {
ae->next = host->health_log.alarms;
host->health_log.alarms = ae;
sql_health_alarm_log_insert(host, ae);
loaded++;
}
else {
sql_health_alarm_log_update(host, ae);
updated++;
}
if(unlikely(ae->unique_id > host->health_max_unique_id))
host->health_max_unique_id = ae->unique_id;
if(unlikely(ae->alarm_id >= host->health_max_alarm_id))
host->health_max_alarm_id = ae->alarm_id;
}
else {
error("HEALTH [%s]: line %zu of file '%s' is invalid (unrecognized entry type '%s').", host->hostname, line, filename, pointers[0]);
errored++;
}
}
netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
freez(buf);
if(!host->health_max_unique_id) host->health_max_unique_id = (uint32_t)now_realtime_sec();
if(!host->health_max_alarm_id) host->health_max_alarm_id = (uint32_t)now_realtime_sec();
host->health_log.next_log_id = host->health_max_unique_id + 1;
if (unlikely(!host->health_log.next_alarm_id || host->health_log.next_alarm_id <= host->health_max_alarm_id))
host->health_log.next_alarm_id = host->health_max_alarm_id + 1;
debug(D_HEALTH, "HEALTH [%s]: loaded file '%s' with %zd new alarm entries, updated %zd alarms, errors %zd entries, duplicate %zd", host->hostname, filename, loaded, updated, errored, duplicate);
return loaded;
}
inline void health_alarm_log_load(RRDHOST *host) {
health_alarm_log_close(host);
char filename[FILENAME_MAX + 1];
snprintfz(filename, FILENAME_MAX, "%s.old", host->health_log_filename);
FILE *fp = fopen(filename, "r");
if(!fp)
error("HEALTH [%s]: cannot open health file: %s", host->hostname, filename);
else {
health_alarm_log_read(host, fp, filename);
fclose(fp);
}
host->health_log_entries_written = 0;
fp = fopen(host->health_log_filename, "r");
if(!fp)
error("HEALTH [%s]: cannot open health file: %s", host->hostname, host->health_log_filename);
else {
health_alarm_log_read(host, fp, host->health_log_filename);
fclose(fp);
}
}
// ----------------------------------------------------------------------------
// health alarm log management
inline ALARM_ENTRY* health_create_alarm_entry(
RRDHOST *host,
uint32_t alarm_id,
uint32_t alarm_event_id,
uuid_t config_hash_id,
time_t when,
const char *name,
const char *chart,
const char *chart_context,
const char *family,
const char *class,
const char *component,
const char *type,
const char *exec,
const char *recipient,
time_t duration,
NETDATA_DOUBLE old_value,
NETDATA_DOUBLE new_value,
RRDCALC_STATUS old_status,
RRDCALC_STATUS new_status,
const char *source,
const char *units,
const char *info,
int delay,
uint32_t flags
) {
debug(D_HEALTH, "Health adding alarm log entry with id: %u", host->health_log.next_log_id);
ALARM_ENTRY *ae = callocz(1, sizeof(ALARM_ENTRY));
ae->name = strdupz(name);
ae->hash_name = simple_hash(ae->name);
if(chart) {
ae->chart = strdupz(chart);
ae->hash_chart = simple_hash(ae->chart);
}
if(chart_context)
ae->chart_context = strdupz(chart_context);
uuid_copy(ae->config_hash_id, *((uuid_t *) config_hash_id));
if(family)
ae->family = strdupz(family);
if (class)
ae->classification = strdupz(class);
if (component)
ae->component = strdupz(component);
if (type)
ae->type = strdupz(type);
if(exec) ae->exec = strdupz(exec);
if(recipient) ae->recipient = strdupz(recipient);
if(source) ae->source = strdupz(source);
if(units) ae->units = strdupz(units);
ae->unique_id = host->health_log.next_log_id++;
ae->alarm_id = alarm_id;
ae->alarm_event_id = alarm_event_id;
ae->when = when;
ae->old_value = old_value;
ae->new_value = new_value;
char value_string[100 + 1];
ae->old_value_string = strdupz(format_value_and_unit(value_string, 100, ae->old_value, ae->units, -1));
ae->new_value_string = strdupz(format_value_and_unit(value_string, 100, ae->new_value, ae->units, -1));
char *replaced_info = NULL;
if (likely(info)) {
char *m;
replaced_info = strdupz(info);
size_t pos = 0;
while ((m = strstr(replaced_info + pos, "$family"))) {
char *buf = NULL;
pos = m - replaced_info;
buf = find_and_replace(replaced_info, "$family", (ae->family) ? ae->family : "", m);
freez(replaced_info);
replaced_info = strdupz(buf);
freez(buf);
}
}
if(replaced_info) ae->info = strdupz(replaced_info);
freez(replaced_info);
ae->old_status = old_status;
ae->new_status = new_status;
ae->duration = duration;
ae->delay = delay;
ae->delay_up_to_timestamp = when + delay;
ae->flags |= flags;
ae->last_repeat = 0;
if(ae->old_status == RRDCALC_STATUS_WARNING || ae->old_status == RRDCALC_STATUS_CRITICAL)
ae->non_clear_duration += ae->duration;
return ae;
}
inline void health_alarm_log(
RRDHOST *host,
ALARM_ENTRY *ae
) {
debug(D_HEALTH, "Health adding alarm log entry with id: %u", ae->unique_id);
// link it
netdata_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
ae->next = host->health_log.alarms;
host->health_log.alarms = ae;
host->health_log.count++;
netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
// match previous alarms
netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
ALARM_ENTRY *t;
for(t = host->health_log.alarms ; t ; t = t->next) {
if(t != ae && t->alarm_id == ae->alarm_id) {
if(!(t->flags & HEALTH_ENTRY_FLAG_UPDATED) && !t->updated_by_id) {
t->flags |= HEALTH_ENTRY_FLAG_UPDATED;
t->updated_by_id = ae->unique_id;
ae->updates_id = t->unique_id;
if((t->new_status == RRDCALC_STATUS_WARNING || t->new_status == RRDCALC_STATUS_CRITICAL) &&
(t->old_status == RRDCALC_STATUS_WARNING || t->old_status == RRDCALC_STATUS_CRITICAL))
ae->non_clear_duration += t->non_clear_duration;
health_alarm_log_save(host, t);
}
// no need to continue
break;
}
}
netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
health_alarm_log_save(host, ae);
}
inline void health_alarm_log_free_one_nochecks_nounlink(ALARM_ENTRY *ae) {
freez(ae->name);
freez(ae->chart);
freez(ae->chart_context);
freez(ae->family);
freez(ae->classification);
freez(ae->component);
freez(ae->type);
freez(ae->exec);
freez(ae->recipient);
freez(ae->source);
freez(ae->units);
freez(ae->info);
freez(ae->old_value_string);
freez(ae->new_value_string);
freez(ae);
}
inline void health_alarm_log_free(RRDHOST *host) {
rrdhost_check_wrlock(host);
netdata_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
ALARM_ENTRY *ae;
while((ae = host->health_log.alarms)) {
host->health_log.alarms = ae->next;
health_alarm_log_free_one_nochecks_nounlink(ae);
}
netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
}
|