health.c 71 KB

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