health.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "health.h"
  3. #define WORKER_HEALTH_JOB_RRD_LOCK 0
  4. #define WORKER_HEALTH_JOB_HOST_LOCK 1
  5. #define WORKER_HEALTH_JOB_DB_QUERY 2
  6. #define WORKER_HEALTH_JOB_CALC_EVAL 3
  7. #define WORKER_HEALTH_JOB_WARNING_EVAL 4
  8. #define WORKER_HEALTH_JOB_CRITICAL_EVAL 5
  9. #define WORKER_HEALTH_JOB_ALARM_LOG_ENTRY 6
  10. #define WORKER_HEALTH_JOB_ALARM_LOG_PROCESS 7
  11. #define WORKER_HEALTH_JOB_DELAYED_INIT_RRDSET 8
  12. #define WORKER_HEALTH_JOB_DELAYED_INIT_RRDDIM 9
  13. #if WORKER_UTILIZATION_MAX_JOB_TYPES < 10
  14. #error WORKER_UTILIZATION_MAX_JOB_TYPES has to be at least 10
  15. #endif
  16. unsigned int default_health_enabled = 1;
  17. char *silencers_filename;
  18. SIMPLE_PATTERN *conf_enabled_alarms = NULL;
  19. DICTIONARY *health_rrdvars;
  20. void health_entry_flags_to_json_array(BUFFER *wb, const char *key, HEALTH_ENTRY_FLAGS flags) {
  21. buffer_json_member_add_array(wb, key);
  22. if(flags & HEALTH_ENTRY_FLAG_PROCESSED)
  23. buffer_json_add_array_item_string(wb, "PROCESSED");
  24. if(flags & HEALTH_ENTRY_FLAG_UPDATED)
  25. buffer_json_add_array_item_string(wb, "UPDATED");
  26. if(flags & HEALTH_ENTRY_FLAG_EXEC_RUN)
  27. buffer_json_add_array_item_string(wb, "EXEC_RUN");
  28. if(flags & HEALTH_ENTRY_FLAG_EXEC_FAILED)
  29. buffer_json_add_array_item_string(wb, "EXEC_FAILED");
  30. if(flags & HEALTH_ENTRY_FLAG_SILENCED)
  31. buffer_json_add_array_item_string(wb, "SILENCED");
  32. if(flags & HEALTH_ENTRY_RUN_ONCE)
  33. buffer_json_add_array_item_string(wb, "RUN_ONCE");
  34. if(flags & HEALTH_ENTRY_FLAG_EXEC_IN_PROGRESS)
  35. buffer_json_add_array_item_string(wb, "EXEC_IN_PROGRESS");
  36. if(flags & HEALTH_ENTRY_FLAG_IS_REPEATING)
  37. buffer_json_add_array_item_string(wb, "RECURRING");
  38. if(flags & HEALTH_ENTRY_FLAG_SAVED)
  39. buffer_json_add_array_item_string(wb, "SAVED");
  40. if(flags & HEALTH_ENTRY_FLAG_ACLK_QUEUED)
  41. buffer_json_add_array_item_string(wb, "ACLK_QUEUED");
  42. if(flags & HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION)
  43. buffer_json_add_array_item_string(wb, "NO_CLEAR_NOTIFICATION");
  44. buffer_json_array_close(wb);
  45. }
  46. static bool prepare_command(BUFFER *wb,
  47. const char *exec,
  48. const char *recipient,
  49. const char *registry_hostname,
  50. uint32_t unique_id,
  51. uint32_t alarm_id,
  52. uint32_t alarm_event_id,
  53. uint32_t when,
  54. const char *alert_name,
  55. const char *alert_chart_name,
  56. const char *new_status,
  57. const char *old_status,
  58. NETDATA_DOUBLE new_value,
  59. NETDATA_DOUBLE old_value,
  60. const char *alert_source,
  61. uint32_t duration,
  62. uint32_t non_clear_duration,
  63. const char *alert_units,
  64. const char *alert_info,
  65. const char *new_value_string,
  66. const char *old_value_string,
  67. const char *source,
  68. const char *error_msg,
  69. int n_warn,
  70. int n_crit,
  71. const char *warn_alarms,
  72. const char *crit_alarms,
  73. const char *classification,
  74. const char *edit_command,
  75. const char *machine_guid,
  76. uuid_t *transition_id,
  77. const char *summary,
  78. const char *context,
  79. const char *component,
  80. const char *type
  81. ) {
  82. char buf[8192];
  83. size_t n = sizeof(buf) - 1;
  84. buffer_strcat(wb, "exec");
  85. if (!sanitize_command_argument_string(buf, exec, n))
  86. return false;
  87. buffer_sprintf(wb, " '%s'", buf);
  88. if (!sanitize_command_argument_string(buf, recipient, n))
  89. return false;
  90. buffer_sprintf(wb, " '%s'", buf);
  91. if (!sanitize_command_argument_string(buf, registry_hostname, n))
  92. return false;
  93. buffer_sprintf(wb, " '%s'", buf);
  94. buffer_sprintf(wb, " '%u'", unique_id);
  95. buffer_sprintf(wb, " '%u'", alarm_id);
  96. buffer_sprintf(wb, " '%u'", alarm_event_id);
  97. buffer_sprintf(wb, " '%u'", when);
  98. if (!sanitize_command_argument_string(buf, alert_name, n))
  99. return false;
  100. buffer_sprintf(wb, " '%s'", buf);
  101. if (!sanitize_command_argument_string(buf, alert_chart_name, n))
  102. return false;
  103. buffer_sprintf(wb, " '%s'", buf);
  104. if (!sanitize_command_argument_string(buf, new_status, n))
  105. return false;
  106. buffer_sprintf(wb, " '%s'", buf);
  107. if (!sanitize_command_argument_string(buf, old_status, n))
  108. return false;
  109. buffer_sprintf(wb, " '%s'", buf);
  110. buffer_sprintf(wb, " '" NETDATA_DOUBLE_FORMAT_ZERO "'", new_value);
  111. buffer_sprintf(wb, " '" NETDATA_DOUBLE_FORMAT_ZERO "'", old_value);
  112. if (!sanitize_command_argument_string(buf, alert_source, n))
  113. return false;
  114. buffer_sprintf(wb, " '%s'", buf);
  115. buffer_sprintf(wb, " '%u'", duration);
  116. buffer_sprintf(wb, " '%u'", non_clear_duration);
  117. if (!sanitize_command_argument_string(buf, alert_units, n))
  118. return false;
  119. buffer_sprintf(wb, " '%s'", buf);
  120. if (!sanitize_command_argument_string(buf, alert_info, n))
  121. return false;
  122. buffer_sprintf(wb, " '%s'", buf);
  123. if (!sanitize_command_argument_string(buf, new_value_string, n))
  124. return false;
  125. buffer_sprintf(wb, " '%s'", buf);
  126. if (!sanitize_command_argument_string(buf, old_value_string, n))
  127. return false;
  128. buffer_sprintf(wb, " '%s'", buf);
  129. if (!sanitize_command_argument_string(buf, source, n))
  130. return false;
  131. buffer_sprintf(wb, " '%s'", buf);
  132. if (!sanitize_command_argument_string(buf, error_msg, n))
  133. return false;
  134. buffer_sprintf(wb, " '%s'", buf);
  135. buffer_sprintf(wb, " '%d'", n_warn);
  136. buffer_sprintf(wb, " '%d'", n_crit);
  137. if (!sanitize_command_argument_string(buf, warn_alarms, n))
  138. return false;
  139. buffer_sprintf(wb, " '%s'", buf);
  140. if (!sanitize_command_argument_string(buf, crit_alarms, n))
  141. return false;
  142. buffer_sprintf(wb, " '%s'", buf);
  143. if (!sanitize_command_argument_string(buf, classification, n))
  144. return false;
  145. buffer_sprintf(wb, " '%s'", buf);
  146. if (!sanitize_command_argument_string(buf, edit_command, n))
  147. return false;
  148. buffer_sprintf(wb, " '%s'", buf);
  149. if (!sanitize_command_argument_string(buf, machine_guid, n))
  150. return false;
  151. buffer_sprintf(wb, " '%s'", buf);
  152. char tr_id[UUID_STR_LEN];
  153. uuid_unparse_lower(*transition_id, tr_id);
  154. if (!sanitize_command_argument_string(buf, tr_id, n))
  155. return false;
  156. buffer_sprintf(wb, " '%s'", buf);
  157. if (!sanitize_command_argument_string(buf, summary, n))
  158. return false;
  159. buffer_sprintf(wb, " '%s'", buf);
  160. if (!sanitize_command_argument_string(buf, context, n))
  161. return false;
  162. buffer_sprintf(wb, " '%s'", buf);
  163. if (!sanitize_command_argument_string(buf, component, n))
  164. return false;
  165. buffer_sprintf(wb, " '%s'", buf);
  166. if (!sanitize_command_argument_string(buf, type, n))
  167. return false;
  168. buffer_sprintf(wb, " '%s'", buf);
  169. return true;
  170. }
  171. // the queue of executed alarm notifications that haven't been waited for yet
  172. static struct {
  173. ALARM_ENTRY *head; // oldest
  174. ALARM_ENTRY *tail; // latest
  175. } alarm_notifications_in_progress = {NULL, NULL};
  176. typedef struct active_alerts {
  177. char *name;
  178. time_t last_status_change;
  179. RRDCALC_STATUS status;
  180. } active_alerts_t;
  181. static inline void enqueue_alarm_notify_in_progress(ALARM_ENTRY *ae)
  182. {
  183. ae->prev_in_progress = NULL;
  184. ae->next_in_progress = NULL;
  185. if (NULL != alarm_notifications_in_progress.tail) {
  186. ae->prev_in_progress = alarm_notifications_in_progress.tail;
  187. alarm_notifications_in_progress.tail->next_in_progress = ae;
  188. }
  189. if (NULL == alarm_notifications_in_progress.head) {
  190. alarm_notifications_in_progress.head = ae;
  191. }
  192. alarm_notifications_in_progress.tail = ae;
  193. }
  194. static inline void unlink_alarm_notify_in_progress(ALARM_ENTRY *ae)
  195. {
  196. struct alarm_entry *prev = ae->prev_in_progress;
  197. struct alarm_entry *next = ae->next_in_progress;
  198. if (NULL != prev) {
  199. prev->next_in_progress = next;
  200. }
  201. if (NULL != next) {
  202. next->prev_in_progress = prev;
  203. }
  204. if (ae == alarm_notifications_in_progress.head) {
  205. alarm_notifications_in_progress.head = next;
  206. }
  207. if (ae == alarm_notifications_in_progress.tail) {
  208. alarm_notifications_in_progress.tail = prev;
  209. }
  210. }
  211. // ----------------------------------------------------------------------------
  212. // health initialization
  213. /**
  214. * User Config directory
  215. *
  216. * Get the config directory for health and return it.
  217. *
  218. * @return a pointer to the user config directory
  219. */
  220. inline char *health_user_config_dir(void) {
  221. char buffer[FILENAME_MAX + 1];
  222. snprintfz(buffer, FILENAME_MAX, "%s/health.d", netdata_configured_user_config_dir);
  223. return config_get(CONFIG_SECTION_DIRECTORIES, "health config", buffer);
  224. }
  225. /**
  226. * Stock Config Directory
  227. *
  228. * Get the Stock config directory and return it.
  229. *
  230. * @return a pointer to the stock config directory.
  231. */
  232. inline char *health_stock_config_dir(void) {
  233. char buffer[FILENAME_MAX + 1];
  234. snprintfz(buffer, FILENAME_MAX, "%s/health.d", netdata_configured_stock_config_dir);
  235. return config_get(CONFIG_SECTION_DIRECTORIES, "stock health config", buffer);
  236. }
  237. /**
  238. * Silencers init
  239. *
  240. * Function used to initialize the silencer structure.
  241. */
  242. static void health_silencers_init(void) {
  243. FILE *fd = fopen(silencers_filename, "r");
  244. if (fd) {
  245. fseek(fd, 0 , SEEK_END);
  246. off_t length = (off_t) ftell(fd);
  247. fseek(fd, 0 , SEEK_SET);
  248. if (length > 0 && length < HEALTH_SILENCERS_MAX_FILE_LEN) {
  249. char *str = mallocz((length+1)* sizeof(char));
  250. if(str) {
  251. size_t copied;
  252. copied = fread(str, sizeof(char), length, fd);
  253. if (copied == (length* sizeof(char))) {
  254. str[length] = 0x00;
  255. json_parse(str, NULL, health_silencers_json_read_callback);
  256. netdata_log_info("Parsed health silencers file %s", silencers_filename);
  257. } else {
  258. netdata_log_error("Cannot read the data from health silencers file %s", silencers_filename);
  259. }
  260. freez(str);
  261. }
  262. } else {
  263. netdata_log_error("Health silencers file %s has the size %" PRId64 " that is out of range[ 1 , %d ]. Aborting read.",
  264. silencers_filename,
  265. (int64_t)length,
  266. HEALTH_SILENCERS_MAX_FILE_LEN);
  267. }
  268. fclose(fd);
  269. } else {
  270. netdata_log_info("Cannot open the file %s, so Netdata will work with the default health configuration.",
  271. silencers_filename);
  272. }
  273. }
  274. /**
  275. * Health Init
  276. *
  277. * Initialize the health thread.
  278. */
  279. void health_init(void) {
  280. netdata_log_debug(D_HEALTH, "Health configuration initializing");
  281. if(!(default_health_enabled = (unsigned int)config_get_boolean(CONFIG_SECTION_HEALTH, "enabled", default_health_enabled))) {
  282. netdata_log_debug(D_HEALTH, "Health is disabled.");
  283. return;
  284. }
  285. health_silencers_init();
  286. }
  287. // ----------------------------------------------------------------------------
  288. // re-load health configuration
  289. /**
  290. * Reload host
  291. *
  292. * Reload configuration for a specific host.
  293. *
  294. * @param host the structure of the host that the function will reload the configuration.
  295. */
  296. static void health_reload_host(RRDHOST *host) {
  297. if(unlikely(!host->health.health_enabled) && !rrdhost_flag_check(host, RRDHOST_FLAG_INITIALIZED_HEALTH))
  298. return;
  299. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  300. "[%s]: Reloading health.",
  301. rrdhost_hostname(host));
  302. char *user_path = health_user_config_dir();
  303. char *stock_path = health_stock_config_dir();
  304. // free all running alarms
  305. rrdcalc_delete_all(host);
  306. rrdcalctemplate_delete_all(host);
  307. // invalidate all previous entries in the alarm log
  308. rw_spinlock_read_lock(&host->health_log.spinlock);
  309. ALARM_ENTRY *t;
  310. for(t = host->health_log.alarms ; t ; t = t->next) {
  311. if(t->new_status != RRDCALC_STATUS_REMOVED)
  312. t->flags |= HEALTH_ENTRY_FLAG_UPDATED;
  313. }
  314. rw_spinlock_read_unlock(&host->health_log.spinlock);
  315. // reset all thresholds to all charts
  316. RRDSET *st;
  317. rrdset_foreach_read(st, host) {
  318. st->green = NAN;
  319. st->red = NAN;
  320. }
  321. rrdset_foreach_done(st);
  322. // load the new alarms
  323. health_readdir(host, user_path, stock_path, NULL);
  324. //Discard alarms with labels that do not apply to host
  325. rrdcalc_delete_alerts_not_matching_host_labels_from_this_host(host);
  326. // link the loaded alarms to their charts
  327. rrdset_foreach_write(st, host) {
  328. rrdcalc_link_matching_alerts_to_rrdset(st);
  329. rrdcalctemplate_link_matching_templates_to_rrdset(st);
  330. }
  331. rrdset_foreach_done(st);
  332. #ifdef ENABLE_ACLK
  333. if (netdata_cloud_enabled) {
  334. struct aclk_sync_cfg_t *wc = host->aclk_config;
  335. if (likely(wc)) {
  336. wc->alert_queue_removed = SEND_REMOVED_AFTER_HEALTH_LOOPS;
  337. }
  338. }
  339. #endif
  340. }
  341. /**
  342. * Reload
  343. *
  344. * Reload the host configuration for all hosts.
  345. */
  346. void health_reload(void) {
  347. sql_refresh_hashes();
  348. RRDHOST *host;
  349. dfe_start_reentrant(rrdhost_root_index, host){
  350. health_reload_host(host);
  351. }
  352. dfe_done(host);
  353. }
  354. // ----------------------------------------------------------------------------
  355. // health main thread and friends
  356. static inline RRDCALC_STATUS rrdcalc_value2status(NETDATA_DOUBLE n) {
  357. if(isnan(n) || isinf(n)) return RRDCALC_STATUS_UNDEFINED;
  358. if(n) return RRDCALC_STATUS_RAISED;
  359. return RRDCALC_STATUS_CLEAR;
  360. }
  361. #define ACTIVE_ALARMS_LIST_EXAMINE 500
  362. #define ACTIVE_ALARMS_LIST 15
  363. static inline int compare_active_alerts(const void * a, const void * b) {
  364. active_alerts_t *active_alerts_a = (active_alerts_t *)a;
  365. active_alerts_t *active_alerts_b = (active_alerts_t *)b;
  366. return (int) ( active_alerts_b->last_status_change - active_alerts_a->last_status_change );
  367. }
  368. static inline void health_alarm_execute(RRDHOST *host, ALARM_ENTRY *ae) {
  369. ae->flags |= HEALTH_ENTRY_FLAG_PROCESSED;
  370. if(unlikely(ae->new_status < RRDCALC_STATUS_CLEAR)) {
  371. // do not send notifications for internal statuses
  372. netdata_log_debug(D_HEALTH, "Health not sending notification for alarm '%s.%s' status %s (internal statuses)", ae_chart_id(ae), ae_name(ae), rrdcalc_status2string(ae->new_status));
  373. goto done;
  374. }
  375. if(unlikely(ae->new_status <= RRDCALC_STATUS_CLEAR && (ae->flags & HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION))) {
  376. // do not send notifications for disabled statuses
  377. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  378. "[%s]: Health not sending notification for alarm '%s.%s' status %s (it has no-clear-notification enabled)",
  379. rrdhost_hostname(host), ae_chart_id(ae), ae_name(ae), rrdcalc_status2string(ae->new_status));
  380. // mark it as run, so that we will send the same alarm if it happens again
  381. goto done;
  382. }
  383. // find the previous notification for the same alarm
  384. // which we have run the exec script
  385. // exception: alarms with HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION set
  386. RRDCALC_STATUS last_executed_status = -3;
  387. if(likely(!(ae->flags & HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION))) {
  388. int ret = sql_health_get_last_executed_event(host, ae, &last_executed_status);
  389. if (likely(ret == 1)) {
  390. // we have executed this alarm notification in the past
  391. if(last_executed_status == ae->new_status && !(ae->flags & HEALTH_ENTRY_FLAG_IS_REPEATING)) {
  392. // don't send the notification for the same status again
  393. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  394. "[%s]: Health not sending again notification for alarm '%s.%s' status %s",
  395. rrdhost_hostname(host), ae_chart_id(ae), ae_name(ae),
  396. rrdcalc_status2string(ae->new_status));
  397. goto done;
  398. }
  399. }
  400. else {
  401. // we have not executed this alarm notification in the past
  402. // so, don't send CLEAR notifications
  403. if(unlikely(ae->new_status == RRDCALC_STATUS_CLEAR)) {
  404. if((!(ae->flags & HEALTH_ENTRY_RUN_ONCE)) || (ae->flags & HEALTH_ENTRY_RUN_ONCE && ae->old_status < RRDCALC_STATUS_RAISED) ) {
  405. netdata_log_debug(D_HEALTH, "Health not sending notification for first initialization of alarm '%s.%s' status %s"
  406. , ae_chart_id(ae), ae_name(ae), rrdcalc_status2string(ae->new_status));
  407. goto done;
  408. }
  409. }
  410. }
  411. }
  412. // Check if alarm notifications are silenced
  413. if (ae->flags & HEALTH_ENTRY_FLAG_SILENCED) {
  414. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  415. "[%s]: Health not sending notification for alarm '%s.%s' status %s "
  416. "(command API has disabled notifications)",
  417. rrdhost_hostname(host), ae_chart_id(ae), ae_name(ae), rrdcalc_status2string(ae->new_status));
  418. goto done;
  419. }
  420. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  421. "[%s]: Sending notification for alarm '%s.%s' status %s.",
  422. rrdhost_hostname(host), ae_chart_id(ae), ae_name(ae), rrdcalc_status2string(ae->new_status));
  423. const char *exec = (ae->exec) ? ae_exec(ae) : string2str(host->health.health_default_exec);
  424. const char *recipient = (ae->recipient) ? ae_recipient(ae) : string2str(host->health.health_default_recipient);
  425. int n_warn=0, n_crit=0;
  426. RRDCALC *rc;
  427. EVAL_EXPRESSION *expr=NULL;
  428. BUFFER *warn_alarms, *crit_alarms;
  429. active_alerts_t *active_alerts = callocz(ACTIVE_ALARMS_LIST_EXAMINE, sizeof(active_alerts_t));
  430. warn_alarms = buffer_create(NETDATA_WEB_RESPONSE_INITIAL_SIZE, &netdata_buffers_statistics.buffers_health);
  431. crit_alarms = buffer_create(NETDATA_WEB_RESPONSE_INITIAL_SIZE, &netdata_buffers_statistics.buffers_health);
  432. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  433. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  434. continue;
  435. if(unlikely((n_warn + n_crit) >= ACTIVE_ALARMS_LIST_EXAMINE))
  436. break;
  437. if (unlikely(rc->status == RRDCALC_STATUS_WARNING)) {
  438. if (likely(ae->alarm_id != rc->id) || likely(ae->alarm_event_id != rc->next_event_id - 1)) {
  439. active_alerts[n_warn+n_crit].name = (char *)rrdcalc_name(rc);
  440. active_alerts[n_warn+n_crit].last_status_change = rc->last_status_change;
  441. active_alerts[n_warn+n_crit].status = rc->status;
  442. n_warn++;
  443. } else if (ae->alarm_id == rc->id)
  444. expr = rc->warning;
  445. } else if (unlikely(rc->status == RRDCALC_STATUS_CRITICAL)) {
  446. if (likely(ae->alarm_id != rc->id) || likely(ae->alarm_event_id != rc->next_event_id - 1)) {
  447. active_alerts[n_warn+n_crit].name = (char *)rrdcalc_name(rc);
  448. active_alerts[n_warn+n_crit].last_status_change = rc->last_status_change;
  449. active_alerts[n_warn+n_crit].status = rc->status;
  450. n_crit++;
  451. } else if (ae->alarm_id == rc->id)
  452. expr = rc->critical;
  453. } else if (unlikely(rc->status == RRDCALC_STATUS_CLEAR)) {
  454. if (ae->alarm_id == rc->id)
  455. expr = rc->warning;
  456. }
  457. }
  458. foreach_rrdcalc_in_rrdhost_done(rc);
  459. if (n_warn+n_crit>1)
  460. qsort (active_alerts, n_warn+n_crit, sizeof(active_alerts_t), compare_active_alerts);
  461. int count_w = 0, count_c = 0;
  462. while (count_w + count_c < n_warn + n_crit && count_w + count_c < ACTIVE_ALARMS_LIST) {
  463. if (active_alerts[count_w+count_c].status == RRDCALC_STATUS_WARNING) {
  464. if (count_w)
  465. buffer_strcat(warn_alarms, ",");
  466. buffer_strcat(warn_alarms, active_alerts[count_w+count_c].name);
  467. buffer_strcat(warn_alarms, "=");
  468. buffer_snprintf(warn_alarms, 11, "%"PRId64"", (int64_t)active_alerts[count_w+count_c].last_status_change);
  469. count_w++;
  470. }
  471. else if (active_alerts[count_w+count_c].status == RRDCALC_STATUS_CRITICAL) {
  472. if (count_c)
  473. buffer_strcat(crit_alarms, ",");
  474. buffer_strcat(crit_alarms, active_alerts[count_w+count_c].name);
  475. buffer_strcat(crit_alarms, "=");
  476. buffer_snprintf(crit_alarms, 11, "%"PRId64"", (int64_t)active_alerts[count_w+count_c].last_status_change);
  477. count_c++;
  478. }
  479. }
  480. char *edit_command = ae->source ? health_edit_command_from_source(ae_source(ae)) : strdupz("UNKNOWN=0=UNKNOWN");
  481. BUFFER *wb = buffer_create(8192, &netdata_buffers_statistics.buffers_health);
  482. bool ok = prepare_command(wb,
  483. exec,
  484. recipient,
  485. rrdhost_registry_hostname(host),
  486. ae->unique_id,
  487. ae->alarm_id,
  488. ae->alarm_event_id,
  489. (unsigned long)ae->when,
  490. ae_name(ae),
  491. ae->chart?ae_chart_id(ae):"NOCHART",
  492. rrdcalc_status2string(ae->new_status),
  493. rrdcalc_status2string(ae->old_status),
  494. ae->new_value,
  495. ae->old_value,
  496. ae->source?ae_source(ae):"UNKNOWN",
  497. (uint32_t)ae->duration,
  498. (ae->flags & HEALTH_ENTRY_FLAG_IS_REPEATING && ae->new_status >= RRDCALC_STATUS_WARNING) ? (uint32_t)ae->duration : (uint32_t)ae->non_clear_duration,
  499. ae_units(ae),
  500. ae_info(ae),
  501. ae_new_value_string(ae),
  502. ae_old_value_string(ae),
  503. (expr && expr->source)?expr->source:"NOSOURCE",
  504. (expr && expr->error_msg)?buffer_tostring(expr->error_msg):"NOERRMSG",
  505. n_warn,
  506. n_crit,
  507. buffer_tostring(warn_alarms),
  508. buffer_tostring(crit_alarms),
  509. ae->classification?ae_classification(ae):"Unknown",
  510. edit_command,
  511. host->machine_guid,
  512. &ae->transition_id,
  513. host->health.use_summary_for_notifications && ae->summary?ae_summary(ae):ae_name(ae),
  514. string2str(ae->chart_context),
  515. string2str(ae->component),
  516. string2str(ae->type)
  517. );
  518. const char *command_to_run = buffer_tostring(wb);
  519. if (ok) {
  520. ae->flags |= HEALTH_ENTRY_FLAG_EXEC_RUN;
  521. ae->exec_run_timestamp = now_realtime_sec(); /* will be updated by real time after spawning */
  522. netdata_log_debug(D_HEALTH, "executing command '%s'", command_to_run);
  523. ae->flags |= HEALTH_ENTRY_FLAG_EXEC_IN_PROGRESS;
  524. ae->exec_spawn_serial = spawn_enq_cmd(command_to_run);
  525. enqueue_alarm_notify_in_progress(ae);
  526. health_alarm_log_save(host, ae);
  527. } else {
  528. netdata_log_error("Failed to format command arguments");
  529. }
  530. buffer_free(wb);
  531. freez(edit_command);
  532. buffer_free(warn_alarms);
  533. buffer_free(crit_alarms);
  534. freez(active_alerts);
  535. return; //health_alarm_wait_for_execution
  536. done:
  537. health_alarm_log_save(host, ae);
  538. }
  539. static inline void health_alarm_wait_for_execution(ALARM_ENTRY *ae) {
  540. if (!(ae->flags & HEALTH_ENTRY_FLAG_EXEC_IN_PROGRESS))
  541. return;
  542. spawn_wait_cmd(ae->exec_spawn_serial, &ae->exec_code, &ae->exec_run_timestamp);
  543. netdata_log_debug(D_HEALTH, "done executing command - returned with code %d", ae->exec_code);
  544. ae->flags &= ~HEALTH_ENTRY_FLAG_EXEC_IN_PROGRESS;
  545. if(ae->exec_code != 0)
  546. ae->flags |= HEALTH_ENTRY_FLAG_EXEC_FAILED;
  547. unlink_alarm_notify_in_progress(ae);
  548. }
  549. static inline void health_process_notifications(RRDHOST *host, ALARM_ENTRY *ae) {
  550. netdata_log_debug(D_HEALTH, "Health alarm '%s.%s' = " NETDATA_DOUBLE_FORMAT_AUTO " - changed status from %s to %s",
  551. ae->chart?ae_chart_id(ae):"NOCHART", ae_name(ae),
  552. ae->new_value,
  553. rrdcalc_status2string(ae->old_status),
  554. rrdcalc_status2string(ae->new_status)
  555. );
  556. health_alarm_execute(host, ae);
  557. }
  558. static inline void health_alarm_log_process(RRDHOST *host) {
  559. uint32_t first_waiting = (host->health_log.alarms)?host->health_log.alarms->unique_id:0;
  560. time_t now = now_realtime_sec();
  561. rw_spinlock_read_lock(&host->health_log.spinlock);
  562. ALARM_ENTRY *ae;
  563. for(ae = host->health_log.alarms; ae && ae->unique_id >= host->health_last_processed_id; ae = ae->next) {
  564. if(unlikely(
  565. !(ae->flags & HEALTH_ENTRY_FLAG_PROCESSED) &&
  566. !(ae->flags & HEALTH_ENTRY_FLAG_UPDATED)
  567. )) {
  568. if(unlikely(ae->unique_id < first_waiting))
  569. first_waiting = ae->unique_id;
  570. if(likely(now >= ae->delay_up_to_timestamp))
  571. health_process_notifications(host, ae);
  572. }
  573. }
  574. rw_spinlock_read_unlock(&host->health_log.spinlock);
  575. // remember this for the next iteration
  576. host->health_last_processed_id = first_waiting;
  577. //delete those that are updated, no in progress execution, and is not repeating
  578. rw_spinlock_write_lock(&host->health_log.spinlock);
  579. ALARM_ENTRY *prev = NULL, *next = NULL;
  580. for(ae = host->health_log.alarms; ae ; ae = next) {
  581. next = ae->next; // set it here, for the next iteration
  582. if((likely(!(ae->flags & HEALTH_ENTRY_FLAG_IS_REPEATING)) &&
  583. (ae->flags & HEALTH_ENTRY_FLAG_UPDATED) &&
  584. (ae->flags & HEALTH_ENTRY_FLAG_SAVED) &&
  585. !(ae->flags & HEALTH_ENTRY_FLAG_EXEC_IN_PROGRESS))
  586. ||
  587. ((ae->new_status == RRDCALC_STATUS_REMOVED) &&
  588. (ae->flags & HEALTH_ENTRY_FLAG_SAVED) &&
  589. (ae->when + 86400 < now_realtime_sec())))
  590. {
  591. if(host->health_log.alarms == ae) {
  592. host->health_log.alarms = next;
  593. // prev is also NULL here
  594. }
  595. else {
  596. prev->next = next;
  597. // prev should not be touched here - we need it for the next iteration
  598. // because we may have to also remove the next item
  599. }
  600. health_alarm_log_free_one_nochecks_nounlink(ae);
  601. }
  602. else
  603. prev = ae;
  604. }
  605. rw_spinlock_write_unlock(&host->health_log.spinlock);
  606. }
  607. static inline int rrdcalc_isrunnable(RRDCALC *rc, time_t now, time_t *next_run) {
  608. if(unlikely(!rc->rrdset)) {
  609. netdata_log_debug(D_HEALTH, "Health not running alarm '%s.%s'. It is not linked to a chart.", rrdcalc_chart_name(rc), rrdcalc_name(rc));
  610. return 0;
  611. }
  612. if(unlikely(rc->next_update > now)) {
  613. if (unlikely(*next_run > rc->next_update)) {
  614. // update the next_run time of the main loop
  615. // to run this alarm precisely the time required
  616. *next_run = rc->next_update;
  617. }
  618. netdata_log_debug(D_HEALTH, "Health not examining alarm '%s.%s' yet (will do in %d secs).", rrdcalc_chart_name(rc), rrdcalc_name(rc), (int) (rc->next_update - now));
  619. return 0;
  620. }
  621. if(unlikely(!rc->update_every)) {
  622. netdata_log_debug(D_HEALTH, "Health not running alarm '%s.%s'. It does not have an update frequency", rrdcalc_chart_name(rc), rrdcalc_name(rc));
  623. return 0;
  624. }
  625. if(unlikely(rrdset_flag_check(rc->rrdset, RRDSET_FLAG_OBSOLETE))) {
  626. netdata_log_debug(D_HEALTH, "Health not running alarm '%s.%s'. The chart has been marked as obsolete", rrdcalc_chart_name(rc), rrdcalc_name(rc));
  627. return 0;
  628. }
  629. if(unlikely(!rc->rrdset->last_collected_time.tv_sec || rc->rrdset->counter_done < 2)) {
  630. netdata_log_debug(D_HEALTH, "Health not running alarm '%s.%s'. Chart is not fully collected yet.", rrdcalc_chart_name(rc), rrdcalc_name(rc));
  631. return 0;
  632. }
  633. int update_every = rc->rrdset->update_every;
  634. time_t first = rrdset_first_entry_s(rc->rrdset);
  635. time_t last = rrdset_last_entry_s(rc->rrdset);
  636. if(unlikely(now + update_every < first /* || now - update_every > last */)) {
  637. netdata_log_debug(D_HEALTH
  638. , "Health not examining alarm '%s.%s' yet (wanted time is out of bounds - we need %lu but got %lu - %lu)."
  639. , rrdcalc_chart_name(rc), rrdcalc_name(rc), (unsigned long) now, (unsigned long) first
  640. , (unsigned long) last);
  641. return 0;
  642. }
  643. if(RRDCALC_HAS_DB_LOOKUP(rc)) {
  644. time_t needed = now + rc->before + rc->after;
  645. if(needed + update_every < first || needed - update_every > last) {
  646. netdata_log_debug(D_HEALTH
  647. , "Health not examining alarm '%s.%s' yet (not enough data yet - we need %lu but got %lu - %lu)."
  648. , rrdcalc_chart_name(rc), rrdcalc_name(rc), (unsigned long) needed, (unsigned long) first
  649. , (unsigned long) last);
  650. return 0;
  651. }
  652. }
  653. return 1;
  654. }
  655. static inline int check_if_resumed_from_suspension(void) {
  656. static usec_t last_realtime = 0, last_monotonic = 0;
  657. usec_t realtime = now_realtime_usec(), monotonic = now_monotonic_usec();
  658. int ret = 0;
  659. // detect if monotonic and realtime have twice the difference
  660. // in which case we assume the system was just waken from hibernation
  661. if(last_realtime && last_monotonic && realtime - last_realtime > 2 * (monotonic - last_monotonic))
  662. ret = 1;
  663. last_realtime = realtime;
  664. last_monotonic = monotonic;
  665. return ret;
  666. }
  667. static void health_main_cleanup(void *ptr) {
  668. worker_unregister();
  669. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  670. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  671. netdata_log_info("cleaning up...");
  672. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  673. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  674. "Health thread ended.");
  675. }
  676. static void initialize_health(RRDHOST *host)
  677. {
  678. if(!host->health.health_enabled ||
  679. rrdhost_flag_check(host, RRDHOST_FLAG_INITIALIZED_HEALTH) ||
  680. !service_running(SERVICE_HEALTH))
  681. return;
  682. rrdhost_flag_set(host, RRDHOST_FLAG_INITIALIZED_HEALTH);
  683. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  684. "[%s]: Initializing health.",
  685. rrdhost_hostname(host));
  686. host->health.health_default_warn_repeat_every = config_get_duration(CONFIG_SECTION_HEALTH, "default repeat warning", "never");
  687. host->health.health_default_crit_repeat_every = config_get_duration(CONFIG_SECTION_HEALTH, "default repeat critical", "never");
  688. host->health_log.next_log_id = 1;
  689. host->health_log.next_alarm_id = 1;
  690. host->health_log.max = 1000;
  691. host->health_log.next_log_id = (uint32_t)now_realtime_sec();
  692. host->health_log.next_alarm_id = 0;
  693. long n = config_get_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", host->health_log.max);
  694. if(n < 10) {
  695. nd_log(NDLS_DAEMON, NDLP_WARNING,
  696. "Host '%s': health configuration has invalid max log entries %ld. "
  697. "Using default %u",
  698. rrdhost_hostname(host), n, host->health_log.max);
  699. config_set_number(CONFIG_SECTION_HEALTH, "in memory max health log entries", (long)host->health_log.max);
  700. }
  701. else
  702. host->health_log.max = (unsigned int)n;
  703. uint32_t m = config_get_number(CONFIG_SECTION_HEALTH, "health log history", HEALTH_LOG_DEFAULT_HISTORY);
  704. if (m < HEALTH_LOG_MINIMUM_HISTORY) {
  705. nd_log(NDLS_DAEMON, NDLP_WARNING,
  706. "Host '%s': health configuration has invalid health log history %u. "
  707. "Using minimum %d",
  708. rrdhost_hostname(host), m, HEALTH_LOG_MINIMUM_HISTORY);
  709. config_set_number(CONFIG_SECTION_HEALTH, "health log history", HEALTH_LOG_MINIMUM_HISTORY);
  710. m = HEALTH_LOG_MINIMUM_HISTORY;
  711. }
  712. //default health log history is 5 days and not less than a day
  713. if (host->health_log.health_log_history) {
  714. if (host->health_log.health_log_history < HEALTH_LOG_MINIMUM_HISTORY)
  715. host->health_log.health_log_history = HEALTH_LOG_MINIMUM_HISTORY;
  716. } else
  717. host->health_log.health_log_history = m;
  718. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  719. "[%s]: Health log history is set to %u seconds (%u days)",
  720. rrdhost_hostname(host), host->health_log.health_log_history, host->health_log.health_log_history / 86400);
  721. conf_enabled_alarms = simple_pattern_create(config_get(CONFIG_SECTION_HEALTH, "enabled alarms", "*"), NULL,
  722. SIMPLE_PATTERN_EXACT, true);
  723. rw_spinlock_init(&host->health_log.spinlock);
  724. char filename[FILENAME_MAX + 1];
  725. snprintfz(filename, FILENAME_MAX, "%s/alarm-notify.sh", netdata_configured_primary_plugins_dir);
  726. host->health.health_default_exec = string_strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename));
  727. host->health.health_default_recipient = string_strdupz("root");
  728. host->health.use_summary_for_notifications = config_get_boolean(CONFIG_SECTION_HEALTH, "use summary for notifications", CONFIG_BOOLEAN_YES);
  729. sql_health_alarm_log_load(host);
  730. // ------------------------------------------------------------------------
  731. // load health configuration
  732. health_readdir(host, health_user_config_dir(), health_stock_config_dir(), NULL);
  733. // link the loaded alarms to their charts
  734. RRDSET *st;
  735. rrdset_foreach_reentrant(st, host) {
  736. rrdcalc_link_matching_alerts_to_rrdset(st);
  737. rrdcalctemplate_link_matching_templates_to_rrdset(st);
  738. }
  739. rrdset_foreach_done(st);
  740. //Discard alarms with labels that do not apply to host
  741. rrdcalc_delete_alerts_not_matching_host_labels_from_this_host(host);
  742. }
  743. static void health_sleep(time_t next_run, unsigned int loop __maybe_unused) {
  744. time_t now = now_realtime_sec();
  745. if(now < next_run) {
  746. worker_is_idle();
  747. netdata_log_debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration in %d secs", loop, (int) (next_run - now));
  748. while (now < next_run && service_running(SERVICE_HEALTH)) {
  749. sleep_usec(USEC_PER_SEC);
  750. now = now_realtime_sec();
  751. }
  752. }
  753. else {
  754. netdata_log_debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration now", loop);
  755. }
  756. }
  757. static SILENCE_TYPE check_silenced(RRDCALC *rc, const char *host)
  758. {
  759. SILENCER *s;
  760. for (s = silencers->silencers; s!=NULL; s=s->next){
  761. if (
  762. (!s->alarms_pattern || (rc->name && s->alarms_pattern && simple_pattern_matches_string(s->alarms_pattern, rc->name))) &&
  763. (!s->contexts_pattern || (rc->rrdset && rc->rrdset->context && s->contexts_pattern && simple_pattern_matches_string(s->contexts_pattern, rc->rrdset->context))) &&
  764. (!s->hosts_pattern || (host && s->hosts_pattern && simple_pattern_matches(s->hosts_pattern, host))) &&
  765. (!s->charts_pattern || (rc->chart && s->charts_pattern && simple_pattern_matches_string(s->charts_pattern, rc->chart)))
  766. ) {
  767. netdata_log_debug(D_HEALTH, "Alarm matches command API silence entry %s:%s:%s:%s", s->alarms,s->charts, s->contexts, s->hosts);
  768. if (unlikely(silencers->stype == STYPE_NONE)) {
  769. netdata_log_debug(D_HEALTH, "Alarm %s matched a silence entry, but no SILENCE or DISABLE command was issued via the command API. The match has no effect.", rrdcalc_name(rc));
  770. } else {
  771. netdata_log_debug(D_HEALTH, "Alarm %s via the command API - name:%s context:%s chart:%s host:%s"
  772. , (silencers->stype == STYPE_DISABLE_ALARMS)?"Disabled":"Silenced"
  773. , rrdcalc_name(rc)
  774. , (rc->rrdset)?rrdset_context(rc->rrdset):""
  775. , rrdcalc_chart_name(rc)
  776. , host
  777. );
  778. }
  779. return silencers->stype;
  780. }
  781. }
  782. return STYPE_NONE;
  783. }
  784. /**
  785. * Update Disabled Silenced
  786. *
  787. * Update the variable rrdcalc_flags of the structure RRDCALC according with the values of the host structure
  788. *
  789. * @param host structure that contains information about the host monitored.
  790. * @param rc structure with information about the alarm
  791. *
  792. * @return It returns 1 case rrdcalc_flags is DISABLED or 0 otherwise
  793. */
  794. static int update_disabled_silenced(RRDHOST *host, RRDCALC *rc) {
  795. uint32_t rrdcalc_flags_old = rc->run_flags;
  796. // Clear the flags
  797. rc->run_flags &= ~(RRDCALC_FLAG_DISABLED | RRDCALC_FLAG_SILENCED);
  798. if (unlikely(silencers->all_alarms)) {
  799. if (silencers->stype == STYPE_DISABLE_ALARMS) rc->run_flags |= RRDCALC_FLAG_DISABLED;
  800. else if (silencers->stype == STYPE_SILENCE_NOTIFICATIONS) rc->run_flags |= RRDCALC_FLAG_SILENCED;
  801. } else {
  802. SILENCE_TYPE st = check_silenced(rc, rrdhost_hostname(host));
  803. if (st == STYPE_DISABLE_ALARMS) rc->run_flags |= RRDCALC_FLAG_DISABLED;
  804. else if (st == STYPE_SILENCE_NOTIFICATIONS) rc->run_flags |= RRDCALC_FLAG_SILENCED;
  805. }
  806. if (rrdcalc_flags_old != rc->run_flags) {
  807. netdata_log_info(
  808. "Alarm silencing changed for host '%s' alarm '%s': Disabled %s->%s Silenced %s->%s",
  809. rrdhost_hostname(host),
  810. rrdcalc_name(rc),
  811. (rrdcalc_flags_old & RRDCALC_FLAG_DISABLED) ? "true" : "false",
  812. (rc->run_flags & RRDCALC_FLAG_DISABLED) ? "true" : "false",
  813. (rrdcalc_flags_old & RRDCALC_FLAG_SILENCED) ? "true" : "false",
  814. (rc->run_flags & RRDCALC_FLAG_SILENCED) ? "true" : "false");
  815. }
  816. if (rc->run_flags & RRDCALC_FLAG_DISABLED)
  817. return 1;
  818. else
  819. return 0;
  820. }
  821. static void sql_health_postpone_queue_removed(RRDHOST *host __maybe_unused) {
  822. #ifdef ENABLE_ACLK
  823. if (netdata_cloud_enabled) {
  824. struct aclk_sync_cfg_t *wc = host->aclk_config;
  825. if (unlikely(!wc)) {
  826. return;
  827. }
  828. if (wc->alert_queue_removed >= 1) {
  829. wc->alert_queue_removed+=6;
  830. }
  831. }
  832. #endif
  833. }
  834. static void health_execute_delayed_initializations(RRDHOST *host) {
  835. RRDSET *st;
  836. bool must_postpone = false;
  837. if (!rrdhost_flag_check(host, RRDHOST_FLAG_PENDING_HEALTH_INITIALIZATION)) return;
  838. rrdhost_flag_clear(host, RRDHOST_FLAG_PENDING_HEALTH_INITIALIZATION);
  839. rrdset_foreach_reentrant(st, host) {
  840. if(!rrdset_flag_check(st, RRDSET_FLAG_PENDING_HEALTH_INITIALIZATION)) continue;
  841. rrdset_flag_clear(st, RRDSET_FLAG_PENDING_HEALTH_INITIALIZATION);
  842. worker_is_busy(WORKER_HEALTH_JOB_DELAYED_INIT_RRDSET);
  843. rrdcalc_link_matching_alerts_to_rrdset(st);
  844. rrdcalctemplate_link_matching_templates_to_rrdset(st);
  845. RRDDIM *rd;
  846. rrddim_foreach_read(rd, st) {
  847. if(!rrddim_flag_check(rd, RRDDIM_FLAG_PENDING_HEALTH_INITIALIZATION)) continue;
  848. rrddim_flag_clear(rd, RRDDIM_FLAG_PENDING_HEALTH_INITIALIZATION);
  849. worker_is_busy(WORKER_HEALTH_JOB_DELAYED_INIT_RRDDIM);
  850. RRDCALCTEMPLATE *rt;
  851. foreach_rrdcalctemplate_read(host, rt) {
  852. if(!rt->foreach_dimension_pattern)
  853. continue;
  854. if(rrdcalctemplate_check_rrdset_conditions(rt, st, host)) {
  855. rrdcalctemplate_check_rrddim_conditions_and_link(rt, st, rd, host);
  856. }
  857. }
  858. foreach_rrdcalctemplate_done(rt);
  859. if (health_variable_check(health_rrdvars, st, rd) || rrdset_flag_check(st, RRDSET_FLAG_HAS_RRDCALC_LINKED))
  860. rrdvar_store_for_chart(host, st);
  861. }
  862. rrddim_foreach_done(rd);
  863. must_postpone = true;
  864. }
  865. rrdset_foreach_done(st);
  866. if (must_postpone)
  867. sql_health_postpone_queue_removed(host);
  868. }
  869. /**
  870. * Health Main
  871. *
  872. * The main thread of the health system. In this function all the alarms will be processed.
  873. *
  874. * @param ptr is a pointer to the netdata_static_thread structure.
  875. *
  876. * @return It always returns NULL
  877. */
  878. void *health_main(void *ptr) {
  879. worker_register("HEALTH");
  880. worker_register_job_name(WORKER_HEALTH_JOB_RRD_LOCK, "rrd lock");
  881. worker_register_job_name(WORKER_HEALTH_JOB_HOST_LOCK, "host lock");
  882. worker_register_job_name(WORKER_HEALTH_JOB_DB_QUERY, "db lookup");
  883. worker_register_job_name(WORKER_HEALTH_JOB_CALC_EVAL, "calc eval");
  884. worker_register_job_name(WORKER_HEALTH_JOB_WARNING_EVAL, "warning eval");
  885. worker_register_job_name(WORKER_HEALTH_JOB_CRITICAL_EVAL, "critical eval");
  886. worker_register_job_name(WORKER_HEALTH_JOB_ALARM_LOG_ENTRY, "alarm log entry");
  887. worker_register_job_name(WORKER_HEALTH_JOB_ALARM_LOG_PROCESS, "alarm log process");
  888. worker_register_job_name(WORKER_HEALTH_JOB_DELAYED_INIT_RRDSET, "rrdset init");
  889. worker_register_job_name(WORKER_HEALTH_JOB_DELAYED_INIT_RRDDIM, "rrddim init");
  890. netdata_thread_cleanup_push(health_main_cleanup, ptr);
  891. int min_run_every = (int)config_get_number(CONFIG_SECTION_HEALTH, "run at least every seconds", 10);
  892. if(min_run_every < 1) min_run_every = 1;
  893. time_t hibernation_delay = config_get_number(CONFIG_SECTION_HEALTH, "postpone alarms during hibernation for seconds", 60);
  894. bool health_running_logged = false;
  895. rrdcalc_delete_alerts_not_matching_host_labels_from_all_hosts();
  896. unsigned int loop = 0;
  897. while(service_running(SERVICE_HEALTH)) {
  898. loop++;
  899. netdata_log_debug(D_HEALTH, "Health monitoring iteration no %u started", loop);
  900. time_t now = now_realtime_sec();
  901. int runnable = 0, apply_hibernation_delay = 0;
  902. time_t next_run = now + min_run_every;
  903. RRDCALC *rc;
  904. RRDHOST *host;
  905. if (unlikely(check_if_resumed_from_suspension())) {
  906. apply_hibernation_delay = 1;
  907. nd_log(NDLS_DAEMON, NDLP_NOTICE,
  908. "Postponing alarm checks for %"PRId64" seconds, "
  909. "because it seems that the system was just resumed from suspension.",
  910. (int64_t)hibernation_delay);
  911. }
  912. if (unlikely(silencers->all_alarms && silencers->stype == STYPE_DISABLE_ALARMS)) {
  913. static int logged=0;
  914. if (!logged) {
  915. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  916. "Skipping health checks, because all alarms are disabled via a %s command.",
  917. HEALTH_CMDAPI_CMD_DISABLEALL);
  918. logged = 1;
  919. }
  920. }
  921. worker_is_busy(WORKER_HEALTH_JOB_RRD_LOCK);
  922. dfe_start_reentrant(rrdhost_root_index, host) {
  923. if(unlikely(!service_running(SERVICE_HEALTH)))
  924. break;
  925. if (unlikely(!host->health.health_enabled))
  926. continue;
  927. if (unlikely(!rrdhost_flag_check(host, RRDHOST_FLAG_INITIALIZED_HEALTH)))
  928. initialize_health(host);
  929. health_execute_delayed_initializations(host);
  930. rrdcalc_delete_alerts_not_matching_host_labels_from_this_host(host);
  931. if (unlikely(apply_hibernation_delay)) {
  932. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  933. "[%s]: Postponing health checks for %"PRId64" seconds.",
  934. rrdhost_hostname(host),
  935. (int64_t)hibernation_delay);
  936. host->health.health_delay_up_to = now + hibernation_delay;
  937. }
  938. if (unlikely(host->health.health_delay_up_to)) {
  939. if (unlikely(now < host->health.health_delay_up_to)) {
  940. continue;
  941. }
  942. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  943. "[%s]: Resuming health checks after delay.",
  944. rrdhost_hostname(host));
  945. host->health.health_delay_up_to = 0;
  946. }
  947. // wait until cleanup of obsolete charts on children is complete
  948. if (host != localhost) {
  949. if (unlikely(host->trigger_chart_obsoletion_check == 1)) {
  950. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  951. "[%s]: Waiting for chart obsoletion check.",
  952. rrdhost_hostname(host));
  953. continue;
  954. }
  955. }
  956. if (!health_running_logged) {
  957. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  958. "[%s]: Health is running.",
  959. rrdhost_hostname(host));
  960. health_running_logged = true;
  961. }
  962. worker_is_busy(WORKER_HEALTH_JOB_HOST_LOCK);
  963. // the first loop is to lookup values from the db
  964. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  965. if(unlikely(!service_running(SERVICE_HEALTH)))
  966. break;
  967. rrdcalc_update_info_using_rrdset_labels(rc);
  968. if (update_disabled_silenced(host, rc))
  969. continue;
  970. // create an alert removed event if the chart is obsolete and
  971. // has stopped being collected for 60 seconds
  972. if (unlikely(rc->rrdset && rc->status != RRDCALC_STATUS_REMOVED &&
  973. rrdset_flag_check(rc->rrdset, RRDSET_FLAG_OBSOLETE) &&
  974. now > (rc->rrdset->last_collected_time.tv_sec + 60))) {
  975. if (!rrdcalc_isrepeating(rc)) {
  976. worker_is_busy(WORKER_HEALTH_JOB_ALARM_LOG_ENTRY);
  977. time_t now = now_realtime_sec();
  978. ALARM_ENTRY *ae = health_create_alarm_entry(
  979. host,
  980. rc->id,
  981. rc->next_event_id++,
  982. rc->config_hash_id,
  983. now,
  984. rc->name,
  985. rc->rrdset->id,
  986. rc->rrdset->context,
  987. rc->rrdset->name,
  988. rc->classification,
  989. rc->component,
  990. rc->type,
  991. rc->exec,
  992. rc->recipient,
  993. now - rc->last_status_change,
  994. rc->value,
  995. NAN,
  996. rc->status,
  997. RRDCALC_STATUS_REMOVED,
  998. rc->source,
  999. rc->units,
  1000. rc->summary,
  1001. rc->info,
  1002. 0,
  1003. rrdcalc_isrepeating(rc)?HEALTH_ENTRY_FLAG_IS_REPEATING:0);
  1004. if (ae) {
  1005. health_log_alert(host, ae);
  1006. health_alarm_log_add_entry(host, ae);
  1007. rc->old_status = rc->status;
  1008. rc->status = RRDCALC_STATUS_REMOVED;
  1009. rc->last_status_change = now;
  1010. rc->last_status_change_value = rc->value;
  1011. rc->last_updated = now;
  1012. rc->value = NAN;
  1013. rc->ae = ae;
  1014. #ifdef ENABLE_ACLK
  1015. if (netdata_cloud_enabled)
  1016. sql_queue_alarm_to_aclk(host, ae, true);
  1017. #endif
  1018. }
  1019. }
  1020. }
  1021. if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run))) {
  1022. if (unlikely(rc->run_flags & RRDCALC_FLAG_RUNNABLE))
  1023. rc->run_flags &= ~RRDCALC_FLAG_RUNNABLE;
  1024. continue;
  1025. }
  1026. runnable++;
  1027. rc->old_value = rc->value;
  1028. rc->run_flags |= RRDCALC_FLAG_RUNNABLE;
  1029. // ------------------------------------------------------------
  1030. // if there is database lookup, do it
  1031. if (unlikely(RRDCALC_HAS_DB_LOOKUP(rc))) {
  1032. worker_is_busy(WORKER_HEALTH_JOB_DB_QUERY);
  1033. /* time_t old_db_timestamp = rc->db_before; */
  1034. int value_is_null = 0;
  1035. int ret = rrdset2value_api_v1(rc->rrdset, NULL, &rc->value, rrdcalc_dimensions(rc), 1,
  1036. rc->after, rc->before, rc->group, NULL,
  1037. 0, rc->options | RRDR_OPTION_SELECTED_TIER,
  1038. &rc->db_after,&rc->db_before,
  1039. NULL, NULL, NULL,
  1040. &value_is_null, NULL, 0, 0,
  1041. QUERY_SOURCE_HEALTH, STORAGE_PRIORITY_LOW);
  1042. if (unlikely(ret != 200)) {
  1043. // database lookup failed
  1044. rc->value = NAN;
  1045. rc->run_flags |= RRDCALC_FLAG_DB_ERROR;
  1046. netdata_log_debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': database lookup returned error %d",
  1047. rrdhost_hostname(host), rrdcalc_chart_name(rc), rrdcalc_name(rc), ret
  1048. );
  1049. } else
  1050. rc->run_flags &= ~RRDCALC_FLAG_DB_ERROR;
  1051. if (unlikely(value_is_null)) {
  1052. // collected value is null
  1053. rc->value = NAN;
  1054. rc->run_flags |= RRDCALC_FLAG_DB_NAN;
  1055. netdata_log_debug(D_HEALTH,
  1056. "Health on host '%s', alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
  1057. rrdhost_hostname(host), rrdcalc_chart_name(rc), rrdcalc_name(rc)
  1058. );
  1059. } else
  1060. rc->run_flags &= ~RRDCALC_FLAG_DB_NAN;
  1061. netdata_log_debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': database lookup gave value " NETDATA_DOUBLE_FORMAT,
  1062. rrdhost_hostname(host), rrdcalc_chart_name(rc), rrdcalc_name(rc), rc->value
  1063. );
  1064. }
  1065. // ------------------------------------------------------------
  1066. // if there is calculation expression, run it
  1067. if (unlikely(rc->calculation)) {
  1068. worker_is_busy(WORKER_HEALTH_JOB_CALC_EVAL);
  1069. if (unlikely(!expression_evaluate(rc->calculation))) {
  1070. // calculation failed
  1071. rc->value = NAN;
  1072. rc->run_flags |= RRDCALC_FLAG_CALC_ERROR;
  1073. netdata_log_debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': expression '%s' failed: %s",
  1074. rrdhost_hostname(host), rrdcalc_chart_name(rc), rrdcalc_name(rc),
  1075. rc->calculation->parsed_as, buffer_tostring(rc->calculation->error_msg)
  1076. );
  1077. } else {
  1078. rc->run_flags &= ~RRDCALC_FLAG_CALC_ERROR;
  1079. netdata_log_debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': expression '%s' gave value "
  1080. NETDATA_DOUBLE_FORMAT
  1081. ": %s (source: %s)", rrdhost_hostname(host), rrdcalc_chart_name(rc), rrdcalc_name(rc),
  1082. rc->calculation->parsed_as, rc->calculation->result,
  1083. buffer_tostring(rc->calculation->error_msg), rrdcalc_source(rc)
  1084. );
  1085. rc->value = rc->calculation->result;
  1086. }
  1087. }
  1088. }
  1089. foreach_rrdcalc_in_rrdhost_done(rc);
  1090. if (unlikely(runnable && service_running(SERVICE_HEALTH))) {
  1091. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  1092. if(unlikely(!service_running(SERVICE_HEALTH)))
  1093. break;
  1094. if (unlikely(!(rc->run_flags & RRDCALC_FLAG_RUNNABLE)))
  1095. continue;
  1096. if (rc->run_flags & RRDCALC_FLAG_DISABLED) {
  1097. continue;
  1098. }
  1099. RRDCALC_STATUS warning_status = RRDCALC_STATUS_UNDEFINED;
  1100. RRDCALC_STATUS critical_status = RRDCALC_STATUS_UNDEFINED;
  1101. // --------------------------------------------------------
  1102. // check the warning expression
  1103. if (likely(rc->warning)) {
  1104. worker_is_busy(WORKER_HEALTH_JOB_WARNING_EVAL);
  1105. if (unlikely(!expression_evaluate(rc->warning))) {
  1106. // calculation failed
  1107. rc->run_flags |= RRDCALC_FLAG_WARN_ERROR;
  1108. netdata_log_debug(D_HEALTH,
  1109. "Health on host '%s', alarm '%s.%s': warning expression failed with error: %s",
  1110. rrdhost_hostname(host), rrdcalc_chart_name(rc), rrdcalc_name(rc),
  1111. buffer_tostring(rc->warning->error_msg)
  1112. );
  1113. } else {
  1114. rc->run_flags &= ~RRDCALC_FLAG_WARN_ERROR;
  1115. netdata_log_debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': warning expression gave value "
  1116. NETDATA_DOUBLE_FORMAT
  1117. ": %s (source: %s)", rrdhost_hostname(host), rrdcalc_chart_name(rc),
  1118. rrdcalc_name(rc), rc->warning->result, buffer_tostring(rc->warning->error_msg), rrdcalc_source(rc)
  1119. );
  1120. warning_status = rrdcalc_value2status(rc->warning->result);
  1121. }
  1122. }
  1123. // --------------------------------------------------------
  1124. // check the critical expression
  1125. if (likely(rc->critical)) {
  1126. worker_is_busy(WORKER_HEALTH_JOB_CRITICAL_EVAL);
  1127. if (unlikely(!expression_evaluate(rc->critical))) {
  1128. // calculation failed
  1129. rc->run_flags |= RRDCALC_FLAG_CRIT_ERROR;
  1130. netdata_log_debug(D_HEALTH,
  1131. "Health on host '%s', alarm '%s.%s': critical expression failed with error: %s",
  1132. rrdhost_hostname(host), rrdcalc_chart_name(rc), rrdcalc_name(rc),
  1133. buffer_tostring(rc->critical->error_msg)
  1134. );
  1135. } else {
  1136. rc->run_flags &= ~RRDCALC_FLAG_CRIT_ERROR;
  1137. netdata_log_debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': critical expression gave value "
  1138. NETDATA_DOUBLE_FORMAT
  1139. ": %s (source: %s)", rrdhost_hostname(host), rrdcalc_chart_name(rc),
  1140. rrdcalc_name(rc), rc->critical->result, buffer_tostring(rc->critical->error_msg),
  1141. rrdcalc_source(rc)
  1142. );
  1143. critical_status = rrdcalc_value2status(rc->critical->result);
  1144. }
  1145. }
  1146. // --------------------------------------------------------
  1147. // decide the final alarm status
  1148. RRDCALC_STATUS status = RRDCALC_STATUS_UNDEFINED;
  1149. switch (warning_status) {
  1150. case RRDCALC_STATUS_CLEAR:
  1151. status = RRDCALC_STATUS_CLEAR;
  1152. break;
  1153. case RRDCALC_STATUS_RAISED:
  1154. status = RRDCALC_STATUS_WARNING;
  1155. break;
  1156. default:
  1157. break;
  1158. }
  1159. switch (critical_status) {
  1160. case RRDCALC_STATUS_CLEAR:
  1161. if (status == RRDCALC_STATUS_UNDEFINED)
  1162. status = RRDCALC_STATUS_CLEAR;
  1163. break;
  1164. case RRDCALC_STATUS_RAISED:
  1165. status = RRDCALC_STATUS_CRITICAL;
  1166. break;
  1167. default:
  1168. break;
  1169. }
  1170. // --------------------------------------------------------
  1171. // check if the new status and the old differ
  1172. if (status != rc->status) {
  1173. worker_is_busy(WORKER_HEALTH_JOB_ALARM_LOG_ENTRY);
  1174. int delay = 0;
  1175. // apply trigger hysteresis
  1176. if (now > rc->delay_up_to_timestamp) {
  1177. rc->delay_up_current = rc->delay_up_duration;
  1178. rc->delay_down_current = rc->delay_down_duration;
  1179. rc->delay_last = 0;
  1180. rc->delay_up_to_timestamp = 0;
  1181. } else {
  1182. rc->delay_up_current = (int) (rc->delay_up_current * rc->delay_multiplier);
  1183. if (rc->delay_up_current > rc->delay_max_duration)
  1184. rc->delay_up_current = rc->delay_max_duration;
  1185. rc->delay_down_current = (int) (rc->delay_down_current * rc->delay_multiplier);
  1186. if (rc->delay_down_current > rc->delay_max_duration)
  1187. rc->delay_down_current = rc->delay_max_duration;
  1188. }
  1189. if (status > rc->status)
  1190. delay = rc->delay_up_current;
  1191. else
  1192. delay = rc->delay_down_current;
  1193. // COMMENTED: because we do need to send raising alarms
  1194. // if(now + delay < rc->delay_up_to_timestamp)
  1195. // delay = (int)(rc->delay_up_to_timestamp - now);
  1196. rc->delay_last = delay;
  1197. rc->delay_up_to_timestamp = now + delay;
  1198. ALARM_ENTRY *ae = health_create_alarm_entry(
  1199. host,
  1200. rc->id,
  1201. rc->next_event_id++,
  1202. rc->config_hash_id,
  1203. now,
  1204. rc->name,
  1205. rc->rrdset->id,
  1206. rc->rrdset->context,
  1207. rc->rrdset->name,
  1208. rc->classification,
  1209. rc->component,
  1210. rc->type,
  1211. rc->exec,
  1212. rc->recipient,
  1213. now - rc->last_status_change,
  1214. rc->old_value,
  1215. rc->value,
  1216. rc->status,
  1217. status,
  1218. rc->source,
  1219. rc->units,
  1220. rc->summary,
  1221. rc->info,
  1222. rc->delay_last,
  1223. (
  1224. ((rc->options & RRDCALC_OPTION_NO_CLEAR_NOTIFICATION)? HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION : 0) |
  1225. ((rc->run_flags & RRDCALC_FLAG_SILENCED)? HEALTH_ENTRY_FLAG_SILENCED : 0) |
  1226. (rrdcalc_isrepeating(rc)?HEALTH_ENTRY_FLAG_IS_REPEATING:0)
  1227. )
  1228. );
  1229. health_log_alert(host, ae);
  1230. health_alarm_log_add_entry(host, ae);
  1231. nd_log(NDLS_DAEMON, NDLP_DEBUG,
  1232. "[%s]: Alert event for [%s.%s], value [%s], status [%s].",
  1233. rrdhost_hostname(host), ae_chart_id(ae), ae_name(ae), ae_new_value_string(ae),
  1234. rrdcalc_status2string(ae->new_status));
  1235. rc->last_status_change_value = rc->value;
  1236. rc->last_status_change = now;
  1237. rc->old_status = rc->status;
  1238. rc->status = status;
  1239. rc->ae = ae;
  1240. if(unlikely(rrdcalc_isrepeating(rc))) {
  1241. rc->last_repeat = now;
  1242. if (rc->status == RRDCALC_STATUS_CLEAR)
  1243. rc->run_flags |= RRDCALC_FLAG_RUN_ONCE;
  1244. }
  1245. }
  1246. rc->last_updated = now;
  1247. rc->next_update = now + rc->update_every;
  1248. if (next_run > rc->next_update)
  1249. next_run = rc->next_update;
  1250. }
  1251. foreach_rrdcalc_in_rrdhost_done(rc);
  1252. // process repeating alarms
  1253. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  1254. if(unlikely(!service_running(SERVICE_HEALTH)))
  1255. break;
  1256. int repeat_every = 0;
  1257. if(unlikely(rrdcalc_isrepeating(rc) && rc->delay_up_to_timestamp <= now)) {
  1258. if(unlikely(rc->status == RRDCALC_STATUS_WARNING)) {
  1259. rc->run_flags &= ~RRDCALC_FLAG_RUN_ONCE;
  1260. repeat_every = rc->warn_repeat_every;
  1261. } else if(unlikely(rc->status == RRDCALC_STATUS_CRITICAL)) {
  1262. rc->run_flags &= ~RRDCALC_FLAG_RUN_ONCE;
  1263. repeat_every = rc->crit_repeat_every;
  1264. } else if(unlikely(rc->status == RRDCALC_STATUS_CLEAR)) {
  1265. if(!(rc->run_flags & RRDCALC_FLAG_RUN_ONCE)) {
  1266. if(rc->old_status == RRDCALC_STATUS_CRITICAL) {
  1267. repeat_every = 1;
  1268. } else if (rc->old_status == RRDCALC_STATUS_WARNING) {
  1269. repeat_every = 1;
  1270. }
  1271. }
  1272. }
  1273. } else {
  1274. continue;
  1275. }
  1276. if(unlikely(repeat_every > 0 && (rc->last_repeat + repeat_every) <= now)) {
  1277. worker_is_busy(WORKER_HEALTH_JOB_ALARM_LOG_ENTRY);
  1278. rc->last_repeat = now;
  1279. if (likely(rc->times_repeat < UINT32_MAX)) rc->times_repeat++;
  1280. ALARM_ENTRY *ae = health_create_alarm_entry(
  1281. host,
  1282. rc->id,
  1283. rc->next_event_id++,
  1284. rc->config_hash_id,
  1285. now,
  1286. rc->name,
  1287. rc->rrdset->id,
  1288. rc->rrdset->context,
  1289. rc->rrdset->name,
  1290. rc->classification,
  1291. rc->component,
  1292. rc->type,
  1293. rc->exec,
  1294. rc->recipient,
  1295. now - rc->last_status_change,
  1296. rc->old_value,
  1297. rc->value,
  1298. rc->old_status,
  1299. rc->status,
  1300. rc->source,
  1301. rc->units,
  1302. rc->summary,
  1303. rc->info,
  1304. rc->delay_last,
  1305. (
  1306. ((rc->options & RRDCALC_OPTION_NO_CLEAR_NOTIFICATION)? HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION : 0) |
  1307. ((rc->run_flags & RRDCALC_FLAG_SILENCED)? HEALTH_ENTRY_FLAG_SILENCED : 0) |
  1308. (rrdcalc_isrepeating(rc)?HEALTH_ENTRY_FLAG_IS_REPEATING:0)
  1309. )
  1310. );
  1311. health_log_alert(host, ae);
  1312. ae->last_repeat = rc->last_repeat;
  1313. if (!(rc->run_flags & RRDCALC_FLAG_RUN_ONCE) && rc->status == RRDCALC_STATUS_CLEAR) {
  1314. ae->flags |= HEALTH_ENTRY_RUN_ONCE;
  1315. }
  1316. rc->run_flags |= RRDCALC_FLAG_RUN_ONCE;
  1317. health_process_notifications(host, ae);
  1318. netdata_log_debug(D_HEALTH, "Notification sent for the repeating alarm %u.", ae->alarm_id);
  1319. health_alarm_wait_for_execution(ae);
  1320. health_alarm_log_free_one_nochecks_nounlink(ae);
  1321. }
  1322. }
  1323. foreach_rrdcalc_in_rrdhost_done(rc);
  1324. }
  1325. if (unlikely(!service_running(SERVICE_HEALTH)))
  1326. break;
  1327. // execute notifications
  1328. // and cleanup
  1329. worker_is_busy(WORKER_HEALTH_JOB_ALARM_LOG_PROCESS);
  1330. health_alarm_log_process(host);
  1331. if (unlikely(!service_running(SERVICE_HEALTH))) {
  1332. // wait for all notifications to finish before allowing health to be cleaned up
  1333. ALARM_ENTRY *ae;
  1334. while (NULL != (ae = alarm_notifications_in_progress.head)) {
  1335. if(unlikely(!service_running(SERVICE_HEALTH)))
  1336. break;
  1337. health_alarm_wait_for_execution(ae);
  1338. }
  1339. break;
  1340. }
  1341. #ifdef ENABLE_ACLK
  1342. if (netdata_cloud_enabled) {
  1343. struct aclk_sync_cfg_t *wc = host->aclk_config;
  1344. if (unlikely(!wc))
  1345. continue;
  1346. if (wc->alert_queue_removed == 1) {
  1347. sql_queue_removed_alerts_to_aclk(host);
  1348. } else if (wc->alert_queue_removed > 1) {
  1349. wc->alert_queue_removed--;
  1350. }
  1351. if (wc->alert_checkpoint_req == 1) {
  1352. aclk_push_alarm_checkpoint(host);
  1353. } else if (wc->alert_checkpoint_req > 1) {
  1354. wc->alert_checkpoint_req--;
  1355. }
  1356. }
  1357. #endif
  1358. }
  1359. dfe_done(host);
  1360. // wait for all notifications to finish before allowing health to be cleaned up
  1361. ALARM_ENTRY *ae;
  1362. while (NULL != (ae = alarm_notifications_in_progress.head)) {
  1363. if(unlikely(!service_running(SERVICE_HEALTH)))
  1364. break;
  1365. health_alarm_wait_for_execution(ae);
  1366. }
  1367. if(unlikely(!service_running(SERVICE_HEALTH)))
  1368. break;
  1369. health_sleep(next_run, loop);
  1370. } // forever
  1371. netdata_thread_cleanup_pop(1);
  1372. return NULL;
  1373. }