health_config.c 50 KB

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