health_config.c 46 KB

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