health_config.c 52 KB

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