health_config.c 52 KB

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