health_config.c 56 KB

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