rrdcalc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "rrd.h"
  3. // ----------------------------------------------------------------------------
  4. // RRDCALC helpers
  5. inline const char *rrdcalc_status2string(RRDCALC_STATUS status) {
  6. switch(status) {
  7. case RRDCALC_STATUS_REMOVED:
  8. return "REMOVED";
  9. case RRDCALC_STATUS_UNDEFINED:
  10. return "UNDEFINED";
  11. case RRDCALC_STATUS_UNINITIALIZED:
  12. return "UNINITIALIZED";
  13. case RRDCALC_STATUS_CLEAR:
  14. return "CLEAR";
  15. case RRDCALC_STATUS_RAISED:
  16. return "RAISED";
  17. case RRDCALC_STATUS_WARNING:
  18. return "WARNING";
  19. case RRDCALC_STATUS_CRITICAL:
  20. return "CRITICAL";
  21. default:
  22. error("Unknown alarm status %d", status);
  23. return "UNKNOWN";
  24. }
  25. }
  26. uint32_t rrdcalc_get_unique_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *next_event_id) {
  27. netdata_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
  28. // re-use old IDs, by looking them up in the alarm log
  29. ALARM_ENTRY *ae = NULL;
  30. for(ae = host->health_log.alarms; ae ;ae = ae->next) {
  31. if(unlikely(name == ae->name && chart == ae->chart)) {
  32. if(next_event_id) *next_event_id = ae->alarm_event_id + 1;
  33. break;
  34. }
  35. }
  36. uint32_t alarm_id;
  37. if(ae)
  38. alarm_id = ae->alarm_id;
  39. else {
  40. if (unlikely(!host->health_log.next_alarm_id))
  41. host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
  42. alarm_id = host->health_log.next_alarm_id++;
  43. }
  44. netdata_rwlock_unlock(&host->health_log.alarm_log_rwlock);
  45. return alarm_id;
  46. }
  47. // ----------------------------------------------------------------------------
  48. // RRDCALC replacing info text variables with RRDSET labels
  49. static STRING *rrdcalc_replace_variables_with_rrdset_labels(const char *line, RRDCALC *rc) {
  50. if (!line || !*line)
  51. return NULL;
  52. size_t pos = 0;
  53. char *temp = strdupz(line);
  54. char var[RRDCALC_VAR_MAX];
  55. char *m, *lbl_value = NULL;
  56. while ((m = strchr(temp + pos, '$')) && *(m+1) == '{') {
  57. int i = 0;
  58. char *e = m;
  59. while (*e) {
  60. var[i++] = *e;
  61. if (*e == '}' || i == RRDCALC_VAR_MAX - 1)
  62. break;
  63. e++;
  64. }
  65. var[i] = '\0';
  66. pos = m - temp + 1;
  67. if (!strcmp(var, RRDCALC_VAR_FAMILY)) {
  68. char *buf = find_and_replace(temp, var, (rc->rrdset && rc->rrdset->family) ? rrdset_family(rc->rrdset) : "", m);
  69. freez(temp);
  70. temp = buf;
  71. }
  72. else if (!strncmp(var, RRDCALC_VAR_LABEL, RRDCALC_VAR_LABEL_LEN)) {
  73. char label_val[RRDCALC_VAR_MAX + RRDCALC_VAR_LABEL_LEN + 1] = { 0 };
  74. strcpy(label_val, var+RRDCALC_VAR_LABEL_LEN);
  75. label_val[i - RRDCALC_VAR_LABEL_LEN - 1] = '\0';
  76. if(likely(rc->rrdset && rc->rrdset->rrdlabels)) {
  77. rrdlabels_get_value_strdup_or_null(rc->rrdset->rrdlabels, &lbl_value, label_val);
  78. if (lbl_value) {
  79. char *buf = find_and_replace(temp, var, lbl_value, m);
  80. freez(temp);
  81. temp = buf;
  82. freez(lbl_value);
  83. }
  84. }
  85. }
  86. }
  87. STRING *ret = string_strdupz(temp);
  88. freez(temp);
  89. return ret;
  90. }
  91. void rrdcalc_update_info_using_rrdset_labels(RRDCALC *rc) {
  92. if(!rc->rrdset || !rc->original_info || !rc->rrdset->rrdlabels) return;
  93. size_t labels_version = dictionary_version(rc->rrdset->rrdlabels);
  94. if(rc->labels_version != labels_version) {
  95. STRING *old = rc->info;
  96. rc->info = rrdcalc_replace_variables_with_rrdset_labels(rrdcalc_original_info(rc), rc);
  97. string_freez(old);
  98. rc->labels_version = labels_version;
  99. }
  100. }
  101. // ----------------------------------------------------------------------------
  102. // RRDCALC index management for RRDSET
  103. // the dictionary requires a unique key for every item
  104. // we use {chart id}.{alert name} for both the RRDHOST and RRDSET alert indexes.
  105. #define RRDCALC_MAX_KEY_SIZE 1024
  106. static size_t rrdcalc_key(char *dst, size_t dst_len, const char *chart, const char *alert) {
  107. return snprintfz(dst, dst_len, "%s/%s", chart, alert);
  108. }
  109. const RRDCALC_ACQUIRED *rrdcalc_from_rrdset_get(RRDSET *st, const char *alert_name) {
  110. char key[RRDCALC_MAX_KEY_SIZE + 1];
  111. size_t key_len = rrdcalc_key(key, RRDCALC_MAX_KEY_SIZE, rrdset_id(st), alert_name);
  112. const RRDCALC_ACQUIRED *rca = (const RRDCALC_ACQUIRED *)dictionary_get_and_acquire_item_advanced(st->rrdhost->rrdcalc_root_index, key, (ssize_t)(key_len + 1));
  113. if(!rca) {
  114. key_len = rrdcalc_key(key, RRDCALC_MAX_KEY_SIZE, rrdset_name(st), alert_name);
  115. rca = (const RRDCALC_ACQUIRED *)dictionary_get_and_acquire_item_advanced(st->rrdhost->rrdcalc_root_index, key, (ssize_t)(key_len + 1));
  116. }
  117. return rca;
  118. }
  119. void rrdcalc_from_rrdset_release(RRDSET *st, const RRDCALC_ACQUIRED *rca) {
  120. if(!rca) return;
  121. dictionary_acquired_item_release(st->rrdhost->rrdcalc_root_index, (const DICTIONARY_ITEM *)rca);
  122. }
  123. RRDCALC *rrdcalc_acquired_to_rrdcalc(const RRDCALC_ACQUIRED *rca) {
  124. if(rca)
  125. return dictionary_acquired_item_value((const DICTIONARY_ITEM *)rca);
  126. return NULL;
  127. }
  128. // ----------------------------------------------------------------------------
  129. // RRDCALC managing the linking with RRDSET
  130. static void rrdcalc_link_to_rrdset(RRDSET *st, RRDCALC *rc) {
  131. RRDHOST *host = st->rrdhost;
  132. debug(D_HEALTH, "Health linking alarm '%s.%s' to chart '%s' of host '%s'", rrdcalc_chart_name(rc), rrdcalc_name(rc), rrdset_id(st), rrdhost_hostname(host));
  133. rc->last_status_change = now_realtime_sec();
  134. rc->rrdset = st;
  135. netdata_rwlock_wrlock(&st->alerts.rwlock);
  136. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(st->alerts.base, rc, prev, next);
  137. netdata_rwlock_unlock(&st->alerts.rwlock);
  138. if(rc->update_every < rc->rrdset->update_every) {
  139. error("Health alarm '%s.%s' has update every %d, less than chart update every %d. Setting alarm update frequency to %d.", rrdset_id(rc->rrdset), rrdcalc_name(rc), rc->update_every, rc->rrdset->update_every, rc->rrdset->update_every);
  140. rc->update_every = rc->rrdset->update_every;
  141. }
  142. if(!isnan(rc->green) && isnan(st->green)) {
  143. debug(D_HEALTH, "Health alarm '%s.%s' green threshold set from " NETDATA_DOUBLE_FORMAT_AUTO
  144. " to " NETDATA_DOUBLE_FORMAT_AUTO ".", rrdset_id(rc->rrdset), rrdcalc_name(rc), rc->rrdset->green, rc->green);
  145. st->green = rc->green;
  146. }
  147. if(!isnan(rc->red) && isnan(st->red)) {
  148. debug(D_HEALTH, "Health alarm '%s.%s' red threshold set from " NETDATA_DOUBLE_FORMAT_AUTO " to " NETDATA_DOUBLE_FORMAT_AUTO
  149. ".", rrdset_id(rc->rrdset), rrdcalc_name(rc), rc->rrdset->red, rc->red);
  150. st->red = rc->red;
  151. }
  152. char buf[RRDVAR_MAX_LENGTH + 1];
  153. snprintfz(buf, RRDVAR_MAX_LENGTH, "%s.%s", rrdset_name(st), rrdcalc_name(rc));
  154. STRING *rrdset_name_rrdcalc_name = string_strdupz(buf);
  155. snprintfz(buf, RRDVAR_MAX_LENGTH, "%s.%s", rrdset_id(st), rrdcalc_name(rc));
  156. STRING *rrdset_id_rrdcalc_name = string_strdupz(buf);
  157. rc->rrdvar_local = rrdvar_add_and_acquire(
  158. "local",
  159. st->rrdvars,
  160. rc->name,
  161. RRDVAR_TYPE_CALCULATED,
  162. RRDVAR_FLAG_RRDCALC_LOCAL_VAR,
  163. &rc->value);
  164. rc->rrdvar_family = rrdvar_add_and_acquire(
  165. "family",
  166. rrdfamily_rrdvars_dict(st->rrdfamily),
  167. rc->name,
  168. RRDVAR_TYPE_CALCULATED,
  169. RRDVAR_FLAG_RRDCALC_FAMILY_VAR,
  170. &rc->value);
  171. rc->rrdvar_host_chart_name = rrdvar_add_and_acquire(
  172. "host",
  173. host->rrdvars,
  174. rrdset_name_rrdcalc_name,
  175. RRDVAR_TYPE_CALCULATED,
  176. RRDVAR_FLAG_RRDCALC_HOST_CHARTNAME_VAR,
  177. &rc->value);
  178. rc->rrdvar_host_chart_id = rrdvar_add_and_acquire(
  179. "host",
  180. host->rrdvars,
  181. rrdset_id_rrdcalc_name,
  182. RRDVAR_TYPE_CALCULATED,
  183. RRDVAR_FLAG_RRDCALC_HOST_CHARTID_VAR | ((rc->rrdvar_host_chart_name) ? 0 : RRDVAR_FLAG_RRDCALC_HOST_CHARTNAME_VAR),
  184. &rc->value);
  185. string_freez(rrdset_id_rrdcalc_name);
  186. string_freez(rrdset_name_rrdcalc_name);
  187. if(!rc->units)
  188. rc->units = string_dup(st->units);
  189. rrdvar_store_for_chart(host, st);
  190. rrdcalc_update_info_using_rrdset_labels(rc);
  191. time_t now = now_realtime_sec();
  192. ALARM_ENTRY *ae = health_create_alarm_entry(
  193. host,
  194. rc->id,
  195. rc->next_event_id++,
  196. rc->config_hash_id,
  197. now,
  198. rc->name,
  199. rc->rrdset->id,
  200. rc->rrdset->context,
  201. rc->rrdset->family,
  202. rc->classification,
  203. rc->component,
  204. rc->type,
  205. rc->exec,
  206. rc->recipient,
  207. now - rc->last_status_change,
  208. rc->old_value,
  209. rc->value,
  210. rc->status,
  211. RRDCALC_STATUS_UNINITIALIZED,
  212. rc->source,
  213. rc->units,
  214. rc->info,
  215. 0,
  216. rrdcalc_isrepeating(rc)?HEALTH_ENTRY_FLAG_IS_REPEATING:0);
  217. health_alarm_log_add_entry(host, ae);
  218. }
  219. static void rrdcalc_unlink_from_rrdset(RRDCALC *rc, bool having_ll_wrlock) {
  220. RRDSET *st = rc->rrdset;
  221. if(!st) {
  222. debug(D_HEALTH, "Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rrdcalc_chart_name(rc), rrdcalc_name(rc));
  223. error("Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rrdcalc_chart_name(rc), rrdcalc_name(rc));
  224. return;
  225. }
  226. RRDHOST *host = st->rrdhost;
  227. time_t now = now_realtime_sec();
  228. if (likely(rc->status != RRDCALC_STATUS_REMOVED)) {
  229. ALARM_ENTRY *ae = health_create_alarm_entry(
  230. host,
  231. rc->id,
  232. rc->next_event_id++,
  233. rc->config_hash_id,
  234. now,
  235. rc->name,
  236. rc->rrdset->id,
  237. rc->rrdset->context,
  238. rc->rrdset->family,
  239. rc->classification,
  240. rc->component,
  241. rc->type,
  242. rc->exec,
  243. rc->recipient,
  244. now - rc->last_status_change,
  245. rc->old_value,
  246. rc->value,
  247. rc->status,
  248. RRDCALC_STATUS_REMOVED,
  249. rc->source,
  250. rc->units,
  251. rc->info,
  252. 0,
  253. 0);
  254. health_alarm_log_add_entry(host, ae);
  255. }
  256. debug(D_HEALTH, "Health unlinking alarm '%s.%s' from chart '%s' of host '%s'", rrdcalc_chart_name(rc), rrdcalc_name(rc), rrdset_id(st), rrdhost_hostname(host));
  257. // unlink it
  258. if(!having_ll_wrlock)
  259. netdata_rwlock_wrlock(&st->alerts.rwlock);
  260. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(st->alerts.base, rc, prev, next);
  261. if(!having_ll_wrlock)
  262. netdata_rwlock_unlock(&st->alerts.rwlock);
  263. rc->rrdset = NULL;
  264. rrdvar_release_and_del(st->rrdvars, rc->rrdvar_local);
  265. rc->rrdvar_local = NULL;
  266. rrdvar_release_and_del(rrdfamily_rrdvars_dict(st->rrdfamily), rc->rrdvar_family);
  267. rc->rrdvar_family = NULL;
  268. rrdvar_release_and_del(host->rrdvars, rc->rrdvar_host_chart_id);
  269. rc->rrdvar_host_chart_id = NULL;
  270. rrdvar_release_and_del(host->rrdvars, rc->rrdvar_host_chart_name);
  271. rc->rrdvar_host_chart_name = NULL;
  272. // RRDCALC will remain in RRDHOST
  273. // so that if the matching chart is found in the future
  274. // it will be applied automatically
  275. }
  276. static inline bool rrdcalc_check_if_it_matches_rrdset(RRDCALC *rc, RRDSET *st) {
  277. if ( (rc->chart != st->id)
  278. && (rc->chart != st->name))
  279. return false;
  280. if (rc->module_pattern && !simple_pattern_matches_string(rc->module_pattern, st->module_name))
  281. return false;
  282. if (rc->plugin_pattern && !simple_pattern_matches_string(rc->plugin_pattern, st->module_name))
  283. return false;
  284. if (st->rrdhost->rrdlabels && rc->host_labels_pattern && !rrdlabels_match_simple_pattern_parsed(
  285. st->rrdhost->rrdlabels, rc->host_labels_pattern, '=', NULL))
  286. return false;
  287. if (st->rrdlabels && rc->chart_labels_pattern && !rrdlabels_match_simple_pattern_parsed(
  288. st->rrdlabels, rc->chart_labels_pattern, '=', NULL))
  289. return false;
  290. return true;
  291. }
  292. void rrdcalc_link_matching_alerts_to_rrdset(RRDSET *st) {
  293. RRDHOST *host = st->rrdhost;
  294. // debug(D_HEALTH, "find matching alarms for chart '%s'", st->id);
  295. RRDCALC *rc;
  296. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  297. if(rc->rrdset)
  298. continue;
  299. if(unlikely(rrdcalc_check_if_it_matches_rrdset(rc, st)))
  300. rrdcalc_link_to_rrdset(st, rc);
  301. }
  302. foreach_rrdcalc_in_rrdhost_done(rc);
  303. }
  304. static inline int rrdcalc_check_and_link_rrdset_callback(RRDSET *st, void *rrdcalc) {
  305. RRDCALC *rc = rrdcalc;
  306. if(unlikely(rrdcalc_check_if_it_matches_rrdset(rc, st))) {
  307. rrdcalc_link_to_rrdset(st, rc);
  308. return -1;
  309. }
  310. return 0;
  311. }
  312. // ----------------------------------------------------------------------------
  313. // RRDCALC rrdhost index management - constructor
  314. struct rrdcalc_constructor {
  315. RRDHOST *rrdhost; // the host we operate upon
  316. RRDCALC *from_config; // points to the original RRDCALC, as loaded from the config
  317. RRDCALCTEMPLATE *from_rrdcalctemplate; // the template this alert is generated from
  318. RRDSET *rrdset; // when this comes from rrdcalctemplate, we have a matching rrdset
  319. const char *overwrite_alert_name; // when we have a dimension foreach, the alert is renamed
  320. const char *overwrite_dimensions; // when we have a dimension foreach, the dimensions filter is renamed
  321. enum {
  322. RRDCALC_REACT_NONE,
  323. RRDCALC_REACT_NEW,
  324. } react_action;
  325. bool existing_from_template;
  326. };
  327. static void rrdcalc_rrdhost_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalc, void *constructor_data) {
  328. RRDCALC *rc = rrdcalc;
  329. struct rrdcalc_constructor *ctr = constructor_data;
  330. RRDHOST *host = ctr->rrdhost;
  331. rc->key = string_strdupz(dictionary_acquired_item_name(item));
  332. if(ctr->from_rrdcalctemplate) {
  333. rc->run_flags |= RRDCALC_FLAG_FROM_TEMPLATE;
  334. RRDCALCTEMPLATE *rt = ctr->from_rrdcalctemplate;
  335. RRDSET *st = ctr->rrdset;
  336. rc->next_event_id = 1;
  337. rc->name = (ctr->overwrite_alert_name) ? string_strdupz(ctr->overwrite_alert_name) : string_dup(rt->name);
  338. rc->chart = string_dup(st->id);
  339. uuid_copy(rc->config_hash_id, rt->config_hash_id);
  340. rc->dimensions = (ctr->overwrite_dimensions) ? string_strdupz(ctr->overwrite_dimensions) : string_dup(rt->dimensions);
  341. rc->foreach_dimension = NULL;
  342. rc->foreach_dimension_pattern = NULL;
  343. rc->green = rt->green;
  344. rc->red = rt->red;
  345. rc->value = NAN;
  346. rc->old_value = NAN;
  347. rc->delay_up_duration = rt->delay_up_duration;
  348. rc->delay_down_duration = rt->delay_down_duration;
  349. rc->delay_max_duration = rt->delay_max_duration;
  350. rc->delay_multiplier = rt->delay_multiplier;
  351. rc->last_repeat = 0;
  352. rc->times_repeat = 0;
  353. rc->warn_repeat_every = rt->warn_repeat_every;
  354. rc->crit_repeat_every = rt->crit_repeat_every;
  355. rc->group = rt->group;
  356. rc->after = rt->after;
  357. rc->before = rt->before;
  358. rc->update_every = rt->update_every;
  359. rc->options = rt->options;
  360. rc->exec = string_dup(rt->exec);
  361. rc->recipient = string_dup(rt->recipient);
  362. rc->source = string_dup(rt->source);
  363. rc->units = string_dup(rt->units);
  364. rc->info = string_dup(rt->info);
  365. rc->original_info = string_dup(rt->info);
  366. rc->classification = string_dup(rt->classification);
  367. rc->component = string_dup(rt->component);
  368. rc->type = string_dup(rt->type);
  369. if(rt->calculation) {
  370. rc->calculation = expression_parse(rt->calculation->source, NULL, NULL);
  371. if(!rc->calculation)
  372. error("Health alarm '%s.%s': failed to parse calculation expression '%s'", rrdset_id(st), rrdcalctemplate_name(rt), rt->calculation->source);
  373. }
  374. if(rt->warning) {
  375. rc->warning = expression_parse(rt->warning->source, NULL, NULL);
  376. if(!rc->warning)
  377. error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", rrdset_id(st), rrdcalctemplate_name(rt), rt->warning->source);
  378. }
  379. if(rt->critical) {
  380. rc->critical = expression_parse(rt->critical->source, NULL, NULL);
  381. if(!rc->critical)
  382. error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", rrdset_id(st), rrdcalctemplate_name(rt), rt->critical->source);
  383. }
  384. }
  385. else if(ctr->from_config) {
  386. // dictionary has already copied all the members values and pointers
  387. // no need for additional work in this case
  388. ;
  389. }
  390. rc->id = rrdcalc_get_unique_id(host, rc->chart, rc->name, &rc->next_event_id);
  391. if(rc->calculation) {
  392. rc->calculation->status = &rc->status;
  393. rc->calculation->myself = &rc->value;
  394. rc->calculation->after = &rc->db_after;
  395. rc->calculation->before = &rc->db_before;
  396. rc->calculation->rrdcalc = rc;
  397. }
  398. if(rc->warning) {
  399. rc->warning->status = &rc->status;
  400. rc->warning->myself = &rc->value;
  401. rc->warning->after = &rc->db_after;
  402. rc->warning->before = &rc->db_before;
  403. rc->warning->rrdcalc = rc;
  404. }
  405. if(rc->critical) {
  406. rc->critical->status = &rc->status;
  407. rc->critical->myself = &rc->value;
  408. rc->critical->after = &rc->db_after;
  409. rc->critical->before = &rc->db_before;
  410. rc->critical->rrdcalc = rc;
  411. }
  412. debug(D_HEALTH, "Health added alarm '%s.%s': exec '%s', recipient '%s', green " NETDATA_DOUBLE_FORMAT_AUTO
  413. ", red " NETDATA_DOUBLE_FORMAT_AUTO
  414. ", 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",
  415. rrdcalc_chart_name(rc),
  416. rrdcalc_name(rc),
  417. (rc->exec)?rrdcalc_exec(rc):"DEFAULT",
  418. (rc->recipient)?rrdcalc_recipient(rc):"DEFAULT",
  419. rc->green,
  420. rc->red,
  421. (int)rc->group,
  422. rc->after,
  423. rc->before,
  424. rc->options,
  425. (rc->dimensions)?rrdcalc_dimensions(rc):"NONE",
  426. (rc->foreach_dimension)?rrdcalc_foreachdim(rc):"NONE",
  427. rc->update_every,
  428. (rc->calculation)?rc->calculation->parsed_as:"NONE",
  429. (rc->warning)?rc->warning->parsed_as:"NONE",
  430. (rc->critical)?rc->critical->parsed_as:"NONE",
  431. rrdcalc_source(rc),
  432. rc->delay_up_duration,
  433. rc->delay_down_duration,
  434. rc->delay_max_duration,
  435. rc->delay_multiplier,
  436. rc->warn_repeat_every,
  437. rc->crit_repeat_every
  438. );
  439. ctr->react_action = RRDCALC_REACT_NEW;
  440. }
  441. static bool rrdcalc_rrdhost_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalc, void *rrdcalc_new __maybe_unused, void *constructor_data ) {
  442. RRDCALC *rc = rrdcalc;
  443. struct rrdcalc_constructor *ctr = constructor_data;
  444. if(rc->run_flags & RRDCALC_FLAG_FROM_TEMPLATE)
  445. ctr->existing_from_template = true;
  446. else
  447. ctr->existing_from_template = false;
  448. ctr->react_action = RRDCALC_REACT_NONE;
  449. return false;
  450. }
  451. static void rrdcalc_rrdhost_react_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalc, void *constructor_data) {
  452. RRDCALC *rc = rrdcalc;
  453. struct rrdcalc_constructor *ctr = constructor_data;
  454. RRDHOST *host = ctr->rrdhost;
  455. if(ctr->react_action == RRDCALC_REACT_NEW) {
  456. if(ctr->rrdset)
  457. rrdcalc_link_to_rrdset(ctr->rrdset, rc);
  458. else if (ctr->from_rrdcalctemplate)
  459. rrdcontext_foreach_instance_with_rrdset_in_context(host, string2str(ctr->from_rrdcalctemplate->context), rrdcalc_check_and_link_rrdset_callback, rc);
  460. }
  461. }
  462. // ----------------------------------------------------------------------------
  463. // RRDCALC rrdhost index management - destructor
  464. static void rrdcalc_free_internals(RRDCALC *rc) {
  465. if(unlikely(!rc)) return;
  466. expression_free(rc->calculation);
  467. expression_free(rc->warning);
  468. expression_free(rc->critical);
  469. string_freez(rc->key);
  470. string_freez(rc->name);
  471. string_freez(rc->chart);
  472. string_freez(rc->dimensions);
  473. string_freez(rc->foreach_dimension);
  474. string_freez(rc->exec);
  475. string_freez(rc->recipient);
  476. string_freez(rc->source);
  477. string_freez(rc->units);
  478. string_freez(rc->info);
  479. string_freez(rc->original_info);
  480. string_freez(rc->classification);
  481. string_freez(rc->component);
  482. string_freez(rc->type);
  483. string_freez(rc->host_labels);
  484. string_freez(rc->module_match);
  485. string_freez(rc->plugin_match);
  486. string_freez(rc->chart_labels);
  487. simple_pattern_free(rc->foreach_dimension_pattern);
  488. simple_pattern_free(rc->host_labels_pattern);
  489. simple_pattern_free(rc->module_pattern);
  490. simple_pattern_free(rc->plugin_pattern);
  491. simple_pattern_free(rc->chart_labels_pattern);
  492. }
  493. static void rrdcalc_rrdhost_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalc, void *rrdhost __maybe_unused) {
  494. RRDCALC *rc = rrdcalc;
  495. //RRDHOST *host = rrdhost;
  496. if(unlikely(rc->rrdset))
  497. rrdcalc_unlink_from_rrdset(rc, false);
  498. // any destruction actions that require other locks
  499. // have to be placed in rrdcalc_del(), because the object is actually locked for deletion
  500. rrdcalc_free_internals(rc);
  501. }
  502. // ----------------------------------------------------------------------------
  503. // RRDCALC rrdhost index management - index API
  504. void rrdcalc_rrdhost_index_init(RRDHOST *host) {
  505. if(!host->rrdcalc_root_index) {
  506. host->rrdcalc_root_index = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  507. &dictionary_stats_category_rrdhealth, sizeof(RRDCALC));
  508. dictionary_register_insert_callback(host->rrdcalc_root_index, rrdcalc_rrdhost_insert_callback, NULL);
  509. dictionary_register_conflict_callback(host->rrdcalc_root_index, rrdcalc_rrdhost_conflict_callback, NULL);
  510. dictionary_register_react_callback(host->rrdcalc_root_index, rrdcalc_rrdhost_react_callback, NULL);
  511. dictionary_register_delete_callback(host->rrdcalc_root_index, rrdcalc_rrdhost_delete_callback, host);
  512. }
  513. }
  514. void rrdcalc_rrdhost_index_destroy(RRDHOST *host) {
  515. dictionary_destroy(host->rrdcalc_root_index);
  516. host->rrdcalc_root_index = NULL;
  517. }
  518. void rrdcalc_add_from_rrdcalctemplate(RRDHOST *host, RRDCALCTEMPLATE *rt, RRDSET *st, const char *overwrite_alert_name, const char *overwrite_dimensions) {
  519. char key[RRDCALC_MAX_KEY_SIZE + 1];
  520. size_t key_len = rrdcalc_key(key, RRDCALC_MAX_KEY_SIZE, rrdset_id(st),
  521. overwrite_alert_name?overwrite_alert_name:string2str(rt->name));
  522. struct rrdcalc_constructor tmp = {
  523. .rrdhost = host,
  524. .from_config = NULL,
  525. .from_rrdcalctemplate = rt,
  526. .rrdset = st,
  527. .overwrite_alert_name = overwrite_alert_name,
  528. .overwrite_dimensions = overwrite_dimensions,
  529. .react_action = RRDCALC_REACT_NONE,
  530. .existing_from_template = false,
  531. };
  532. dictionary_set_advanced(host->rrdcalc_root_index, key, (ssize_t)(key_len + 1), NULL, sizeof(RRDCALC), &tmp);
  533. if(tmp.react_action != RRDCALC_REACT_NEW && tmp.existing_from_template == false)
  534. error("RRDCALC: from template '%s' on chart '%s' with key '%s', failed to be added to host '%s'. It is manually configured.",
  535. string2str(rt->name), rrdset_id(st), key, rrdhost_hostname(host));
  536. }
  537. int rrdcalc_add_from_config(RRDHOST *host, RRDCALC *rc) {
  538. if(!rc->chart) {
  539. error("Health configuration for alarm '%s' does not have a chart", rrdcalc_name(rc));
  540. return 0;
  541. }
  542. if(!rc->update_every) {
  543. error("Health configuration for alarm '%s.%s' has no frequency (parameter 'every'). Ignoring it.", rrdcalc_chart_name(rc), rrdcalc_name(rc));
  544. return 0;
  545. }
  546. if(!RRDCALC_HAS_DB_LOOKUP(rc) && !rc->calculation && !rc->warning && !rc->critical) {
  547. error("Health configuration for alarm '%s.%s' is useless (no db lookup, no calculation, no warning and no critical expressions)", rrdcalc_chart_name(rc), rrdcalc_name(rc));
  548. return 0;
  549. }
  550. char key[RRDCALC_MAX_KEY_SIZE + 1];
  551. size_t key_len = rrdcalc_key(key, RRDCALC_MAX_KEY_SIZE, string2str(rc->chart), string2str(rc->name));
  552. struct rrdcalc_constructor tmp = {
  553. .rrdhost = host,
  554. .from_config = rc,
  555. .from_rrdcalctemplate = NULL,
  556. .rrdset = NULL,
  557. .react_action = RRDCALC_REACT_NONE,
  558. };
  559. int ret = 1;
  560. RRDCALC *t = dictionary_set_advanced(host->rrdcalc_root_index, key, (ssize_t)(key_len + 1), rc, sizeof(RRDCALC), &tmp);
  561. if(tmp.react_action == RRDCALC_REACT_NEW) {
  562. // we copied rc into the dictionary, so we have to free the container here
  563. freez(rc);
  564. rc = t;
  565. // since we loaded this config from configuration, we need to check if we can link it to alarms
  566. RRDSET *st;
  567. rrdset_foreach_read(st, host) {
  568. if (unlikely(rrdcalc_check_and_link_rrdset_callback(st, rc) == -1))
  569. break;
  570. }
  571. rrdset_foreach_done(st);
  572. }
  573. else {
  574. error(
  575. "RRDCALC: from config '%s' on chart '%s' failed to be added to host '%s'. It already exists.",
  576. string2str(rc->name),
  577. string2str(rc->chart),
  578. rrdhost_hostname(host));
  579. ret = 0;
  580. // free all of it, internals and the container
  581. rrdcalc_free_unused_rrdcalc_loaded_from_config(rc);
  582. }
  583. return ret;
  584. }
  585. static void rrdcalc_unlink_and_delete(RRDHOST *host, RRDCALC *rc, bool having_ll_wrlock) {
  586. if(rc->rrdset)
  587. rrdcalc_unlink_from_rrdset(rc, having_ll_wrlock);
  588. dictionary_del_advanced(host->rrdcalc_root_index, string2str(rc->key), (ssize_t)string_strlen(rc->key) + 1);
  589. }
  590. // ----------------------------------------------------------------------------
  591. // RRDCALC cleanup API functions
  592. void rrdcalc_delete_alerts_not_matching_host_labels_from_this_host(RRDHOST *host) {
  593. RRDCALC *rc;
  594. foreach_rrdcalc_in_rrdhost_reentrant(host, rc) {
  595. if (!rc->host_labels)
  596. continue;
  597. if(!rrdlabels_match_simple_pattern_parsed(host->rrdlabels, rc->host_labels_pattern, '=', NULL)) {
  598. log_health("Health configuration for alarm '%s' cannot be applied, because the host %s does not have the label(s) '%s'",
  599. rrdcalc_name(rc),
  600. rrdhost_hostname(host),
  601. rrdcalc_host_labels(rc));
  602. rrdcalc_unlink_and_delete(host, rc, false);
  603. }
  604. }
  605. foreach_rrdcalc_in_rrdhost_done(rc);
  606. }
  607. void rrdcalc_delete_alerts_not_matching_host_labels_from_all_hosts() {
  608. RRDHOST *host;
  609. dfe_start_reentrant(rrdhost_root_index, host) {
  610. if (unlikely(!host->health.health_enabled))
  611. continue;
  612. if (host->rrdlabels)
  613. rrdcalc_delete_alerts_not_matching_host_labels_from_this_host(host);
  614. }
  615. dfe_done(host);
  616. }
  617. void rrdcalc_unlink_all_rrdset_alerts(RRDSET *st) {
  618. RRDCALC *rc, *last = NULL;
  619. netdata_rwlock_wrlock(&st->alerts.rwlock);
  620. while((rc = st->alerts.base)) {
  621. if(last == rc) {
  622. error("RRDCALC: malformed list of alerts linked to chart - cannot cleanup - giving up.");
  623. break;
  624. }
  625. last = rc;
  626. if(rc->run_flags & RRDCALC_FLAG_FROM_TEMPLATE) {
  627. // if the alert comes from a template we can just delete it
  628. rrdcalc_unlink_and_delete(st->rrdhost, rc, true);
  629. }
  630. else {
  631. // this is a configuration for a specific chart
  632. // it should stay in the list
  633. rrdcalc_unlink_from_rrdset(rc, true);
  634. }
  635. }
  636. netdata_rwlock_unlock(&st->alerts.rwlock);
  637. }
  638. void rrdcalc_delete_all(RRDHOST *host) {
  639. dictionary_flush(host->rrdcalc_root_index);
  640. }
  641. void rrdcalc_free_unused_rrdcalc_loaded_from_config(RRDCALC *rc) {
  642. if(rc->rrdset)
  643. rrdcalc_unlink_from_rrdset(rc, false);
  644. rrdcalc_free_internals(rc);
  645. freez(rc);
  646. }