3
#include "health.h"
4
5
// ----------------------------------------------------------------------------
6
-// health alarm log load/save
7
-// no need for locking - only one thread is reading / writing the alarms log
8
-
9
-inline int health_alarm_log_open(RRDHOST *host) {
10
- if(host->health.health_log_fp)
11
- fclose(host->health.health_log_fp);
12
-
13
- host->health.health_log_fp = fopen(host->health.health_log_filename, "a");
14
-
15
- if(host->health.health_log_fp) {
16
- if (setvbuf(host->health.health_log_fp, NULL, _IOLBF, 0) != 0)
17
- error("HEALTH [%s]: cannot set line buffering on health log file '%s'.", rrdhost_hostname(host), host->health.health_log_filename);
18
- return 0;
19
- }
20
-
21
- error("HEALTH [%s]: cannot open health log file '%s'. Health data will be lost in case of netdata or server crash.", rrdhost_hostname(host), host->health.health_log_filename);
22
- return -1;
23
-}
24
-
25
-static inline void health_alarm_log_close(RRDHOST *host) {
26
- if(host->health.health_log_fp) {
27
- fclose(host->health.health_log_fp);
28
- host->health.health_log_fp = NULL;
29
- }
30
-}
31
-
32
-static inline void health_log_rotate(RRDHOST *host) {
33
- static size_t rotate_every = 0;
34
-
35
- if(unlikely(rotate_every == 0)) {
36
- rotate_every = (size_t)config_get_number(CONFIG_SECTION_HEALTH, "rotate log every lines", 2000);
37
- if(rotate_every < 100) rotate_every = 100;
38
- }
39
-
40
- if(unlikely(host->health.health_log_entries_written > rotate_every)) {
41
- if(unlikely(host->health.health_log_fp)) {
42
- health_alarm_log_close(host);
43
-
44
- char old_filename[FILENAME_MAX + 1];
45
- snprintfz(old_filename, FILENAME_MAX, "%s.old", host->health.health_log_filename);
46
-
47
- if(unlink(old_filename) == -1 && errno != ENOENT)
48
- error("HEALTH [%s]: cannot remove old alarms log file '%s'", rrdhost_hostname(host), old_filename);
49
-
50
- if(link(host->health.health_log_filename, old_filename) == -1 && errno != ENOENT)
51
- error("HEALTH [%s]: cannot move file '%s' to '%s'.", rrdhost_hostname(host), host->health.health_log_filename, old_filename);
52
-
53
- if(unlink(host->health.health_log_filename) == -1 && errno != ENOENT)
54
- error("HEALTH [%s]: cannot remove old alarms log file '%s'", rrdhost_hostname(host), host->health.health_log_filename);
55
-
56
- // open it with truncate
57
- host->health.health_log_fp = fopen(host->health.health_log_filename, "w");
58
-
59
- if(host->health.health_log_fp)
60
- fclose(host->health.health_log_fp);
61
- else
62
- error("HEALTH [%s]: cannot truncate health log '%s'", rrdhost_hostname(host), host->health.health_log_filename);
63
-
64
- host->health.health_log_fp = NULL;
65
-
66
- host->health.health_log_entries_written = 0;
67
- health_alarm_log_open(host);
68
- }
69
- }
70
-}
71
-
72
-inline void health_label_log_save(RRDHOST *host) {
73
- health_log_rotate(host);
74
-
75
- if(unlikely(host->health.health_log_fp)) {
76
- BUFFER *wb = buffer_create(1024, &netdata_buffers_statistics.buffers_health);
77
-
78
- rrdlabels_to_buffer(localhost->rrdlabels, wb, "", "=", "", "\t ", NULL, NULL, NULL, NULL);
79
- char *write = (char *) buffer_tostring(wb);
80
-
81
- if (unlikely(fprintf(host->health.health_log_fp, "L\t%s", write) < 0))
82
- error("HEALTH [%s]: failed to save alarm log entry to '%s'. Health data may be lost in case of abnormal restart.",
83
- rrdhost_hostname(host), host->health.health_log_filename);
84
- else
85
- host->health.health_log_entries_written++;
86
-
87
- buffer_free(wb);
88
- }
89
-}
6
7
inline void health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae) {
92
- health_log_rotate(host);
93
- if(unlikely(host->health.health_log_fp)) {
94
- if(unlikely(fprintf(host->health.health_log_fp
95
- , "%c\t%s"
96
- "\t%08x\t%08x\t%08x\t%08x\t%08x"
97
- "\t%08x\t%08x\t%08x"
98
- "\t%08x\t%08x\t%08x"
99
- "\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s"
100
- "\t%d\t%d\t%d\t%d"
101
- "\t" NETDATA_DOUBLE_FORMAT_AUTO "\t" NETDATA_DOUBLE_FORMAT_AUTO
102
- "\t%016"PRIx64""
103
- "\t%s\t%s\t%s"
104
- "\n"
105
- , (ae->flags & HEALTH_ENTRY_FLAG_SAVED)?'U':'A'
106
- , rrdhost_hostname(host)
107
-
108
- , ae->unique_id
109
- , ae->alarm_id
110
- , ae->alarm_event_id
111
- , ae->updated_by_id
112
- , ae->updates_id
113
-
114
- , (uint32_t)ae->when
115
- , (uint32_t)ae->duration
116
- , (uint32_t)ae->non_clear_duration
117
- , (uint32_t)ae->flags
118
- , (uint32_t)ae->exec_run_timestamp
119
- , (uint32_t)ae->delay_up_to_timestamp
120
-
121
- , ae_name(ae)
122
- , ae_chart_name(ae)
123
- , ae_family(ae)
124
- , ae_exec(ae)
125
- , ae_recipient(ae)
126
- , ae_source(ae)
127
- , ae_units(ae)
128
- , ae_info(ae)
8
130
- , ae->exec_code
131
- , ae->new_status
132
- , ae->old_status
133
- , ae->delay
134
-
135
- , ae->new_value
136
- , ae->old_value
137
- , (uint64_t)ae->last_repeat
138
- , (ae->classification)?ae_classification(ae):"Unknown"
139
- , (ae->component)?ae_component(ae):"Unknown"
140
- , (ae->type)?ae_type(ae):"Unknown"
141
- ) < 0))
142
- error("HEALTH [%s]: failed to save alarm log entry to '%s'. Health data may be lost in case of abnormal restart.", rrdhost_hostname(host), host->health.health_log_filename);
143
- else {
144
- ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
145
- host->health.health_log_entries_written++;
146
- }
147
- }else
148
- sql_health_alarm_log_save(host, ae);
9
+ sql_health_alarm_log_save(host, ae);
10
11
#ifdef ENABLE_ACLK
12
if (netdata_cloud_setting) {
15
#endif
16
}
17
157
-static uint32_t is_valid_alarm_id(RRDHOST *host, const char *chart, const char *name, uint32_t alarm_id)
158
-{
159
- STRING *chart_string = string_strdupz(chart);
160
- STRING *name_string = string_strdupz(name);
161
-
162
- uint32_t ret = 1;
163
-
164
- ALARM_ENTRY *ae;
165
- for(ae = host->health_log.alarms; ae ;ae = ae->next) {
166
- if (unlikely(ae->alarm_id == alarm_id && (!(chart_string == ae->chart && name_string == ae->name)))) {
167
- ret = 0;
168
- break;
169
- }
170
- }
171
-
172
- string_freez(chart_string);
173
- string_freez(name_string);
174
-
175
- return ret;
176
-}
177
-
178
-static inline ssize_t health_alarm_log_read(RRDHOST *host, FILE *fp, const char *filename) {
179
- errno = 0;
180
-
181
- char *s, *buf = mallocz(65536 + 1);
182
- size_t line = 0, len = 0;
183
- ssize_t loaded = 0, updated = 0, errored = 0, duplicate = 0;
184
-
185
- DICTIONARY *all_rrdcalcs = dictionary_create_advanced(
186
- DICT_OPTION_NAME_LINK_DONT_CLONE | DICT_OPTION_VALUE_LINK_DONT_CLONE | DICT_OPTION_DONT_OVERWRITE_VALUE,
187
- &dictionary_stats_category_rrdhealth);
188
-
189
- RRDCALC *rc;
190
- foreach_rrdcalc_in_rrdhost_read(host, rc) {
191
- dictionary_set(all_rrdcalcs, rrdcalc_name(rc), rc, sizeof(*rc));
192
- }
193
- foreach_rrdcalc_in_rrdhost_done(rc);
194
-
195
- netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
196
-
197
- while((s = fgets_trim_len(buf, 65536, fp, &len))) {
198
- host->health.health_log_entries_written++;
199
- line++;
200
-
201
- int max_entries = 33, entries = 0;
202
- char *pointers[max_entries];
203
-
204
- pointers[entries++] = s++;
205
- while(*s) {
206
- if(unlikely(*s == '\t')) {
207
- *s = '\0';
208
- pointers[entries++] = ++s;
209
- if(entries >= max_entries) {
210
- error("HEALTH [%s]: line %zu of file '%s' has more than %d entries. Ignoring excessive entries.", rrdhost_hostname(host), line, filename, max_entries);
211
- break;
212
- }
213
- }
214
- else s++;
215
- }
216
-
217
- if(likely(*pointers[0] == 'L'))
218
- continue;
219
-
220
- if(likely(*pointers[0] == 'U' || *pointers[0] == 'A')) {
221
- ALARM_ENTRY *ae = NULL;
222
-
223
- if(entries < 27) {
224
- error("HEALTH [%s]: line %zu of file '%s' should have at least 27 entries, but it has %d. Ignoring it.", rrdhost_hostname(host), line, filename, entries);
225
- errored++;
226
- continue;
227
- }
228
-
229
- // check that we have valid ids
230
- uint32_t unique_id = (uint32_t)strtoul(pointers[2], NULL, 16);
231
- if(!unique_id) {
232
- error("HEALTH [%s]: line %zu of file '%s' states alarm entry with invalid unique id %u (%s). Ignoring it.", rrdhost_hostname(host), line, filename, unique_id, pointers[2]);
233
- errored++;
234
- continue;
235
- }
236
-
237
- uint32_t alarm_id = (uint32_t)strtoul(pointers[3], NULL, 16);
238
- if(!alarm_id) {
239
- error("HEALTH [%s]: line %zu of file '%s' states alarm entry for invalid alarm id %u (%s). Ignoring it.", rrdhost_hostname(host), line, filename, alarm_id, pointers[3]);
240
- errored++;
241
- continue;
242
- }
243
-
244
- // Check if we got last_repeat field
245
- time_t last_repeat = 0;
246
- if(entries > 27) {
247
- char* alarm_name = pointers[13];
248
- last_repeat = (time_t)strtoul(pointers[27], NULL, 16);
249
-
250
- rc = dictionary_get(all_rrdcalcs, alarm_name);
251
- if(unlikely(rc)) {
252
- if (rrdcalc_isrepeating(rc)) {
253
- rc->last_repeat = last_repeat;
254
- // We iterate through repeating alarm entries only to
255
- // find the latest last_repeat timestamp. Otherwise,
256
- // there is no need to keep them in memory.
257
- continue;
258
- }
259
- }
260
- }
261
-
262
- if(unlikely(*pointers[0] == 'A')) {
263
- // make sure it is properly numbered
264
- if(unlikely(host->health_log.alarms && unique_id < host->health_log.alarms->unique_id)) {
265
- error( "HEALTH [%s]: line %zu of file '%s' has alarm log entry %u in wrong order. Ignoring it."
266
- , rrdhost_hostname(host), line, filename, unique_id);
267
- errored++;
268
- continue;
269
- }
270
-
271
- ae = callocz(1, sizeof(ALARM_ENTRY));
272
- }
273
- else if(unlikely(*pointers[0] == 'U')) {
274
- // find the original
275
- for(ae = host->health_log.alarms; ae ; ae = ae->next) {
276
- if(unlikely(unique_id == ae->unique_id)) {
277
- if(unlikely(*pointers[0] == 'A')) {
278
- error("HEALTH [%s]: line %zu of file '%s' adds duplicate alarm log entry %u. Using the later."
279
- , rrdhost_hostname(host), line, filename, unique_id);
280
- *pointers[0] = 'U';
281
- duplicate++;
282
- }
283
- break;
284
- }
285
- else if(unlikely(unique_id > ae->unique_id)) {
286
- // no need to continue
287
- // the linked list is sorted
288
- ae = NULL;
289
- break;
290
- }
291
- }
292
- }
293
-
294
- // if not found, skip this line
295
- if(unlikely(!ae)) {
296
- // 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);
297
- continue;
298
- }
299
-
300
- // check for a possible host mismatch
301
- //if(strcmp(pointers[1], host->hostname))
302
- // 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);
303
-
304
- ae->unique_id = unique_id;
305
- if (!is_valid_alarm_id(host, pointers[14], pointers[13], alarm_id)) {
306
- STRING *chart = string_strdupz(pointers[14]);
307
- STRING *name = string_strdupz(pointers[13]);
308
- alarm_id = rrdcalc_get_unique_id(host, chart, name, NULL);
309
- string_freez(chart);
310
- string_freez(name);
311
- }
312
- ae->alarm_id = alarm_id;
313
- ae->alarm_event_id = (uint32_t)strtoul(pointers[4], NULL, 16);
314
- ae->updated_by_id = (uint32_t)strtoul(pointers[5], NULL, 16);
315
- ae->updates_id = (uint32_t)strtoul(pointers[6], NULL, 16);
316
-
317
- ae->when = (uint32_t)strtoul(pointers[7], NULL, 16);
318
- ae->duration = (uint32_t)strtoul(pointers[8], NULL, 16);
319
- ae->non_clear_duration = (uint32_t)strtoul(pointers[9], NULL, 16);
320
-
321
- ae->flags = (uint32_t)strtoul(pointers[10], NULL, 16);
322
- ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
323
-
324
- ae->exec_run_timestamp = (uint32_t)strtoul(pointers[11], NULL, 16);
325
- ae->delay_up_to_timestamp = (uint32_t)strtoul(pointers[12], NULL, 16);
326
-
327
- string_freez(ae->name);
328
- ae->name = string_strdupz(pointers[13]);
329
-
330
- string_freez(ae->chart);
331
- ae->chart = string_strdupz(pointers[14]);
332
-
333
- string_freez(ae->family);
334
- ae->family = string_strdupz(pointers[15]);
335
-
336
- string_freez(ae->exec);
337
- ae->exec = string_strdupz(pointers[16]);
338
-
339
- string_freez(ae->recipient);
340
- ae->recipient = string_strdupz(pointers[17]);
341
-
342
- string_freez(ae->source);
343
- ae->source = string_strdupz(pointers[18]);
344
-
345
- string_freez(ae->units);
346
- ae->units = string_strdupz(pointers[19]);
347
-
348
- string_freez(ae->info);
349
- ae->info = string_strdupz(pointers[20]);
350
-
351
- ae->exec_code = str2i(pointers[21]);
352
- ae->new_status = str2i(pointers[22]);
353
- ae->old_status = str2i(pointers[23]);
354
- ae->delay = str2i(pointers[24]);
355
-
356
- ae->new_value = str2l(pointers[25]);
357
- ae->old_value = str2l(pointers[26]);
358
-
359
- ae->last_repeat = last_repeat;
360
-
361
- if (likely(entries > 30)) {
362
- string_freez(ae->classification);
363
- ae->classification = string_strdupz(pointers[28]);
364
-
365
- string_freez(ae->component);
366
- ae->component = string_strdupz(pointers[29]);
367
-
368
- string_freez(ae->type);
369
- ae->type = string_strdupz(pointers[30]);
370
- }
371
-
372
- char value_string[100 + 1];
373
- string_freez(ae->old_value_string);
374
- string_freez(ae->new_value_string);
375
- ae->old_value_string = string_strdupz(format_value_and_unit(value_string, 100, ae->old_value, ae_units(ae), -1));
376
- ae->new_value_string = string_strdupz(format_value_and_unit(value_string, 100, ae->new_value, ae_units(ae), -1));
377
-
378
- // add it to host if not already there
379
- if(unlikely(*pointers[0] == 'A')) {
380
- ae->next = host->health_log.alarms;
381
- host->health_log.alarms = ae;
382
- sql_health_alarm_log_insert(host, ae);
383
- loaded++;
384
- }
385
- else {
386
- sql_health_alarm_log_update(host, ae);
387
- updated++;
388
- }
389
-
390
- if(unlikely(ae->unique_id > host->health_max_unique_id))
391
- host->health_max_unique_id = ae->unique_id;
392
-
393
- if(unlikely(ae->alarm_id >= host->health_max_alarm_id))
394
- host->health_max_alarm_id = ae->alarm_id;
395
- }
396
- else {
397
- error("HEALTH [%s]: line %zu of file '%s' is invalid (unrecognized entry type '%s').", rrdhost_hostname(host), line, filename, pointers[0]);
398
- errored++;
399
- }
400
- }
401
-
402
- netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
403
-
404
- dictionary_destroy(all_rrdcalcs);
405
- all_rrdcalcs = NULL;
406
-
407
- freez(buf);
408
-
409
- if(!host->health_max_unique_id) host->health_max_unique_id = (uint32_t)now_realtime_sec();
410
- if(!host->health_max_alarm_id) host->health_max_alarm_id = (uint32_t)now_realtime_sec();
411
-
412
- host->health_log.next_log_id = host->health_max_unique_id + 1;
413
- if (unlikely(!host->health_log.next_alarm_id || host->health_log.next_alarm_id <= host->health_max_alarm_id))
414
- host->health_log.next_alarm_id = host->health_max_alarm_id + 1;
415
-
416
- debug(D_HEALTH, "HEALTH [%s]: loaded file '%s' with %zd new alarm entries, updated %zd alarms, errors %zd entries, duplicate %zd", rrdhost_hostname(host), filename, loaded, updated, errored, duplicate);
417
- return loaded;
418
-}
419
-
420
-inline void health_alarm_log_load(RRDHOST *host) {
421
- health_alarm_log_close(host);
422
-
423
- char filename[FILENAME_MAX + 1];
424
- snprintfz(filename, FILENAME_MAX, "%s.old", host->health.health_log_filename);
425
- FILE *fp = fopen(filename, "r");
426
- if(!fp)
427
- error("HEALTH [%s]: cannot open health file: %s", rrdhost_hostname(host), filename);
428
- else {
429
- health_alarm_log_read(host, fp, filename);
430
- fclose(fp);
431
- }
432
-
433
- host->health.health_log_entries_written = 0;
434
- fp = fopen(host->health.health_log_filename, "r");
435
- if(!fp)
436
- error("HEALTH [%s]: cannot open health file: %s", rrdhost_hostname(host), host->health.health_log_filename);
437
- else {
438
- health_alarm_log_read(host, fp, host->health.health_log_filename);
439
- fclose(fp);
440
- }
441
-}
442
-
443
-
18
// ----------------------------------------------------------------------------
19
// health alarm log management
20