rrdcalc.c 24 KB

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