rrdcalc.c 24 KB

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