health_config.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  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. size_t line = 0, append = 0;
  508. char *s;
  509. while((s = fgets(&buffer[append], (int)(HEALTH_CONF_MAX_LINE - append), fp)) || append) {
  510. int stop_appending = !s;
  511. line++;
  512. s = trim(buffer);
  513. if(!s || *s == '#') continue;
  514. append = strlen(s);
  515. if(!stop_appending && s[append - 1] == '\\') {
  516. s[append - 1] = ' ';
  517. append = &s[append] - buffer;
  518. if(append < HEALTH_CONF_MAX_LINE)
  519. continue;
  520. else {
  521. netdata_log_error("Health configuration has too long multi-line at line %zu of file '%s'.",
  522. line, filename);
  523. }
  524. }
  525. append = 0;
  526. char *key = s;
  527. while(*s && *s != ':') s++;
  528. if(!*s) {
  529. netdata_log_error("Health configuration has invalid line %zu of file '%s'. It does not contain a ':'. Ignoring it.",
  530. line, filename);
  531. continue;
  532. }
  533. *s = '\0';
  534. s++;
  535. char *value = s;
  536. key = trim_all(key);
  537. value = trim_all(value);
  538. if(!key) {
  539. netdata_log_error("Health configuration has invalid line %zu of file '%s'. Keyword is empty. Ignoring it.",
  540. line, filename);
  541. continue;
  542. }
  543. if(!value) {
  544. netdata_log_error("Health configuration has invalid line %zu of file '%s'. value is empty. Ignoring it.",
  545. line, filename);
  546. continue;
  547. }
  548. uint32_t hash = simple_uhash(key);
  549. if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
  550. if(rc) {
  551. if(!alert_hash_and_store_config(rc->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  552. rrdcalc_free_unused_rrdcalc_loaded_from_config(rc);
  553. else
  554. rrdcalc_add_from_config(host, rc);
  555. // health_add_alarms_loop(host, rc, ignore_this) ;
  556. }
  557. if(rt) {
  558. if(!alert_hash_and_store_config(rt->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  559. rrdcalctemplate_free_unused_rrdcalctemplate_loaded_from_config(rt);
  560. else
  561. rrdcalctemplate_add_from_config(host, rt);
  562. rt = NULL;
  563. }
  564. if (simple_pattern_matches(conf_enabled_alarms, value)) {
  565. rc = callocz(1, sizeof(RRDCALC));
  566. rc->next_event_id = 1;
  567. {
  568. char *tmp = strdupz(value);
  569. if(rrdvar_fix_name(tmp))
  570. netdata_log_error("Health configuration renamed alarm '%s' to '%s'", value, tmp);
  571. rc->name = string_strdupz(tmp);
  572. freez(tmp);
  573. }
  574. rc->source = health_source_file(line, filename);
  575. rc->green = NAN;
  576. rc->red = NAN;
  577. rc->value = NAN;
  578. rc->old_value = NAN;
  579. rc->delay_multiplier = 1.0;
  580. rc->old_status = RRDCALC_STATUS_UNINITIALIZED;
  581. rc->warn_repeat_every = host->health.health_default_warn_repeat_every;
  582. rc->crit_repeat_every = host->health.health_default_crit_repeat_every;
  583. if (alert_cfg)
  584. alert_config_free(alert_cfg);
  585. alert_cfg = callocz(1, sizeof(struct alert_config));
  586. alert_cfg->alarm = string_dup(rc->name);
  587. alert_cfg->source = health_source_file(line, filename);
  588. ignore_this = 0;
  589. } else {
  590. rc = NULL;
  591. }
  592. }
  593. else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
  594. if(rc) {
  595. // health_add_alarms_loop(host, rc, ignore_this) ;
  596. if(!alert_hash_and_store_config(rc->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  597. rrdcalc_free_unused_rrdcalc_loaded_from_config(rc);
  598. else
  599. rrdcalc_add_from_config(host, rc);
  600. rc = NULL;
  601. }
  602. if(rt) {
  603. if(!alert_hash_and_store_config(rt->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  604. rrdcalctemplate_free_unused_rrdcalctemplate_loaded_from_config(rt);
  605. else
  606. rrdcalctemplate_add_from_config(host, rt);
  607. }
  608. if (simple_pattern_matches(conf_enabled_alarms, value)) {
  609. rt = callocz(1, sizeof(RRDCALCTEMPLATE));
  610. {
  611. char *tmp = strdupz(value);
  612. if(rrdvar_fix_name(tmp))
  613. netdata_log_error("Health configuration renamed template '%s' to '%s'", value, tmp);
  614. rt->name = string_strdupz(tmp);
  615. freez(tmp);
  616. }
  617. rt->source = health_source_file(line, filename);
  618. rt->green = NAN;
  619. rt->red = NAN;
  620. rt->delay_multiplier = (float)1.0;
  621. rt->warn_repeat_every = host->health.health_default_warn_repeat_every;
  622. rt->crit_repeat_every = host->health.health_default_crit_repeat_every;
  623. if (alert_cfg)
  624. alert_config_free(alert_cfg);
  625. alert_cfg = callocz(1, sizeof(struct alert_config));
  626. alert_cfg->template_key = string_dup(rt->name);
  627. alert_cfg->source = health_source_file(line, filename);
  628. ignore_this = 0;
  629. } else {
  630. rt = NULL;
  631. }
  632. }
  633. else if(hash == hash_os && !strcasecmp(key, HEALTH_OS_KEY)) {
  634. char *os_match = value;
  635. if (alert_cfg) alert_cfg->os = string_strdupz(value);
  636. SIMPLE_PATTERN *os_pattern = simple_pattern_create(os_match, NULL, SIMPLE_PATTERN_EXACT, true);
  637. if(!simple_pattern_matches_string(os_pattern, host->os)) {
  638. if(rc)
  639. 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);
  640. if(rt)
  641. 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);
  642. ignore_this = 1;
  643. }
  644. simple_pattern_free(os_pattern);
  645. }
  646. else if(hash == hash_host && !strcasecmp(key, HEALTH_HOST_KEY)) {
  647. char *host_match = value;
  648. if (alert_cfg) alert_cfg->host = string_strdupz(value);
  649. SIMPLE_PATTERN *host_pattern = simple_pattern_create(host_match, NULL, SIMPLE_PATTERN_EXACT, true);
  650. if(!simple_pattern_matches_string(host_pattern, host->hostname)) {
  651. if(rc)
  652. 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);
  653. if(rt)
  654. 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);
  655. ignore_this = 1;
  656. }
  657. simple_pattern_free(host_pattern);
  658. }
  659. else if(rc) {
  660. if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
  661. alert_cfg->on = string_strdupz(value);
  662. if(rc->chart) {
  663. if(strcmp(rrdcalc_chart_name(rc), value) != 0)
  664. 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').",
  665. line, filename, rrdcalc_name(rc), key, rrdcalc_chart_name(rc), value, value);
  666. string_freez(rc->chart);
  667. }
  668. rc->chart = string_strdupz(value);
  669. }
  670. else if(hash == hash_class && !strcasecmp(key, HEALTH_CLASS_KEY)) {
  671. strip_quotes(value);
  672. alert_cfg->classification = string_strdupz(value);
  673. if(rc->classification) {
  674. if(strcmp(rrdcalc_classification(rc), value) != 0)
  675. 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').",
  676. line, filename, rrdcalc_name(rc), key, rrdcalc_classification(rc), value, value);
  677. string_freez(rc->classification);
  678. }
  679. rc->classification = string_strdupz(value);
  680. }
  681. else if(hash == hash_component && !strcasecmp(key, HEALTH_COMPONENT_KEY)) {
  682. strip_quotes(value);
  683. alert_cfg->component = string_strdupz(value);
  684. if(rc->component) {
  685. if(strcmp(rrdcalc_component(rc), value) != 0)
  686. 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').",
  687. line, filename, rrdcalc_name(rc), key, rrdcalc_component(rc), value, value);
  688. string_freez(rc->component);
  689. }
  690. rc->component = string_strdupz(value);
  691. }
  692. else if(hash == hash_type && !strcasecmp(key, HEALTH_TYPE_KEY)) {
  693. strip_quotes(value);
  694. alert_cfg->type = string_strdupz(value);
  695. if(rc->type) {
  696. if(strcmp(rrdcalc_type(rc), value) != 0)
  697. 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').",
  698. line, filename, rrdcalc_name(rc), key, rrdcalc_type(rc), value, value);
  699. string_freez(rc->type);
  700. }
  701. rc->type = string_strdupz(value);
  702. }
  703. else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
  704. alert_cfg->lookup = string_strdupz(value);
  705. health_parse_db_lookup(line, filename, value, &rc->group, &rc->after, &rc->before,
  706. &rc->update_every, &rc->options, &rc->dimensions, &rc->foreach_dimension);
  707. if(rc->foreach_dimension)
  708. rc->foreach_dimension_pattern = health_pattern_from_foreach(rrdcalc_foreachdim(rc));
  709. if (rc->after) {
  710. if (rc->dimensions)
  711. alert_cfg->p_db_lookup_dimensions = string_dup(rc->dimensions);
  712. if (rc->group)
  713. alert_cfg->p_db_lookup_method = string_strdupz(time_grouping_method2string(rc->group));
  714. alert_cfg->p_db_lookup_options = rc->options;
  715. alert_cfg->p_db_lookup_after = rc->after;
  716. alert_cfg->p_db_lookup_before = rc->before;
  717. alert_cfg->p_update_every = rc->update_every;
  718. }
  719. }
  720. else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
  721. alert_cfg->every = string_strdupz(value);
  722. if(!config_parse_duration(value, &rc->update_every))
  723. netdata_log_error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' cannot parse duration: '%s'.",
  724. line, filename, rrdcalc_name(rc), key, value);
  725. alert_cfg->p_update_every = rc->update_every;
  726. }
  727. else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
  728. alert_cfg->green = string_strdupz(value);
  729. char *e;
  730. rc->green = str2ndd(value, &e);
  731. if(e && *e) {
  732. netdata_log_error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
  733. line, filename, rrdcalc_name(rc), key, e);
  734. }
  735. }
  736. else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
  737. alert_cfg->red = string_strdupz(value);
  738. char *e;
  739. rc->red = str2ndd(value, &e);
  740. if(e && *e) {
  741. netdata_log_error("Health configuration at line %zu of file '%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
  742. line, filename, rrdcalc_name(rc), key, e);
  743. }
  744. }
  745. else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
  746. alert_cfg->calc = string_strdupz(value);
  747. const char *failed_at = NULL;
  748. int error = 0;
  749. rc->calculation = expression_parse(value, &failed_at, &error);
  750. if(!rc->calculation) {
  751. 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'",
  752. line, filename, rrdcalc_name(rc), key, value, expression_strerror(error), failed_at);
  753. }
  754. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  755. }
  756. else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
  757. alert_cfg->warn = string_strdupz(value);
  758. const char *failed_at = NULL;
  759. int error = 0;
  760. rc->warning = expression_parse(value, &failed_at, &error);
  761. if(!rc->warning) {
  762. 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'",
  763. line, filename, rrdcalc_name(rc), key, value, expression_strerror(error), failed_at);
  764. }
  765. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  766. }
  767. else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
  768. alert_cfg->crit = string_strdupz(value);
  769. const char *failed_at = NULL;
  770. int error = 0;
  771. rc->critical = expression_parse(value, &failed_at, &error);
  772. if(!rc->critical) {
  773. 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'",
  774. line, filename, rrdcalc_name(rc), key, value, expression_strerror(error), failed_at);
  775. }
  776. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  777. }
  778. else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
  779. alert_cfg->exec = string_strdupz(value);
  780. if(rc->exec) {
  781. if(strcmp(rrdcalc_exec(rc), value) != 0)
  782. 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').",
  783. line, filename, rrdcalc_name(rc), key, rrdcalc_exec(rc), value, value);
  784. string_freez(rc->exec);
  785. }
  786. rc->exec = string_strdupz(value);
  787. }
  788. else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
  789. alert_cfg->to = string_strdupz(value);
  790. if(rc->recipient) {
  791. if(strcmp(rrdcalc_recipient(rc), value) != 0)
  792. 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').",
  793. line, filename, rrdcalc_name(rc), key, rrdcalc_recipient(rc), value, value);
  794. string_freez(rc->recipient);
  795. }
  796. rc->recipient = string_strdupz(value);
  797. }
  798. else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
  799. strip_quotes(value);
  800. alert_cfg->units = string_strdupz(value);
  801. if(rc->units) {
  802. if(strcmp(rrdcalc_units(rc), value) != 0)
  803. 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').",
  804. line, filename, rrdcalc_name(rc), key, rrdcalc_units(rc), value, value);
  805. string_freez(rc->units);
  806. }
  807. rc->units = string_strdupz(value);
  808. }
  809. else if(hash == hash_summary && !strcasecmp(key, HEALTH_SUMMARY_KEY)) {
  810. strip_quotes(value);
  811. alert_cfg->summary = string_strdupz(value);
  812. if(rc->summary) {
  813. if(strcmp(rrdcalc_summary(rc), value) != 0)
  814. 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').",
  815. line, filename, rrdcalc_name(rc), key, rrdcalc_summary(rc), value, value);
  816. string_freez(rc->summary);
  817. string_freez(rc->original_summary);
  818. }
  819. rc->summary = string_strdupz(value);
  820. rc->original_summary = string_dup(rc->summary);
  821. }
  822. else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
  823. strip_quotes(value);
  824. alert_cfg->info = string_strdupz(value);
  825. if(rc->info) {
  826. if(strcmp(rrdcalc_info(rc), value) != 0)
  827. 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').",
  828. line, filename, rrdcalc_name(rc), key, rrdcalc_info(rc), value, value);
  829. string_freez(rc->info);
  830. string_freez(rc->original_info);
  831. }
  832. rc->info = string_strdupz(value);
  833. rc->original_info = string_dup(rc->info);
  834. }
  835. else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
  836. alert_cfg->delay = string_strdupz(value);
  837. health_parse_delay(line, filename, value, &rc->delay_up_duration, &rc->delay_down_duration, &rc->delay_max_duration, &rc->delay_multiplier);
  838. }
  839. else if(hash == hash_options && !strcasecmp(key, HEALTH_OPTIONS_KEY)) {
  840. alert_cfg->options = string_strdupz(value);
  841. rc->options |= health_parse_options(value);
  842. }
  843. else if(hash == hash_repeat && !strcasecmp(key, HEALTH_REPEAT_KEY)){
  844. alert_cfg->repeat = string_strdupz(value);
  845. health_parse_repeat(line, filename, value,
  846. &rc->warn_repeat_every,
  847. &rc->crit_repeat_every);
  848. }
  849. else if(hash == hash_host_label && !strcasecmp(key, HEALTH_HOST_LABEL_KEY)) {
  850. alert_cfg->host_labels = string_strdupz(value);
  851. if(rc->host_labels) {
  852. if(strcmp(rrdcalc_host_labels(rc), value) != 0)
  853. 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'.",
  854. line, filename, rrdcalc_name(rc), key, value, value);
  855. string_freez(rc->host_labels);
  856. simple_pattern_free(rc->host_labels_pattern);
  857. }
  858. {
  859. char *tmp = simple_pattern_trim_around_equal(value);
  860. rc->host_labels = string_strdupz(tmp);
  861. freez(tmp);
  862. }
  863. rc->host_labels_pattern = simple_pattern_create(rrdcalc_host_labels(rc), NULL, SIMPLE_PATTERN_EXACT,
  864. true);
  865. }
  866. else if(hash == hash_plugin && !strcasecmp(key, HEALTH_PLUGIN_KEY)) {
  867. alert_cfg->plugin = string_strdupz(value);
  868. string_freez(rc->plugin_match);
  869. simple_pattern_free(rc->plugin_pattern);
  870. rc->plugin_match = string_strdupz(value);
  871. rc->plugin_pattern = simple_pattern_create(rrdcalc_plugin_match(rc), NULL, SIMPLE_PATTERN_EXACT, true);
  872. }
  873. else if(hash == hash_module && !strcasecmp(key, HEALTH_MODULE_KEY)) {
  874. alert_cfg->module = string_strdupz(value);
  875. string_freez(rc->module_match);
  876. simple_pattern_free(rc->module_pattern);
  877. rc->module_match = string_strdupz(value);
  878. rc->module_pattern = simple_pattern_create(rrdcalc_module_match(rc), NULL, SIMPLE_PATTERN_EXACT, true);
  879. }
  880. else if(hash == hash_chart_label && !strcasecmp(key, HEALTH_CHART_LABEL_KEY)) {
  881. alert_cfg->chart_labels = string_strdupz(value);
  882. if(rc->chart_labels) {
  883. if(strcmp(rrdcalc_chart_labels(rc), value) != 0)
  884. 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'.",
  885. line, filename, rrdcalc_name(rc), key, value, value);
  886. string_freez(rc->chart_labels);
  887. simple_pattern_free(rc->chart_labels_pattern);
  888. }
  889. {
  890. char *tmp = simple_pattern_trim_around_equal(value);
  891. char *tmp_2 = health_config_add_key_to_values(tmp);
  892. rc->chart_labels = string_strdupz(tmp_2);
  893. freez(tmp);
  894. freez(tmp_2);
  895. }
  896. rc->chart_labels_pattern = simple_pattern_create(rrdcalc_chart_labels(rc), NULL, SIMPLE_PATTERN_EXACT,
  897. true);
  898. }
  899. else {
  900. // "families" has become obsolete and has been removed from standard alarms, but some still have it:
  901. // alarms of obsolete collectors (e.g. fping, wmi).
  902. if (strcmp(key, "families"))
  903. netdata_log_error(
  904. "Health configuration at line %zu of file '%s' for alarm '%s' has unknown key '%s'.",
  905. line,
  906. filename,
  907. rrdcalc_name(rc),
  908. key);
  909. }
  910. }
  911. else if(rt) {
  912. if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
  913. alert_cfg->on = string_strdupz(value);
  914. if(rt->context) {
  915. if(strcmp(string2str(rt->context), value) != 0)
  916. 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').",
  917. line, filename, rrdcalctemplate_name(rt), key, string2str(rt->context), value, value);
  918. string_freez(rt->context);
  919. }
  920. rt->context = string_strdupz(value);
  921. }
  922. else if(hash == hash_class && !strcasecmp(key, HEALTH_CLASS_KEY)) {
  923. strip_quotes(value);
  924. alert_cfg->classification = string_strdupz(value);
  925. if(rt->classification) {
  926. if(strcmp(rrdcalctemplate_classification(rt), value) != 0)
  927. 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').",
  928. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_classification(rt), value, value);
  929. string_freez(rt->classification);
  930. }
  931. rt->classification = string_strdupz(value);
  932. }
  933. else if(hash == hash_component && !strcasecmp(key, HEALTH_COMPONENT_KEY)) {
  934. strip_quotes(value);
  935. alert_cfg->component = string_strdupz(value);
  936. if(rt->component) {
  937. if(strcmp(rrdcalctemplate_component(rt), value) != 0)
  938. 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').",
  939. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_component(rt), value, value);
  940. string_freez(rt->component);
  941. }
  942. rt->component = string_strdupz(value);
  943. }
  944. else if(hash == hash_type && !strcasecmp(key, HEALTH_TYPE_KEY)) {
  945. strip_quotes(value);
  946. alert_cfg->type = string_strdupz(value);
  947. if(rt->type) {
  948. if(strcmp(rrdcalctemplate_type(rt), value) != 0)
  949. 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').",
  950. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_type(rt), value, value);
  951. string_freez(rt->type);
  952. }
  953. rt->type = string_strdupz(value);
  954. }
  955. else if(hash == hash_plugin && !strcasecmp(key, HEALTH_PLUGIN_KEY)) {
  956. alert_cfg->plugin = string_strdupz(value);
  957. string_freez(rt->plugin_match);
  958. simple_pattern_free(rt->plugin_pattern);
  959. rt->plugin_match = string_strdupz(value);
  960. rt->plugin_pattern = simple_pattern_create(rrdcalctemplate_plugin_match(rt), NULL, SIMPLE_PATTERN_EXACT,
  961. true);
  962. }
  963. else if(hash == hash_module && !strcasecmp(key, HEALTH_MODULE_KEY)) {
  964. alert_cfg->module = string_strdupz(value);
  965. string_freez(rt->module_match);
  966. simple_pattern_free(rt->module_pattern);
  967. rt->module_match = string_strdupz(value);
  968. rt->module_pattern = simple_pattern_create(rrdcalctemplate_module_match(rt), NULL, SIMPLE_PATTERN_EXACT,
  969. true);
  970. }
  971. else if(hash == hash_charts && !strcasecmp(key, HEALTH_CHARTS_KEY)) {
  972. alert_cfg->charts = string_strdupz(value);
  973. string_freez(rt->charts_match);
  974. simple_pattern_free(rt->charts_pattern);
  975. rt->charts_match = string_strdupz(value);
  976. rt->charts_pattern = simple_pattern_create(rrdcalctemplate_charts_match(rt), NULL, SIMPLE_PATTERN_EXACT,
  977. true);
  978. }
  979. else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
  980. alert_cfg->lookup = string_strdupz(value);
  981. health_parse_db_lookup(line, filename, value, &rt->group, &rt->after, &rt->before,
  982. &rt->update_every, &rt->options, &rt->dimensions, &rt->foreach_dimension);
  983. if(rt->foreach_dimension)
  984. rt->foreach_dimension_pattern = health_pattern_from_foreach(rrdcalctemplate_foreachdim(rt));
  985. if (rt->after) {
  986. if (rt->dimensions)
  987. alert_cfg->p_db_lookup_dimensions = string_dup(rt->dimensions);
  988. if (rt->group)
  989. alert_cfg->p_db_lookup_method = string_strdupz(time_grouping_method2string(rt->group));
  990. alert_cfg->p_db_lookup_options = rt->options;
  991. alert_cfg->p_db_lookup_after = rt->after;
  992. alert_cfg->p_db_lookup_before = rt->before;
  993. alert_cfg->p_update_every = rt->update_every;
  994. }
  995. }
  996. else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
  997. alert_cfg->every = string_strdupz(value);
  998. if(!config_parse_duration(value, &rt->update_every))
  999. netdata_log_error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' cannot parse duration: '%s'.",
  1000. line, filename, rrdcalctemplate_name(rt), key, value);
  1001. alert_cfg->p_update_every = rt->update_every;
  1002. }
  1003. else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
  1004. alert_cfg->green = string_strdupz(value);
  1005. char *e;
  1006. rt->green = str2ndd(value, &e);
  1007. if(e && *e) {
  1008. netdata_log_error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
  1009. line, filename, rrdcalctemplate_name(rt), key, e);
  1010. }
  1011. }
  1012. else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
  1013. alert_cfg->red = string_strdupz(value);
  1014. char *e;
  1015. rt->red = str2ndd(value, &e);
  1016. if(e && *e) {
  1017. netdata_log_error("Health configuration at line %zu of file '%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
  1018. line, filename, rrdcalctemplate_name(rt), key, e);
  1019. }
  1020. }
  1021. else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
  1022. alert_cfg->calc = string_strdupz(value);
  1023. const char *failed_at = NULL;
  1024. int error = 0;
  1025. rt->calculation = expression_parse(value, &failed_at, &error);
  1026. if(!rt->calculation) {
  1027. 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'",
  1028. line, filename, rrdcalctemplate_name(rt), key, value, expression_strerror(error), failed_at);
  1029. }
  1030. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  1031. }
  1032. else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
  1033. alert_cfg->warn = string_strdupz(value);
  1034. const char *failed_at = NULL;
  1035. int error = 0;
  1036. rt->warning = expression_parse(value, &failed_at, &error);
  1037. if(!rt->warning) {
  1038. 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'",
  1039. line, filename, rrdcalctemplate_name(rt), key, value, expression_strerror(error), failed_at);
  1040. }
  1041. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  1042. }
  1043. else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
  1044. alert_cfg->crit = string_strdupz(value);
  1045. const char *failed_at = NULL;
  1046. int error = 0;
  1047. rt->critical = expression_parse(value, &failed_at, &error);
  1048. if(!rt->critical) {
  1049. 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'",
  1050. line, filename, rrdcalctemplate_name(rt), key, value, expression_strerror(error), failed_at);
  1051. }
  1052. parse_variables_and_store_in_health_rrdvars(value, HEALTH_CONF_MAX_LINE);
  1053. }
  1054. else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
  1055. alert_cfg->exec = string_strdupz(value);
  1056. if(rt->exec) {
  1057. if(strcmp(rrdcalctemplate_exec(rt), value) != 0)
  1058. 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').",
  1059. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_exec(rt), value, value);
  1060. string_freez(rt->exec);
  1061. }
  1062. rt->exec = string_strdupz(value);
  1063. }
  1064. else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
  1065. alert_cfg->to = string_strdupz(value);
  1066. if(rt->recipient) {
  1067. if(strcmp(rrdcalctemplate_recipient(rt), value) != 0)
  1068. 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').",
  1069. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_recipient(rt), value, value);
  1070. string_freez(rt->recipient);
  1071. }
  1072. rt->recipient = string_strdupz(value);
  1073. }
  1074. else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
  1075. strip_quotes(value);
  1076. alert_cfg->units = string_strdupz(value);
  1077. if(rt->units) {
  1078. if(strcmp(rrdcalctemplate_units(rt), value) != 0)
  1079. 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').",
  1080. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_units(rt), value, value);
  1081. string_freez(rt->units);
  1082. }
  1083. rt->units = string_strdupz(value);
  1084. }
  1085. else if(hash == hash_summary && !strcasecmp(key, HEALTH_SUMMARY_KEY)) {
  1086. strip_quotes(value);
  1087. alert_cfg->summary = string_strdupz(value);
  1088. if(rt->summary) {
  1089. if(strcmp(rrdcalctemplate_summary(rt), value) != 0)
  1090. 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').",
  1091. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_summary(rt), value, value);
  1092. string_freez(rt->summary);
  1093. }
  1094. rt->summary = string_strdupz(value);
  1095. }
  1096. else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
  1097. strip_quotes(value);
  1098. alert_cfg->info = string_strdupz(value);
  1099. if(rt->info) {
  1100. if(strcmp(rrdcalctemplate_info(rt), value) != 0)
  1101. 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').",
  1102. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_info(rt), value, value);
  1103. string_freez(rt->info);
  1104. }
  1105. rt->info = string_strdupz(value);
  1106. }
  1107. else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
  1108. alert_cfg->delay = string_strdupz(value);
  1109. health_parse_delay(line, filename, value, &rt->delay_up_duration, &rt->delay_down_duration, &rt->delay_max_duration, &rt->delay_multiplier);
  1110. }
  1111. else if(hash == hash_options && !strcasecmp(key, HEALTH_OPTIONS_KEY)) {
  1112. alert_cfg->options = string_strdupz(value);
  1113. rt->options |= health_parse_options(value);
  1114. }
  1115. else if(hash == hash_repeat && !strcasecmp(key, HEALTH_REPEAT_KEY)){
  1116. alert_cfg->repeat = string_strdupz(value);
  1117. health_parse_repeat(line, filename, value,
  1118. &rt->warn_repeat_every,
  1119. &rt->crit_repeat_every);
  1120. }
  1121. else if(hash == hash_host_label && !strcasecmp(key, HEALTH_HOST_LABEL_KEY)) {
  1122. alert_cfg->host_labels = string_strdupz(value);
  1123. if(rt->host_labels) {
  1124. if(strcmp(rrdcalctemplate_host_labels(rt), value) != 0)
  1125. 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').",
  1126. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_host_labels(rt), value, value);
  1127. string_freez(rt->host_labels);
  1128. simple_pattern_free(rt->host_labels_pattern);
  1129. }
  1130. {
  1131. char *tmp = simple_pattern_trim_around_equal(value);
  1132. rt->host_labels = string_strdupz(tmp);
  1133. freez(tmp);
  1134. }
  1135. rt->host_labels_pattern = simple_pattern_create(rrdcalctemplate_host_labels(rt), NULL,
  1136. SIMPLE_PATTERN_EXACT, true);
  1137. }
  1138. else if(hash == hash_chart_label && !strcasecmp(key, HEALTH_CHART_LABEL_KEY)) {
  1139. alert_cfg->chart_labels = string_strdupz(value);
  1140. if(rt->chart_labels) {
  1141. if(strcmp(rrdcalctemplate_chart_labels(rt), value) != 0)
  1142. 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').",
  1143. line, filename, rrdcalctemplate_name(rt), key, rrdcalctemplate_chart_labels(rt), value, value);
  1144. string_freez(rt->chart_labels);
  1145. simple_pattern_free(rt->chart_labels_pattern);
  1146. }
  1147. {
  1148. char *tmp = simple_pattern_trim_around_equal(value);
  1149. char *tmp_2 = health_config_add_key_to_values(tmp);
  1150. rt->chart_labels = string_strdupz(tmp_2);
  1151. freez(tmp);
  1152. freez(tmp_2);
  1153. }
  1154. rt->chart_labels_pattern = simple_pattern_create(rrdcalctemplate_chart_labels(rt), NULL,
  1155. SIMPLE_PATTERN_EXACT, true);
  1156. }
  1157. else {
  1158. if (strcmp(key, "families") != 0)
  1159. netdata_log_error("Health configuration at line %zu of file '%s' for template '%s' has unknown key '%s'.",
  1160. line, filename, rrdcalctemplate_name(rt), key);
  1161. }
  1162. }
  1163. else {
  1164. netdata_log_error("Health configuration at line %zu of file '%s' has unknown key '%s'. Expected either '" HEALTH_ALARM_KEY "' or '" HEALTH_TEMPLATE_KEY "'.",
  1165. line, filename, key);
  1166. }
  1167. }
  1168. if(rc) {
  1169. //health_add_alarms_loop(host, rc, ignore_this) ;
  1170. if(!alert_hash_and_store_config(rc->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  1171. rrdcalc_free_unused_rrdcalc_loaded_from_config(rc);
  1172. else
  1173. rrdcalc_add_from_config(host, rc);
  1174. }
  1175. if(rt) {
  1176. if(!alert_hash_and_store_config(rt->config_hash_id, alert_cfg, sql_store_hashes) || ignore_this)
  1177. rrdcalctemplate_free_unused_rrdcalctemplate_loaded_from_config(rt);
  1178. else
  1179. rrdcalctemplate_add_from_config(host, rt);
  1180. }
  1181. if (alert_cfg)
  1182. alert_config_free(alert_cfg);
  1183. fclose(fp);
  1184. return 1;
  1185. }
  1186. void sql_refresh_hashes(void)
  1187. {
  1188. sql_store_hashes = 1;
  1189. }
  1190. void health_readdir(RRDHOST *host, const char *user_path, const char *stock_path, const char *subpath) {
  1191. if(unlikely((!host->health.health_enabled) && !rrdhost_flag_check(host, RRDHOST_FLAG_INITIALIZED_HEALTH)) ||
  1192. !service_running(SERVICE_HEALTH)) {
  1193. netdata_log_debug(D_HEALTH, "CONFIG health is not enabled for host '%s'", rrdhost_hostname(host));
  1194. return;
  1195. }
  1196. int stock_enabled = (int)config_get_boolean(CONFIG_SECTION_HEALTH, "enable stock health configuration",
  1197. CONFIG_BOOLEAN_YES);
  1198. if (!stock_enabled) {
  1199. netdata_log_health("[%s]: Netdata will not load stock alarms.", rrdhost_hostname(host));
  1200. stock_path = user_path;
  1201. }
  1202. if (!health_rrdvars)
  1203. health_rrdvars = health_rrdvariables_create();
  1204. recursive_config_double_dir_load(user_path, stock_path, subpath, health_readfile, (void *) host, 0);
  1205. netdata_log_health("[%s]: Read health configuration.", rrdhost_hostname(host));
  1206. sql_store_hashes = 0;
  1207. }