health.c 70 KB

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