rrdcalc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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 + 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_to_char_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. rrdcalc_update_info_using_rrdset_labels(rc);
  190. time_t now = now_realtime_sec();
  191. ALARM_ENTRY *ae = health_create_alarm_entry(
  192. host,
  193. rc->id,
  194. rc->next_event_id++,
  195. rc->config_hash_id,
  196. now,
  197. rc->name,
  198. rc->rrdset->id,
  199. rc->rrdset->context,
  200. rc->rrdset->family,
  201. rc->classification,
  202. rc->component,
  203. rc->type,
  204. rc->exec,
  205. rc->recipient,
  206. now - rc->last_status_change,
  207. rc->old_value,
  208. rc->value,
  209. rc->status,
  210. RRDCALC_STATUS_UNINITIALIZED,
  211. rc->source,
  212. rc->units,
  213. rc->info,
  214. 0,
  215. rrdcalc_isrepeating(rc)?HEALTH_ENTRY_FLAG_IS_REPEATING:0);
  216. health_alarm_log_add_entry(host, ae);
  217. }
  218. static void rrdcalc_unlink_from_rrdset(RRDCALC *rc, bool having_ll_wrlock) {
  219. RRDSET *st = rc->rrdset;
  220. if(!st) {
  221. debug(D_HEALTH, "Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rrdcalc_chart_name(rc), rrdcalc_name(rc));
  222. error("Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rrdcalc_chart_name(rc), rrdcalc_name(rc));
  223. return;
  224. }
  225. RRDHOST *host = st->rrdhost;
  226. time_t now = now_realtime_sec();
  227. if (likely(rc->status != RRDCALC_STATUS_REMOVED)) {
  228. ALARM_ENTRY *ae = health_create_alarm_entry(
  229. host,
  230. rc->id,
  231. rc->next_event_id++,
  232. rc->config_hash_id,
  233. now,
  234. rc->name,
  235. rc->rrdset->id,
  236. rc->rrdset->context,
  237. rc->rrdset->family,
  238. rc->classification,
  239. rc->component,
  240. rc->type,
  241. rc->exec,
  242. rc->recipient,
  243. now - rc->last_status_change,
  244. rc->old_value,
  245. rc->value,
  246. rc->status,
  247. RRDCALC_STATUS_REMOVED,
  248. rc->source,
  249. rc->units,
  250. rc->info,
  251. 0,
  252. 0);
  253. health_alarm_log_add_entry(host, ae);
  254. }
  255. 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));
  256. // unlink it
  257. if(!having_ll_wrlock)
  258. netdata_rwlock_wrlock(&st->alerts.rwlock);
  259. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(st->alerts.base, rc, prev, next);
  260. if(!having_ll_wrlock)
  261. netdata_rwlock_unlock(&st->alerts.rwlock);
  262. rc->rrdset = NULL;
  263. rrdvar_release_and_del(st->rrdvars, rc->rrdvar_local);
  264. rc->rrdvar_local = NULL;
  265. rrdvar_release_and_del(rrdfamily_rrdvars_dict(st->rrdfamily), rc->rrdvar_family);
  266. rc->rrdvar_family = NULL;
  267. rrdvar_release_and_del(host->rrdvars, rc->rrdvar_host_chart_id);
  268. rc->rrdvar_host_chart_id = NULL;
  269. rrdvar_release_and_del(host->rrdvars, rc->rrdvar_host_chart_name);
  270. rc->rrdvar_host_chart_name = NULL;
  271. // RRDCALC will remain in RRDHOST
  272. // so that if the matching chart is found in the future
  273. // it will be applied automatically
  274. }
  275. static inline bool rrdcalc_check_if_it_matches_rrdset(RRDCALC *rc, RRDSET *st) {
  276. if ( (rc->chart != st->id)
  277. && (rc->chart != st->name))
  278. return false;
  279. if (rc->module_pattern && !simple_pattern_matches(rc->module_pattern, rrdset_module_name(st)))
  280. return false;
  281. if (rc->plugin_pattern && !simple_pattern_matches(rc->plugin_pattern, rrdset_plugin_name(st)))
  282. return false;
  283. if (st->rrdhost->rrdlabels && rc->host_labels_pattern && !rrdlabels_match_simple_pattern_parsed(st->rrdhost->rrdlabels, rc->host_labels_pattern, '='))
  284. return false;
  285. return true;
  286. }
  287. void rrdcalc_link_matching_alerts_to_rrdset(RRDSET *st) {
  288. RRDHOST *host = st->rrdhost;
  289. // debug(D_HEALTH, "find matching alarms for chart '%s'", st->id);
  290. RRDCALC *rc;
  291. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  292. if(rc->rrdset)
  293. continue;
  294. if(unlikely(rrdcalc_check_if_it_matches_rrdset(rc, st)))
  295. rrdcalc_link_to_rrdset(st, rc);
  296. }
  297. foreach_rrdcalc_in_rrdhost_done(rc);
  298. }
  299. static inline int rrdcalc_check_and_link_rrdset_callback(RRDSET *st, void *rrdcalc) {
  300. RRDCALC *rc = rrdcalc;
  301. if(unlikely(rrdcalc_check_if_it_matches_rrdset(rc, st))) {
  302. rrdcalc_link_to_rrdset(st, rc);
  303. return -1;
  304. }
  305. return 0;
  306. }
  307. // ----------------------------------------------------------------------------
  308. // RRDCALC rrdhost index management - constructor
  309. struct rrdcalc_constructor {
  310. RRDHOST *rrdhost; // the host we operate upon
  311. RRDCALC *from_config; // points to the original RRDCALC, as loaded from the config
  312. RRDCALCTEMPLATE *from_rrdcalctemplate; // the template this alert is generated from
  313. RRDSET *rrdset; // when this comes from rrdcalctemplate, we have a matching rrdset
  314. const char *overwrite_alert_name; // when we have a dimension foreach, the alert is renamed
  315. const char *overwrite_dimensions; // when we have a dimension foreach, the dimensions filter is renamed
  316. enum {
  317. RRDCALC_REACT_NONE,
  318. RRDCALC_REACT_NEW,
  319. } react_action;
  320. bool existing_from_template;
  321. };
  322. static void rrdcalc_rrdhost_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalc, void *constructor_data) {
  323. RRDCALC *rc = rrdcalc;
  324. struct rrdcalc_constructor *ctr = constructor_data;
  325. RRDHOST *host = ctr->rrdhost;
  326. rc->key = string_strdupz(dictionary_acquired_item_name(item));
  327. if(ctr->from_rrdcalctemplate) {
  328. rc->run_flags |= RRDCALC_FLAG_FROM_TEMPLATE;
  329. RRDCALCTEMPLATE *rt = ctr->from_rrdcalctemplate;
  330. RRDSET *st = ctr->rrdset;
  331. rc->next_event_id = 1;
  332. rc->name = (ctr->overwrite_alert_name) ? string_strdupz(ctr->overwrite_alert_name) : string_dup(rt->name);
  333. rc->chart = string_dup(st->id);
  334. uuid_copy(rc->config_hash_id, rt->config_hash_id);
  335. rc->dimensions = (ctr->overwrite_dimensions) ? string_strdupz(ctr->overwrite_dimensions) : string_dup(rt->dimensions);
  336. rc->foreach_dimension = NULL;
  337. rc->foreach_dimension_pattern = NULL;
  338. rc->green = rt->green;
  339. rc->red = rt->red;
  340. rc->value = NAN;
  341. rc->old_value = NAN;
  342. rc->delay_up_duration = rt->delay_up_duration;
  343. rc->delay_down_duration = rt->delay_down_duration;
  344. rc->delay_max_duration = rt->delay_max_duration;
  345. rc->delay_multiplier = rt->delay_multiplier;
  346. rc->last_repeat = 0;
  347. rc->times_repeat = 0;
  348. rc->warn_repeat_every = rt->warn_repeat_every;
  349. rc->crit_repeat_every = rt->crit_repeat_every;
  350. rc->group = rt->group;
  351. rc->after = rt->after;
  352. rc->before = rt->before;
  353. rc->update_every = rt->update_every;
  354. rc->options = rt->options;
  355. rc->exec = string_dup(rt->exec);
  356. rc->recipient = string_dup(rt->recipient);
  357. rc->source = string_dup(rt->source);
  358. rc->units = string_dup(rt->units);
  359. rc->info = string_dup(rt->info);
  360. rc->original_info = string_dup(rt->info);
  361. rc->classification = string_dup(rt->classification);
  362. rc->component = string_dup(rt->component);
  363. rc->type = string_dup(rt->type);
  364. if(rt->calculation) {
  365. rc->calculation = expression_parse(rt->calculation->source, NULL, NULL);
  366. if(!rc->calculation)
  367. error("Health alarm '%s.%s': failed to parse calculation expression '%s'", rrdset_id(st), rrdcalctemplate_name(rt), rt->calculation->source);
  368. }
  369. if(rt->warning) {
  370. rc->warning = expression_parse(rt->warning->source, NULL, NULL);
  371. if(!rc->warning)
  372. error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", rrdset_id(st), rrdcalctemplate_name(rt), rt->warning->source);
  373. }
  374. if(rt->critical) {
  375. rc->critical = expression_parse(rt->critical->source, NULL, NULL);
  376. if(!rc->critical)
  377. error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", rrdset_id(st), rrdcalctemplate_name(rt), rt->critical->source);
  378. }
  379. }
  380. else if(ctr->from_config) {
  381. // dictionary has already copied all the members values and pointers
  382. // no need for additional work in this case
  383. ;
  384. }
  385. rc->id = rrdcalc_get_unique_id(host, rc->chart, rc->name, &rc->next_event_id);
  386. if(rc->calculation) {
  387. rc->calculation->status = &rc->status;
  388. rc->calculation->myself = &rc->value;
  389. rc->calculation->after = &rc->db_after;
  390. rc->calculation->before = &rc->db_before;
  391. rc->calculation->rrdcalc = rc;
  392. }
  393. if(rc->warning) {
  394. rc->warning->status = &rc->status;
  395. rc->warning->myself = &rc->value;
  396. rc->warning->after = &rc->db_after;
  397. rc->warning->before = &rc->db_before;
  398. rc->warning->rrdcalc = rc;
  399. }
  400. if(rc->critical) {
  401. rc->critical->status = &rc->status;
  402. rc->critical->myself = &rc->value;
  403. rc->critical->after = &rc->db_after;
  404. rc->critical->before = &rc->db_before;
  405. rc->critical->rrdcalc = rc;
  406. }
  407. debug(D_HEALTH, "Health added alarm '%s.%s': exec '%s', recipient '%s', green " NETDATA_DOUBLE_FORMAT_AUTO
  408. ", red " NETDATA_DOUBLE_FORMAT_AUTO
  409. ", 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",
  410. rrdcalc_chart_name(rc),
  411. rrdcalc_name(rc),
  412. (rc->exec)?rrdcalc_exec(rc):"DEFAULT",
  413. (rc->recipient)?rrdcalc_recipient(rc):"DEFAULT",
  414. rc->green,
  415. rc->red,
  416. (int)rc->group,
  417. rc->after,
  418. rc->before,
  419. rc->options,
  420. (rc->dimensions)?rrdcalc_dimensions(rc):"NONE",
  421. (rc->foreach_dimension)?rrdcalc_foreachdim(rc):"NONE",
  422. rc->update_every,
  423. (rc->calculation)?rc->calculation->parsed_as:"NONE",
  424. (rc->warning)?rc->warning->parsed_as:"NONE",
  425. (rc->critical)?rc->critical->parsed_as:"NONE",
  426. rrdcalc_source(rc),
  427. rc->delay_up_duration,
  428. rc->delay_down_duration,
  429. rc->delay_max_duration,
  430. rc->delay_multiplier,
  431. rc->warn_repeat_every,
  432. rc->crit_repeat_every
  433. );
  434. ctr->react_action = RRDCALC_REACT_NEW;
  435. }
  436. static bool rrdcalc_rrdhost_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalc, void *rrdcalc_new __maybe_unused, void *constructor_data ) {
  437. RRDCALC *rc = rrdcalc;
  438. struct rrdcalc_constructor *ctr = constructor_data;
  439. if(rc->run_flags & RRDCALC_FLAG_FROM_TEMPLATE)
  440. ctr->existing_from_template = true;
  441. else
  442. ctr->existing_from_template = false;
  443. ctr->react_action = RRDCALC_REACT_NONE;
  444. return false;
  445. }
  446. static void rrdcalc_rrdhost_react_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalc, void *constructor_data) {
  447. RRDCALC *rc = rrdcalc;
  448. struct rrdcalc_constructor *ctr = constructor_data;
  449. RRDHOST *host = ctr->rrdhost;
  450. if(ctr->react_action == RRDCALC_REACT_NEW) {
  451. if(ctr->rrdset)
  452. rrdcalc_link_to_rrdset(ctr->rrdset, rc);
  453. else if (ctr->from_rrdcalctemplate)
  454. rrdcontext_foreach_instance_with_rrdset_in_context(host, string2str(ctr->from_rrdcalctemplate->context), rrdcalc_check_and_link_rrdset_callback, rc);
  455. }
  456. }
  457. // ----------------------------------------------------------------------------
  458. // RRDCALC rrdhost index management - destructor
  459. static void rrdcalc_free_internals(RRDCALC *rc) {
  460. if(unlikely(!rc)) return;
  461. expression_free(rc->calculation);
  462. expression_free(rc->warning);
  463. expression_free(rc->critical);
  464. string_freez(rc->key);
  465. string_freez(rc->name);
  466. string_freez(rc->chart);
  467. string_freez(rc->dimensions);
  468. string_freez(rc->foreach_dimension);
  469. string_freez(rc->exec);
  470. string_freez(rc->recipient);
  471. string_freez(rc->source);
  472. string_freez(rc->units);
  473. string_freez(rc->info);
  474. string_freez(rc->original_info);
  475. string_freez(rc->classification);
  476. string_freez(rc->component);
  477. string_freez(rc->type);
  478. string_freez(rc->host_labels);
  479. string_freez(rc->module_match);
  480. string_freez(rc->plugin_match);
  481. simple_pattern_free(rc->foreach_dimension_pattern);
  482. simple_pattern_free(rc->host_labels_pattern);
  483. simple_pattern_free(rc->module_pattern);
  484. simple_pattern_free(rc->plugin_pattern);
  485. }
  486. static void rrdcalc_rrdhost_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *rrdcalc, void *rrdhost __maybe_unused) {
  487. RRDCALC *rc = rrdcalc;
  488. //RRDHOST *host = rrdhost;
  489. if(unlikely(rc->rrdset))
  490. rrdcalc_unlink_from_rrdset(rc, false);
  491. // any destruction actions that require other locks
  492. // have to be placed in rrdcalc_del(), because the object is actually locked for deletion
  493. rrdcalc_free_internals(rc);
  494. }
  495. // ----------------------------------------------------------------------------
  496. // RRDCALC rrdhost index management - index API
  497. void rrdcalc_rrdhost_index_init(RRDHOST *host) {
  498. if(!host->rrdcalc_root_index) {
  499. host->rrdcalc_root_index = dictionary_create_advanced(DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  500. &dictionary_stats_category_rrdhealth, sizeof(RRDCALC));
  501. dictionary_register_insert_callback(host->rrdcalc_root_index, rrdcalc_rrdhost_insert_callback, NULL);
  502. dictionary_register_conflict_callback(host->rrdcalc_root_index, rrdcalc_rrdhost_conflict_callback, NULL);
  503. dictionary_register_react_callback(host->rrdcalc_root_index, rrdcalc_rrdhost_react_callback, NULL);
  504. dictionary_register_delete_callback(host->rrdcalc_root_index, rrdcalc_rrdhost_delete_callback, host);
  505. }
  506. }
  507. void rrdcalc_rrdhost_index_destroy(RRDHOST *host) {
  508. dictionary_destroy(host->rrdcalc_root_index);
  509. host->rrdcalc_root_index = NULL;
  510. }
  511. void rrdcalc_add_from_rrdcalctemplate(RRDHOST *host, RRDCALCTEMPLATE *rt, RRDSET *st, const char *overwrite_alert_name, const char *overwrite_dimensions) {
  512. char key[RRDCALC_MAX_KEY_SIZE + 1];
  513. size_t key_len = rrdcalc_key(key, RRDCALC_MAX_KEY_SIZE, rrdset_id(st),
  514. overwrite_alert_name?overwrite_alert_name:string2str(rt->name));
  515. struct rrdcalc_constructor tmp = {
  516. .rrdhost = host,
  517. .from_config = NULL,
  518. .from_rrdcalctemplate = rt,
  519. .rrdset = st,
  520. .overwrite_alert_name = overwrite_alert_name,
  521. .overwrite_dimensions = overwrite_dimensions,
  522. .react_action = RRDCALC_REACT_NONE,
  523. .existing_from_template = false,
  524. };
  525. dictionary_set_advanced(host->rrdcalc_root_index, key, (ssize_t)(key_len + 1), NULL, sizeof(RRDCALC), &tmp);
  526. if(tmp.react_action != RRDCALC_REACT_NEW && tmp.existing_from_template == false)
  527. error("RRDCALC: from template '%s' on chart '%s' with key '%s', failed to be added to host '%s'. It is manually configured.",
  528. string2str(rt->name), rrdset_id(st), key, rrdhost_hostname(host));
  529. }
  530. int rrdcalc_add_from_config(RRDHOST *host, RRDCALC *rc) {
  531. if(!rc->chart) {
  532. error("Health configuration for alarm '%s' does not have a chart", rrdcalc_name(rc));
  533. return 0;
  534. }
  535. if(!rc->update_every) {
  536. error("Health configuration for alarm '%s.%s' has no frequency (parameter 'every'). Ignoring it.", rrdcalc_chart_name(rc), rrdcalc_name(rc));
  537. return 0;
  538. }
  539. if(!RRDCALC_HAS_DB_LOOKUP(rc) && !rc->calculation && !rc->warning && !rc->critical) {
  540. 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));
  541. return 0;
  542. }
  543. char key[RRDCALC_MAX_KEY_SIZE + 1];
  544. size_t key_len = rrdcalc_key(key, RRDCALC_MAX_KEY_SIZE, string2str(rc->chart), string2str(rc->name));
  545. struct rrdcalc_constructor tmp = {
  546. .rrdhost = host,
  547. .from_config = rc,
  548. .from_rrdcalctemplate = NULL,
  549. .rrdset = NULL,
  550. .react_action = RRDCALC_REACT_NONE,
  551. };
  552. int ret = 1;
  553. RRDCALC *t = dictionary_set_advanced(host->rrdcalc_root_index, key, (ssize_t)(key_len + 1), rc, sizeof(RRDCALC), &tmp);
  554. if(tmp.react_action == RRDCALC_REACT_NEW) {
  555. // we copied rc into the dictionary, so we have to free the container here
  556. freez(rc);
  557. rc = t;
  558. // since we loaded this config from configuration, we need to check if we can link it to alarms
  559. RRDSET *st;
  560. rrdset_foreach_read(st, host) {
  561. if (unlikely(rrdcalc_check_and_link_rrdset_callback(st, rc) == -1))
  562. break;
  563. }
  564. rrdset_foreach_done(st);
  565. }
  566. else {
  567. error(
  568. "RRDCALC: from config '%s' on chart '%s' failed to be added to host '%s'. It already exists.",
  569. string2str(rc->name),
  570. string2str(rc->chart),
  571. rrdhost_hostname(host));
  572. ret = 0;
  573. // free all of it, internals and the container
  574. rrdcalc_free_unused_rrdcalc_loaded_from_config(rc);
  575. }
  576. return ret;
  577. }
  578. static void rrdcalc_unlink_and_delete(RRDHOST *host, RRDCALC *rc, bool having_ll_wrlock) {
  579. if(rc->rrdset)
  580. rrdcalc_unlink_from_rrdset(rc, having_ll_wrlock);
  581. dictionary_del_advanced(host->rrdcalc_root_index, string2str(rc->key), (ssize_t)string_strlen(rc->key) + 1);
  582. }
  583. // ----------------------------------------------------------------------------
  584. // RRDCALC cleanup API functions
  585. void rrdcalc_delete_alerts_not_matching_host_labels_from_this_host(RRDHOST *host) {
  586. RRDCALC *rc;
  587. foreach_rrdcalc_in_rrdhost_reentrant(host, rc) {
  588. if (!rc->host_labels)
  589. continue;
  590. if(!rrdlabels_match_simple_pattern_parsed(host->rrdlabels, rc->host_labels_pattern, '=')) {
  591. log_health("Health configuration for alarm '%s' cannot be applied, because the host %s does not have the label(s) '%s'",
  592. rrdcalc_name(rc),
  593. rrdhost_hostname(host),
  594. rrdcalc_host_labels(rc));
  595. rrdcalc_unlink_and_delete(host, rc, false);
  596. }
  597. }
  598. foreach_rrdcalc_in_rrdhost_done(rc);
  599. }
  600. void rrdcalc_delete_alerts_not_matching_host_labels_from_all_hosts() {
  601. rrd_rdlock();
  602. RRDHOST *host;
  603. rrdhost_foreach_read(host) {
  604. if (unlikely(!host->health.health_enabled))
  605. continue;
  606. if (host->rrdlabels)
  607. rrdcalc_delete_alerts_not_matching_host_labels_from_this_host(host);
  608. }
  609. rrd_unlock();
  610. }
  611. void rrdcalc_unlink_all_rrdset_alerts(RRDSET *st) {
  612. RRDCALC *rc, *last = NULL;
  613. netdata_rwlock_wrlock(&st->alerts.rwlock);
  614. while((rc = st->alerts.base)) {
  615. if(last == rc) {
  616. error("RRDCALC: malformed list of alerts linked to chart - cannot cleanup - giving up.");
  617. break;
  618. }
  619. last = rc;
  620. if(rc->run_flags & RRDCALC_FLAG_FROM_TEMPLATE) {
  621. // if the alert comes from a template we can just delete it
  622. rrdcalc_unlink_and_delete(st->rrdhost, rc, true);
  623. }
  624. else {
  625. // this is a configuration for a specific chart
  626. // it should stay in the list
  627. rrdcalc_unlink_from_rrdset(rc, true);
  628. }
  629. }
  630. netdata_rwlock_unlock(&st->alerts.rwlock);
  631. }
  632. void rrdcalc_delete_all(RRDHOST *host) {
  633. dictionary_flush(host->rrdcalc_root_index);
  634. }
  635. void rrdcalc_free_unused_rrdcalc_loaded_from_config(RRDCALC *rc) {
  636. if(rc->rrdset)
  637. rrdcalc_unlink_from_rrdset(rc, false);
  638. rrdcalc_free_internals(rc);
  639. freez(rc);
  640. }