rrdcalc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #define NETDATA_HEALTH_INTERNALS
  3. #include "rrd.h"
  4. // ----------------------------------------------------------------------------
  5. // RRDCALC management
  6. inline const char *rrdcalc_status2string(RRDCALC_STATUS status) {
  7. switch(status) {
  8. case RRDCALC_STATUS_REMOVED:
  9. return "REMOVED";
  10. case RRDCALC_STATUS_UNDEFINED:
  11. return "UNDEFINED";
  12. case RRDCALC_STATUS_UNINITIALIZED:
  13. return "UNINITIALIZED";
  14. case RRDCALC_STATUS_CLEAR:
  15. return "CLEAR";
  16. case RRDCALC_STATUS_RAISED:
  17. return "RAISED";
  18. case RRDCALC_STATUS_WARNING:
  19. return "WARNING";
  20. case RRDCALC_STATUS_CRITICAL:
  21. return "CRITICAL";
  22. default:
  23. error("Unknown alarm status %d", status);
  24. return "UNKNOWN";
  25. }
  26. }
  27. static void rrdsetcalc_link(RRDSET *st, RRDCALC *rc) {
  28. RRDHOST *host = st->rrdhost;
  29. debug(D_HEALTH, "Health linking alarm '%s.%s' to chart '%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, st->id, host->hostname);
  30. rc->last_status_change = now_realtime_sec();
  31. rc->rrdset = st;
  32. rc->rrdset_next = st->alarms;
  33. rc->rrdset_prev = NULL;
  34. if(rc->rrdset_next)
  35. rc->rrdset_next->rrdset_prev = rc;
  36. st->alarms = rc;
  37. if(rc->update_every < rc->rrdset->update_every) {
  38. error("Health alarm '%s.%s' has update every %d, less than chart update every %d. Setting alarm update frequency to %d.", rc->rrdset->id, rc->name, rc->update_every, rc->rrdset->update_every, rc->rrdset->update_every);
  39. rc->update_every = rc->rrdset->update_every;
  40. }
  41. if(!isnan(rc->green) && isnan(st->green)) {
  42. debug(D_HEALTH, "Health alarm '%s.%s' green threshold set from " CALCULATED_NUMBER_FORMAT_AUTO " to " CALCULATED_NUMBER_FORMAT_AUTO ".", rc->rrdset->id, rc->name, rc->rrdset->green, rc->green);
  43. st->green = rc->green;
  44. }
  45. if(!isnan(rc->red) && isnan(st->red)) {
  46. debug(D_HEALTH, "Health alarm '%s.%s' red threshold set from " CALCULATED_NUMBER_FORMAT_AUTO " to " CALCULATED_NUMBER_FORMAT_AUTO ".", rc->rrdset->id, rc->name, rc->rrdset->red, rc->red);
  47. st->red = rc->red;
  48. }
  49. rc->local = rrdvar_create_and_index("local", &st->rrdvar_root_index, rc->name, RRDVAR_TYPE_CALCULATED, RRDVAR_OPTION_RRDCALC_LOCAL_VAR, &rc->value);
  50. rc->family = rrdvar_create_and_index("family", &st->rrdfamily->rrdvar_root_index, rc->name, RRDVAR_TYPE_CALCULATED, RRDVAR_OPTION_RRDCALC_FAMILY_VAR, &rc->value);
  51. char fullname[RRDVAR_MAX_LENGTH + 1];
  52. snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->id, rc->name);
  53. rc->hostid = rrdvar_create_and_index("host", &host->rrdvar_root_index, fullname, RRDVAR_TYPE_CALCULATED, RRDVAR_OPTION_RRDCALC_HOST_CHARTID_VAR, &rc->value);
  54. snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rc->name);
  55. rc->hostname = rrdvar_create_and_index("host", &host->rrdvar_root_index, fullname, RRDVAR_TYPE_CALCULATED, RRDVAR_OPTION_RRDCALC_HOST_CHARTNAME_VAR, &rc->value);
  56. if(rc->hostid && !rc->hostname)
  57. rc->hostid->options |= RRDVAR_OPTION_RRDCALC_HOST_CHARTNAME_VAR;
  58. if(!rc->units) rc->units = strdupz(st->units);
  59. if(!rrdcalc_isrepeating(rc)) {
  60. time_t now = now_realtime_sec();
  61. ALARM_ENTRY *ae = health_create_alarm_entry(
  62. host,
  63. rc->id,
  64. rc->next_event_id++,
  65. now,
  66. rc->name,
  67. rc->rrdset->id,
  68. rc->rrdset->family,
  69. rc->exec,
  70. rc->recipient,
  71. now - rc->last_status_change,
  72. rc->old_value,
  73. rc->value,
  74. rc->status,
  75. RRDCALC_STATUS_UNINITIALIZED,
  76. rc->source,
  77. rc->units,
  78. rc->info,
  79. 0,
  80. 0
  81. );
  82. health_alarm_log(host, ae);
  83. }
  84. }
  85. static inline int rrdcalc_is_matching_this_rrdset(RRDCALC *rc, RRDSET *st) {
  86. if( (rc->hash_chart == st->hash && !strcmp(rc->chart, st->id)) ||
  87. (rc->hash_chart == st->hash_name && !strcmp(rc->chart, st->name)))
  88. return 1;
  89. return 0;
  90. }
  91. // this has to be called while the RRDHOST is locked
  92. inline void rrdsetcalc_link_matching(RRDSET *st) {
  93. RRDHOST *host = st->rrdhost;
  94. // debug(D_HEALTH, "find matching alarms for chart '%s'", st->id);
  95. RRDCALC *rc;
  96. for(rc = host->alarms; rc ; rc = rc->next) {
  97. if(unlikely(rc->rrdset))
  98. continue;
  99. if(unlikely(rrdcalc_is_matching_this_rrdset(rc, st)))
  100. rrdsetcalc_link(st, rc);
  101. }
  102. }
  103. // this has to be called while the RRDHOST is locked
  104. inline void rrdsetcalc_unlink(RRDCALC *rc) {
  105. RRDSET *st = rc->rrdset;
  106. if(!st) {
  107. debug(D_HEALTH, "Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
  108. error("Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
  109. return;
  110. }
  111. RRDHOST *host = st->rrdhost;
  112. if(!rrdcalc_isrepeating(rc)) {
  113. time_t now = now_realtime_sec();
  114. ALARM_ENTRY *ae = health_create_alarm_entry(
  115. host,
  116. rc->id,
  117. rc->next_event_id++,
  118. now,
  119. rc->name,
  120. rc->rrdset->id,
  121. rc->rrdset->family,
  122. rc->exec,
  123. rc->recipient,
  124. now - rc->last_status_change,
  125. rc->old_value,
  126. rc->value,
  127. rc->status,
  128. RRDCALC_STATUS_REMOVED,
  129. rc->source,
  130. rc->units,
  131. rc->info,
  132. 0,
  133. 0
  134. );
  135. health_alarm_log(host, ae);
  136. }
  137. debug(D_HEALTH, "Health unlinking alarm '%s.%s' from chart '%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, st->id, host->hostname);
  138. // unlink it
  139. if(rc->rrdset_prev)
  140. rc->rrdset_prev->rrdset_next = rc->rrdset_next;
  141. if(rc->rrdset_next)
  142. rc->rrdset_next->rrdset_prev = rc->rrdset_prev;
  143. if(st->alarms == rc)
  144. st->alarms = rc->rrdset_next;
  145. rc->rrdset_prev = rc->rrdset_next = NULL;
  146. rrdvar_free(host, &st->rrdvar_root_index, rc->local);
  147. rc->local = NULL;
  148. rrdvar_free(host, &st->rrdfamily->rrdvar_root_index, rc->family);
  149. rc->family = NULL;
  150. rrdvar_free(host, &host->rrdvar_root_index, rc->hostid);
  151. rc->hostid = NULL;
  152. rrdvar_free(host, &host->rrdvar_root_index, rc->hostname);
  153. rc->hostname = NULL;
  154. rc->rrdset = NULL;
  155. // RRDCALC will remain in RRDHOST
  156. // so that if the matching chart is found in the future
  157. // it will be applied automatically
  158. }
  159. RRDCALC *rrdcalc_find(RRDSET *st, const char *name) {
  160. RRDCALC *rc;
  161. uint32_t hash = simple_hash(name);
  162. for( rc = st->alarms; rc ; rc = rc->rrdset_next ) {
  163. if(unlikely(rc->hash == hash && !strcmp(rc->name, name)))
  164. return rc;
  165. }
  166. return NULL;
  167. }
  168. inline int rrdcalc_exists(RRDHOST *host, const char *chart, const char *name, uint32_t hash_chart, uint32_t hash_name) {
  169. RRDCALC *rc;
  170. if(unlikely(!chart)) {
  171. error("attempt to find RRDCALC '%s' without giving a chart name", name);
  172. return 1;
  173. }
  174. if(unlikely(!hash_chart)) hash_chart = simple_hash(chart);
  175. if(unlikely(!hash_name)) hash_name = simple_hash(name);
  176. // make sure it does not already exist
  177. for(rc = host->alarms; rc ; rc = rc->next) {
  178. if (unlikely(rc->chart && rc->hash == hash_name && rc->hash_chart == hash_chart && !strcmp(name, rc->name) && !strcmp(chart, rc->chart))) {
  179. debug(D_HEALTH, "Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
  180. info("Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
  181. return 1;
  182. }
  183. }
  184. return 0;
  185. }
  186. inline uint32_t rrdcalc_get_unique_id(RRDHOST *host, const char *chart, const char *name, uint32_t *next_event_id) {
  187. if(chart && name) {
  188. uint32_t hash_chart = simple_hash(chart);
  189. uint32_t hash_name = simple_hash(name);
  190. // re-use old IDs, by looking them up in the alarm log
  191. ALARM_ENTRY *ae;
  192. for(ae = host->health_log.alarms; ae ;ae = ae->next) {
  193. if(unlikely(ae->hash_name == hash_name && ae->hash_chart == hash_chart && !strcmp(name, ae->name) && !strcmp(chart, ae->chart))) {
  194. if(next_event_id) *next_event_id = ae->alarm_event_id + 1;
  195. return ae->alarm_id;
  196. }
  197. }
  198. }
  199. return host->health_log.next_alarm_id++;
  200. }
  201. /**
  202. * Alarm name with dimension
  203. *
  204. * Change the name of the current alarm appending a new diagram.
  205. *
  206. * @param name the alarm name
  207. * @param namelen is the length of the previous vector.
  208. * @param dim the dimension of the chart.
  209. * @param dimlen is the length of the previous vector.
  210. *
  211. * @return It returns the new name on success and the old otherwise
  212. */
  213. char *alarm_name_with_dim(char *name, size_t namelen, const char *dim, size_t dimlen) {
  214. char *newname,*move;
  215. newname = malloc(namelen + dimlen + 2);
  216. if(newname) {
  217. move = newname;
  218. memcpy(move, name, namelen);
  219. move += namelen;
  220. *move++ = '_';
  221. memcpy(move, dim, dimlen);
  222. move += dimlen;
  223. *move = '\0';
  224. } else {
  225. newname = name;
  226. }
  227. return newname;
  228. }
  229. /**
  230. * Remove pipe comma
  231. *
  232. * Remove the pipes and commas converting to space.
  233. *
  234. * @param str the string to change.
  235. */
  236. void dimension_remove_pipe_comma(char *str) {
  237. while(*str) {
  238. if(*str == '|' || *str == ',') *str = ' ';
  239. str++;
  240. }
  241. }
  242. inline void rrdcalc_add_to_host(RRDHOST *host, RRDCALC *rc) {
  243. rrdhost_check_rdlock(host);
  244. if(rc->calculation) {
  245. rc->calculation->status = &rc->status;
  246. rc->calculation->this = &rc->value;
  247. rc->calculation->after = &rc->db_after;
  248. rc->calculation->before = &rc->db_before;
  249. rc->calculation->rrdcalc = rc;
  250. }
  251. if(rc->warning) {
  252. rc->warning->status = &rc->status;
  253. rc->warning->this = &rc->value;
  254. rc->warning->after = &rc->db_after;
  255. rc->warning->before = &rc->db_before;
  256. rc->warning->rrdcalc = rc;
  257. }
  258. if(rc->critical) {
  259. rc->critical->status = &rc->status;
  260. rc->critical->this = &rc->value;
  261. rc->critical->after = &rc->db_after;
  262. rc->critical->before = &rc->db_before;
  263. rc->critical->rrdcalc = rc;
  264. }
  265. if(!rc->foreachdim) {
  266. // link it to the host alarms list
  267. if(likely(host->alarms)) {
  268. // append it
  269. RRDCALC *t;
  270. for(t = host->alarms; t && t->next ; t = t->next) ;
  271. t->next = rc;
  272. }
  273. else {
  274. host->alarms = rc;
  275. }
  276. // link it to its chart
  277. RRDSET *st;
  278. rrdset_foreach_read(st, host) {
  279. if(rrdcalc_is_matching_this_rrdset(rc, st)) {
  280. rrdsetcalc_link(st, rc);
  281. break;
  282. }
  283. }
  284. } else {
  285. //link it case there is a foreach
  286. if(likely(host->alarms_with_foreach)) {
  287. // append it
  288. RRDCALC *t;
  289. for(t = host->alarms_with_foreach; t && t->next ; t = t->next) ;
  290. t->next = rc;
  291. }
  292. else {
  293. host->alarms_with_foreach = rc;
  294. }
  295. //I am not linking this alarm direct to the host here, this will be done when the children is created
  296. }
  297. }
  298. inline RRDCALC *rrdcalc_create_from_template(RRDHOST *host, RRDCALCTEMPLATE *rt, const char *chart) {
  299. debug(D_HEALTH, "Health creating dynamic alarm (from template) '%s.%s'", chart, rt->name);
  300. if(rrdcalc_exists(host, chart, rt->name, 0, 0))
  301. return NULL;
  302. RRDCALC *rc = callocz(1, sizeof(RRDCALC));
  303. rc->next_event_id = 1;
  304. rc->name = strdupz(rt->name);
  305. rc->hash = simple_hash(rc->name);
  306. rc->chart = strdupz(chart);
  307. rc->hash_chart = simple_hash(rc->chart);
  308. rc->id = rrdcalc_get_unique_id(host, rc->chart, rc->name, &rc->next_event_id);
  309. if(rt->dimensions) rc->dimensions = strdupz(rt->dimensions);
  310. if(rt->foreachdim) {
  311. rc->foreachdim = strdupz(rt->foreachdim);
  312. rc->spdim = health_pattern_from_foreach(rc->foreachdim);
  313. }
  314. rc->foreachcounter = rt->foreachcounter;
  315. rc->green = rt->green;
  316. rc->red = rt->red;
  317. rc->value = NAN;
  318. rc->old_value = NAN;
  319. rc->delay_up_duration = rt->delay_up_duration;
  320. rc->delay_down_duration = rt->delay_down_duration;
  321. rc->delay_max_duration = rt->delay_max_duration;
  322. rc->delay_multiplier = rt->delay_multiplier;
  323. rc->last_repeat = 0;
  324. rc->warn_repeat_every = rt->warn_repeat_every;
  325. rc->crit_repeat_every = rt->crit_repeat_every;
  326. rc->group = rt->group;
  327. rc->after = rt->after;
  328. rc->before = rt->before;
  329. rc->update_every = rt->update_every;
  330. rc->options = rt->options;
  331. if(rt->exec) rc->exec = strdupz(rt->exec);
  332. if(rt->recipient) rc->recipient = strdupz(rt->recipient);
  333. if(rt->source) rc->source = strdupz(rt->source);
  334. if(rt->units) rc->units = strdupz(rt->units);
  335. if(rt->info) rc->info = strdupz(rt->info);
  336. if(rt->calculation) {
  337. rc->calculation = expression_parse(rt->calculation->source, NULL, NULL);
  338. if(!rc->calculation)
  339. error("Health alarm '%s.%s': failed to parse calculation expression '%s'", chart, rt->name, rt->calculation->source);
  340. }
  341. if(rt->warning) {
  342. rc->warning = expression_parse(rt->warning->source, NULL, NULL);
  343. if(!rc->warning)
  344. error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", chart, rt->name, rt->warning->source);
  345. }
  346. if(rt->critical) {
  347. rc->critical = expression_parse(rt->critical->source, NULL, NULL);
  348. if(!rc->critical)
  349. error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", chart, rt->name, rt->critical->source);
  350. }
  351. debug(D_HEALTH, "Health runtime added alarm '%s.%s': exec '%s', recipient '%s', green " CALCULATED_NUMBER_FORMAT_AUTO ", red " CALCULATED_NUMBER_FORMAT_AUTO ", lookup: group %d, after %d, before %d, options %u, dimensions '%s', for each dimension '%s', update every %d, calculation '%s', warning '%s', critical '%s', source '%s', delay up %d, delay down %d, delay max %d, delay_multiplier %f, warn_repeat_every %u, crit_repeat_every %u",
  352. (rc->chart)?rc->chart:"NOCHART",
  353. rc->name,
  354. (rc->exec)?rc->exec:"DEFAULT",
  355. (rc->recipient)?rc->recipient:"DEFAULT",
  356. rc->green,
  357. rc->red,
  358. (int)rc->group,
  359. rc->after,
  360. rc->before,
  361. rc->options,
  362. (rc->dimensions)?rc->dimensions:"NONE",
  363. (rc->foreachdim)?rc->foreachdim:"NONE",
  364. rc->update_every,
  365. (rc->calculation)?rc->calculation->parsed_as:"NONE",
  366. (rc->warning)?rc->warning->parsed_as:"NONE",
  367. (rc->critical)?rc->critical->parsed_as:"NONE",
  368. rc->source,
  369. rc->delay_up_duration,
  370. rc->delay_down_duration,
  371. rc->delay_max_duration,
  372. rc->delay_multiplier,
  373. rc->warn_repeat_every,
  374. rc->crit_repeat_every
  375. );
  376. rrdcalc_add_to_host(host, rc);
  377. if(!rt->foreachdim) {
  378. RRDCALC *rdcmp = (RRDCALC *) avl_insert_lock(&(host)->alarms_idx_health_log,(avl *)rc);
  379. if (rdcmp != rc) {
  380. error("Cannot insert the alarm index ID %s",rc->name);
  381. }
  382. }
  383. return rc;
  384. }
  385. /**
  386. * Create from RRDCALC
  387. *
  388. * Create a new alarm using another alarm as template.
  389. *
  390. * @param rc is the alarm that will be used as source
  391. * @param host is the host structure.
  392. * @param name is the newest chart name.
  393. * @param dimension is the current dimension
  394. * @param foreachdim the whole list of dimension
  395. *
  396. * @return it returns the new alarm changed.
  397. */
  398. inline RRDCALC *rrdcalc_create_from_rrdcalc(RRDCALC *rc, RRDHOST *host, const char *name, const char *dimension) {
  399. RRDCALC *newrc = callocz(1, sizeof(RRDCALC));
  400. newrc->next_event_id = 1;
  401. newrc->id = rrdcalc_get_unique_id(host, rc->chart, name, &rc->next_event_id);
  402. newrc->name = (char *)name;
  403. newrc->hash = simple_hash(newrc->name);
  404. newrc->chart = strdupz(rc->chart);
  405. newrc->hash_chart = simple_hash(rc->chart);
  406. newrc->dimensions = strdupz(dimension);
  407. newrc->foreachdim = NULL;
  408. rc->foreachcounter++;
  409. newrc->foreachcounter = rc->foreachcounter;
  410. newrc->green = rc->green;
  411. newrc->red = rc->red;
  412. newrc->value = NAN;
  413. newrc->old_value = NAN;
  414. newrc->delay_up_duration = rc->delay_up_duration;
  415. newrc->delay_down_duration = rc->delay_down_duration;
  416. newrc->delay_max_duration = rc->delay_max_duration;
  417. newrc->delay_multiplier = rc->delay_multiplier;
  418. newrc->last_repeat = 0;
  419. newrc->warn_repeat_every = rc->warn_repeat_every;
  420. newrc->crit_repeat_every = rc->crit_repeat_every;
  421. newrc->group = rc->group;
  422. newrc->after = rc->after;
  423. newrc->before = rc->before;
  424. newrc->update_every = rc->update_every;
  425. newrc->options = rc->options;
  426. if(rc->exec) newrc->exec = strdupz(rc->exec);
  427. if(rc->recipient) newrc->recipient = strdupz(rc->recipient);
  428. if(rc->source) newrc->source = strdupz(rc->source);
  429. if(rc->units) newrc->units = strdupz(rc->units);
  430. if(rc->info) newrc->info = strdupz(rc->info);
  431. if(rc->calculation) {
  432. newrc->calculation = expression_parse(rc->calculation->source, NULL, NULL);
  433. if(!newrc->calculation)
  434. error("Health alarm '%s.%s': failed to parse calculation expression '%s'", rc->chart, rc->name, rc->calculation->source);
  435. }
  436. if(rc->warning) {
  437. newrc->warning = expression_parse(rc->warning->source, NULL, NULL);
  438. if(!newrc->warning)
  439. error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", rc->chart, rc->name, rc->warning->source);
  440. }
  441. if(rc->critical) {
  442. newrc->critical = expression_parse(rc->critical->source, NULL, NULL);
  443. if(!newrc->critical)
  444. error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", rc->chart, rc->name, rc->critical->source);
  445. }
  446. return newrc;
  447. }
  448. void rrdcalc_free(RRDCALC *rc) {
  449. if(unlikely(!rc)) return;
  450. expression_free(rc->calculation);
  451. expression_free(rc->warning);
  452. expression_free(rc->critical);
  453. freez(rc->name);
  454. freez(rc->chart);
  455. freez(rc->family);
  456. freez(rc->dimensions);
  457. freez(rc->foreachdim);
  458. freez(rc->exec);
  459. freez(rc->recipient);
  460. freez(rc->source);
  461. freez(rc->units);
  462. freez(rc->info);
  463. simple_pattern_free(rc->spdim);
  464. freez(rc->labels);
  465. simple_pattern_free(rc->splabels);
  466. freez(rc);
  467. }
  468. void rrdcalc_unlink_and_free(RRDHOST *host, RRDCALC *rc) {
  469. if(unlikely(!rc)) return;
  470. debug(D_HEALTH, "Health removing alarm '%s.%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
  471. // unlink it from RRDSET
  472. if(rc->rrdset) rrdsetcalc_unlink(rc);
  473. // unlink it from RRDHOST
  474. if(unlikely(rc == host->alarms))
  475. host->alarms = rc->next;
  476. else {
  477. RRDCALC *t;
  478. for(t = host->alarms; t && t->next != rc; t = t->next) ;
  479. if(t) {
  480. t->next = rc->next;
  481. rc->next = NULL;
  482. }
  483. else
  484. error("Cannot unlink alarm '%s.%s' from host '%s': not found", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
  485. }
  486. RRDCALC *rdcmp = (RRDCALC *) avl_search_lock(&(host)->alarms_idx_health_log, (avl *)rc);
  487. if (rdcmp) {
  488. rdcmp = (RRDCALC *) avl_remove_lock(&(host)->alarms_idx_health_log, (avl *)rc);
  489. if (!rdcmp) {
  490. error("Cannot remove the health alarm index from health_log");
  491. }
  492. }
  493. rdcmp = (RRDCALC *) avl_search_lock(&(host)->alarms_idx_name, (avl *)rc);
  494. if (rdcmp) {
  495. rdcmp = (RRDCALC *) avl_remove_lock(&(host)->alarms_idx_name, (avl *)rc);
  496. if (!rdcmp) {
  497. error("Cannot remove the health alarm index from idx_name");
  498. }
  499. }
  500. rrdcalc_free(rc);
  501. }
  502. void rrdcalc_foreach_unlink_and_free(RRDHOST *host, RRDCALC *rc) {
  503. if(unlikely(rc == host->alarms_with_foreach))
  504. host->alarms_with_foreach = rc->next;
  505. else {
  506. RRDCALC *t;
  507. for(t = host->alarms_with_foreach; t && t->next != rc; t = t->next) ;
  508. if(t) {
  509. t->next = rc->next;
  510. rc->next = NULL;
  511. }
  512. else
  513. error("Cannot unlink alarm '%s.%s' from host '%s': not found", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
  514. }
  515. rrdcalc_free(rc);
  516. }
  517. static void rrdcalc_labels_unlink_alarm_loop(RRDHOST *host, RRDCALC *alarms) {
  518. RRDCALC *rc = alarms;
  519. while (rc) {
  520. if (!rc->labels) {
  521. rc = rc->next;
  522. continue;
  523. }
  524. char cmp[CONFIG_FILE_LINE_MAX+1];
  525. struct label *move = host->labels;
  526. while(move) {
  527. snprintf(cmp, CONFIG_FILE_LINE_MAX, "%s=%s", move->key, move->value);
  528. if (simple_pattern_matches(rc->splabels, move->key) ||
  529. simple_pattern_matches(rc->splabels, cmp)) {
  530. break;
  531. }
  532. move = move->next;
  533. }
  534. RRDCALC *next = rc->next;
  535. if(!move) {
  536. info("Health configuration for alarm '%s' cannot be applied, because the host %s does not have the label(s) '%s'",
  537. rc->name,
  538. host->hostname,
  539. rc->labels);
  540. if(host->alarms == alarms) {
  541. rrdcalc_unlink_and_free(host, rc);
  542. } else
  543. rrdcalc_foreach_unlink_and_free(host, rc);
  544. }
  545. rc = next;
  546. }
  547. }
  548. void rrdcalc_labels_unlink_alarm_from_host(RRDHOST *host) {
  549. rrdhost_check_rdlock(host);
  550. netdata_rwlock_rdlock(&host->labels_rwlock);
  551. rrdcalc_labels_unlink_alarm_loop(host, host->alarms);
  552. rrdcalc_labels_unlink_alarm_loop(host, host->alarms_with_foreach);
  553. netdata_rwlock_unlock(&host->labels_rwlock);
  554. }
  555. void rrdcalc_labels_unlink() {
  556. rrd_rdlock();
  557. RRDHOST *host;
  558. rrdhost_foreach_read(host) {
  559. if (unlikely(!host->health_enabled))
  560. continue;
  561. if (host->labels) {
  562. rrdhost_wrlock(host);
  563. rrdcalc_labels_unlink_alarm_from_host(host);
  564. rrdhost_unlock(host);
  565. }
  566. }
  567. rrd_unlock();
  568. }
  569. // ----------------------------------------------------------------------------
  570. // Alarm
  571. /**
  572. * Alarm is repeating
  573. *
  574. * Is this alarm repeating ?
  575. *
  576. * @param host The structure that has the binary tree
  577. * @param alarm_id the id of the alarm to search
  578. *
  579. * @return It returns 1 case it is repeating and 0 otherwise
  580. */
  581. int alarm_isrepeating(RRDHOST *host, uint32_t alarm_id) {
  582. RRDCALC findme;
  583. findme.id = alarm_id;
  584. RRDCALC *rc = (RRDCALC *)avl_search_lock(&host->alarms_idx_health_log, (avl *)&findme);
  585. if (!rc) {
  586. return 0;
  587. }
  588. return rrdcalc_isrepeating(rc);
  589. }
  590. /**
  591. * Entry is repeating
  592. *
  593. * Check whether the id of alarm entry is yet present in the host structure
  594. *
  595. * @param host The structure that has the binary tree
  596. * @param ae the alarm entry
  597. *
  598. * @return It returns 1 case it is repeating and 0 otherwise
  599. */
  600. int alarm_entry_isrepeating(RRDHOST *host, ALARM_ENTRY *ae) {
  601. return alarm_isrepeating(host, ae->alarm_id);
  602. }
  603. /**
  604. * Max last repeat
  605. *
  606. * Check the maximum last_repeat for the alarms associated a host
  607. *
  608. * @param host The structure that has the binary tree
  609. *
  610. * @return It returns 1 case it is repeating and 0 otherwise
  611. */
  612. RRDCALC *alarm_max_last_repeat(RRDHOST *host, char *alarm_name,uint32_t hash) {
  613. RRDCALC findme;
  614. findme.name = alarm_name;
  615. findme.hash = hash;
  616. RRDCALC *rc = (RRDCALC *)avl_search_lock(&host->alarms_idx_name, (avl *)&findme);
  617. return rc;
  618. }