health_config.c 52 KB

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