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