health.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "health.h"
  3. unsigned int default_health_enabled = 1;
  4. char *silencers_filename;
  5. // the queue of executed alarm notifications that haven't been waited for yet
  6. static struct {
  7. ALARM_ENTRY *head; // oldest
  8. ALARM_ENTRY *tail; // latest
  9. } alarm_notifications_in_progress = {NULL, NULL};
  10. static inline void enqueue_alarm_notify_in_progress(ALARM_ENTRY *ae)
  11. {
  12. ae->prev_in_progress = NULL;
  13. ae->next_in_progress = NULL;
  14. if (NULL != alarm_notifications_in_progress.tail) {
  15. ae->prev_in_progress = alarm_notifications_in_progress.tail;
  16. alarm_notifications_in_progress.tail->next_in_progress = ae;
  17. }
  18. if (NULL == alarm_notifications_in_progress.head) {
  19. alarm_notifications_in_progress.head = ae;
  20. }
  21. alarm_notifications_in_progress.tail = ae;
  22. }
  23. static inline void unlink_alarm_notify_in_progress(ALARM_ENTRY *ae)
  24. {
  25. struct alarm_entry *prev = ae->prev_in_progress;
  26. struct alarm_entry *next = ae->next_in_progress;
  27. if (NULL != prev) {
  28. prev->next_in_progress = next;
  29. }
  30. if (NULL != next) {
  31. next->prev_in_progress = prev;
  32. }
  33. if (ae == alarm_notifications_in_progress.head) {
  34. alarm_notifications_in_progress.head = next;
  35. }
  36. if (ae == alarm_notifications_in_progress.tail) {
  37. alarm_notifications_in_progress.tail = prev;
  38. }
  39. }
  40. // ----------------------------------------------------------------------------
  41. // health initialization
  42. /**
  43. * User Config directory
  44. *
  45. * Get the config directory for health and return it.
  46. *
  47. * @return a pointer to the user config directory
  48. */
  49. inline char *health_user_config_dir(void) {
  50. char buffer[FILENAME_MAX + 1];
  51. snprintfz(buffer, FILENAME_MAX, "%s/health.d", netdata_configured_user_config_dir);
  52. return config_get(CONFIG_SECTION_HEALTH, "health configuration directory", buffer);
  53. }
  54. /**
  55. * Stock Config Directory
  56. *
  57. * Get the Stock config directory and return it.
  58. *
  59. * @return a pointer to the stock config directory.
  60. */
  61. inline char *health_stock_config_dir(void) {
  62. char buffer[FILENAME_MAX + 1];
  63. snprintfz(buffer, FILENAME_MAX, "%s/health.d", netdata_configured_stock_config_dir);
  64. return config_get(CONFIG_SECTION_HEALTH, "stock health configuration directory", buffer);
  65. }
  66. /**
  67. * Silencers init
  68. *
  69. * Function used to initialize the silencer structure.
  70. */
  71. static void health_silencers_init(void) {
  72. FILE *fd = fopen(silencers_filename, "r");
  73. if (fd) {
  74. fseek(fd, 0 , SEEK_END);
  75. off_t length = (off_t) ftell(fd);
  76. fseek(fd, 0 , SEEK_SET);
  77. if (length > 0 && length < HEALTH_SILENCERS_MAX_FILE_LEN) {
  78. char *str = mallocz((length+1)* sizeof(char));
  79. if(str) {
  80. size_t copied;
  81. copied = fread(str, sizeof(char), length, fd);
  82. if (copied == (length* sizeof(char))) {
  83. str[length] = 0x00;
  84. json_parse(str, NULL, health_silencers_json_read_callback);
  85. info("Parsed health silencers file %s", silencers_filename);
  86. } else {
  87. error("Cannot read the data from health silencers file %s", silencers_filename);
  88. }
  89. freez(str);
  90. }
  91. } else {
  92. error("Health silencers file %s has the size %ld that is out of range[ 1 , %d ]. Aborting read.", silencers_filename, length, HEALTH_SILENCERS_MAX_FILE_LEN);
  93. }
  94. fclose(fd);
  95. } else {
  96. info("Cannot open the file %s, so Netdata will work with the default health configuration.",silencers_filename);
  97. }
  98. }
  99. /**
  100. * Health Init
  101. *
  102. * Initialize the health thread.
  103. */
  104. void health_init(void) {
  105. debug(D_HEALTH, "Health configuration initializing");
  106. if(!(default_health_enabled = (unsigned int)config_get_boolean(CONFIG_SECTION_HEALTH, "enabled", default_health_enabled))) {
  107. debug(D_HEALTH, "Health is disabled.");
  108. return;
  109. }
  110. health_silencers_init();
  111. }
  112. // ----------------------------------------------------------------------------
  113. // re-load health configuration
  114. /**
  115. * Reload host
  116. *
  117. * Reload configuration for a specific host.
  118. *
  119. * @param host the structure of the host that the function will reload the configuration.
  120. */
  121. static void health_reload_host(RRDHOST *host) {
  122. if(unlikely(!host->health_enabled))
  123. return;
  124. char *user_path = health_user_config_dir();
  125. char *stock_path = health_stock_config_dir();
  126. // free all running alarms
  127. rrdhost_wrlock(host);
  128. while(host->templates)
  129. rrdcalctemplate_unlink_and_free(host, host->templates);
  130. RRDCALCTEMPLATE *rt,*next;
  131. for(rt = host->alarms_template_with_foreach; rt ; rt = next) {
  132. next = rt->next;
  133. rrdcalctemplate_free(rt);
  134. }
  135. host->alarms_template_with_foreach = NULL;
  136. while(host->alarms)
  137. rrdcalc_unlink_and_free(host, host->alarms);
  138. RRDCALC *rc,*nc;
  139. for(rc = host->alarms_with_foreach; rc ; rc = nc) {
  140. nc = rc->next;
  141. rrdcalc_free(rc);
  142. }
  143. host->alarms_with_foreach = NULL;
  144. rrdhost_unlock(host);
  145. // invalidate all previous entries in the alarm log
  146. ALARM_ENTRY *t;
  147. for(t = host->health_log.alarms ; t ; t = t->next) {
  148. if(t->new_status != RRDCALC_STATUS_REMOVED)
  149. t->flags |= HEALTH_ENTRY_FLAG_UPDATED;
  150. }
  151. rrdhost_rdlock(host);
  152. // reset all thresholds to all charts
  153. RRDSET *st;
  154. rrdset_foreach_read(st, host) {
  155. st->green = NAN;
  156. st->red = NAN;
  157. }
  158. rrdhost_unlock(host);
  159. // load the new alarms
  160. rrdhost_wrlock(host);
  161. health_readdir(host, user_path, stock_path, NULL);
  162. //Discard alarms with labels that do not apply to host
  163. rrdcalc_labels_unlink_alarm_from_host(host);
  164. // link the loaded alarms to their charts
  165. RRDDIM *rd;
  166. rrdset_foreach_write(st, host) {
  167. if (rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED))
  168. continue;
  169. rrdsetcalc_link_matching(st);
  170. rrdcalctemplate_link_matching(st);
  171. //This loop must be the last, because ` rrdcalctemplate_link_matching` will create alarms related to it.
  172. rrdset_rdlock(st);
  173. rrddim_foreach_read(rd, st) {
  174. rrdcalc_link_to_rrddim(rd, st, host);
  175. }
  176. rrdset_unlock(st);
  177. }
  178. rrdhost_unlock(host);
  179. }
  180. /**
  181. * Reload
  182. *
  183. * Reload the host configuration for all hosts.
  184. */
  185. void health_reload(void) {
  186. #ifdef ENABLE_ACLK
  187. if (netdata_cloud_setting)
  188. aclk_single_update_disable();
  189. #endif
  190. rrd_rdlock();
  191. RRDHOST *host;
  192. rrdhost_foreach_read(host)
  193. health_reload_host(host);
  194. rrd_unlock();
  195. #ifdef ENABLE_ACLK
  196. if (netdata_cloud_setting) {
  197. aclk_single_update_enable();
  198. aclk_alarm_reload();
  199. }
  200. #endif
  201. }
  202. // ----------------------------------------------------------------------------
  203. // health main thread and friends
  204. static inline RRDCALC_STATUS rrdcalc_value2status(calculated_number n) {
  205. if(isnan(n) || isinf(n)) return RRDCALC_STATUS_UNDEFINED;
  206. if(n) return RRDCALC_STATUS_RAISED;
  207. return RRDCALC_STATUS_CLEAR;
  208. }
  209. #define ALARM_EXEC_COMMAND_LENGTH 8192
  210. static inline void health_alarm_execute(RRDHOST *host, ALARM_ENTRY *ae) {
  211. ae->flags |= HEALTH_ENTRY_FLAG_PROCESSED;
  212. if(unlikely(ae->new_status < RRDCALC_STATUS_CLEAR)) {
  213. // do not send notifications for internal statuses
  214. debug(D_HEALTH, "Health not sending notification for alarm '%s.%s' status %s (internal statuses)", ae->chart, ae->name, rrdcalc_status2string(ae->new_status));
  215. goto done;
  216. }
  217. if(unlikely(ae->new_status <= RRDCALC_STATUS_CLEAR && (ae->flags & HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION))) {
  218. // do not send notifications for disabled statuses
  219. debug(D_HEALTH, "Health not sending notification for alarm '%s.%s' status %s (it has no-clear-notification enabled)", ae->chart, ae->name, rrdcalc_status2string(ae->new_status));
  220. // mark it as run, so that we will send the same alarm if it happens again
  221. goto done;
  222. }
  223. // find the previous notification for the same alarm
  224. // which we have run the exec script
  225. // exception: alarms with HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION set
  226. if(likely(!(ae->flags & HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION))) {
  227. uint32_t id = ae->alarm_id;
  228. ALARM_ENTRY *t;
  229. for(t = ae->next; t ; t = t->next) {
  230. if(t->alarm_id == id && t->flags & HEALTH_ENTRY_FLAG_EXEC_RUN)
  231. break;
  232. }
  233. if(likely(t)) {
  234. // we have executed this alarm notification in the past
  235. if(t && t->new_status == ae->new_status) {
  236. // don't send the notification for the same status again
  237. debug(D_HEALTH, "Health not sending again notification for alarm '%s.%s' status %s", ae->chart, ae->name
  238. , rrdcalc_status2string(ae->new_status));
  239. goto done;
  240. }
  241. }
  242. else {
  243. // we have not executed this alarm notification in the past
  244. // so, don't send CLEAR notifications
  245. if(unlikely(ae->new_status == RRDCALC_STATUS_CLEAR)) {
  246. if((!(ae->flags & HEALTH_ENTRY_RUN_ONCE)) || (ae->flags & HEALTH_ENTRY_RUN_ONCE && ae->old_status < RRDCALC_STATUS_RAISED) ) {
  247. debug(D_HEALTH, "Health not sending notification for first initialization of alarm '%s.%s' status %s"
  248. , ae->chart, ae->name, rrdcalc_status2string(ae->new_status));
  249. goto done;
  250. }
  251. }
  252. }
  253. }
  254. // Check if alarm notifications are silenced
  255. if (ae->flags & HEALTH_ENTRY_FLAG_SILENCED) {
  256. info("Health not sending notification for alarm '%s.%s' status %s (command API has disabled notifications)", ae->chart, ae->name, rrdcalc_status2string(ae->new_status));
  257. goto done;
  258. }
  259. static char command_to_run[ALARM_EXEC_COMMAND_LENGTH + 1];
  260. const char *exec = (ae->exec) ? ae->exec : host->health_default_exec;
  261. const char *recipient = (ae->recipient) ? ae->recipient : host->health_default_recipient;
  262. int n_warn=0, n_crit=0;
  263. RRDCALC *rc;
  264. EVAL_EXPRESSION *expr=NULL;
  265. for(rc = host->alarms; rc ; rc = rc->next) {
  266. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  267. continue;
  268. if(unlikely(rc->status == RRDCALC_STATUS_WARNING)) {
  269. n_warn++;
  270. if (ae->alarm_id == rc->id)
  271. expr=rc->warning;
  272. } else if (unlikely(rc->status == RRDCALC_STATUS_CRITICAL)) {
  273. n_crit++;
  274. if (ae->alarm_id == rc->id)
  275. expr=rc->critical;
  276. } else if (unlikely(rc->status == RRDCALC_STATUS_CLEAR)) {
  277. if (ae->alarm_id == rc->id)
  278. expr=rc->warning;
  279. }
  280. }
  281. snprintfz(command_to_run, ALARM_EXEC_COMMAND_LENGTH, "exec %s '%s' '%s' '%u' '%u' '%u' '%lu' '%s' '%s' '%s' '%s' '%s' '" CALCULATED_NUMBER_FORMAT_ZERO "' '" CALCULATED_NUMBER_FORMAT_ZERO "' '%s' '%u' '%u' '%s' '%s' '%s' '%s' '%s' '%s' '%d' '%d'",
  282. exec,
  283. recipient,
  284. host->registry_hostname,
  285. ae->unique_id,
  286. ae->alarm_id,
  287. ae->alarm_event_id,
  288. (unsigned long)ae->when,
  289. ae->name,
  290. ae->chart?ae->chart:"NOCHART",
  291. ae->family?ae->family:"NOFAMILY",
  292. rrdcalc_status2string(ae->new_status),
  293. rrdcalc_status2string(ae->old_status),
  294. ae->new_value,
  295. ae->old_value,
  296. ae->source?ae->source:"UNKNOWN",
  297. (uint32_t)ae->duration,
  298. (uint32_t)ae->non_clear_duration,
  299. ae->units?ae->units:"",
  300. ae->info?ae->info:"",
  301. ae->new_value_string,
  302. ae->old_value_string,
  303. (expr && expr->source)?expr->source:"NOSOURCE",
  304. (expr && expr->error_msg)?buffer_tostring(expr->error_msg):"NOERRMSG",
  305. n_warn,
  306. n_crit
  307. );
  308. ae->flags |= HEALTH_ENTRY_FLAG_EXEC_RUN;
  309. ae->exec_run_timestamp = now_realtime_sec(); /* will be updated by real time after spawning */
  310. debug(D_HEALTH, "executing command '%s'", command_to_run);
  311. ae->flags |= HEALTH_ENTRY_FLAG_EXEC_IN_PROGRESS;
  312. ae->exec_spawn_serial = spawn_enq_cmd(command_to_run);
  313. enqueue_alarm_notify_in_progress(ae);
  314. return; //health_alarm_wait_for_execution
  315. done:
  316. health_alarm_log_save(host, ae);
  317. }
  318. static inline void health_alarm_wait_for_execution(ALARM_ENTRY *ae) {
  319. if (!(ae->flags & HEALTH_ENTRY_FLAG_EXEC_IN_PROGRESS))
  320. return;
  321. spawn_wait_cmd(ae->exec_spawn_serial, &ae->exec_code, &ae->exec_run_timestamp);
  322. debug(D_HEALTH, "done executing command - returned with code %d", ae->exec_code);
  323. ae->flags &= ~HEALTH_ENTRY_FLAG_EXEC_IN_PROGRESS;
  324. if(ae->exec_code != 0)
  325. ae->flags |= HEALTH_ENTRY_FLAG_EXEC_FAILED;
  326. unlink_alarm_notify_in_progress(ae);
  327. }
  328. static inline void health_process_notifications(RRDHOST *host, ALARM_ENTRY *ae) {
  329. debug(D_HEALTH, "Health alarm '%s.%s' = " CALCULATED_NUMBER_FORMAT_AUTO " - changed status from %s to %s",
  330. ae->chart?ae->chart:"NOCHART", ae->name,
  331. ae->new_value,
  332. rrdcalc_status2string(ae->old_status),
  333. rrdcalc_status2string(ae->new_status)
  334. );
  335. health_alarm_execute(host, ae);
  336. }
  337. static inline void health_alarm_log_process(RRDHOST *host) {
  338. uint32_t first_waiting = (host->health_log.alarms)?host->health_log.alarms->unique_id:0;
  339. time_t now = now_realtime_sec();
  340. netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
  341. ALARM_ENTRY *ae;
  342. for(ae = host->health_log.alarms; ae && ae->unique_id >= host->health_last_processed_id; ae = ae->next) {
  343. if(likely(!alarm_entry_isrepeating(host, ae))) {
  344. if(unlikely(
  345. !(ae->flags & HEALTH_ENTRY_FLAG_PROCESSED) &&
  346. !(ae->flags & HEALTH_ENTRY_FLAG_UPDATED)
  347. )) {
  348. if(unlikely(ae->unique_id < first_waiting))
  349. first_waiting = ae->unique_id;
  350. if(likely(now >= ae->delay_up_to_timestamp))
  351. health_process_notifications(host, ae);
  352. }
  353. }
  354. }
  355. // remember this for the next iteration
  356. host->health_last_processed_id = first_waiting;
  357. netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
  358. if(host->health_log.count <= host->health_log.max)
  359. return;
  360. // cleanup excess entries in the log
  361. netdata_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
  362. ALARM_ENTRY *last = NULL;
  363. unsigned int count = host->health_log.max * 2 / 3;
  364. for(ae = host->health_log.alarms; ae && count ; count--, last = ae, ae = ae->next) ;
  365. if(ae && last && last->next == ae)
  366. last->next = NULL;
  367. else
  368. ae = NULL;
  369. while(ae) {
  370. debug(D_HEALTH, "Health removing alarm log entry with id: %u", ae->unique_id);
  371. ALARM_ENTRY *t = ae->next;
  372. if(likely(!alarm_entry_isrepeating(host, ae))) {
  373. health_alarm_wait_for_execution(ae);
  374. health_alarm_log_free_one_nochecks_nounlink(ae);
  375. host->health_log.count--;
  376. }
  377. ae = t;
  378. }
  379. netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
  380. }
  381. static inline int rrdcalc_isrunnable(RRDCALC *rc, time_t now, time_t *next_run) {
  382. if(unlikely(!rc->rrdset)) {
  383. debug(D_HEALTH, "Health not running alarm '%s.%s'. It is not linked to a chart.", rc->chart?rc->chart:"NOCHART", rc->name);
  384. return 0;
  385. }
  386. if(unlikely(rc->next_update > now)) {
  387. if (unlikely(*next_run > rc->next_update)) {
  388. // update the next_run time of the main loop
  389. // to run this alarm precisely the time required
  390. *next_run = rc->next_update;
  391. }
  392. debug(D_HEALTH, "Health not examining alarm '%s.%s' yet (will do in %d secs).", rc->chart?rc->chart:"NOCHART", rc->name, (int) (rc->next_update - now));
  393. return 0;
  394. }
  395. if(unlikely(!rc->update_every)) {
  396. debug(D_HEALTH, "Health not running alarm '%s.%s'. It does not have an update frequency", rc->chart?rc->chart:"NOCHART", rc->name);
  397. return 0;
  398. }
  399. if(unlikely(rrdset_flag_check(rc->rrdset, RRDSET_FLAG_OBSOLETE))) {
  400. debug(D_HEALTH, "Health not running alarm '%s.%s'. The chart has been marked as obsolete", rc->chart?rc->chart:"NOCHART", rc->name);
  401. return 0;
  402. }
  403. if(unlikely(!rrdset_flag_check(rc->rrdset, RRDSET_FLAG_ENABLED))) {
  404. debug(D_HEALTH, "Health not running alarm '%s.%s'. The chart is not enabled", rc->chart?rc->chart:"NOCHART", rc->name);
  405. return 0;
  406. }
  407. if(unlikely(rrdset_flag_check(rc->rrdset, RRDSET_FLAG_ARCHIVED))) {
  408. debug(D_HEALTH, "Health not running alarm '%s.%s'. The chart has been marked as archived", rc->chart?rc->chart:"NOCHART", rc->name);
  409. return 0;
  410. }
  411. if(unlikely(!rc->rrdset->last_collected_time.tv_sec || rc->rrdset->counter_done < 2)) {
  412. debug(D_HEALTH, "Health not running alarm '%s.%s'. Chart is not fully collected yet.", rc->chart?rc->chart:"NOCHART", rc->name);
  413. return 0;
  414. }
  415. int update_every = rc->rrdset->update_every;
  416. rrdset_rdlock(rc->rrdset);
  417. time_t first = rrdset_first_entry_t_nolock(rc->rrdset);
  418. time_t last = rrdset_last_entry_t_nolock(rc->rrdset);
  419. rrdset_unlock(rc->rrdset);
  420. if(unlikely(now + update_every < first /* || now - update_every > last */)) {
  421. debug(D_HEALTH
  422. , "Health not examining alarm '%s.%s' yet (wanted time is out of bounds - we need %lu but got %lu - %lu)."
  423. , rc->chart ? rc->chart : "NOCHART", rc->name, (unsigned long) now, (unsigned long) first
  424. , (unsigned long) last);
  425. return 0;
  426. }
  427. if(RRDCALC_HAS_DB_LOOKUP(rc)) {
  428. time_t needed = now + rc->before + rc->after;
  429. if(needed + update_every < first || needed - update_every > last) {
  430. debug(D_HEALTH
  431. , "Health not examining alarm '%s.%s' yet (not enough data yet - we need %lu but got %lu - %lu)."
  432. , rc->chart ? rc->chart : "NOCHART", rc->name, (unsigned long) needed, (unsigned long) first
  433. , (unsigned long) last);
  434. return 0;
  435. }
  436. }
  437. return 1;
  438. }
  439. static inline int check_if_resumed_from_suspension(void) {
  440. static usec_t last_realtime = 0, last_monotonic = 0;
  441. usec_t realtime = now_realtime_usec(), monotonic = now_monotonic_usec();
  442. int ret = 0;
  443. // detect if monotonic and realtime have twice the difference
  444. // in which case we assume the system was just waken from hibernation
  445. if(last_realtime && last_monotonic && realtime - last_realtime > 2 * (monotonic - last_monotonic))
  446. ret = 1;
  447. last_realtime = realtime;
  448. last_monotonic = monotonic;
  449. return ret;
  450. }
  451. static void health_main_cleanup(void *ptr) {
  452. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  453. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  454. info("cleaning up...");
  455. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  456. }
  457. static SILENCE_TYPE check_silenced(RRDCALC *rc, char* host, SILENCERS *silencers) {
  458. SILENCER *s;
  459. debug(D_HEALTH, "Checking if alarm was silenced via the command API. Alarm info name:%s context:%s chart:%s host:%s family:%s",
  460. rc->name, (rc->rrdset)?rc->rrdset->context:"", rc->chart, host, (rc->rrdset)?rc->rrdset->family:"");
  461. for (s = silencers->silencers; s!=NULL; s=s->next){
  462. if (
  463. (!s->alarms_pattern || (rc->name && s->alarms_pattern && simple_pattern_matches(s->alarms_pattern,rc->name))) &&
  464. (!s->contexts_pattern || (rc->rrdset && rc->rrdset->context && s->contexts_pattern && simple_pattern_matches(s->contexts_pattern,rc->rrdset->context))) &&
  465. (!s->hosts_pattern || (host && s->hosts_pattern && simple_pattern_matches(s->hosts_pattern,host))) &&
  466. (!s->charts_pattern || (rc->chart && s->charts_pattern && simple_pattern_matches(s->charts_pattern,rc->chart))) &&
  467. (!s->families_pattern || (rc->rrdset && rc->rrdset->family && s->families_pattern && simple_pattern_matches(s->families_pattern,rc->rrdset->family)))
  468. ) {
  469. debug(D_HEALTH, "Alarm matches command API silence entry %s:%s:%s:%s:%s", s->alarms,s->charts, s->contexts, s->hosts, s->families);
  470. if (unlikely(silencers->stype == STYPE_NONE)) {
  471. 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.", rc->name);
  472. } else {
  473. debug(D_HEALTH, "Alarm %s via the command API - name:%s context:%s chart:%s host:%s family:%s"
  474. , (silencers->stype == STYPE_DISABLE_ALARMS)?"Disabled":"Silenced"
  475. , rc->name
  476. , (rc->rrdset)?rc->rrdset->context:""
  477. , rc->chart
  478. , host
  479. , (rc->rrdset)?rc->rrdset->family:""
  480. );
  481. }
  482. return silencers->stype;
  483. }
  484. }
  485. return STYPE_NONE;
  486. }
  487. /**
  488. * Update Disabled Silenced
  489. *
  490. * Update the variable rrdcalc_flags of the structure RRDCALC according with the values of the host structure
  491. *
  492. * @param host structure that contains information about the host monitored.
  493. * @param rc structure with information about the alarm
  494. *
  495. * @return It returns 1 case rrdcalc_flags is DISABLED or 0 otherwise
  496. */
  497. static int update_disabled_silenced(RRDHOST *host, RRDCALC *rc) {
  498. uint32_t rrdcalc_flags_old = rc->rrdcalc_flags;
  499. // Clear the flags
  500. rc->rrdcalc_flags &= ~(RRDCALC_FLAG_DISABLED | RRDCALC_FLAG_SILENCED);
  501. if (unlikely(silencers->all_alarms)) {
  502. if (silencers->stype == STYPE_DISABLE_ALARMS) rc->rrdcalc_flags |= RRDCALC_FLAG_DISABLED;
  503. else if (silencers->stype == STYPE_SILENCE_NOTIFICATIONS) rc->rrdcalc_flags |= RRDCALC_FLAG_SILENCED;
  504. } else {
  505. SILENCE_TYPE st = check_silenced(rc, host->hostname, silencers);
  506. if (st == STYPE_DISABLE_ALARMS) rc->rrdcalc_flags |= RRDCALC_FLAG_DISABLED;
  507. else if (st == STYPE_SILENCE_NOTIFICATIONS) rc->rrdcalc_flags |= RRDCALC_FLAG_SILENCED;
  508. }
  509. if (rrdcalc_flags_old != rc->rrdcalc_flags) {
  510. info("Alarm silencing changed for host '%s' alarm '%s': Disabled %s->%s Silenced %s->%s",
  511. host->hostname,
  512. rc->name,
  513. (rrdcalc_flags_old & RRDCALC_FLAG_DISABLED)?"true":"false",
  514. (rc->rrdcalc_flags & RRDCALC_FLAG_DISABLED)?"true":"false",
  515. (rrdcalc_flags_old & RRDCALC_FLAG_SILENCED)?"true":"false",
  516. (rc->rrdcalc_flags & RRDCALC_FLAG_SILENCED)?"true":"false"
  517. );
  518. }
  519. if (rc->rrdcalc_flags & RRDCALC_FLAG_DISABLED)
  520. return 1;
  521. else
  522. return 0;
  523. }
  524. /**
  525. * Health Main
  526. *
  527. * The main thread of the health system. In this function all the alarms will be processed.
  528. *
  529. * @param ptr is a pointer to the netdata_static_thread structure.
  530. *
  531. * @return It always returns NULL
  532. */
  533. void *health_main(void *ptr) {
  534. netdata_thread_cleanup_push(health_main_cleanup, ptr);
  535. int min_run_every = (int)config_get_number(CONFIG_SECTION_HEALTH, "run at least every seconds", 10);
  536. if(min_run_every < 1) min_run_every = 1;
  537. time_t now = now_realtime_sec();
  538. time_t hibernation_delay = config_get_number(CONFIG_SECTION_HEALTH, "postpone alarms during hibernation for seconds", 60);
  539. rrdcalc_labels_unlink();
  540. unsigned int loop = 0;
  541. while(!netdata_exit) {
  542. loop++;
  543. debug(D_HEALTH, "Health monitoring iteration no %u started", loop);
  544. int runnable = 0, apply_hibernation_delay = 0;
  545. time_t next_run = now + min_run_every;
  546. RRDCALC *rc;
  547. if (unlikely(check_if_resumed_from_suspension())) {
  548. apply_hibernation_delay = 1;
  549. info("Postponing alarm checks for %ld seconds, because it seems that the system was just resumed from suspension.",
  550. hibernation_delay
  551. );
  552. }
  553. if (unlikely(silencers->all_alarms && silencers->stype == STYPE_DISABLE_ALARMS)) {
  554. static int logged=0;
  555. if (!logged) {
  556. info("Skipping health checks, because all alarms are disabled via a %s command.",
  557. HEALTH_CMDAPI_CMD_DISABLEALL);
  558. logged = 1;
  559. }
  560. }
  561. rrd_rdlock();
  562. RRDHOST *host;
  563. rrdhost_foreach_read(host) {
  564. if (unlikely(!host->health_enabled))
  565. continue;
  566. if (unlikely(apply_hibernation_delay)) {
  567. info("Postponing health checks for %ld seconds, on host '%s'.", hibernation_delay, host->hostname
  568. );
  569. host->health_delay_up_to = now + hibernation_delay;
  570. }
  571. if (unlikely(host->health_delay_up_to)) {
  572. if (unlikely(now < host->health_delay_up_to))
  573. continue;
  574. info("Resuming health checks on host '%s'.", host->hostname);
  575. host->health_delay_up_to = 0;
  576. }
  577. rrdhost_rdlock(host);
  578. // the first loop is to lookup values from the db
  579. for (rc = host->alarms; rc; rc = rc->next) {
  580. if (update_disabled_silenced(host, rc))
  581. continue;
  582. if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run))) {
  583. if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_RUNNABLE))
  584. rc->rrdcalc_flags &= ~RRDCALC_FLAG_RUNNABLE;
  585. continue;
  586. }
  587. runnable++;
  588. rc->old_value = rc->value;
  589. rc->rrdcalc_flags |= RRDCALC_FLAG_RUNNABLE;
  590. // ------------------------------------------------------------
  591. // if there is database lookup, do it
  592. if (unlikely(RRDCALC_HAS_DB_LOOKUP(rc))) {
  593. /* time_t old_db_timestamp = rc->db_before; */
  594. int value_is_null = 0;
  595. int ret = rrdset2value_api_v1(rc->rrdset, NULL, &rc->value, rc->dimensions, 1, rc->after,
  596. rc->before, rc->group, 0, rc->options, &rc->db_after,
  597. &rc->db_before, &value_is_null
  598. );
  599. if (unlikely(ret != 200)) {
  600. // database lookup failed
  601. rc->value = NAN;
  602. rc->rrdcalc_flags |= RRDCALC_FLAG_DB_ERROR;
  603. debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': database lookup returned error %d",
  604. host->hostname, rc->chart ? rc->chart : "NOCHART", rc->name, ret
  605. );
  606. } else
  607. rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_ERROR;
  608. /* - RRDCALC_FLAG_DB_STALE not currently used
  609. if (unlikely(old_db_timestamp == rc->db_before)) {
  610. // database is stale
  611. debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': database is stale", host->hostname, rc->chart?rc->chart:"NOCHART", rc->name);
  612. if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))) {
  613. rc->rrdcalc_flags |= RRDCALC_FLAG_DB_STALE;
  614. error("Health on host '%s', alarm '%s.%s': database is stale", host->hostname, rc->chart?rc->chart:"NOCHART", rc->name);
  615. }
  616. }
  617. else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))
  618. rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_STALE;
  619. */
  620. if (unlikely(value_is_null)) {
  621. // collected value is null
  622. rc->value = NAN;
  623. rc->rrdcalc_flags |= RRDCALC_FLAG_DB_NAN;
  624. debug(D_HEALTH,
  625. "Health on host '%s', alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
  626. host->hostname, rc->chart ? rc->chart : "NOCHART", rc->name
  627. );
  628. } else
  629. rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_NAN;
  630. debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': database lookup gave value "
  631. CALCULATED_NUMBER_FORMAT, host->hostname, rc->chart ? rc->chart : "NOCHART", rc->name,
  632. rc->value
  633. );
  634. }
  635. // ------------------------------------------------------------
  636. // if there is calculation expression, run it
  637. if (unlikely(rc->calculation)) {
  638. if (unlikely(!expression_evaluate(rc->calculation))) {
  639. // calculation failed
  640. rc->value = NAN;
  641. rc->rrdcalc_flags |= RRDCALC_FLAG_CALC_ERROR;
  642. debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': expression '%s' failed: %s",
  643. host->hostname, rc->chart ? rc->chart : "NOCHART", rc->name,
  644. rc->calculation->parsed_as, buffer_tostring(rc->calculation->error_msg)
  645. );
  646. } else {
  647. rc->rrdcalc_flags &= ~RRDCALC_FLAG_CALC_ERROR;
  648. debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': expression '%s' gave value "
  649. CALCULATED_NUMBER_FORMAT
  650. ": %s (source: %s)", host->hostname, rc->chart ? rc->chart : "NOCHART", rc->name,
  651. rc->calculation->parsed_as, rc->calculation->result,
  652. buffer_tostring(rc->calculation->error_msg), rc->source
  653. );
  654. rc->value = rc->calculation->result;
  655. if (rc->local) rc->local->last_updated = now;
  656. if (rc->family) rc->family->last_updated = now;
  657. if (rc->hostid) rc->hostid->last_updated = now;
  658. if (rc->hostname) rc->hostname->last_updated = now;
  659. }
  660. }
  661. }
  662. rrdhost_unlock(host);
  663. if (unlikely(runnable && !netdata_exit)) {
  664. rrdhost_rdlock(host);
  665. for (rc = host->alarms; rc; rc = rc->next) {
  666. if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_RUNNABLE)))
  667. continue;
  668. if (rc->rrdcalc_flags & RRDCALC_FLAG_DISABLED) {
  669. continue;
  670. }
  671. RRDCALC_STATUS warning_status = RRDCALC_STATUS_UNDEFINED;
  672. RRDCALC_STATUS critical_status = RRDCALC_STATUS_UNDEFINED;
  673. // --------------------------------------------------------
  674. // check the warning expression
  675. if (likely(rc->warning)) {
  676. if (unlikely(!expression_evaluate(rc->warning))) {
  677. // calculation failed
  678. rc->rrdcalc_flags |= RRDCALC_FLAG_WARN_ERROR;
  679. debug(D_HEALTH,
  680. "Health on host '%s', alarm '%s.%s': warning expression failed with error: %s",
  681. host->hostname, rc->chart ? rc->chart : "NOCHART", rc->name,
  682. buffer_tostring(rc->warning->error_msg)
  683. );
  684. } else {
  685. rc->rrdcalc_flags &= ~RRDCALC_FLAG_WARN_ERROR;
  686. debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': warning expression gave value "
  687. CALCULATED_NUMBER_FORMAT
  688. ": %s (source: %s)", host->hostname, rc->chart ? rc->chart : "NOCHART",
  689. rc->name, rc->warning->result, buffer_tostring(rc->warning->error_msg), rc->source
  690. );
  691. warning_status = rrdcalc_value2status(rc->warning->result);
  692. }
  693. }
  694. // --------------------------------------------------------
  695. // check the critical expression
  696. if (likely(rc->critical)) {
  697. if (unlikely(!expression_evaluate(rc->critical))) {
  698. // calculation failed
  699. rc->rrdcalc_flags |= RRDCALC_FLAG_CRIT_ERROR;
  700. debug(D_HEALTH,
  701. "Health on host '%s', alarm '%s.%s': critical expression failed with error: %s",
  702. host->hostname, rc->chart ? rc->chart : "NOCHART", rc->name,
  703. buffer_tostring(rc->critical->error_msg)
  704. );
  705. } else {
  706. rc->rrdcalc_flags &= ~RRDCALC_FLAG_CRIT_ERROR;
  707. debug(D_HEALTH, "Health on host '%s', alarm '%s.%s': critical expression gave value "
  708. CALCULATED_NUMBER_FORMAT
  709. ": %s (source: %s)", host->hostname, rc->chart ? rc->chart : "NOCHART",
  710. rc->name, rc->critical->result, buffer_tostring(rc->critical->error_msg),
  711. rc->source
  712. );
  713. critical_status = rrdcalc_value2status(rc->critical->result);
  714. }
  715. }
  716. // --------------------------------------------------------
  717. // decide the final alarm status
  718. RRDCALC_STATUS status = RRDCALC_STATUS_UNDEFINED;
  719. switch (warning_status) {
  720. case RRDCALC_STATUS_CLEAR:
  721. status = RRDCALC_STATUS_CLEAR;
  722. break;
  723. case RRDCALC_STATUS_RAISED:
  724. status = RRDCALC_STATUS_WARNING;
  725. break;
  726. default:
  727. break;
  728. }
  729. switch (critical_status) {
  730. case RRDCALC_STATUS_CLEAR:
  731. if (status == RRDCALC_STATUS_UNDEFINED)
  732. status = RRDCALC_STATUS_CLEAR;
  733. break;
  734. case RRDCALC_STATUS_RAISED:
  735. status = RRDCALC_STATUS_CRITICAL;
  736. break;
  737. default:
  738. break;
  739. }
  740. // --------------------------------------------------------
  741. // check if the new status and the old differ
  742. if (status != rc->status) {
  743. int delay = 0;
  744. // apply trigger hysteresis
  745. if (now > rc->delay_up_to_timestamp) {
  746. rc->delay_up_current = rc->delay_up_duration;
  747. rc->delay_down_current = rc->delay_down_duration;
  748. rc->delay_last = 0;
  749. rc->delay_up_to_timestamp = 0;
  750. } else {
  751. rc->delay_up_current = (int) (rc->delay_up_current * rc->delay_multiplier);
  752. if (rc->delay_up_current > rc->delay_max_duration)
  753. rc->delay_up_current = rc->delay_max_duration;
  754. rc->delay_down_current = (int) (rc->delay_down_current * rc->delay_multiplier);
  755. if (rc->delay_down_current > rc->delay_max_duration)
  756. rc->delay_down_current = rc->delay_max_duration;
  757. }
  758. if (status > rc->status)
  759. delay = rc->delay_up_current;
  760. else
  761. delay = rc->delay_down_current;
  762. // COMMENTED: because we do need to send raising alarms
  763. // if(now + delay < rc->delay_up_to_timestamp)
  764. // delay = (int)(rc->delay_up_to_timestamp - now);
  765. rc->delay_last = delay;
  766. rc->delay_up_to_timestamp = now + delay;
  767. if(likely(!rrdcalc_isrepeating(rc))) {
  768. ALARM_ENTRY *ae = health_create_alarm_entry(
  769. host, rc->id, rc->next_event_id++, now, rc->name, rc->rrdset->id,
  770. rc->rrdset->family, rc->exec, rc->recipient, now - rc->last_status_change,
  771. rc->old_value, rc->value, rc->status, status, rc->source, rc->units, rc->info,
  772. rc->delay_last,
  773. (
  774. ((rc->options & RRDCALC_FLAG_NO_CLEAR_NOTIFICATION)? HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION : 0) |
  775. ((rc->rrdcalc_flags & RRDCALC_FLAG_SILENCED)? HEALTH_ENTRY_FLAG_SILENCED : 0)
  776. )
  777. );
  778. health_alarm_log(host, ae);
  779. }
  780. rc->last_status_change = now;
  781. rc->old_status = rc->status;
  782. rc->status = status;
  783. }
  784. rc->last_updated = now;
  785. rc->next_update = now + rc->update_every;
  786. if (next_run > rc->next_update)
  787. next_run = rc->next_update;
  788. }
  789. // process repeating alarms
  790. RRDCALC *rc;
  791. for(rc = host->alarms; rc ; rc = rc->next) {
  792. int repeat_every = 0;
  793. if(unlikely(rrdcalc_isrepeating(rc))) {
  794. if(unlikely(rc->status == RRDCALC_STATUS_WARNING)) {
  795. rc->rrdcalc_flags &= ~RRDCALC_FLAG_RUN_ONCE;
  796. repeat_every = rc->warn_repeat_every;
  797. } else if(unlikely(rc->status == RRDCALC_STATUS_CRITICAL)) {
  798. rc->rrdcalc_flags &= ~RRDCALC_FLAG_RUN_ONCE;
  799. repeat_every = rc->crit_repeat_every;
  800. } else if(unlikely(rc->status == RRDCALC_STATUS_CLEAR)) {
  801. if(!(rc->rrdcalc_flags & RRDCALC_FLAG_RUN_ONCE)) {
  802. if(rc->old_status == RRDCALC_STATUS_CRITICAL) {
  803. repeat_every = 1;
  804. } else if (rc->old_status == RRDCALC_STATUS_WARNING) {
  805. repeat_every = 1;
  806. }
  807. }
  808. }
  809. } else {
  810. continue;
  811. }
  812. if(unlikely(repeat_every > 0 && (rc->last_repeat + repeat_every) <= now)) {
  813. rc->last_repeat = now;
  814. ALARM_ENTRY *ae = health_create_alarm_entry(
  815. host, rc->id, rc->next_event_id++, now, rc->name, rc->rrdset->id,
  816. rc->rrdset->family, rc->exec, rc->recipient, now - rc->last_status_change,
  817. rc->old_value, rc->value, rc->old_status, rc->status, rc->source, rc->units, rc->info,
  818. rc->delay_last,
  819. (
  820. ((rc->options & RRDCALC_FLAG_NO_CLEAR_NOTIFICATION)? HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION : 0) |
  821. ((rc->rrdcalc_flags & RRDCALC_FLAG_SILENCED)? HEALTH_ENTRY_FLAG_SILENCED : 0)
  822. )
  823. );
  824. ae->last_repeat = rc->last_repeat;
  825. if (!(rc->rrdcalc_flags & RRDCALC_FLAG_RUN_ONCE) && rc->status == RRDCALC_STATUS_CLEAR) {
  826. ae->flags |= HEALTH_ENTRY_RUN_ONCE;
  827. }
  828. rc->rrdcalc_flags |= RRDCALC_FLAG_RUN_ONCE;
  829. health_process_notifications(host, ae);
  830. debug(D_HEALTH, "Notification sent for the repeating alarm %u.", ae->alarm_id);
  831. health_alarm_wait_for_execution(ae);
  832. health_alarm_log_free_one_nochecks_nounlink(ae);
  833. }
  834. }
  835. rrdhost_unlock(host);
  836. }
  837. if (unlikely(netdata_exit))
  838. break;
  839. // execute notifications
  840. // and cleanup
  841. health_alarm_log_process(host);
  842. if (unlikely(netdata_exit)) {
  843. // wait for all notifications to finish before allowing health to be cleaned up
  844. ALARM_ENTRY *ae;
  845. while (NULL != (ae = alarm_notifications_in_progress.head)) {
  846. health_alarm_wait_for_execution(ae);
  847. }
  848. break;
  849. }
  850. } /* rrdhost_foreach */
  851. // wait for all notifications to finish before allowing health to be cleaned up
  852. ALARM_ENTRY *ae;
  853. while (NULL != (ae = alarm_notifications_in_progress.head)) {
  854. health_alarm_wait_for_execution(ae);
  855. }
  856. rrd_unlock();
  857. if(unlikely(netdata_exit))
  858. break;
  859. now = now_realtime_sec();
  860. if(now < next_run) {
  861. debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration in %d secs", loop, (int) (next_run - now));
  862. sleep_usec(USEC_PER_SEC * (usec_t) (next_run - now));
  863. now = now_realtime_sec();
  864. }
  865. else
  866. debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration now", loop);
  867. } // forever
  868. netdata_thread_cleanup_pop(1);
  869. return NULL;
  870. }