health_config.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "health.h"
  3. #define HEALTH_CONF_MAX_LINE 4096
  4. #define HEALTH_ALARM_KEY "alarm"
  5. #define HEALTH_TEMPLATE_KEY "template"
  6. #define HEALTH_ON_KEY "on"
  7. #define HEALTH_HOST_KEY "hosts"
  8. #define HEALTH_OS_KEY "os"
  9. #define HEALTH_FAMILIES_KEY "families"
  10. #define HEALTH_LOOKUP_KEY "lookup"
  11. #define HEALTH_CALC_KEY "calc"
  12. #define HEALTH_EVERY_KEY "every"
  13. #define HEALTH_GREEN_KEY "green"
  14. #define HEALTH_RED_KEY "red"
  15. #define HEALTH_WARN_KEY "warn"
  16. #define HEALTH_CRIT_KEY "crit"
  17. #define HEALTH_EXEC_KEY "exec"
  18. #define HEALTH_RECIPIENT_KEY "to"
  19. #define HEALTH_UNITS_KEY "units"
  20. #define HEALTH_INFO_KEY "info"
  21. #define HEALTH_DELAY_KEY "delay"
  22. #define HEALTH_OPTIONS_KEY "options"
  23. #define HEALTH_REPEAT_KEY "repeat"
  24. #define HEALTH_HOST_LABEL_KEY "host labels"
  25. static inline int rrdcalc_add_alarm_from_config(RRDHOST *host, RRDCALC *rc) {
  26. if(!rc->chart) {
  27. error("Health configuration for alarm '%s' does not have a chart", rc->name);
  28. return 0;
  29. }
  30. if(!rc->update_every) {
  31. error("Health configuration for alarm '%s.%s' has no frequency (parameter 'every'). Ignoring it.", rc->chart?rc->chart:"NOCHART", rc->name);
  32. return 0;
  33. }
  34. if(!RRDCALC_HAS_DB_LOOKUP(rc) && !rc->calculation && !rc->warning && !rc->critical) {
  35. error("Health configuration for alarm '%s.%s' is useless (no db lookup, no calculation, no warning and no critical expressions)", rc->chart?rc->chart:"NOCHART", rc->name);
  36. return 0;
  37. }
  38. if (rrdcalc_exists(host, rc->chart, rc->name, rc->hash_chart, rc->hash))
  39. return 0;
  40. rc->id = rrdcalc_get_unique_id(host, rc->chart, rc->name, &rc->next_event_id);
  41. debug(D_HEALTH, "Health configuration adding alarm '%s.%s' (%u): 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",
  42. rc->chart?rc->chart:"NOCHART",
  43. rc->name,
  44. rc->id,
  45. (rc->exec)?rc->exec:"DEFAULT",
  46. (rc->recipient)?rc->recipient:"DEFAULT",
  47. rc->green,
  48. rc->red,
  49. (int)rc->group,
  50. rc->after,
  51. rc->before,
  52. rc->options,
  53. (rc->dimensions)?rc->dimensions:"NONE",
  54. (rc->foreachdim)?rc->foreachdim:"NONE",
  55. rc->update_every,
  56. (rc->calculation)?rc->calculation->parsed_as:"NONE",
  57. (rc->warning)?rc->warning->parsed_as:"NONE",
  58. (rc->critical)?rc->critical->parsed_as:"NONE",
  59. rc->source,
  60. rc->delay_up_duration,
  61. rc->delay_down_duration,
  62. rc->delay_max_duration,
  63. rc->delay_multiplier,
  64. rc->warn_repeat_every,
  65. rc->crit_repeat_every
  66. );
  67. rrdcalc_add_to_host(host, rc);
  68. return 1;
  69. }
  70. static inline int rrdcalctemplate_add_template_from_config(RRDHOST *host, RRDCALCTEMPLATE *rt) {
  71. if(unlikely(!rt->context)) {
  72. error("Health configuration for template '%s' does not have a context", rt->name);
  73. return 0;
  74. }
  75. if(unlikely(!rt->update_every)) {
  76. error("Health configuration for template '%s' has no frequency (parameter 'every'). Ignoring it.", rt->name);
  77. return 0;
  78. }
  79. if(unlikely(!RRDCALCTEMPLATE_HAS_DB_LOOKUP(rt) && !rt->calculation && !rt->warning && !rt->critical)) {
  80. error("Health configuration for template '%s' is useless (no calculation, no warning and no critical evaluation)", rt->name);
  81. return 0;
  82. }
  83. RRDCALCTEMPLATE *t, *last = NULL;
  84. if(!rt->foreachdim) {
  85. for (t = host->templates; t ; last = t, t = t->next) {
  86. if(unlikely(t->hash_name == rt->hash_name
  87. && !strcmp(t->name, rt->name)
  88. && !strcmp(t->family_match?t->family_match:"*", rt->family_match?rt->family_match:"*")
  89. )) {
  90. error("Health configuration template '%s' already exists for host '%s'.", rt->name, host->hostname);
  91. return 0;
  92. }
  93. }
  94. if(likely(last)) {
  95. last->next = rt;
  96. }
  97. else {
  98. rt->next = host->templates;
  99. host->templates = rt;
  100. }
  101. } else {
  102. for (t = host->alarms_template_with_foreach; t ; last = t, t = t->next) {
  103. if(unlikely(t->hash_name == rt->hash_name
  104. && !strcmp(t->name, rt->name)
  105. && !strcmp(t->family_match?t->family_match:"*", rt->family_match?rt->family_match:"*")
  106. )) {
  107. error("Health configuration template '%s' already exists for host '%s'.", rt->name, host->hostname);
  108. return 0;
  109. }
  110. }
  111. if(likely(last)) {
  112. last->next = rt;
  113. }
  114. else {
  115. rt->next = host->alarms_template_with_foreach;
  116. host->alarms_template_with_foreach = rt;
  117. }
  118. }
  119. debug(D_HEALTH, "Health configuration adding template '%s': context '%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",
  120. rt->name,
  121. (rt->context)?rt->context:"NONE",
  122. (rt->exec)?rt->exec:"DEFAULT",
  123. (rt->recipient)?rt->recipient:"DEFAULT",
  124. rt->green,
  125. rt->red,
  126. (int)rt->group,
  127. rt->after,
  128. rt->before,
  129. rt->options,
  130. (rt->dimensions)?rt->dimensions:"NONE",
  131. (rt->foreachdim)?rt->foreachdim:"NONE",
  132. rt->update_every,
  133. (rt->calculation)?rt->calculation->parsed_as:"NONE",
  134. (rt->warning)?rt->warning->parsed_as:"NONE",
  135. (rt->critical)?rt->critical->parsed_as:"NONE",
  136. rt->source,
  137. rt->delay_up_duration,
  138. rt->delay_down_duration,
  139. rt->delay_max_duration,
  140. rt->delay_multiplier,
  141. rt->warn_repeat_every,
  142. rt->crit_repeat_every
  143. );
  144. return 1;
  145. }
  146. static inline int health_parse_delay(
  147. size_t line, const char *filename, char *string,
  148. int *delay_up_duration,
  149. int *delay_down_duration,
  150. int *delay_max_duration,
  151. float *delay_multiplier) {
  152. char given_up = 0;
  153. char given_down = 0;
  154. char given_max = 0;
  155. char given_multiplier = 0;
  156. char *s = string;
  157. while(*s) {
  158. char *key = s;
  159. while(*s && !isspace(*s)) s++;
  160. while(*s && isspace(*s)) *s++ = '\0';
  161. if(!*key) break;
  162. char *value = s;
  163. while(*s && !isspace(*s)) s++;
  164. while(*s && isspace(*s)) *s++ = '\0';
  165. if(!strcasecmp(key, "up")) {
  166. if (!config_parse_duration(value, delay_up_duration)) {
  167. error("Health configuration at line %zu of file '%s': invalid value '%s' for '%s' keyword",
  168. line, filename, value, key);
  169. }
  170. else given_up = 1;
  171. }
  172. else if(!strcasecmp(key, "down")) {
  173. if (!config_parse_duration(value, delay_down_duration)) {
  174. error("Health configuration at line %zu of file '%s': invalid value '%s' for '%s' keyword",
  175. line, filename, value, key);
  176. }
  177. else given_down = 1;
  178. }
  179. else if(!strcasecmp(key, "multiplier")) {
  180. *delay_multiplier = strtof(value, NULL);
  181. if(isnan(*delay_multiplier) || isinf(*delay_multiplier) || islessequal(*delay_multiplier, 0)) {
  182. error("Health configuration at line %zu of file '%s': invalid value '%s' for '%s' keyword",
  183. line, filename, value, key);
  184. }
  185. else given_multiplier = 1;
  186. }
  187. else if(!strcasecmp(key, "max")) {
  188. if (!config_parse_duration(value, delay_max_duration)) {
  189. error("Health configuration at line %zu of file '%s': invalid value '%s' for '%s' keyword",
  190. line, filename, value, key);
  191. }
  192. else given_max = 1;
  193. }
  194. else {
  195. error("Health configuration at line %zu of file '%s': unknown keyword '%s'",
  196. line, filename, key);
  197. }
  198. }
  199. if(!given_up)
  200. *delay_up_duration = 0;
  201. if(!given_down)
  202. *delay_down_duration = 0;
  203. if(!given_multiplier)
  204. *delay_multiplier = 1.0;
  205. if(!given_max) {
  206. if((*delay_max_duration) < (*delay_up_duration) * (*delay_multiplier))
  207. *delay_max_duration = (int)((*delay_up_duration) * (*delay_multiplier));
  208. if((*delay_max_duration) < (*delay_down_duration) * (*delay_multiplier))
  209. *delay_max_duration = (int)((*delay_down_duration) * (*delay_multiplier));
  210. }
  211. return 1;
  212. }
  213. static inline uint32_t health_parse_options(const char *s) {
  214. uint32_t options = 0;
  215. char buf[100+1] = "";
  216. while(*s) {
  217. buf[0] = '\0';
  218. // skip spaces
  219. while(*s && isspace(*s))
  220. s++;
  221. // find the next space
  222. size_t count = 0;
  223. while(*s && count < 100 && !isspace(*s))
  224. buf[count++] = *s++;
  225. if(buf[0]) {
  226. buf[count] = '\0';
  227. if(!strcasecmp(buf, "no-clear-notification") || !strcasecmp(buf, "no-clear"))
  228. options |= RRDCALC_FLAG_NO_CLEAR_NOTIFICATION;
  229. else
  230. error("Ignoring unknown alarm option '%s'", buf);
  231. }
  232. }
  233. return options;
  234. }
  235. static inline int health_parse_repeat(
  236. size_t line,
  237. const char *file,
  238. char *string,
  239. uint32_t *warn_repeat_every,
  240. uint32_t *crit_repeat_every
  241. ) {
  242. char *s = string;
  243. while(*s) {
  244. char *key = s;
  245. while(*s && !isspace(*s)) s++;
  246. while(*s && isspace(*s)) *s++ = '\0';
  247. if(!*key) break;
  248. char *value = s;
  249. while(*s && !isspace(*s)) s++;
  250. while(*s && isspace(*s)) *s++ = '\0';
  251. if(!strcasecmp(key, "off")) {
  252. *warn_repeat_every = 0;
  253. *crit_repeat_every = 0;
  254. return 1;
  255. }
  256. if(!strcasecmp(key, "warning")) {
  257. if (!config_parse_duration(value, (int*)warn_repeat_every)) {
  258. error("Health configuration at line %zu of file '%s': invalid value '%s' for '%s' keyword",
  259. line, file, value, key);
  260. }
  261. }
  262. else if(!strcasecmp(key, "critical")) {
  263. if (!config_parse_duration(value, (int*)crit_repeat_every)) {
  264. error("Health configuration at line %zu of file '%s': invalid value '%s' for '%s' keyword",
  265. line, file, value, key);
  266. }
  267. }
  268. }
  269. return 1;
  270. }
  271. /**
  272. * Health pattern from Foreach
  273. *
  274. * Create a new simple pattern using the user input
  275. *
  276. * @param s the string that will be used to create the simple pattern.
  277. */
  278. SIMPLE_PATTERN *health_pattern_from_foreach(char *s) {
  279. char *convert= strdupz(s);
  280. SIMPLE_PATTERN *val = NULL;
  281. if(convert) {
  282. dimension_remove_pipe_comma(convert);
  283. val = simple_pattern_create(convert, NULL, SIMPLE_PATTERN_EXACT);
  284. freez(convert);
  285. }
  286. return val;
  287. }
  288. static inline int health_parse_db_lookup(
  289. size_t line, const char *filename, char *string,
  290. RRDR_GROUPING *group_method, int *after, int *before, int *every,
  291. uint32_t *options, char **dimensions, char **foreachdim
  292. ) {
  293. debug(D_HEALTH, "Health configuration parsing database lookup %zu@%s: %s", line, filename, string);
  294. if(*dimensions) freez(*dimensions);
  295. if(*foreachdim) freez(*foreachdim);
  296. *dimensions = NULL;
  297. *foreachdim = NULL;
  298. *after = 0;
  299. *before = 0;
  300. *every = 0;
  301. *options = 0;
  302. char *s = string, *key;
  303. // first is the group method
  304. key = s;
  305. while(*s && !isspace(*s)) s++;
  306. while(*s && isspace(*s)) *s++ = '\0';
  307. if(!*s) {
  308. error("Health configuration invalid chart calculation at line %zu of file '%s': expected group method followed by the 'after' time, but got '%s'",
  309. line, filename, key);
  310. return 0;
  311. }
  312. if((*group_method = web_client_api_request_v1_data_group(key, RRDR_GROUPING_UNDEFINED)) == RRDR_GROUPING_UNDEFINED) {
  313. error("Health configuration at line %zu of file '%s': invalid group method '%s'",
  314. line, filename, key);
  315. return 0;
  316. }
  317. // then is the 'after' time
  318. key = s;
  319. while(*s && !isspace(*s)) s++;
  320. while(*s && isspace(*s)) *s++ = '\0';
  321. if(!config_parse_duration(key, after)) {
  322. error("Health configuration at line %zu of file '%s': invalid duration '%s' after group method",
  323. line, filename, key);
  324. return 0;
  325. }
  326. // sane defaults
  327. *every = abs(*after);
  328. // now we may have optional parameters
  329. while(*s) {
  330. key = s;
  331. while(*s && !isspace(*s)) s++;
  332. while(*s && isspace(*s)) *s++ = '\0';
  333. if(!*key) break;
  334. if(!strcasecmp(key, "at")) {
  335. char *value = s;
  336. while(*s && !isspace(*s)) s++;
  337. while(*s && isspace(*s)) *s++ = '\0';
  338. if (!config_parse_duration(value, before)) {
  339. error("Health configuration at line %zu of file '%s': invalid duration '%s' for '%s' keyword",
  340. line, filename, value, key);
  341. }
  342. }
  343. else if(!strcasecmp(key, HEALTH_EVERY_KEY)) {
  344. char *value = s;
  345. while(*s && !isspace(*s)) s++;
  346. while(*s && isspace(*s)) *s++ = '\0';
  347. if (!config_parse_duration(value, every)) {
  348. error("Health configuration at line %zu of file '%s': invalid duration '%s' for '%s' keyword",
  349. line, filename, value, key);
  350. }
  351. }
  352. else if(!strcasecmp(key, "absolute") || !strcasecmp(key, "abs") || !strcasecmp(key, "absolute_sum")) {
  353. *options |= RRDR_OPTION_ABSOLUTE;
  354. }
  355. else if(!strcasecmp(key, "min2max")) {
  356. *options |= RRDR_OPTION_MIN2MAX;
  357. }
  358. else if(!strcasecmp(key, "null2zero")) {
  359. *options |= RRDR_OPTION_NULL2ZERO;
  360. }
  361. else if(!strcasecmp(key, "percentage")) {
  362. *options |= RRDR_OPTION_PERCENTAGE;
  363. }
  364. else if(!strcasecmp(key, "unaligned")) {
  365. *options |= RRDR_OPTION_NOT_ALIGNED;
  366. }
  367. else if(!strcasecmp(key, "match-ids") || !strcasecmp(key, "match_ids")) {
  368. *options |= RRDR_OPTION_MATCH_IDS;
  369. }
  370. else if(!strcasecmp(key, "match-names") || !strcasecmp(key, "match_names")) {
  371. *options |= RRDR_OPTION_MATCH_NAMES;
  372. }
  373. else if(!strcasecmp(key, "of")) {
  374. char *find = NULL;
  375. if(*s && strcasecmp(s, "all") != 0) {
  376. find = strcasestr(s, " foreach");
  377. if(find) {
  378. *find = '\0';
  379. }
  380. *dimensions = strdupz(s);
  381. }
  382. if(!find) {
  383. break;
  384. }
  385. s = ++find;
  386. }
  387. else if(!strcasecmp(key, HEALTH_FOREACH_KEY )) {
  388. *foreachdim = strdupz(s);
  389. break;
  390. }
  391. else {
  392. error("Health configuration at line %zu of file '%s': unknown keyword '%s'",
  393. line, filename, key);
  394. }
  395. }
  396. return 1;
  397. }
  398. static inline char *health_source_file(size_t line, const char *file) {
  399. char buffer[FILENAME_MAX + 1];
  400. snprintfz(buffer, FILENAME_MAX, "%zu@%s", line, file);
  401. return strdupz(buffer);
  402. }
  403. static inline void strip_quotes(char *s) {
  404. while(*s) {
  405. if(*s == '\'' || *s == '"') *s = ' ';
  406. s++;
  407. }
  408. }
  409. static int health_readfile(const char *filename, void *data) {
  410. RRDHOST *host = (RRDHOST *)data;
  411. debug(D_HEALTH, "Health configuration reading file '%s'", filename);
  412. static uint32_t
  413. hash_alarm = 0,
  414. hash_template = 0,
  415. hash_os = 0,
  416. hash_on = 0,
  417. hash_host = 0,
  418. hash_families = 0,
  419. hash_calc = 0,
  420. hash_green = 0,
  421. hash_red = 0,
  422. hash_warn = 0,
  423. hash_crit = 0,
  424. hash_exec = 0,
  425. hash_every = 0,
  426. hash_lookup = 0,
  427. hash_units = 0,
  428. hash_info = 0,
  429. hash_recipient = 0,
  430. hash_delay = 0,
  431. hash_options = 0,
  432. hash_repeat = 0,
  433. hash_host_label = 0;
  434. char buffer[HEALTH_CONF_MAX_LINE + 1];
  435. if(unlikely(!hash_alarm)) {
  436. hash_alarm = simple_uhash(HEALTH_ALARM_KEY);
  437. hash_template = simple_uhash(HEALTH_TEMPLATE_KEY);
  438. hash_on = simple_uhash(HEALTH_ON_KEY);
  439. hash_os = simple_uhash(HEALTH_OS_KEY);
  440. hash_host = simple_uhash(HEALTH_HOST_KEY);
  441. hash_families = simple_uhash(HEALTH_FAMILIES_KEY);
  442. hash_calc = simple_uhash(HEALTH_CALC_KEY);
  443. hash_lookup = simple_uhash(HEALTH_LOOKUP_KEY);
  444. hash_green = simple_uhash(HEALTH_GREEN_KEY);
  445. hash_red = simple_uhash(HEALTH_RED_KEY);
  446. hash_warn = simple_uhash(HEALTH_WARN_KEY);
  447. hash_crit = simple_uhash(HEALTH_CRIT_KEY);
  448. hash_exec = simple_uhash(HEALTH_EXEC_KEY);
  449. hash_every = simple_uhash(HEALTH_EVERY_KEY);
  450. hash_units = simple_hash(HEALTH_UNITS_KEY);
  451. hash_info = simple_hash(HEALTH_INFO_KEY);
  452. hash_recipient = simple_hash(HEALTH_RECIPIENT_KEY);
  453. hash_delay = simple_uhash(HEALTH_DELAY_KEY);
  454. hash_options = simple_uhash(HEALTH_OPTIONS_KEY);
  455. hash_repeat = simple_uhash(HEALTH_REPEAT_KEY);
  456. hash_host_label = simple_uhash(HEALTH_HOST_LABEL_KEY);
  457. }
  458. FILE *fp = fopen(filename, "r");
  459. if(!fp) {
  460. error("Health configuration cannot read file '%s'.", filename);
  461. return 0;
  462. }
  463. RRDCALC *rc = NULL;
  464. RRDCALCTEMPLATE *rt = NULL;
  465. int ignore_this = 0;
  466. size_t line = 0, append = 0;
  467. char *s;
  468. while((s = fgets(&buffer[append], (int)(HEALTH_CONF_MAX_LINE - append), fp)) || append) {
  469. int stop_appending = !s;
  470. line++;
  471. s = trim(buffer);
  472. if(!s || *s == '#') continue;
  473. append = strlen(s);
  474. if(!stop_appending && s[append - 1] == '\\') {
  475. s[append - 1] = ' ';
  476. append = &s[append] - buffer;
  477. if(append < HEALTH_CONF_MAX_LINE)
  478. continue;
  479. else {
  480. error("Health configuration has too long multi-line at line %zu of file '%s'.", line, filename);
  481. }
  482. }
  483. append = 0;
  484. char *key = s;
  485. while(*s && *s != ':') s++;
  486. if(!*s) {
  487. error("Health configuration has invalid line %zu of file '%s'. It does not contain a ':'. Ignoring it.", line, filename);
  488. continue;
  489. }
  490. *s = '\0';
  491. s++;
  492. char *value = s;
  493. key = trim_all(key);
  494. value = trim_all(value);
  495. if(!key) {
  496. error("Health configuration has invalid line %zu of file '%s'. Keyword is empty. Ignoring it.", line, filename);
  497. continue;
  498. }
  499. if(!value) {
  500. error("Health configuration has invalid line %zu of file '%s'. value is empty. Ignoring it.", line, filename);
  501. continue;
  502. }
  503. uint32_t hash = simple_uhash(key);
  504. if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
  505. if(rc) {
  506. if(ignore_this || !rrdcalc_add_alarm_from_config(host, rc)) {
  507. rrdcalc_free(rc);
  508. }
  509. // health_add_alarms_loop(host, rc, ignore_this) ;
  510. }
  511. if(rt) {
  512. if (ignore_this || !rrdcalctemplate_add_template_from_config(host, rt))
  513. rrdcalctemplate_free(rt);
  514. rt = NULL;
  515. }
  516. rc = callocz(1, sizeof(RRDCALC));
  517. rc->next_event_id = 1;
  518. rc->name = strdupz(value);
  519. rc->hash = simple_hash(rc->name);
  520. rc->source = health_source_file(line, filename);
  521. rc->green = NAN;
  522. rc->red = NAN;
  523. rc->value = NAN;
  524. rc->old_value = NAN;
  525. rc->delay_multiplier = 1.0;
  526. rc->old_status = RRDCALC_STATUS_UNINITIALIZED;
  527. rc->warn_repeat_every = host->health_default_warn_repeat_every;
  528. rc->crit_repeat_every = host->health_default_crit_repeat_every;
  529. if(rrdvar_fix_name(rc->name))
  530. error("Health configuration renamed alarm '%s' to '%s'", value, rc->name);
  531. ignore_this = 0;
  532. }
  533. else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
  534. if(rc) {
  535. // health_add_alarms_loop(host, rc, ignore_this) ;
  536. if(ignore_this || !rrdcalc_add_alarm_from_config(host, rc)) {
  537. rrdcalc_free(rc);
  538. }
  539. rc = NULL;
  540. }
  541. if(rt) {
  542. if(ignore_this || !rrdcalctemplate_add_template_from_config(host, rt))
  543. rrdcalctemplate_free(rt);
  544. }
  545. rt = callocz(1, sizeof(RRDCALCTEMPLATE));
  546. rt->name = strdupz(value);
  547. rt->hash_name = simple_hash(rt->name);
  548. rt->source = health_source_file(line, filename);
  549. rt->green = NAN;
  550. rt->red = NAN;
  551. rt->delay_multiplier = 1.0;
  552. rt->warn_repeat_every = host->health_default_warn_repeat_every;
  553. rt->crit_repeat_every = host->health_default_crit_repeat_every;
  554. if(rrdvar_fix_name(rt->name))
  555. error("Health configuration renamed template '%s' to '%s'", value, rt->name);
  556. ignore_this = 0;
  557. }
  558. else if(hash == hash_os && !strcasecmp(key, HEALTH_OS_KEY)) {
  559. char *os_match = value;
  560. SIMPLE_PATTERN *os_pattern = simple_pattern_create(os_match, NULL, SIMPLE_PATTERN_EXACT);
  561. if(!simple_pattern_matches(os_pattern, host->os)) {
  562. if(rc)
  563. debug(D_HEALTH, "HEALTH on '%s' ignoring alarm '%s' defined at %zu@%s: host O/S does not match '%s'", host->hostname, rc->name, line, filename, os_match);
  564. if(rt)
  565. debug(D_HEALTH, "HEALTH on '%s' ignoring template '%s' defined at %zu@%s: host O/S does not match '%s'", host->hostname, rt->name, line, filename, os_match);
  566. ignore_this = 1;
  567. }
  568. simple_pattern_free(os_pattern);
  569. }
  570. else if(hash == hash_host && !strcasecmp(key, HEALTH_HOST_KEY)) {
  571. char *host_match = value;
  572. SIMPLE_PATTERN *host_pattern = simple_pattern_create(host_match, NULL, SIMPLE_PATTERN_EXACT);
  573. if(!simple_pattern_matches(host_pattern, host->hostname)) {
  574. if(rc)
  575. debug(D_HEALTH, "HEALTH on '%s' ignoring alarm '%s' defined at %zu@%s: hostname does not match '%s'", host->hostname, rc->name, line, filename, host_match);
  576. if(rt)
  577. debug(D_HEALTH, "HEALTH on '%s' ignoring template '%s' defined at %zu@%s: hostname does not match '%s'", host->hostname, rt->name, line, filename, host_match);
  578. ignore_this = 1;
  579. }
  580. simple_pattern_free(host_pattern);
  581. }
  582. else if(rc) {
  583. if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
  584. if(rc->chart) {
  585. if(strcmp(rc->chart, value) != 0)
  586. error("Health configuration at line %zu of file '%s' for alarm '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  587. line, filename, rc->name, key, rc->chart, value, value);
  588. freez(rc->chart);
  589. }
  590. rc->chart = strdupz(value);
  591. rc->hash_chart = simple_hash(rc->chart);
  592. }
  593. else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
  594. health_parse_db_lookup(line, filename, value, &rc->group, &rc->after, &rc->before,
  595. &rc->update_every, &rc->options, &rc->dimensions, &rc->foreachdim);
  596. if(rc->foreachdim) {
  597. rc->spdim = health_pattern_from_foreach(rc->foreachdim);
  598. }
  599. }
  600. else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
  601. if(!config_parse_duration(value, &rc->update_every))
  602. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' cannot parse duration: '%s'.",
  603. line, filename, rc->name, key, value);
  604. }
  605. else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
  606. char *e;
  607. rc->green = str2ld(value, &e);
  608. if(e && *e) {
  609. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
  610. line, filename, rc->name, key, e);
  611. }
  612. }
  613. else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
  614. char *e;
  615. rc->red = str2ld(value, &e);
  616. if(e && *e) {
  617. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
  618. line, filename, rc->name, key, e);
  619. }
  620. }
  621. else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
  622. const char *failed_at = NULL;
  623. int error = 0;
  624. rc->calculation = expression_parse(value, &failed_at, &error);
  625. if(!rc->calculation) {
  626. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  627. line, filename, rc->name, key, value, expression_strerror(error), failed_at);
  628. }
  629. }
  630. else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
  631. const char *failed_at = NULL;
  632. int error = 0;
  633. rc->warning = expression_parse(value, &failed_at, &error);
  634. if(!rc->warning) {
  635. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  636. line, filename, rc->name, key, value, expression_strerror(error), failed_at);
  637. }
  638. }
  639. else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
  640. const char *failed_at = NULL;
  641. int error = 0;
  642. rc->critical = expression_parse(value, &failed_at, &error);
  643. if(!rc->critical) {
  644. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  645. line, filename, rc->name, key, value, expression_strerror(error), failed_at);
  646. }
  647. }
  648. else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
  649. if(rc->exec) {
  650. if(strcmp(rc->exec, value) != 0)
  651. error("Health configuration at line %zu of file '%s' for alarm '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  652. line, filename, rc->name, key, rc->exec, value, value);
  653. freez(rc->exec);
  654. }
  655. rc->exec = strdupz(value);
  656. }
  657. else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
  658. if(rc->recipient) {
  659. if(strcmp(rc->recipient, value) != 0)
  660. error("Health configuration at line %zu of file '%s' for alarm '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  661. line, filename, rc->name, key, rc->recipient, value, value);
  662. freez(rc->recipient);
  663. }
  664. rc->recipient = strdupz(value);
  665. }
  666. else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
  667. if(rc->units) {
  668. if(strcmp(rc->units, value) != 0)
  669. error("Health configuration at line %zu of file '%s' for alarm '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  670. line, filename, rc->name, key, rc->units, value, value);
  671. freez(rc->units);
  672. }
  673. rc->units = strdupz(value);
  674. strip_quotes(rc->units);
  675. }
  676. else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
  677. if(rc->info) {
  678. if(strcmp(rc->info, value) != 0)
  679. error("Health configuration at line %zu of file '%s' for alarm '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  680. line, filename, rc->name, key, rc->info, value, value);
  681. freez(rc->info);
  682. }
  683. rc->info = strdupz(value);
  684. strip_quotes(rc->info);
  685. }
  686. else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
  687. health_parse_delay(line, filename, value, &rc->delay_up_duration, &rc->delay_down_duration, &rc->delay_max_duration, &rc->delay_multiplier);
  688. }
  689. else if(hash == hash_options && !strcasecmp(key, HEALTH_OPTIONS_KEY)) {
  690. rc->options |= health_parse_options(value);
  691. }
  692. else if(hash == hash_repeat && !strcasecmp(key, HEALTH_REPEAT_KEY)){
  693. health_parse_repeat(line, filename, value,
  694. &rc->warn_repeat_every,
  695. &rc->crit_repeat_every);
  696. }
  697. else if(hash == hash_host_label && !strcasecmp(key, HEALTH_HOST_LABEL_KEY)) {
  698. if(rc->labels) {
  699. if(strcmp(rc->labels, value) != 0)
  700. error("Health configuration at line %zu of file '%s' for alarm '%s' has key '%s' twice, once with value '%s' and later with value '%s'.",
  701. line, filename, rc->name, key, value, value);
  702. freez(rc->labels);
  703. simple_pattern_free(rc->splabels);
  704. }
  705. rc->labels = simple_pattern_trim_around_equal(value);
  706. rc->splabels = simple_pattern_create(rc->labels, NULL, SIMPLE_PATTERN_EXACT);
  707. }
  708. else {
  709. error("Health configuration at line %zu of file '%s' for alarm '%s' has unknown key '%s'.",
  710. line, filename, rc->name, key);
  711. }
  712. }
  713. else if(rt) {
  714. if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
  715. if(rt->context) {
  716. if(strcmp(rt->context, value) != 0)
  717. error("Health configuration at line %zu of file '%s' for template '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  718. line, filename, rt->name, key, rt->context, value, value);
  719. freez(rt->context);
  720. }
  721. rt->context = strdupz(value);
  722. rt->hash_context = simple_hash(rt->context);
  723. }
  724. else if(hash == hash_families && !strcasecmp(key, HEALTH_FAMILIES_KEY)) {
  725. freez(rt->family_match);
  726. simple_pattern_free(rt->family_pattern);
  727. rt->family_match = strdupz(value);
  728. rt->family_pattern = simple_pattern_create(rt->family_match, NULL, SIMPLE_PATTERN_EXACT);
  729. }
  730. else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
  731. health_parse_db_lookup(line, filename, value, &rt->group, &rt->after, &rt->before,
  732. &rt->update_every, &rt->options, &rt->dimensions, &rt->foreachdim);
  733. if(rt->foreachdim) {
  734. rt->spdim = health_pattern_from_foreach(rt->foreachdim);
  735. }
  736. }
  737. else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
  738. if(!config_parse_duration(value, &rt->update_every))
  739. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' cannot parse duration: '%s'.",
  740. line, filename, rt->name, key, value);
  741. }
  742. else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
  743. char *e;
  744. rt->green = str2ld(value, &e);
  745. if(e && *e) {
  746. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
  747. line, filename, rt->name, key, e);
  748. }
  749. }
  750. else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
  751. char *e;
  752. rt->red = str2ld(value, &e);
  753. if(e && *e) {
  754. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
  755. line, filename, rt->name, key, e);
  756. }
  757. }
  758. else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
  759. const char *failed_at = NULL;
  760. int error = 0;
  761. rt->calculation = expression_parse(value, &failed_at, &error);
  762. if(!rt->calculation) {
  763. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  764. line, filename, rt->name, key, value, expression_strerror(error), failed_at);
  765. }
  766. }
  767. else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
  768. const char *failed_at = NULL;
  769. int error = 0;
  770. rt->warning = expression_parse(value, &failed_at, &error);
  771. if(!rt->warning) {
  772. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  773. line, filename, rt->name, key, value, expression_strerror(error), failed_at);
  774. }
  775. }
  776. else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
  777. const char *failed_at = NULL;
  778. int error = 0;
  779. rt->critical = expression_parse(value, &failed_at, &error);
  780. if(!rt->critical) {
  781. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  782. line, filename, rt->name, key, value, expression_strerror(error), failed_at);
  783. }
  784. }
  785. else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
  786. if(rt->exec) {
  787. if(strcmp(rt->exec, value) != 0)
  788. error("Health configuration at line %zu of file '%s' for template '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  789. line, filename, rt->name, key, rt->exec, value, value);
  790. freez(rt->exec);
  791. }
  792. rt->exec = strdupz(value);
  793. }
  794. else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
  795. if(rt->recipient) {
  796. if(strcmp(rt->recipient, value) != 0)
  797. error("Health configuration at line %zu of file '%s' for template '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  798. line, filename, rt->name, key, rt->recipient, value, value);
  799. freez(rt->recipient);
  800. }
  801. rt->recipient = strdupz(value);
  802. }
  803. else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
  804. if(rt->units) {
  805. if(strcmp(rt->units, value) != 0)
  806. error("Health configuration at line %zu of file '%s' for template '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  807. line, filename, rt->name, key, rt->units, value, value);
  808. freez(rt->units);
  809. }
  810. rt->units = strdupz(value);
  811. strip_quotes(rt->units);
  812. }
  813. else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
  814. if(rt->info) {
  815. if(strcmp(rt->info, value) != 0)
  816. error("Health configuration at line %zu of file '%s' for template '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  817. line, filename, rt->name, key, rt->info, value, value);
  818. freez(rt->info);
  819. }
  820. rt->info = strdupz(value);
  821. strip_quotes(rt->info);
  822. }
  823. else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
  824. health_parse_delay(line, filename, value, &rt->delay_up_duration, &rt->delay_down_duration, &rt->delay_max_duration, &rt->delay_multiplier);
  825. }
  826. else if(hash == hash_options && !strcasecmp(key, HEALTH_OPTIONS_KEY)) {
  827. rt->options |= health_parse_options(value);
  828. }
  829. else if(hash == hash_repeat && !strcasecmp(key, HEALTH_REPEAT_KEY)){
  830. health_parse_repeat(line, filename, value,
  831. &rt->warn_repeat_every,
  832. &rt->crit_repeat_every);
  833. }
  834. else if(hash == hash_host_label && !strcasecmp(key, HEALTH_HOST_LABEL_KEY)) {
  835. if(rt->labels) {
  836. if(strcmp(rt->labels, value) != 0)
  837. error("Health configuration at line %zu of file '%s' for template '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  838. line, filename, rt->name, key, rt->labels, value, value);
  839. freez(rt->labels);
  840. simple_pattern_free(rt->splabels);
  841. }
  842. rt->labels = simple_pattern_trim_around_equal(value);
  843. rt->splabels = simple_pattern_create(rt->labels, NULL, SIMPLE_PATTERN_EXACT);
  844. }
  845. else {
  846. error("Health configuration at line %zu of file '%s' for template '%s' has unknown key '%s'.",
  847. line, filename, rt->name, key);
  848. }
  849. }
  850. else {
  851. error("Health configuration at line %zu of file '%s' has unknown key '%s'. Expected either '" HEALTH_ALARM_KEY "' or '" HEALTH_TEMPLATE_KEY "'.",
  852. line, filename, key);
  853. }
  854. }
  855. if(rc) {
  856. //health_add_alarms_loop(host, rc, ignore_this) ;
  857. if(ignore_this || !rrdcalc_add_alarm_from_config(host, rc)) {
  858. rrdcalc_free(rc);
  859. }
  860. }
  861. if(rt) {
  862. if(ignore_this || !rrdcalctemplate_add_template_from_config(host, rt))
  863. rrdcalctemplate_free(rt);
  864. }
  865. fclose(fp);
  866. return 1;
  867. }
  868. void health_readdir(RRDHOST *host, const char *user_path, const char *stock_path, const char *subpath) {
  869. if(unlikely(!host->health_enabled)) {
  870. debug(D_HEALTH, "CONFIG health is not enabled for host '%s'", host->hostname);
  871. return;
  872. }
  873. recursive_config_double_dir_load(user_path, stock_path, subpath, health_readfile, (void *) host, 0);
  874. }