health_config.c 52 KB

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