health_config.c 50 KB

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