health_config.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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. freez(cfg);
  432. }
  433. int sql_store_hashes = 1;
  434. static int health_readfile(const char *filename, void *data) {
  435. RRDHOST *host = (RRDHOST *)data;
  436. debug(D_HEALTH, "Health configuration reading file '%s'", filename);
  437. static uint32_t
  438. hash_alarm = 0,
  439. hash_template = 0,
  440. hash_os = 0,
  441. hash_on = 0,
  442. hash_host = 0,
  443. hash_families = 0,
  444. hash_plugin = 0,
  445. hash_module = 0,
  446. hash_charts = 0,
  447. hash_calc = 0,
  448. hash_green = 0,
  449. hash_red = 0,
  450. hash_warn = 0,
  451. hash_crit = 0,
  452. hash_exec = 0,
  453. hash_every = 0,
  454. hash_lookup = 0,
  455. hash_units = 0,
  456. hash_info = 0,
  457. hash_class = 0,
  458. hash_component = 0,
  459. hash_type = 0,
  460. hash_recipient = 0,
  461. hash_delay = 0,
  462. hash_options = 0,
  463. hash_repeat = 0,
  464. hash_host_label = 0,
  465. hash_chart_label = 0;
  466. char buffer[HEALTH_CONF_MAX_LINE + 1];
  467. if(unlikely(!hash_alarm)) {
  468. hash_alarm = simple_uhash(HEALTH_ALARM_KEY);
  469. hash_template = simple_uhash(HEALTH_TEMPLATE_KEY);
  470. hash_on = simple_uhash(HEALTH_ON_KEY);
  471. hash_os = simple_uhash(HEALTH_OS_KEY);
  472. hash_host = simple_uhash(HEALTH_HOST_KEY);
  473. hash_families = simple_uhash(HEALTH_FAMILIES_KEY);
  474. hash_plugin = simple_uhash(HEALTH_PLUGIN_KEY);
  475. hash_module = simple_uhash(HEALTH_MODULE_KEY);
  476. hash_charts = simple_uhash(HEALTH_CHARTS_KEY);
  477. hash_calc = simple_uhash(HEALTH_CALC_KEY);
  478. hash_lookup = simple_uhash(HEALTH_LOOKUP_KEY);
  479. hash_green = simple_uhash(HEALTH_GREEN_KEY);
  480. hash_red = simple_uhash(HEALTH_RED_KEY);
  481. hash_warn = simple_uhash(HEALTH_WARN_KEY);
  482. hash_crit = simple_uhash(HEALTH_CRIT_KEY);
  483. hash_exec = simple_uhash(HEALTH_EXEC_KEY);
  484. hash_every = simple_uhash(HEALTH_EVERY_KEY);
  485. hash_units = simple_hash(HEALTH_UNITS_KEY);
  486. hash_info = simple_hash(HEALTH_INFO_KEY);
  487. hash_class = simple_uhash(HEALTH_CLASS_KEY);
  488. hash_component = simple_uhash(HEALTH_COMPONENT_KEY);
  489. hash_type = simple_uhash(HEALTH_TYPE_KEY);
  490. hash_recipient = simple_hash(HEALTH_RECIPIENT_KEY);
  491. hash_delay = simple_uhash(HEALTH_DELAY_KEY);
  492. hash_options = simple_uhash(HEALTH_OPTIONS_KEY);
  493. hash_repeat = simple_uhash(HEALTH_REPEAT_KEY);
  494. hash_host_label = simple_uhash(HEALTH_HOST_LABEL_KEY);
  495. hash_chart_label = simple_uhash(HEALTH_CHART_LABEL_KEY);
  496. }
  497. FILE *fp = fopen(filename, "r");
  498. if(!fp) {
  499. error("Health configuration cannot read file '%s'.", filename);
  500. return 0;
  501. }
  502. RRDCALC *rc = NULL;
  503. RRDCALCTEMPLATE *rt = NULL;
  504. struct alert_config *alert_cfg = NULL;
  505. int ignore_this = 0;
  506. size_t line = 0, append = 0;
  507. char *s;
  508. while((s = fgets(&buffer[append], (int)(HEALTH_CONF_MAX_LINE - append), fp)) || append) {
  509. int stop_appending = !s;
  510. line++;
  511. s = trim(buffer);
  512. if(!s || *s == '#') continue;
  513. append = strlen(s);
  514. if(!stop_appending && s[append - 1] == '\\') {
  515. s[append - 1] = ' ';
  516. append = &s[append] - buffer;
  517. if(append < HEALTH_CONF_MAX_LINE)
  518. continue;
  519. else {
  520. error("Health configuration has too long multi-line at line %zu of file '%s'.", line, filename);
  521. }
  522. }
  523. append = 0;
  524. char *key = s;
  525. while(*s && *s != ':') s++;
  526. if(!*s) {
  527. error("Health configuration has invalid line %zu of file '%s'. It does not contain a ':'. Ignoring it.", line, filename);
  528. continue;
  529. }
  530. *s = '\0';
  531. s++;
  532. char *value = s;
  533. key = trim_all(key);
  534. value = trim_all(value);
  535. if(!key) {
  536. error("Health configuration has invalid line %zu of file '%s'. Keyword is empty. Ignoring it.", line, filename);
  537. continue;
  538. }
  539. if(!value) {
  540. error("Health configuration has invalid line %zu of file '%s'. value is empty. Ignoring it.", line, filename);
  541. continue;
  542. }
  543. uint32_t hash = simple_uhash(key);
  544. if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
  545. if(rc) {
  546. if(!alert_hash_and_store_config(rc->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  547. rrdcalc_free_unused_rrdcalc_loaded_from_config(rc);
  548. else
  549. rrdcalc_add_from_config(host, rc);
  550. // health_add_alarms_loop(host, rc, ignore_this) ;
  551. }
  552. if(rt) {
  553. if(!alert_hash_and_store_config(rt->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  554. rrdcalctemplate_free_unused_rrdcalctemplate_loaded_from_config(rt);
  555. else
  556. rrdcalctemplate_add_from_config(host, rt);
  557. rt = NULL;
  558. }
  559. if (simple_pattern_matches(conf_enabled_alarms, value)) {
  560. rc = callocz(1, sizeof(RRDCALC));
  561. rc->next_event_id = 1;
  562. {
  563. char *tmp = strdupz(value);
  564. if(rrdvar_fix_name(tmp))
  565. error("Health configuration renamed alarm '%s' to '%s'", value, tmp);
  566. rc->name = string_strdupz(tmp);
  567. freez(tmp);
  568. }
  569. rc->source = health_source_file(line, filename);
  570. rc->green = NAN;
  571. rc->red = NAN;
  572. rc->value = NAN;
  573. rc->old_value = NAN;
  574. rc->delay_multiplier = 1.0;
  575. rc->old_status = RRDCALC_STATUS_UNINITIALIZED;
  576. rc->warn_repeat_every = host->health.health_default_warn_repeat_every;
  577. rc->crit_repeat_every = host->health.health_default_crit_repeat_every;
  578. if (alert_cfg)
  579. alert_config_free(alert_cfg);
  580. alert_cfg = callocz(1, sizeof(struct alert_config));
  581. alert_cfg->alarm = string_dup(rc->name);
  582. ignore_this = 0;
  583. } else {
  584. rc = NULL;
  585. }
  586. }
  587. else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
  588. if(rc) {
  589. // health_add_alarms_loop(host, rc, ignore_this) ;
  590. if(!alert_hash_and_store_config(rc->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  591. rrdcalc_free_unused_rrdcalc_loaded_from_config(rc);
  592. else
  593. rrdcalc_add_from_config(host, rc);
  594. rc = NULL;
  595. }
  596. if(rt) {
  597. if(!alert_hash_and_store_config(rt->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  598. rrdcalctemplate_free_unused_rrdcalctemplate_loaded_from_config(rt);
  599. else
  600. rrdcalctemplate_add_from_config(host, rt);
  601. }
  602. if (simple_pattern_matches(conf_enabled_alarms, value)) {
  603. rt = callocz(1, sizeof(RRDCALCTEMPLATE));
  604. {
  605. char *tmp = strdupz(value);
  606. if(rrdvar_fix_name(tmp))
  607. error("Health configuration renamed template '%s' to '%s'", value, tmp);
  608. rt->name = string_strdupz(tmp);
  609. freez(tmp);
  610. }
  611. rt->source = health_source_file(line, filename);
  612. rt->green = NAN;
  613. rt->red = NAN;
  614. rt->delay_multiplier = (float)1.0;
  615. rt->warn_repeat_every = host->health.health_default_warn_repeat_every;
  616. rt->crit_repeat_every = host->health.health_default_crit_repeat_every;
  617. if (alert_cfg)
  618. alert_config_free(alert_cfg);
  619. alert_cfg = callocz(1, sizeof(struct alert_config));
  620. alert_cfg->template_key = string_dup(rt->name);
  621. ignore_this = 0;
  622. } else {
  623. rt = NULL;
  624. }
  625. }
  626. else if(hash == hash_os && !strcasecmp(key, HEALTH_OS_KEY)) {
  627. char *os_match = value;
  628. if (alert_cfg) alert_cfg->os = string_strdupz(value);
  629. SIMPLE_PATTERN *os_pattern = simple_pattern_create(os_match, NULL, SIMPLE_PATTERN_EXACT, true);
  630. if(!simple_pattern_matches_string(os_pattern, host->os)) {
  631. if(rc)
  632. 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);
  633. if(rt)
  634. 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);
  635. ignore_this = 1;
  636. }
  637. simple_pattern_free(os_pattern);
  638. }
  639. else if(hash == hash_host && !strcasecmp(key, HEALTH_HOST_KEY)) {
  640. char *host_match = value;
  641. if (alert_cfg) alert_cfg->host = string_strdupz(value);
  642. SIMPLE_PATTERN *host_pattern = simple_pattern_create(host_match, NULL, SIMPLE_PATTERN_EXACT, true);
  643. if(!simple_pattern_matches_string(host_pattern, host->hostname)) {
  644. if(rc)
  645. 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);
  646. if(rt)
  647. 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);
  648. ignore_this = 1;
  649. }
  650. simple_pattern_free(host_pattern);
  651. }
  652. else if(rc) {
  653. if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
  654. alert_cfg->on = string_strdupz(value);
  655. if(rc->chart) {
  656. if(strcmp(rrdcalc_chart_name(rc), value) != 0)
  657. error("Health configuration at line %zu of file '%s' for alarm '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
  658. line, filename, rrdcalc_name(rc), key, rrdcalc_chart_name(rc), value, value);
  659. string_freez(rc->chart);
  660. }
  661. rc->chart = string_strdupz(value);
  662. }
  663. else if(hash == hash_class && !strcasecmp(key, HEALTH_CLASS_KEY)) {
  664. strip_quotes(value);
  665. alert_cfg->classification = string_strdupz(value);
  666. if(rc->classification) {
  667. if(strcmp(rrdcalc_classification(rc), value) != 0)
  668. 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').",
  669. line, filename, rrdcalc_name(rc), key, rrdcalc_classification(rc), value, value);
  670. string_freez(rc->classification);
  671. }
  672. rc->classification = string_strdupz(value);
  673. }
  674. else if(hash == hash_component && !strcasecmp(key, HEALTH_COMPONENT_KEY)) {
  675. strip_quotes(value);
  676. alert_cfg->component = string_strdupz(value);
  677. if(rc->component) {
  678. if(strcmp(rrdcalc_component(rc), value) != 0)
  679. 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').",
  680. line, filename, rrdcalc_name(rc), key, rrdcalc_component(rc), value, value);
  681. string_freez(rc->component);
  682. }
  683. rc->component = string_strdupz(value);
  684. }
  685. else if(hash == hash_type && !strcasecmp(key, HEALTH_TYPE_KEY)) {
  686. strip_quotes(value);
  687. alert_cfg->type = string_strdupz(value);
  688. if(rc->type) {
  689. if(strcmp(rrdcalc_type(rc), value) != 0)
  690. 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').",
  691. line, filename, rrdcalc_name(rc), key, rrdcalc_type(rc), value, value);
  692. string_freez(rc->type);
  693. }
  694. rc->type = string_strdupz(value);
  695. }
  696. else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
  697. alert_cfg->lookup = string_strdupz(value);
  698. health_parse_db_lookup(line, filename, value, &rc->group, &rc->after, &rc->before,
  699. &rc->update_every, &rc->options, &rc->dimensions, &rc->foreach_dimension);
  700. if(rc->foreach_dimension)
  701. rc->foreach_dimension_pattern = health_pattern_from_foreach(rrdcalc_foreachdim(rc));
  702. if (rc->after) {
  703. if (rc->dimensions)
  704. alert_cfg->p_db_lookup_dimensions = string_dup(rc->dimensions);
  705. if (rc->group)
  706. alert_cfg->p_db_lookup_method = string_strdupz(time_grouping_method2string(rc->group));
  707. alert_cfg->p_db_lookup_options = rc->options;
  708. alert_cfg->p_db_lookup_after = rc->after;
  709. alert_cfg->p_db_lookup_before = rc->before;
  710. alert_cfg->p_update_every = rc->update_every;
  711. }
  712. }
  713. else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
  714. alert_cfg->every = string_strdupz(value);
  715. if(!config_parse_duration(value, &rc->update_every))
  716. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' cannot parse duration: '%s'.",
  717. line, filename, rrdcalc_name(rc), key, value);
  718. alert_cfg->p_update_every = rc->update_every;
  719. }
  720. else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
  721. alert_cfg->green = string_strdupz(value);
  722. char *e;
  723. rc->green = str2ndd(value, &e);
  724. if(e && *e) {
  725. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
  726. line, filename, rrdcalc_name(rc), key, e);
  727. }
  728. }
  729. else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
  730. alert_cfg->red = string_strdupz(value);
  731. char *e;
  732. rc->red = str2ndd(value, &e);
  733. if(e && *e) {
  734. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
  735. line, filename, rrdcalc_name(rc), key, e);
  736. }
  737. }
  738. else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
  739. alert_cfg->calc = string_strdupz(value);
  740. const char *failed_at = NULL;
  741. int error = 0;
  742. rc->calculation = expression_parse(value, &failed_at, &error);
  743. if(!rc->calculation) {
  744. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  745. line, filename, rrdcalc_name(rc), key, value, expression_strerror(error), failed_at);
  746. }
  747. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  748. }
  749. else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
  750. alert_cfg->warn = string_strdupz(value);
  751. const char *failed_at = NULL;
  752. int error = 0;
  753. rc->warning = expression_parse(value, &failed_at, &error);
  754. if(!rc->warning) {
  755. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  756. line, filename, rrdcalc_name(rc), key, value, expression_strerror(error), failed_at);
  757. }
  758. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  759. }
  760. else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
  761. alert_cfg->crit = string_strdupz(value);
  762. const char *failed_at = NULL;
  763. int error = 0;
  764. rc->critical = expression_parse(value, &failed_at, &error);
  765. if(!rc->critical) {
  766. error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  767. line, filename, rrdcalc_name(rc), key, value, expression_strerror(error), failed_at);
  768. }
  769. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  770. }
  771. else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
  772. alert_cfg->exec = string_strdupz(value);
  773. if(rc->exec) {
  774. if(strcmp(rrdcalc_exec(rc), value) != 0)
  775. 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').",
  776. line, filename, rrdcalc_name(rc), key, rrdcalc_exec(rc), value, value);
  777. string_freez(rc->exec);
  778. }
  779. rc->exec = string_strdupz(value);
  780. }
  781. else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
  782. alert_cfg->to = string_strdupz(value);
  783. if(rc->recipient) {
  784. if(strcmp(rrdcalc_recipient(rc), value) != 0)
  785. 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').",
  786. line, filename, rrdcalc_name(rc), key, rrdcalc_recipient(rc), value, value);
  787. string_freez(rc->recipient);
  788. }
  789. rc->recipient = string_strdupz(value);
  790. }
  791. else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
  792. strip_quotes(value);
  793. alert_cfg->units = string_strdupz(value);
  794. if(rc->units) {
  795. if(strcmp(rrdcalc_units(rc), value) != 0)
  796. 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').",
  797. line, filename, rrdcalc_name(rc), key, rrdcalc_units(rc), value, value);
  798. string_freez(rc->units);
  799. }
  800. rc->units = string_strdupz(value);
  801. }
  802. else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
  803. strip_quotes(value);
  804. alert_cfg->info = string_strdupz(value);
  805. if(rc->info) {
  806. if(strcmp(rrdcalc_info(rc), value) != 0)
  807. 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').",
  808. line, filename, rrdcalc_name(rc), key, rrdcalc_info(rc), value, value);
  809. string_freez(rc->info);
  810. string_freez(rc->original_info);
  811. }
  812. rc->info = string_strdupz(value);
  813. rc->original_info = string_dup(rc->info);
  814. }
  815. else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
  816. alert_cfg->delay = string_strdupz(value);
  817. health_parse_delay(line, filename, value, &rc->delay_up_duration, &rc->delay_down_duration, &rc->delay_max_duration, &rc->delay_multiplier);
  818. }
  819. else if(hash == hash_options && !strcasecmp(key, HEALTH_OPTIONS_KEY)) {
  820. alert_cfg->options = string_strdupz(value);
  821. rc->options |= health_parse_options(value);
  822. }
  823. else if(hash == hash_repeat && !strcasecmp(key, HEALTH_REPEAT_KEY)){
  824. alert_cfg->repeat = string_strdupz(value);
  825. health_parse_repeat(line, filename, value,
  826. &rc->warn_repeat_every,
  827. &rc->crit_repeat_every);
  828. }
  829. else if(hash == hash_host_label && !strcasecmp(key, HEALTH_HOST_LABEL_KEY)) {
  830. alert_cfg->host_labels = string_strdupz(value);
  831. if(rc->host_labels) {
  832. if(strcmp(rrdcalc_host_labels(rc), value) != 0)
  833. 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'.",
  834. line, filename, rrdcalc_name(rc), key, value, value);
  835. string_freez(rc->host_labels);
  836. simple_pattern_free(rc->host_labels_pattern);
  837. }
  838. {
  839. char *tmp = simple_pattern_trim_around_equal(value);
  840. rc->host_labels = string_strdupz(tmp);
  841. freez(tmp);
  842. }
  843. rc->host_labels_pattern = simple_pattern_create(rrdcalc_host_labels(rc), NULL, SIMPLE_PATTERN_EXACT,
  844. true);
  845. }
  846. else if(hash == hash_plugin && !strcasecmp(key, HEALTH_PLUGIN_KEY)) {
  847. alert_cfg->plugin = string_strdupz(value);
  848. string_freez(rc->plugin_match);
  849. simple_pattern_free(rc->plugin_pattern);
  850. rc->plugin_match = string_strdupz(value);
  851. rc->plugin_pattern = simple_pattern_create(rrdcalc_plugin_match(rc), NULL, SIMPLE_PATTERN_EXACT, true);
  852. }
  853. else if(hash == hash_module && !strcasecmp(key, HEALTH_MODULE_KEY)) {
  854. alert_cfg->module = string_strdupz(value);
  855. string_freez(rc->module_match);
  856. simple_pattern_free(rc->module_pattern);
  857. rc->module_match = string_strdupz(value);
  858. rc->module_pattern = simple_pattern_create(rrdcalc_module_match(rc), NULL, SIMPLE_PATTERN_EXACT, true);
  859. }
  860. else if(hash == hash_chart_label && !strcasecmp(key, HEALTH_CHART_LABEL_KEY)) {
  861. alert_cfg->chart_labels = string_strdupz(value);
  862. if(rc->chart_labels) {
  863. if(strcmp(rrdcalc_chart_labels(rc), value) != 0)
  864. 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'.",
  865. line, filename, rrdcalc_name(rc), key, value, value);
  866. string_freez(rc->chart_labels);
  867. simple_pattern_free(rc->chart_labels_pattern);
  868. }
  869. {
  870. char *tmp = simple_pattern_trim_around_equal(value);
  871. char *tmp_2 = health_config_add_key_to_values(tmp);
  872. rc->chart_labels = string_strdupz(tmp_2);
  873. freez(tmp);
  874. freez(tmp_2);
  875. }
  876. rc->chart_labels_pattern = simple_pattern_create(rrdcalc_chart_labels(rc), NULL, SIMPLE_PATTERN_EXACT,
  877. true);
  878. }
  879. else {
  880. error("Health configuration at line %zu of file '%s' for alarm '%s' has unknown key '%s'.",
  881. line, filename, rrdcalc_name(rc), key);
  882. }
  883. }
  884. else if(rt) {
  885. if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
  886. alert_cfg->on = string_strdupz(value);
  887. if(rt->context) {
  888. if(strcmp(string2str(rt->context), value) != 0)
  889. 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').",
  890. line, filename, rrdcalctemplate_name(rt), key, string2str(rt->context), value, value);
  891. string_freez(rt->context);
  892. }
  893. rt->context = string_strdupz(value);
  894. }
  895. else if(hash == hash_class && !strcasecmp(key, HEALTH_CLASS_KEY)) {
  896. strip_quotes(value);
  897. alert_cfg->classification = string_strdupz(value);
  898. if(rt->classification) {
  899. if(strcmp(rrdcalctemplate_classification(rt), value) != 0)
  900. 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').",
  901. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_classification(rt), value, value);
  902. string_freez(rt->classification);
  903. }
  904. rt->classification = string_strdupz(value);
  905. }
  906. else if(hash == hash_component && !strcasecmp(key, HEALTH_COMPONENT_KEY)) {
  907. strip_quotes(value);
  908. alert_cfg->component = string_strdupz(value);
  909. if(rt->component) {
  910. if(strcmp(rrdcalctemplate_component(rt), value) != 0)
  911. 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').",
  912. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_component(rt), value, value);
  913. string_freez(rt->component);
  914. }
  915. rt->component = string_strdupz(value);
  916. }
  917. else if(hash == hash_type && !strcasecmp(key, HEALTH_TYPE_KEY)) {
  918. strip_quotes(value);
  919. alert_cfg->type = string_strdupz(value);
  920. if(rt->type) {
  921. if(strcmp(rrdcalctemplate_type(rt), value) != 0)
  922. 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').",
  923. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_type(rt), value, value);
  924. string_freez(rt->type);
  925. }
  926. rt->type = string_strdupz(value);
  927. }
  928. else if(hash == hash_families && !strcasecmp(key, HEALTH_FAMILIES_KEY)) {
  929. alert_cfg->families = string_strdupz(value);
  930. string_freez(rt->family_match);
  931. simple_pattern_free(rt->family_pattern);
  932. rt->family_match = string_strdupz(value);
  933. rt->family_pattern = simple_pattern_create(rrdcalctemplate_family_match(rt), NULL, SIMPLE_PATTERN_EXACT,
  934. true);
  935. }
  936. else if(hash == hash_plugin && !strcasecmp(key, HEALTH_PLUGIN_KEY)) {
  937. alert_cfg->plugin = string_strdupz(value);
  938. string_freez(rt->plugin_match);
  939. simple_pattern_free(rt->plugin_pattern);
  940. rt->plugin_match = string_strdupz(value);
  941. rt->plugin_pattern = simple_pattern_create(rrdcalctemplate_plugin_match(rt), NULL, SIMPLE_PATTERN_EXACT,
  942. true);
  943. }
  944. else if(hash == hash_module && !strcasecmp(key, HEALTH_MODULE_KEY)) {
  945. alert_cfg->module = string_strdupz(value);
  946. string_freez(rt->module_match);
  947. simple_pattern_free(rt->module_pattern);
  948. rt->module_match = string_strdupz(value);
  949. rt->module_pattern = simple_pattern_create(rrdcalctemplate_module_match(rt), NULL, SIMPLE_PATTERN_EXACT,
  950. true);
  951. }
  952. else if(hash == hash_charts && !strcasecmp(key, HEALTH_CHARTS_KEY)) {
  953. alert_cfg->charts = string_strdupz(value);
  954. string_freez(rt->charts_match);
  955. simple_pattern_free(rt->charts_pattern);
  956. rt->charts_match = string_strdupz(value);
  957. rt->charts_pattern = simple_pattern_create(rrdcalctemplate_charts_match(rt), NULL, SIMPLE_PATTERN_EXACT,
  958. true);
  959. }
  960. else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
  961. alert_cfg->lookup = string_strdupz(value);
  962. health_parse_db_lookup(line, filename, value, &rt->group, &rt->after, &rt->before,
  963. &rt->update_every, &rt->options, &rt->dimensions, &rt->foreach_dimension);
  964. if(rt->foreach_dimension)
  965. rt->foreach_dimension_pattern = health_pattern_from_foreach(rrdcalctemplate_foreachdim(rt));
  966. if (rt->after) {
  967. if (rt->dimensions)
  968. alert_cfg->p_db_lookup_dimensions = string_dup(rt->dimensions);
  969. if (rt->group)
  970. alert_cfg->p_db_lookup_method = string_strdupz(time_grouping_method2string(rt->group));
  971. alert_cfg->p_db_lookup_options = rt->options;
  972. alert_cfg->p_db_lookup_after = rt->after;
  973. alert_cfg->p_db_lookup_before = rt->before;
  974. alert_cfg->p_update_every = rt->update_every;
  975. }
  976. }
  977. else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
  978. alert_cfg->every = string_strdupz(value);
  979. if(!config_parse_duration(value, &rt->update_every))
  980. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' cannot parse duration: '%s'.",
  981. line, filename, rrdcalctemplate_name(rt), key, value);
  982. alert_cfg->p_update_every = rt->update_every;
  983. }
  984. else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
  985. alert_cfg->green = string_strdupz(value);
  986. char *e;
  987. rt->green = str2ndd(value, &e);
  988. if(e && *e) {
  989. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
  990. line, filename, rrdcalctemplate_name(rt), key, e);
  991. }
  992. }
  993. else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
  994. alert_cfg->red = string_strdupz(value);
  995. char *e;
  996. rt->red = str2ndd(value, &e);
  997. if(e && *e) {
  998. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
  999. line, filename, rrdcalctemplate_name(rt), key, e);
  1000. }
  1001. }
  1002. else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
  1003. alert_cfg->calc = string_strdupz(value);
  1004. const char *failed_at = NULL;
  1005. int error = 0;
  1006. rt->calculation = expression_parse(value, &failed_at, &error);
  1007. if(!rt->calculation) {
  1008. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  1009. line, filename, rrdcalctemplate_name(rt), key, value, expression_strerror(error), failed_at);
  1010. }
  1011. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  1012. }
  1013. else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
  1014. alert_cfg->warn = string_strdupz(value);
  1015. const char *failed_at = NULL;
  1016. int error = 0;
  1017. rt->warning = expression_parse(value, &failed_at, &error);
  1018. if(!rt->warning) {
  1019. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  1020. line, filename, rrdcalctemplate_name(rt), key, value, expression_strerror(error), failed_at);
  1021. }
  1022. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  1023. }
  1024. else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
  1025. alert_cfg->crit = string_strdupz(value);
  1026. const char *failed_at = NULL;
  1027. int error = 0;
  1028. rt->critical = expression_parse(value, &failed_at, &error);
  1029. if(!rt->critical) {
  1030. error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
  1031. line, filename, rrdcalctemplate_name(rt), key, value, expression_strerror(error), failed_at);
  1032. }
  1033. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  1034. }
  1035. else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
  1036. alert_cfg->exec = string_strdupz(value);
  1037. if(rt->exec) {
  1038. if(strcmp(rrdcalctemplate_exec(rt), value) != 0)
  1039. 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').",
  1040. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_exec(rt), value, value);
  1041. string_freez(rt->exec);
  1042. }
  1043. rt->exec = string_strdupz(value);
  1044. }
  1045. else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
  1046. alert_cfg->to = string_strdupz(value);
  1047. if(rt->recipient) {
  1048. if(strcmp(rrdcalctemplate_recipient(rt), value) != 0)
  1049. 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').",
  1050. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_recipient(rt), value, value);
  1051. string_freez(rt->recipient);
  1052. }
  1053. rt->recipient = string_strdupz(value);
  1054. }
  1055. else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
  1056. strip_quotes(value);
  1057. alert_cfg->units = string_strdupz(value);
  1058. if(rt->units) {
  1059. if(strcmp(rrdcalctemplate_units(rt), value) != 0)
  1060. 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').",
  1061. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_units(rt), value, value);
  1062. string_freez(rt->units);
  1063. }
  1064. rt->units = string_strdupz(value);
  1065. }
  1066. else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
  1067. strip_quotes(value);
  1068. alert_cfg->info = string_strdupz(value);
  1069. if(rt->info) {
  1070. if(strcmp(rrdcalctemplate_info(rt), value) != 0)
  1071. 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').",
  1072. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_info(rt), value, value);
  1073. string_freez(rt->info);
  1074. }
  1075. rt->info = string_strdupz(value);
  1076. }
  1077. else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
  1078. alert_cfg->delay = string_strdupz(value);
  1079. health_parse_delay(line, filename, value, &rt->delay_up_duration, &rt->delay_down_duration, &rt->delay_max_duration, &rt->delay_multiplier);
  1080. }
  1081. else if(hash == hash_options && !strcasecmp(key, HEALTH_OPTIONS_KEY)) {
  1082. alert_cfg->options = string_strdupz(value);
  1083. rt->options |= health_parse_options(value);
  1084. }
  1085. else if(hash == hash_repeat && !strcasecmp(key, HEALTH_REPEAT_KEY)){
  1086. alert_cfg->repeat = string_strdupz(value);
  1087. health_parse_repeat(line, filename, value,
  1088. &rt->warn_repeat_every,
  1089. &rt->crit_repeat_every);
  1090. }
  1091. else if(hash == hash_host_label && !strcasecmp(key, HEALTH_HOST_LABEL_KEY)) {
  1092. alert_cfg->host_labels = string_strdupz(value);
  1093. if(rt->host_labels) {
  1094. if(strcmp(rrdcalctemplate_host_labels(rt), value) != 0)
  1095. 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').",
  1096. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_host_labels(rt), value, value);
  1097. string_freez(rt->host_labels);
  1098. simple_pattern_free(rt->host_labels_pattern);
  1099. }
  1100. {
  1101. char *tmp = simple_pattern_trim_around_equal(value);
  1102. rt->host_labels = string_strdupz(tmp);
  1103. freez(tmp);
  1104. }
  1105. rt->host_labels_pattern = simple_pattern_create(rrdcalctemplate_host_labels(rt), NULL,
  1106. SIMPLE_PATTERN_EXACT, true);
  1107. }
  1108. else if(hash == hash_chart_label && !strcasecmp(key, HEALTH_CHART_LABEL_KEY)) {
  1109. alert_cfg->chart_labels = string_strdupz(value);
  1110. if(rt->chart_labels) {
  1111. if(strcmp(rrdcalctemplate_chart_labels(rt), value) != 0)
  1112. 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').",
  1113. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_chart_labels(rt), value, value);
  1114. string_freez(rt->chart_labels);
  1115. simple_pattern_free(rt->chart_labels_pattern);
  1116. }
  1117. {
  1118. char *tmp = simple_pattern_trim_around_equal(value);
  1119. char *tmp_2 = health_config_add_key_to_values(tmp);
  1120. rt->chart_labels = string_strdupz(tmp_2);
  1121. freez(tmp);
  1122. freez(tmp_2);
  1123. }
  1124. rt->chart_labels_pattern = simple_pattern_create(rrdcalctemplate_chart_labels(rt), NULL,
  1125. SIMPLE_PATTERN_EXACT, true);
  1126. }
  1127. else {
  1128. error("Health configuration at line %zu of file '%s' for template '%s' has unknown key '%s'.",
  1129. line, filename, rrdcalctemplate_name(rt), key);
  1130. }
  1131. }
  1132. else {
  1133. error("Health configuration at line %zu of file '%s' has unknown key '%s'. Expected either '" HEALTH_ALARM_KEY "' or '" HEALTH_TEMPLATE_KEY "'.",
  1134. line, filename, key);
  1135. }
  1136. }
  1137. if(rc) {
  1138. //health_add_alarms_loop(host, rc, ignore_this) ;
  1139. if(!alert_hash_and_store_config(rc->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  1140. rrdcalc_free_unused_rrdcalc_loaded_from_config(rc);
  1141. else
  1142. rrdcalc_add_from_config(host, rc);
  1143. }
  1144. if(rt) {
  1145. if(!alert_hash_and_store_config(rt->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  1146. rrdcalctemplate_free_unused_rrdcalctemplate_loaded_from_config(rt);
  1147. else
  1148. rrdcalctemplate_add_from_config(host, rt);
  1149. }
  1150. if (alert_cfg)
  1151. alert_config_free(alert_cfg);
  1152. fclose(fp);
  1153. return 1;
  1154. }
  1155. void sql_refresh_hashes(void)
  1156. {
  1157. sql_store_hashes = 1;
  1158. }
  1159. void health_readdir(RRDHOST *host, const char *user_path, const char *stock_path, const char *subpath) {
  1160. if(unlikely((!host->health.health_enabled) && !rrdhost_flag_check(host, RRDHOST_FLAG_INITIALIZED_HEALTH)) ||
  1161. !service_running(SERVICE_HEALTH)) {
  1162. debug(D_HEALTH, "CONFIG health is not enabled for host '%s'", rrdhost_hostname(host));
  1163. return;
  1164. }
  1165. int stock_enabled = (int)config_get_boolean(CONFIG_SECTION_HEALTH, "enable stock health configuration",
  1166. CONFIG_BOOLEAN_YES);
  1167. if (!stock_enabled) {
  1168. log_health("[%s]: Netdata will not load stock alarms.", rrdhost_hostname(host));
  1169. stock_path = user_path;
  1170. }
  1171. if (!health_rrdvars)
  1172. health_rrdvars = health_rrdvariables_create();
  1173. recursive_config_double_dir_load(user_path, stock_path, subpath, health_readfile, (void *) host, 0);
  1174. log_health("[%s]: Read health configuration.", rrdhost_hostname(host));
  1175. sql_store_hashes = 0;
  1176. }