health_json.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "health.h"
  3. void health_string2json(BUFFER *wb, const char *prefix, const char *label, const char *value, const char *suffix) {
  4. if(value && *value) {
  5. buffer_sprintf(wb, "%s\"%s\":\"", prefix, label);
  6. buffer_strcat_htmlescape(wb, value);
  7. buffer_strcat(wb, "\"");
  8. buffer_strcat(wb, suffix);
  9. }
  10. else
  11. buffer_sprintf(wb, "%s\"%s\":null%s", prefix, label, suffix);
  12. }
  13. static inline void health_rrdcalc_values2json_nolock(RRDHOST *host, BUFFER *wb, RRDCALC *rc) {
  14. (void)host;
  15. buffer_sprintf(wb,
  16. "\t\t\"%s.%s\": {\n"
  17. "\t\t\t\"id\": %lu,\n"
  18. , rrdcalc_chart_name(rc), rrdcalc_name(rc)
  19. , (unsigned long)rc->id);
  20. buffer_strcat(wb, "\t\t\t\"value\":");
  21. buffer_print_netdata_double(wb, rc->value);
  22. buffer_strcat(wb, ",\n");
  23. buffer_strcat(wb, "\t\t\t\"last_updated\":");
  24. buffer_sprintf(wb, "%lu", (unsigned long)rc->last_updated);
  25. buffer_strcat(wb, ",\n");
  26. buffer_sprintf(wb,
  27. "\t\t\t\"status\": \"%s\"\n"
  28. , rrdcalc_status2string(rc->status));
  29. buffer_strcat(wb, "\t\t}");
  30. }
  31. static inline void health_rrdcalc2json_nolock(RRDHOST *host, BUFFER *wb, RRDCALC *rc) {
  32. char value_string[100 + 1];
  33. format_value_and_unit(value_string, 100, rc->value, rrdcalc_units(rc), -1);
  34. char hash_id[GUID_LEN + 1];
  35. uuid_unparse_lower(rc->config_hash_id, hash_id);
  36. buffer_sprintf(wb,
  37. "\t\t\"%s.%s\": {\n"
  38. "\t\t\t\"id\": %lu,\n"
  39. "\t\t\t\"config_hash_id\": \"%s\",\n"
  40. "\t\t\t\"name\": \"%s\",\n"
  41. "\t\t\t\"chart\": \"%s\",\n"
  42. "\t\t\t\"family\": \"%s\",\n"
  43. "\t\t\t\"class\": \"%s\",\n"
  44. "\t\t\t\"component\": \"%s\",\n"
  45. "\t\t\t\"type\": \"%s\",\n"
  46. "\t\t\t\"active\": %s,\n"
  47. "\t\t\t\"disabled\": %s,\n"
  48. "\t\t\t\"silenced\": %s,\n"
  49. "\t\t\t\"exec\": \"%s\",\n"
  50. "\t\t\t\"recipient\": \"%s\",\n"
  51. "\t\t\t\"source\": \"%s\",\n"
  52. "\t\t\t\"units\": \"%s\",\n"
  53. "\t\t\t\"info\": \"%s\",\n"
  54. "\t\t\t\"status\": \"%s\",\n"
  55. "\t\t\t\"last_status_change\": %lu,\n"
  56. "\t\t\t\"last_updated\": %lu,\n"
  57. "\t\t\t\"next_update\": %lu,\n"
  58. "\t\t\t\"update_every\": %d,\n"
  59. "\t\t\t\"delay_up_duration\": %d,\n"
  60. "\t\t\t\"delay_down_duration\": %d,\n"
  61. "\t\t\t\"delay_max_duration\": %d,\n"
  62. "\t\t\t\"delay_multiplier\": %f,\n"
  63. "\t\t\t\"delay\": %d,\n"
  64. "\t\t\t\"delay_up_to_timestamp\": %lu,\n"
  65. "\t\t\t\"warn_repeat_every\": \"%u\",\n"
  66. "\t\t\t\"crit_repeat_every\": \"%u\",\n"
  67. "\t\t\t\"value_string\": \"%s\",\n"
  68. "\t\t\t\"last_repeat\": \"%lu\",\n"
  69. "\t\t\t\"times_repeat\": %lu,\n"
  70. , rrdcalc_chart_name(rc), rrdcalc_name(rc)
  71. , (unsigned long)rc->id
  72. , hash_id
  73. , rrdcalc_name(rc)
  74. , rrdcalc_chart_name(rc)
  75. , (rc->rrdset)?rrdset_family(rc->rrdset):""
  76. , rc->classification?rrdcalc_classification(rc):"Unknown"
  77. , rc->component?rrdcalc_component(rc):"Unknown"
  78. , rc->type?rrdcalc_type(rc):"Unknown"
  79. , (rc->rrdset)?"true":"false"
  80. , (rc->run_flags & RRDCALC_FLAG_DISABLED)?"true":"false"
  81. , (rc->run_flags & RRDCALC_FLAG_SILENCED)?"true":"false"
  82. , rc->exec?rrdcalc_exec(rc):string2str(host->health.health_default_exec)
  83. , rc->recipient?rrdcalc_recipient(rc):string2str(host->health.health_default_recipient)
  84. , rrdcalc_source(rc)
  85. , rrdcalc_units(rc)
  86. , rrdcalc_info(rc)
  87. , rrdcalc_status2string(rc->status)
  88. , (unsigned long)rc->last_status_change
  89. , (unsigned long)rc->last_updated
  90. , (unsigned long)rc->next_update
  91. , rc->update_every
  92. , rc->delay_up_duration
  93. , rc->delay_down_duration
  94. , rc->delay_max_duration
  95. , rc->delay_multiplier
  96. , rc->delay_last
  97. , (unsigned long)rc->delay_up_to_timestamp
  98. , rc->warn_repeat_every
  99. , rc->crit_repeat_every
  100. , value_string
  101. , (unsigned long)rc->last_repeat
  102. , (unsigned long)rc->times_repeat
  103. );
  104. if(unlikely(rc->options & RRDCALC_OPTION_NO_CLEAR_NOTIFICATION)) {
  105. buffer_strcat(wb, "\t\t\t\"no_clear_notification\": true,\n");
  106. }
  107. if(RRDCALC_HAS_DB_LOOKUP(rc)) {
  108. if(rc->dimensions)
  109. health_string2json(wb, "\t\t\t", "lookup_dimensions", rrdcalc_dimensions(rc), ",\n");
  110. buffer_sprintf(wb,
  111. "\t\t\t\"db_after\": %lu,\n"
  112. "\t\t\t\"db_before\": %lu,\n"
  113. "\t\t\t\"lookup_method\": \"%s\",\n"
  114. "\t\t\t\"lookup_after\": %d,\n"
  115. "\t\t\t\"lookup_before\": %d,\n"
  116. "\t\t\t\"lookup_options\": \"",
  117. (unsigned long) rc->db_after,
  118. (unsigned long) rc->db_before,
  119. time_grouping_method2string(rc->group),
  120. rc->after,
  121. rc->before
  122. );
  123. buffer_data_options2string(wb, rc->options);
  124. buffer_strcat(wb, "\",\n");
  125. }
  126. if(rc->calculation) {
  127. health_string2json(wb, "\t\t\t", "calc", rc->calculation->source, ",\n");
  128. health_string2json(wb, "\t\t\t", "calc_parsed", rc->calculation->parsed_as, ",\n");
  129. }
  130. if(rc->warning) {
  131. health_string2json(wb, "\t\t\t", "warn", rc->warning->source, ",\n");
  132. health_string2json(wb, "\t\t\t", "warn_parsed", rc->warning->parsed_as, ",\n");
  133. }
  134. if(rc->critical) {
  135. health_string2json(wb, "\t\t\t", "crit", rc->critical->source, ",\n");
  136. health_string2json(wb, "\t\t\t", "crit_parsed", rc->critical->parsed_as, ",\n");
  137. }
  138. buffer_strcat(wb, "\t\t\t\"green\":");
  139. buffer_print_netdata_double(wb, rc->green);
  140. buffer_strcat(wb, ",\n");
  141. buffer_strcat(wb, "\t\t\t\"red\":");
  142. buffer_print_netdata_double(wb, rc->red);
  143. buffer_strcat(wb, ",\n");
  144. buffer_strcat(wb, "\t\t\t\"value\":");
  145. buffer_print_netdata_double(wb, rc->value);
  146. buffer_strcat(wb, "\n");
  147. buffer_strcat(wb, "\t\t}");
  148. }
  149. static inline void health_alerts_rrdcalc2json_nolock(RRDHOST *host __maybe_unused, BUFFER *wb,
  150. RRDCALC *rc, ALERT_OPTIONS options __maybe_unused,
  151. Pvoid_t JudyHS, time_t after, time_t before, uint32_t top)
  152. {
  153. ssize_t idx= get_alert_index(JudyHS, &rc->config_hash_id);
  154. // If not in index then skip it
  155. if (idx < 0)
  156. return;
  157. char value_string[100 + 1];
  158. format_value_and_unit(value_string, 100, rc->value, rrdcalc_units(rc), -1);
  159. char hash_id[UUID_STR_LEN];
  160. uuid_unparse_lower(rc->config_hash_id, hash_id);
  161. buffer_json_add_array_item_object(wb);
  162. if ((!after || after <= rc->last_updated) && (!before || before >= rc->last_updated)) {
  163. buffer_json_member_add_uint64(wb, "li", (size_t) idx);
  164. char trans_uuid_str[UUID_STR_LEN];
  165. if (rc->ae) {
  166. uuid_unparse_lower(rc->ae->transition_id, trans_uuid_str);
  167. buffer_json_member_add_string(wb, "transition_id", trans_uuid_str);
  168. buffer_json_member_add_uint64(wb, "gi", rc->ae->global_id);
  169. }
  170. else {
  171. buffer_json_member_add_quoted_string(wb, "transition_id", "NULL");
  172. buffer_json_member_add_quoted_string(wb, "gi", "NULL");
  173. }
  174. buffer_json_member_add_string(wb, "status", rrdcalc_status2string(rc->status));
  175. buffer_json_member_add_uint64(wb, "last_status_change", (unsigned long)rc->last_status_change);
  176. buffer_json_member_add_uint64(wb, "last_updated", (unsigned long)rc->last_updated);
  177. buffer_json_member_add_uint64(wb, "next_update", (unsigned long)rc->next_update);
  178. buffer_json_member_add_uint64(wb, "delay_up_to_timestamp", (unsigned long)rc->delay_up_to_timestamp);
  179. buffer_json_member_add_string(wb, "value_string", value_string);
  180. buffer_json_member_add_uint64(wb, "last_repeat", (unsigned long)rc->last_repeat);
  181. buffer_json_member_add_uint64(wb, "times_repeat", (unsigned long)rc->times_repeat);
  182. buffer_json_member_add_uint64(wb, "db_after", (unsigned long)rc->db_after);
  183. buffer_json_member_add_uint64(wb, "db_before", (unsigned long)rc->db_before);
  184. buffer_json_member_add_double(wb, "green", rc->green);
  185. buffer_json_member_add_double(wb, "red", rc->red);
  186. buffer_json_member_add_double(wb, "value", rc->value);
  187. if (options & ALERT_OPTION_INSTANCES) {
  188. buffer_json_member_add_array(wb, "transitions");
  189. sql_health_alarm_log2json_v2(host, wb, rc->id, NULL, after, before, top);
  190. buffer_json_array_close(wb);
  191. }
  192. }
  193. buffer_json_object_close(wb); // array entry
  194. }
  195. //void health_rrdcalctemplate2json_nolock(BUFFER *wb, RRDCALCTEMPLATE *rt) {
  196. //
  197. //}
  198. void health_aggregate_alarms(RRDHOST *host, BUFFER *wb, BUFFER* contexts, RRDCALC_STATUS status) {
  199. RRDCALC *rc;
  200. int numberOfAlarms = 0;
  201. char *tok = NULL;
  202. char *p = NULL;
  203. if (contexts) {
  204. p = (char*)buffer_tostring(contexts);
  205. while(p && *p && (tok = strsep_skip_consecutive_separators(&p, ", |"))) {
  206. if(!*tok) continue;
  207. STRING *tok_string = string_strdupz(tok);
  208. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  209. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  210. continue;
  211. if (unlikely(!rrdset_is_available_for_exporting_and_alarms(rc->rrdset)))
  212. continue;
  213. if(unlikely(rc->rrdset
  214. && rc->rrdset->context == tok_string
  215. && ((status==RRDCALC_STATUS_RAISED)?(rc->status >= RRDCALC_STATUS_WARNING):rc->status == status)))
  216. numberOfAlarms++;
  217. }
  218. foreach_rrdcalc_in_rrdhost_done(rc);
  219. string_freez(tok_string);
  220. }
  221. }
  222. else {
  223. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  224. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  225. continue;
  226. if (unlikely(!rrdset_is_available_for_exporting_and_alarms(rc->rrdset)))
  227. continue;
  228. if(unlikely((status==RRDCALC_STATUS_RAISED)?(rc->status >= RRDCALC_STATUS_WARNING):rc->status == status))
  229. numberOfAlarms++;
  230. }
  231. foreach_rrdcalc_in_rrdhost_done(rc);
  232. }
  233. buffer_sprintf(wb, "%d", numberOfAlarms);
  234. }
  235. static void health_alarms2json_fill_alarms(RRDHOST *host, BUFFER *wb, int all, void (*fp)(RRDHOST *, BUFFER *, RRDCALC *)) {
  236. RRDCALC *rc;
  237. int i = 0;
  238. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  239. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  240. continue;
  241. if (unlikely(!rrdset_is_available_for_exporting_and_alarms(rc->rrdset)))
  242. continue;
  243. if(likely(!all && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
  244. continue;
  245. if(likely(i)) buffer_strcat(wb, ",\n");
  246. fp(host, wb, rc);
  247. i++;
  248. }
  249. foreach_rrdcalc_in_rrdhost_done(rc);
  250. }
  251. static void health_alerts2json_fill_alarms(
  252. RRDHOST *host,
  253. BUFFER *wb,
  254. ALERT_OPTIONS all,
  255. Pvoid_t JudyHS,
  256. time_t after,
  257. time_t before,
  258. uint32_t top,
  259. void (*fp)(RRDHOST *, BUFFER *, RRDCALC *, ALERT_OPTIONS, Pvoid_t , time_t, time_t, uint32_t))
  260. {
  261. RRDCALC *rc;
  262. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  263. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  264. continue;
  265. if (unlikely(!rrdset_is_available_for_exporting_and_alarms(rc->rrdset)))
  266. continue;
  267. if(likely((all & ALERT_OPTION_ACTIVE) && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
  268. continue;
  269. fp(host, wb, rc, all, JudyHS, after, before, top);
  270. }
  271. foreach_rrdcalc_in_rrdhost_done(rc);
  272. }
  273. void health_alert2json(RRDHOST *host, BUFFER *wb, ALERT_OPTIONS options, Pvoid_t JudyHS, time_t after, time_t before, uint32_t top)
  274. {
  275. health_alerts2json_fill_alarms(host, wb, options, JudyHS, after, before, top, health_alerts_rrdcalc2json_nolock);
  276. }
  277. void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
  278. buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
  279. "\n\t\"latest_alarm_log_unique_id\": %u,"
  280. "\n\t\"status\": %s,"
  281. "\n\t\"now\": %lu,"
  282. "\n\t\"alarms\": {\n",
  283. rrdhost_hostname(host),
  284. (host->health_log.next_log_id > 0)?(host->health_log.next_log_id - 1):0,
  285. host->health.health_enabled?"true":"false",
  286. (unsigned long)now_realtime_sec());
  287. health_alarms2json_fill_alarms(host, wb, all, health_rrdcalc2json_nolock);
  288. // rrdhost_rdlock(host);
  289. // buffer_strcat(wb, "\n\t},\n\t\"templates\": {");
  290. // RRDCALCTEMPLATE *rt;
  291. // for(rt = host->templates; rt ; rt = rt->next)
  292. // health_rrdcalctemplate2json_nolock(wb, rt);
  293. // rrdhost_unlock(host);
  294. buffer_strcat(wb, "\n\t}\n}\n");
  295. }
  296. void health_alarms_values2json(RRDHOST *host, BUFFER *wb, int all) {
  297. buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
  298. "\n\t\"alarms\": {\n",
  299. rrdhost_hostname(host));
  300. health_alarms2json_fill_alarms(host, wb, all, health_rrdcalc_values2json_nolock);
  301. buffer_strcat(wb, "\n\t}\n}\n");
  302. }