health_config.c 42 KB

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