health_config.c 59 KB

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