rrdcalc.c 25 KB

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