health_json.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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\"class\": \"%s\",\n"
  43. "\t\t\t\"component\": \"%s\",\n"
  44. "\t\t\t\"type\": \"%s\",\n"
  45. "\t\t\t\"active\": %s,\n"
  46. "\t\t\t\"disabled\": %s,\n"
  47. "\t\t\t\"silenced\": %s,\n"
  48. "\t\t\t\"exec\": \"%s\",\n"
  49. "\t\t\t\"recipient\": \"%s\",\n"
  50. "\t\t\t\"source\": \"%s\",\n"
  51. "\t\t\t\"units\": \"%s\",\n"
  52. "\t\t\t\"summary\": \"%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->classification?rrdcalc_classification(rc):"Unknown"
  76. , rc->component?rrdcalc_component(rc):"Unknown"
  77. , rc->type?rrdcalc_type(rc):"Unknown"
  78. , (rc->rrdset)?"true":"false"
  79. , (rc->run_flags & RRDCALC_FLAG_DISABLED)?"true":"false"
  80. , (rc->run_flags & RRDCALC_FLAG_SILENCED)?"true":"false"
  81. , rc->exec?rrdcalc_exec(rc):string2str(host->health.health_default_exec)
  82. , rc->recipient?rrdcalc_recipient(rc):string2str(host->health.health_default_recipient)
  83. , rrdcalc_source(rc)
  84. , rrdcalc_units(rc)
  85. , rrdcalc_summary(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. void health_aggregate_alarms(RRDHOST *host, BUFFER *wb, BUFFER* contexts, RRDCALC_STATUS status) {
  150. RRDCALC *rc;
  151. int numberOfAlarms = 0;
  152. char *tok = NULL;
  153. char *p = NULL;
  154. if (contexts) {
  155. p = (char*)buffer_tostring(contexts);
  156. while(p && *p && (tok = strsep_skip_consecutive_separators(&p, ", |"))) {
  157. if(!*tok) continue;
  158. STRING *tok_string = string_strdupz(tok);
  159. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  160. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  161. continue;
  162. if (unlikely(!rrdset_is_available_for_exporting_and_alarms(rc->rrdset)))
  163. continue;
  164. if(unlikely(rc->rrdset
  165. && rc->rrdset->context == tok_string
  166. && ((status==RRDCALC_STATUS_RAISED)?(rc->status >= RRDCALC_STATUS_WARNING):rc->status == status)))
  167. numberOfAlarms++;
  168. }
  169. foreach_rrdcalc_in_rrdhost_done(rc);
  170. string_freez(tok_string);
  171. }
  172. }
  173. else {
  174. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  175. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  176. continue;
  177. if (unlikely(!rrdset_is_available_for_exporting_and_alarms(rc->rrdset)))
  178. continue;
  179. if(unlikely((status==RRDCALC_STATUS_RAISED)?(rc->status >= RRDCALC_STATUS_WARNING):rc->status == status))
  180. numberOfAlarms++;
  181. }
  182. foreach_rrdcalc_in_rrdhost_done(rc);
  183. }
  184. buffer_sprintf(wb, "%d", numberOfAlarms);
  185. }
  186. static void health_alarms2json_fill_alarms(RRDHOST *host, BUFFER *wb, int all, void (*fp)(RRDHOST *, BUFFER *, RRDCALC *)) {
  187. RRDCALC *rc;
  188. int i = 0;
  189. foreach_rrdcalc_in_rrdhost_read(host, rc) {
  190. if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
  191. continue;
  192. if (unlikely(!rrdset_is_available_for_exporting_and_alarms(rc->rrdset)))
  193. continue;
  194. if(likely(!all && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
  195. continue;
  196. if(likely(i)) buffer_strcat(wb, ",\n");
  197. fp(host, wb, rc);
  198. i++;
  199. }
  200. foreach_rrdcalc_in_rrdhost_done(rc);
  201. }
  202. void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
  203. buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
  204. "\n\t\"latest_alarm_log_unique_id\": %u,"
  205. "\n\t\"status\": %s,"
  206. "\n\t\"now\": %lu,"
  207. "\n\t\"alarms\": {\n",
  208. rrdhost_hostname(host),
  209. (host->health_log.next_log_id > 0)?(host->health_log.next_log_id - 1):0,
  210. host->health.health_enabled?"true":"false",
  211. (unsigned long)now_realtime_sec());
  212. health_alarms2json_fill_alarms(host, wb, all, health_rrdcalc2json_nolock);
  213. // rrdhost_rdlock(host);
  214. // buffer_strcat(wb, "\n\t},\n\t\"templates\": {");
  215. // RRDCALCTEMPLATE *rt;
  216. // for(rt = host->templates; rt ; rt = rt->next)
  217. // health_rrdcalctemplate2json_nolock(wb, rt);
  218. // rrdhost_unlock(host);
  219. buffer_strcat(wb, "\n\t}\n}\n");
  220. }
  221. void health_alarms_values2json(RRDHOST *host, BUFFER *wb, int all) {
  222. buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
  223. "\n\t\"alarms\": {\n",
  224. rrdhost_hostname(host));
  225. health_alarms2json_fill_alarms(host, wb, all, health_rrdcalc_values2json_nolock);
  226. buffer_strcat(wb, "\n\t}\n}\n");
  227. }