rrdcalc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. if (unlikely(!host->health_log.next_alarm_id))
  200. host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
  201. return host->health_log.next_alarm_id++;
  202. }
  203. /**
  204. * Alarm name with dimension
  205. *
  206. * Change the name of the current alarm appending a new diagram.
  207. *
  208. * @param name the alarm name
  209. * @param namelen is the length of the previous vector.
  210. * @param dim the dimension of the chart.
  211. * @param dimlen is the length of the previous vector.
  212. *
  213. * @return It returns the new name on success and the old otherwise
  214. */
  215. char *alarm_name_with_dim(char *name, size_t namelen, const char *dim, size_t dimlen) {
  216. char *newname,*move;
  217. newname = malloc(namelen + dimlen + 2);
  218. if(newname) {
  219. move = newname;
  220. memcpy(move, name, namelen);
  221. move += namelen;
  222. *move++ = '_';
  223. memcpy(move, dim, dimlen);
  224. move += dimlen;
  225. *move = '\0';
  226. } else {
  227. newname = name;
  228. }
  229. return newname;
  230. }
  231. /**
  232. * Remove pipe comma
  233. *
  234. * Remove the pipes and commas converting to space.
  235. *
  236. * @param str the string to change.
  237. */
  238. void dimension_remove_pipe_comma(char *str) {
  239. while(*str) {
  240. if(*str == '|' || *str == ',') *str = ' ';
  241. str++;
  242. }
  243. }
  244. inline void rrdcalc_add_to_host(RRDHOST *host, RRDCALC *rc) {
  245. rrdhost_check_rdlock(host);
  246. if(rc->calculation) {
  247. rc->calculation->status = &rc->status;
  248. rc->calculation->this = &rc->value;
  249. rc->calculation->after = &rc->db_after;
  250. rc->calculation->before = &rc->db_before;
  251. rc->calculation->rrdcalc = rc;
  252. }
  253. if(rc->warning) {
  254. rc->warning->status = &rc->status;
  255. rc->warning->this = &rc->value;
  256. rc->warning->after = &rc->db_after;
  257. rc->warning->before = &rc->db_before;
  258. rc->warning->rrdcalc = rc;
  259. }
  260. if(rc->critical) {
  261. rc->critical->status = &rc->status;
  262. rc->critical->this = &rc->value;
  263. rc->critical->after = &rc->db_after;
  264. rc->critical->before = &rc->db_before;
  265. rc->critical->rrdcalc = rc;
  266. }
  267. if(!rc->foreachdim) {
  268. // link it to the host alarms list
  269. if(likely(host->alarms)) {
  270. // append it
  271. RRDCALC *t;
  272. for(t = host->alarms; t && t->next ; t = t->next) ;
  273. t->next = rc;
  274. }
  275. else {
  276. host->alarms = rc;
  277. }
  278. // link it to its chart
  279. RRDSET *st;
  280. rrdset_foreach_read(st, host) {
  281. if(rrdcalc_is_matching_this_rrdset(rc, st)) {
  282. rrdsetcalc_link(st, rc);
  283. break;
  284. }
  285. }
  286. } else {
  287. //link it case there is a foreach
  288. if(likely(host->alarms_with_foreach)) {
  289. // append it
  290. RRDCALC *t;
  291. for(t = host->alarms_with_foreach; t && t->next ; t = t->next) ;
  292. t->next = rc;
  293. }
  294. else {
  295. host->alarms_with_foreach = rc;
  296. }
  297. //I am not linking this alarm direct to the host here, this will be done when the children is created
  298. }
  299. }
  300. inline RRDCALC *rrdcalc_create_from_template(RRDHOST *host, RRDCALCTEMPLATE *rt, const char *chart) {
  301. debug(D_HEALTH, "Health creating dynamic alarm (from template) '%s.%s'", chart, rt->name);
  302. if(rrdcalc_exists(host, chart, rt->name, 0, 0))
  303. return NULL;
  304. RRDCALC *rc = callocz(1, sizeof(RRDCALC));
  305. rc->next_event_id = 1;
  306. rc->name = strdupz(rt->name);
  307. rc->hash = simple_hash(rc->name);
  308. rc->chart = strdupz(chart);
  309. rc->hash_chart = simple_hash(rc->chart);
  310. rc->id = rrdcalc_get_unique_id(host, rc->chart, rc->name, &rc->next_event_id);
  311. if(rt->dimensions) rc->dimensions = strdupz(rt->dimensions);
  312. if(rt->foreachdim) {
  313. rc->foreachdim = strdupz(rt->foreachdim);
  314. rc->spdim = health_pattern_from_foreach(rc->foreachdim);
  315. }
  316. rc->foreachcounter = rt->foreachcounter;
  317. rc->green = rt->green;
  318. rc->red = rt->red;
  319. rc->value = NAN;
  320. rc->old_value = NAN;
  321. rc->delay_up_duration = rt->delay_up_duration;
  322. rc->delay_down_duration = rt->delay_down_duration;
  323. rc->delay_max_duration = rt->delay_max_duration;
  324. rc->delay_multiplier = rt->delay_multiplier;
  325. rc->last_repeat = 0;
  326. rc->warn_repeat_every = rt->warn_repeat_every;
  327. rc->crit_repeat_every = rt->crit_repeat_every;
  328. rc->group = rt->group;
  329. rc->after = rt->after;
  330. rc->before = rt->before;
  331. rc->update_every = rt->update_every;
  332. rc->options = rt->options;
  333. if(rt->exec) rc->exec = strdupz(rt->exec);
  334. if(rt->recipient) rc->recipient = strdupz(rt->recipient);
  335. if(rt->source) rc->source = strdupz(rt->source);
  336. if(rt->units) rc->units = strdupz(rt->units);
  337. if(rt->info) rc->info = strdupz(rt->info);
  338. if(rt->calculation) {
  339. rc->calculation = expression_parse(rt->calculation->source, NULL, NULL);
  340. if(!rc->calculation)
  341. error("Health alarm '%s.%s': failed to parse calculation expression '%s'", chart, rt->name, rt->calculation->source);
  342. }
  343. if(rt->warning) {
  344. rc->warning = expression_parse(rt->warning->source, NULL, NULL);
  345. if(!rc->warning)
  346. error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", chart, rt->name, rt->warning->source);
  347. }
  348. if(rt->critical) {
  349. rc->critical = expression_parse(rt->critical->source, NULL, NULL);
  350. if(!rc->critical)
  351. error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", chart, rt->name, rt->critical->source);
  352. }
  353. 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",
  354. (rc->chart)?rc->chart:"NOCHART",
  355. rc->name,
  356. (rc->exec)?rc->exec:"DEFAULT",
  357. (rc->recipient)?rc->recipient:"DEFAULT",
  358. rc->green,
  359. rc->red,
  360. (int)rc->group,
  361. rc->after,
  362. rc->before,
  363. rc->options,
  364. (rc->dimensions)?rc->dimensions:"NONE",
  365. (rc->foreachdim)?rc->foreachdim:"NONE",
  366. rc->update_every,
  367. (rc->calculation)?rc->calculation->parsed_as:"NONE",
  368. (rc->warning)?rc->warning->parsed_as:"NONE",
  369. (rc->critical)?rc->critical->parsed_as:"NONE",
  370. rc->source,
  371. rc->delay_up_duration,
  372. rc->delay_down_duration,
  373. rc->delay_max_duration,
  374. rc->delay_multiplier,
  375. rc->warn_repeat_every,
  376. rc->crit_repeat_every
  377. );
  378. rrdcalc_add_to_host(host, rc);
  379. if(!rt->foreachdim) {
  380. RRDCALC *rdcmp = (RRDCALC *) avl_insert_lock(&(host)->alarms_idx_health_log,(avl *)rc);
  381. if (rdcmp != rc) {
  382. error("Cannot insert the alarm index ID %s",rc->name);
  383. }
  384. }
  385. return rc;
  386. }
  387. /**
  388. * Create from RRDCALC
  389. *
  390. * Create a new alarm using another alarm as template.
  391. *
  392. * @param rc is the alarm that will be used as source
  393. * @param host is the host structure.
  394. * @param name is the newest chart name.
  395. * @param dimension is the current dimension
  396. * @param foreachdim the whole list of dimension
  397. *
  398. * @return it returns the new alarm changed.
  399. */
  400. inline RRDCALC *rrdcalc_create_from_rrdcalc(RRDCALC *rc, RRDHOST *host, const char *name, const char *dimension) {
  401. RRDCALC *newrc = callocz(1, sizeof(RRDCALC));
  402. newrc->next_event_id = 1;
  403. newrc->id = rrdcalc_get_unique_id(host, rc->chart, name, &rc->next_event_id);
  404. newrc->name = (char *)name;
  405. newrc->hash = simple_hash(newrc->name);
  406. newrc->chart = strdupz(rc->chart);
  407. newrc->hash_chart = simple_hash(rc->chart);
  408. newrc->dimensions = strdupz(dimension);
  409. newrc->foreachdim = NULL;
  410. rc->foreachcounter++;
  411. newrc->foreachcounter = rc->foreachcounter;
  412. newrc->green = rc->green;
  413. newrc->red = rc->red;
  414. newrc->value = NAN;
  415. newrc->old_value = NAN;
  416. newrc->delay_up_duration = rc->delay_up_duration;
  417. newrc->delay_down_duration = rc->delay_down_duration;
  418. newrc->delay_max_duration = rc->delay_max_duration;
  419. newrc->delay_multiplier = rc->delay_multiplier;
  420. newrc->last_repeat = 0;
  421. newrc->warn_repeat_every = rc->warn_repeat_every;
  422. newrc->crit_repeat_every = rc->crit_repeat_every;
  423. newrc->group = rc->group;
  424. newrc->after = rc->after;
  425. newrc->before = rc->before;
  426. newrc->update_every = rc->update_every;
  427. newrc->options = rc->options;
  428. if(rc->exec) newrc->exec = strdupz(rc->exec);
  429. if(rc->recipient) newrc->recipient = strdupz(rc->recipient);
  430. if(rc->source) newrc->source = strdupz(rc->source);
  431. if(rc->units) newrc->units = strdupz(rc->units);
  432. if(rc->info) newrc->info = strdupz(rc->info);
  433. if(rc->calculation) {
  434. newrc->calculation = expression_parse(rc->calculation->source, NULL, NULL);
  435. if(!newrc->calculation)
  436. error("Health alarm '%s.%s': failed to parse calculation expression '%s'", rc->chart, rc->name, rc->calculation->source);
  437. }
  438. if(rc->warning) {
  439. newrc->warning = expression_parse(rc->warning->source, NULL, NULL);
  440. if(!newrc->warning)
  441. error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", rc->chart, rc->name, rc->warning->source);
  442. }
  443. if(rc->critical) {
  444. newrc->critical = expression_parse(rc->critical->source, NULL, NULL);
  445. if(!newrc->critical)
  446. error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", rc->chart, rc->name, rc->critical->source);
  447. }
  448. return newrc;
  449. }
  450. void rrdcalc_free(RRDCALC *rc) {
  451. if(unlikely(!rc)) return;
  452. expression_free(rc->calculation);
  453. expression_free(rc->warning);
  454. expression_free(rc->critical);
  455. freez(rc->name);
  456. freez(rc->chart);
  457. freez(rc->family);
  458. freez(rc->dimensions);
  459. freez(rc->foreachdim);
  460. freez(rc->exec);
  461. freez(rc->recipient);
  462. freez(rc->source);
  463. freez(rc->units);
  464. freez(rc->info);
  465. simple_pattern_free(rc->spdim);
  466. freez(rc->labels);
  467. simple_pattern_free(rc->splabels);
  468. freez(rc);
  469. }
  470. void rrdcalc_unlink_and_free(RRDHOST *host, RRDCALC *rc) {
  471. if(unlikely(!rc)) return;
  472. debug(D_HEALTH, "Health removing alarm '%s.%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
  473. // unlink it from RRDSET
  474. if(rc->rrdset) rrdsetcalc_unlink(rc);
  475. // unlink it from RRDHOST
  476. if(unlikely(rc == host->alarms))
  477. host->alarms = rc->next;
  478. else {
  479. RRDCALC *t;
  480. for(t = host->alarms; t && t->next != rc; t = t->next) ;
  481. if(t) {
  482. t->next = rc->next;
  483. rc->next = NULL;
  484. }
  485. else
  486. error("Cannot unlink alarm '%s.%s' from host '%s': not found", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
  487. }
  488. RRDCALC *rdcmp = (RRDCALC *) avl_search_lock(&(host)->alarms_idx_health_log, (avl *)rc);
  489. if (rdcmp) {
  490. rdcmp = (RRDCALC *) avl_remove_lock(&(host)->alarms_idx_health_log, (avl *)rc);
  491. if (!rdcmp) {
  492. error("Cannot remove the health alarm index from health_log");
  493. }
  494. }
  495. rdcmp = (RRDCALC *) avl_search_lock(&(host)->alarms_idx_name, (avl *)rc);
  496. if (rdcmp) {
  497. rdcmp = (RRDCALC *) avl_remove_lock(&(host)->alarms_idx_name, (avl *)rc);
  498. if (!rdcmp) {
  499. error("Cannot remove the health alarm index from idx_name");
  500. }
  501. }
  502. rrdcalc_free(rc);
  503. }
  504. void rrdcalc_foreach_unlink_and_free(RRDHOST *host, RRDCALC *rc) {
  505. if(unlikely(rc == host->alarms_with_foreach))
  506. host->alarms_with_foreach = rc->next;
  507. else {
  508. RRDCALC *t;
  509. for(t = host->alarms_with_foreach; t && t->next != rc; t = t->next) ;
  510. if(t) {
  511. t->next = rc->next;
  512. rc->next = NULL;
  513. }
  514. else
  515. error("Cannot unlink alarm '%s.%s' from host '%s': not found", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
  516. }
  517. rrdcalc_free(rc);
  518. }
  519. static void rrdcalc_labels_unlink_alarm_loop(RRDHOST *host, RRDCALC *alarms) {
  520. RRDCALC *rc = alarms;
  521. while (rc) {
  522. if (!rc->labels) {
  523. rc = rc->next;
  524. continue;
  525. }
  526. char cmp[CONFIG_FILE_LINE_MAX+1];
  527. struct label *move = host->labels;
  528. while(move) {
  529. snprintf(cmp, CONFIG_FILE_LINE_MAX, "%s=%s", move->key, move->value);
  530. if (simple_pattern_matches(rc->splabels, move->key) ||
  531. simple_pattern_matches(rc->splabels, cmp)) {
  532. break;
  533. }
  534. move = move->next;
  535. }
  536. RRDCALC *next = rc->next;
  537. if(!move) {
  538. info("Health configuration for alarm '%s' cannot be applied, because the host %s does not have the label(s) '%s'",
  539. rc->name,
  540. host->hostname,
  541. rc->labels);
  542. if(host->alarms == alarms) {
  543. rrdcalc_unlink_and_free(host, rc);
  544. } else
  545. rrdcalc_foreach_unlink_and_free(host, rc);
  546. }
  547. rc = next;
  548. }
  549. }
  550. void rrdcalc_labels_unlink_alarm_from_host(RRDHOST *host) {
  551. rrdhost_check_rdlock(host);
  552. netdata_rwlock_rdlock(&host->labels_rwlock);
  553. rrdcalc_labels_unlink_alarm_loop(host, host->alarms);
  554. rrdcalc_labels_unlink_alarm_loop(host, host->alarms_with_foreach);
  555. netdata_rwlock_unlock(&host->labels_rwlock);
  556. }
  557. void rrdcalc_labels_unlink() {
  558. rrd_rdlock();
  559. RRDHOST *host;
  560. rrdhost_foreach_read(host) {
  561. if (unlikely(!host->health_enabled))
  562. continue;
  563. if (host->labels) {
  564. rrdhost_wrlock(host);
  565. rrdcalc_labels_unlink_alarm_from_host(host);
  566. rrdhost_unlock(host);
  567. }
  568. }
  569. rrd_unlock();
  570. }
  571. // ----------------------------------------------------------------------------
  572. // Alarm
  573. /**
  574. * Alarm is repeating
  575. *
  576. * Is this alarm repeating ?
  577. *
  578. * @param host The structure that has the binary tree
  579. * @param alarm_id the id of the alarm to search
  580. *
  581. * @return It returns 1 case it is repeating and 0 otherwise
  582. */
  583. int alarm_isrepeating(RRDHOST *host, uint32_t alarm_id) {
  584. RRDCALC findme;
  585. findme.id = alarm_id;
  586. RRDCALC *rc = (RRDCALC *)avl_search_lock(&host->alarms_idx_health_log, (avl *)&findme);
  587. if (!rc) {
  588. return 0;
  589. }
  590. return rrdcalc_isrepeating(rc);
  591. }
  592. /**
  593. * Entry is repeating
  594. *
  595. * Check whether the id of alarm entry is yet present in the host structure
  596. *
  597. * @param host The structure that has the binary tree
  598. * @param ae the alarm entry
  599. *
  600. * @return It returns 1 case it is repeating and 0 otherwise
  601. */
  602. int alarm_entry_isrepeating(RRDHOST *host, ALARM_ENTRY *ae) {
  603. return alarm_isrepeating(host, ae->alarm_id);
  604. }
  605. /**
  606. * Max last repeat
  607. *
  608. * Check the maximum last_repeat for the alarms associated a host
  609. *
  610. * @param host The structure that has the binary tree
  611. *
  612. * @return It returns 1 case it is repeating and 0 otherwise
  613. */
  614. RRDCALC *alarm_max_last_repeat(RRDHOST *host, char *alarm_name,uint32_t hash) {
  615. RRDCALC findme;
  616. findme.name = alarm_name;
  617. findme.hash = hash;
  618. RRDCALC *rc = (RRDCALC *)avl_search_lock(&host->alarms_idx_name, (avl *)&findme);
  619. return rc;
  620. }