health_log.c 21 KB

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