api_v2.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "internal.h"
  3. #include "aclk/aclk_capas.h"
  4. // ----------------------------------------------------------------------------
  5. // /api/v2/contexts API
  6. struct alert_transitions_facets alert_transition_facets[] = {
  7. [ATF_STATUS] = {
  8. .id = "f_status",
  9. .name = "Alert Status",
  10. .query_param = "f_status",
  11. .order = 1,
  12. },
  13. [ATF_TYPE] = {
  14. .id = "f_type",
  15. .name = "Alert Type",
  16. .query_param = "f_type",
  17. .order = 2,
  18. },
  19. [ATF_ROLE] = {
  20. .id = "f_role",
  21. .name = "Recipient Role",
  22. .query_param = "f_role",
  23. .order = 3,
  24. },
  25. [ATF_CLASS] = {
  26. .id = "f_class",
  27. .name = "Alert Class",
  28. .query_param = "f_class",
  29. .order = 4,
  30. },
  31. [ATF_COMPONENT] = {
  32. .id = "f_component",
  33. .name = "Alert Component",
  34. .query_param = "f_component",
  35. .order = 5,
  36. },
  37. [ATF_NODE] = {
  38. .id = "f_node",
  39. .name = "Alert Node",
  40. .query_param = "f_node",
  41. .order = 6,
  42. },
  43. [ATF_ALERT_NAME] = {
  44. .id = "f_alert",
  45. .name = "Alert Name",
  46. .query_param = "f_alert",
  47. .order = 7,
  48. },
  49. [ATF_CHART_NAME] = {
  50. .id = "f_instance",
  51. .name = "Instance Name",
  52. .query_param = "f_instance",
  53. .order = 8,
  54. },
  55. [ATF_CONTEXT] = {
  56. .id = "f_context",
  57. .name = "Context",
  58. .query_param = "f_context",
  59. .order = 9,
  60. },
  61. // terminator
  62. [ATF_TOTAL_ENTRIES] = {
  63. .id = NULL,
  64. .name = NULL,
  65. .query_param = NULL,
  66. .order = 9999,
  67. }
  68. };
  69. struct facet_entry {
  70. uint32_t count;
  71. };
  72. struct alert_transitions_callback_data {
  73. struct rrdcontext_to_json_v2_data *ctl;
  74. BUFFER *wb;
  75. bool debug;
  76. bool only_one_config;
  77. struct {
  78. SIMPLE_PATTERN *pattern;
  79. DICTIONARY *dict;
  80. } facets[ATF_TOTAL_ENTRIES];
  81. uint32_t max_items_to_return;
  82. uint32_t items_to_return;
  83. uint32_t items_evaluated;
  84. uint32_t items_matched;
  85. struct sql_alert_transition_fixed_size *base; // double linked list - last item is base->prev
  86. struct sql_alert_transition_fixed_size *last_added; // the last item added, not the last of the list
  87. struct {
  88. size_t first;
  89. size_t skips_before;
  90. size_t skips_after;
  91. size_t backwards;
  92. size_t forwards;
  93. size_t prepend;
  94. size_t append;
  95. size_t shifts;
  96. } operations;
  97. uint32_t configs_added;
  98. };
  99. typedef enum __attribute__ ((__packed__)) {
  100. FTS_MATCHED_NONE = 0,
  101. FTS_MATCHED_HOST,
  102. FTS_MATCHED_CONTEXT,
  103. FTS_MATCHED_INSTANCE,
  104. FTS_MATCHED_DIMENSION,
  105. FTS_MATCHED_LABEL,
  106. FTS_MATCHED_ALERT,
  107. FTS_MATCHED_ALERT_INFO,
  108. FTS_MATCHED_FAMILY,
  109. FTS_MATCHED_TITLE,
  110. FTS_MATCHED_UNITS,
  111. } FTS_MATCH;
  112. static const char *fts_match_to_string(FTS_MATCH match) {
  113. switch(match) {
  114. case FTS_MATCHED_HOST:
  115. return "HOST";
  116. case FTS_MATCHED_CONTEXT:
  117. return "CONTEXT";
  118. case FTS_MATCHED_INSTANCE:
  119. return "INSTANCE";
  120. case FTS_MATCHED_DIMENSION:
  121. return "DIMENSION";
  122. case FTS_MATCHED_ALERT:
  123. return "ALERT";
  124. case FTS_MATCHED_ALERT_INFO:
  125. return "ALERT_INFO";
  126. case FTS_MATCHED_LABEL:
  127. return "LABEL";
  128. case FTS_MATCHED_FAMILY:
  129. return "FAMILY";
  130. case FTS_MATCHED_TITLE:
  131. return "TITLE";
  132. case FTS_MATCHED_UNITS:
  133. return "UNITS";
  134. default:
  135. return "NONE";
  136. }
  137. }
  138. struct function_v2_entry {
  139. size_t size;
  140. size_t used;
  141. size_t *node_ids;
  142. STRING *help;
  143. };
  144. struct context_v2_entry {
  145. size_t count;
  146. STRING *id;
  147. STRING *family;
  148. uint32_t priority;
  149. time_t first_time_s;
  150. time_t last_time_s;
  151. RRD_FLAGS flags;
  152. FTS_MATCH match;
  153. };
  154. struct alert_v2_entry {
  155. RRDCALC *tmp;
  156. STRING *name;
  157. size_t ati;
  158. size_t critical;
  159. size_t warning;
  160. size_t clear;
  161. size_t error;
  162. size_t instances;
  163. DICTIONARY *nodes;
  164. DICTIONARY *configs;
  165. };
  166. typedef struct full_text_search_index {
  167. size_t searches;
  168. size_t string_searches;
  169. size_t char_searches;
  170. } FTS_INDEX;
  171. static inline bool full_text_search_string(FTS_INDEX *fts, SIMPLE_PATTERN *q, STRING *ptr) {
  172. fts->searches++;
  173. fts->string_searches++;
  174. return simple_pattern_matches_string(q, ptr);
  175. }
  176. static inline bool full_text_search_char(FTS_INDEX *fts, SIMPLE_PATTERN *q, char *ptr) {
  177. fts->searches++;
  178. fts->char_searches++;
  179. return simple_pattern_matches(q, ptr);
  180. }
  181. struct contexts_v2_node {
  182. size_t ni;
  183. RRDHOST *host;
  184. };
  185. struct rrdcontext_to_json_v2_data {
  186. time_t now;
  187. BUFFER *wb;
  188. struct api_v2_contexts_request *request;
  189. CONTEXTS_V2_MODE mode;
  190. CONTEXTS_V2_OPTIONS options;
  191. struct query_versions versions;
  192. struct {
  193. SIMPLE_PATTERN *scope_pattern;
  194. SIMPLE_PATTERN *pattern;
  195. size_t ni;
  196. DICTIONARY *dict; // the result set
  197. } nodes;
  198. struct {
  199. SIMPLE_PATTERN *scope_pattern;
  200. SIMPLE_PATTERN *pattern;
  201. size_t ci;
  202. DICTIONARY *dict; // the result set
  203. } contexts;
  204. struct {
  205. SIMPLE_PATTERN *alert_name_pattern;
  206. time_t alarm_id_filter;
  207. size_t ati;
  208. DICTIONARY *alerts;
  209. DICTIONARY *alert_instances;
  210. } alerts;
  211. struct {
  212. FTS_MATCH host_match;
  213. char host_node_id_str[UUID_STR_LEN];
  214. SIMPLE_PATTERN *pattern;
  215. FTS_INDEX fts;
  216. } q;
  217. struct {
  218. DICTIONARY *dict; // the result set
  219. } functions;
  220. struct {
  221. bool enabled;
  222. bool relative;
  223. time_t after;
  224. time_t before;
  225. } window;
  226. struct query_timings timings;
  227. };
  228. static void alerts_v2_add(struct alert_v2_entry *t, RRDCALC *rc) {
  229. t->instances++;
  230. switch(rc->status) {
  231. case RRDCALC_STATUS_CRITICAL:
  232. t->critical++;
  233. break;
  234. case RRDCALC_STATUS_WARNING:
  235. t->warning++;
  236. break;
  237. case RRDCALC_STATUS_CLEAR:
  238. t->clear++;
  239. break;
  240. case RRDCALC_STATUS_REMOVED:
  241. case RRDCALC_STATUS_UNINITIALIZED:
  242. break;
  243. case RRDCALC_STATUS_UNDEFINED:
  244. default:
  245. if(!netdata_double_isnumber(rc->value))
  246. t->error++;
  247. break;
  248. }
  249. dictionary_set(t->nodes, rc->rrdset->rrdhost->machine_guid, NULL, 0);
  250. char key[UUID_STR_LEN + 1];
  251. uuid_unparse_lower(rc->config_hash_id, key);
  252. dictionary_set(t->configs, key, NULL, 0);
  253. }
  254. static void alerts_v2_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data) {
  255. struct rrdcontext_to_json_v2_data *ctl = data;
  256. struct alert_v2_entry *t = value;
  257. RRDCALC *rc = t->tmp;
  258. t->name = rc->name;
  259. t->ati = ctl->alerts.ati++;
  260. t->nodes = dictionary_create(DICT_OPTION_SINGLE_THREADED|DICT_OPTION_VALUE_LINK_DONT_CLONE|DICT_OPTION_NAME_LINK_DONT_CLONE);
  261. t->configs = dictionary_create(DICT_OPTION_SINGLE_THREADED|DICT_OPTION_VALUE_LINK_DONT_CLONE|DICT_OPTION_NAME_LINK_DONT_CLONE);
  262. alerts_v2_add(t, rc);
  263. }
  264. static bool alerts_v2_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *old_value, void *new_value, void *data __maybe_unused) {
  265. struct alert_v2_entry *t = old_value, *n = new_value;
  266. RRDCALC *rc = n->tmp;
  267. alerts_v2_add(t, rc);
  268. return true;
  269. }
  270. static void alerts_v2_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
  271. struct alert_v2_entry *t = value;
  272. dictionary_destroy(t->nodes);
  273. dictionary_destroy(t->configs);
  274. }
  275. static void alert_instances_v2_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data) {
  276. struct rrdcontext_to_json_v2_data *ctl = data;
  277. struct sql_alert_instance_v2_entry *t = value;
  278. RRDCALC *rc = t->tmp;
  279. t->context = rc->rrdset->context;
  280. t->chart_id = rc->rrdset->id;
  281. t->chart_name = rc->rrdset->name;
  282. t->family = rc->rrdset->family;
  283. t->units = rc->units;
  284. t->classification = rc->classification;
  285. t->type = rc->type;
  286. t->recipient = rc->recipient;
  287. t->component = rc->component;
  288. t->name = rc->name;
  289. t->source = rc->source;
  290. t->status = rc->status;
  291. t->flags = rc->run_flags;
  292. t->info = rc->info;
  293. t->value = rc->value;
  294. t->last_updated = rc->last_updated;
  295. t->last_status_change = rc->last_status_change;
  296. t->last_status_change_value = rc->last_status_change_value;
  297. t->host = rc->rrdset->rrdhost;
  298. t->alarm_id = rc->id;
  299. t->ni = ctl->nodes.ni;
  300. t->global_id = rc->ae ? rc->ae->global_id : 0;
  301. t->name = rc->name;
  302. uuid_copy(t->config_hash_id, rc->config_hash_id);
  303. if(rc->ae)
  304. uuid_copy(t->last_transition_id, rc->ae->transition_id);
  305. }
  306. static bool alert_instances_v2_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *old_value __maybe_unused, void *new_value __maybe_unused, void *data __maybe_unused) {
  307. internal_fatal(true, "This should never happen!");
  308. return true;
  309. }
  310. static void alert_instances_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value __maybe_unused, void *data __maybe_unused) {
  311. ;
  312. }
  313. static FTS_MATCH rrdcontext_to_json_v2_full_text_search(struct rrdcontext_to_json_v2_data *ctl, RRDCONTEXT *rc, SIMPLE_PATTERN *q) {
  314. if(unlikely(full_text_search_string(&ctl->q.fts, q, rc->id) ||
  315. full_text_search_string(&ctl->q.fts, q, rc->family)))
  316. return FTS_MATCHED_CONTEXT;
  317. if(unlikely(full_text_search_string(&ctl->q.fts, q, rc->title)))
  318. return FTS_MATCHED_TITLE;
  319. if(unlikely(full_text_search_string(&ctl->q.fts, q, rc->units)))
  320. return FTS_MATCHED_UNITS;
  321. FTS_MATCH matched = FTS_MATCHED_NONE;
  322. RRDINSTANCE *ri;
  323. dfe_start_read(rc->rrdinstances, ri) {
  324. if(matched) break;
  325. if(ctl->window.enabled && !query_matches_retention(ctl->window.after, ctl->window.before, ri->first_time_s, (ri->flags & RRD_FLAG_COLLECTED) ? ctl->now : ri->last_time_s, 0))
  326. continue;
  327. if(unlikely(full_text_search_string(&ctl->q.fts, q, ri->id)) ||
  328. (ri->name != ri->id && full_text_search_string(&ctl->q.fts, q, ri->name))) {
  329. matched = FTS_MATCHED_INSTANCE;
  330. break;
  331. }
  332. RRDMETRIC *rm;
  333. dfe_start_read(ri->rrdmetrics, rm) {
  334. if(ctl->window.enabled && !query_matches_retention(ctl->window.after, ctl->window.before, rm->first_time_s, (rm->flags & RRD_FLAG_COLLECTED) ? ctl->now : rm->last_time_s, 0))
  335. continue;
  336. if(unlikely(full_text_search_string(&ctl->q.fts, q, rm->id)) ||
  337. (rm->name != rm->id && full_text_search_string(&ctl->q.fts, q, rm->name))) {
  338. matched = FTS_MATCHED_DIMENSION;
  339. break;
  340. }
  341. }
  342. dfe_done(rm);
  343. size_t label_searches = 0;
  344. if(unlikely(ri->rrdlabels && dictionary_entries(ri->rrdlabels) &&
  345. rrdlabels_match_simple_pattern_parsed(ri->rrdlabels, q, ':', &label_searches))) {
  346. ctl->q.fts.searches += label_searches;
  347. ctl->q.fts.char_searches += label_searches;
  348. matched = FTS_MATCHED_LABEL;
  349. break;
  350. }
  351. ctl->q.fts.searches += label_searches;
  352. ctl->q.fts.char_searches += label_searches;
  353. if(ri->rrdset) {
  354. RRDSET *st = ri->rrdset;
  355. rw_spinlock_read_lock(&st->alerts.spinlock);
  356. for (RRDCALC *rcl = st->alerts.base; rcl; rcl = rcl->next) {
  357. if(unlikely(full_text_search_string(&ctl->q.fts, q, rcl->name))) {
  358. matched = FTS_MATCHED_ALERT;
  359. break;
  360. }
  361. if(unlikely(full_text_search_string(&ctl->q.fts, q, rcl->info))) {
  362. matched = FTS_MATCHED_ALERT_INFO;
  363. break;
  364. }
  365. }
  366. rw_spinlock_read_unlock(&st->alerts.spinlock);
  367. }
  368. }
  369. dfe_done(ri);
  370. return matched;
  371. }
  372. static bool rrdcontext_matches_alert(struct rrdcontext_to_json_v2_data *ctl, RRDCONTEXT *rc) {
  373. size_t matches = 0;
  374. RRDINSTANCE *ri;
  375. dfe_start_read(rc->rrdinstances, ri) {
  376. if(ri->rrdset) {
  377. RRDSET *st = ri->rrdset;
  378. rw_spinlock_read_lock(&st->alerts.spinlock);
  379. for (RRDCALC *rcl = st->alerts.base; rcl; rcl = rcl->next) {
  380. if(ctl->alerts.alert_name_pattern && !simple_pattern_matches_string(ctl->alerts.alert_name_pattern, rcl->name))
  381. continue;
  382. if(ctl->alerts.alarm_id_filter && ctl->alerts.alarm_id_filter != rcl->id)
  383. continue;
  384. size_t m = ctl->request->alerts.status & CONTEXTS_V2_ALERT_STATUSES ? 0 : 1;
  385. if (!m) {
  386. if ((ctl->request->alerts.status & CONTEXT_V2_ALERT_UNINITIALIZED) &&
  387. rcl->status == RRDCALC_STATUS_UNINITIALIZED)
  388. m++;
  389. if ((ctl->request->alerts.status & CONTEXT_V2_ALERT_UNDEFINED) &&
  390. rcl->status == RRDCALC_STATUS_UNDEFINED)
  391. m++;
  392. if ((ctl->request->alerts.status & CONTEXT_V2_ALERT_CLEAR) &&
  393. rcl->status == RRDCALC_STATUS_CLEAR)
  394. m++;
  395. if ((ctl->request->alerts.status & CONTEXT_V2_ALERT_RAISED) &&
  396. rcl->status >= RRDCALC_STATUS_RAISED)
  397. m++;
  398. if ((ctl->request->alerts.status & CONTEXT_V2_ALERT_WARNING) &&
  399. rcl->status == RRDCALC_STATUS_WARNING)
  400. m++;
  401. if ((ctl->request->alerts.status & CONTEXT_V2_ALERT_CRITICAL) &&
  402. rcl->status == RRDCALC_STATUS_CRITICAL)
  403. m++;
  404. if(!m)
  405. continue;
  406. }
  407. struct alert_v2_entry t = {
  408. .tmp = rcl,
  409. };
  410. struct alert_v2_entry *a2e = dictionary_set(ctl->alerts.alerts, string2str(rcl->name), &t,
  411. sizeof(struct alert_v2_entry));
  412. size_t ati = a2e->ati;
  413. matches++;
  414. if (ctl->options & (CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES | CONTEXT_V2_OPTION_ALERTS_WITH_VALUES)) {
  415. char key[20 + 1];
  416. snprintfz(key, 20, "%p", rcl);
  417. struct sql_alert_instance_v2_entry z = {
  418. .ati = ati,
  419. .tmp = rcl,
  420. };
  421. dictionary_set(ctl->alerts.alert_instances, key, &z, sizeof(z));
  422. }
  423. }
  424. rw_spinlock_read_unlock(&st->alerts.spinlock);
  425. }
  426. }
  427. dfe_done(ri);
  428. return matches != 0;
  429. }
  430. static ssize_t rrdcontext_to_json_v2_add_context(void *data, RRDCONTEXT_ACQUIRED *rca, bool queryable_context __maybe_unused) {
  431. struct rrdcontext_to_json_v2_data *ctl = data;
  432. RRDCONTEXT *rc = rrdcontext_acquired_value(rca);
  433. if(ctl->window.enabled && !query_matches_retention(ctl->window.after, ctl->window.before, rc->first_time_s, (rc->flags & RRD_FLAG_COLLECTED) ? ctl->now : rc->last_time_s, 0))
  434. return 0; // continue to next context
  435. FTS_MATCH match = ctl->q.host_match;
  436. if((ctl->mode & CONTEXTS_V2_SEARCH) && ctl->q.pattern) {
  437. match = rrdcontext_to_json_v2_full_text_search(ctl, rc, ctl->q.pattern);
  438. if(match == FTS_MATCHED_NONE)
  439. return 0; // continue to next context
  440. }
  441. if(ctl->mode & CONTEXTS_V2_ALERTS) {
  442. if(!rrdcontext_matches_alert(ctl, rc))
  443. return 0; // continue to next context
  444. }
  445. if(ctl->contexts.dict) {
  446. struct context_v2_entry t = {
  447. .count = 1,
  448. .id = rc->id,
  449. .family = string_dup(rc->family),
  450. .priority = rc->priority,
  451. .first_time_s = rc->first_time_s,
  452. .last_time_s = rc->last_time_s,
  453. .flags = rc->flags,
  454. .match = match,
  455. };
  456. dictionary_set(ctl->contexts.dict, string2str(rc->id), &t, sizeof(struct context_v2_entry));
  457. }
  458. return 1;
  459. }
  460. void buffer_json_agent_status_id(BUFFER *wb, size_t ai, usec_t duration_ut) {
  461. buffer_json_member_add_object(wb, "st");
  462. {
  463. buffer_json_member_add_uint64(wb, "ai", ai);
  464. buffer_json_member_add_uint64(wb, "code", 200);
  465. buffer_json_member_add_string(wb, "msg", "");
  466. if (duration_ut)
  467. buffer_json_member_add_double(wb, "ms", (NETDATA_DOUBLE) duration_ut / 1000.0);
  468. }
  469. buffer_json_object_close(wb);
  470. }
  471. void buffer_json_node_add_v2(BUFFER *wb, RRDHOST *host, size_t ni, usec_t duration_ut, bool status) {
  472. buffer_json_member_add_string(wb, "mg", host->machine_guid);
  473. if(host->node_id)
  474. buffer_json_member_add_uuid(wb, "nd", host->node_id);
  475. buffer_json_member_add_string(wb, "nm", rrdhost_hostname(host));
  476. buffer_json_member_add_uint64(wb, "ni", ni);
  477. if(status)
  478. buffer_json_agent_status_id(wb, 0, duration_ut);
  479. }
  480. static void rrdhost_receiver_to_json(BUFFER *wb, RRDHOST_STATUS *s, const char *key) {
  481. buffer_json_member_add_object(wb, key);
  482. {
  483. buffer_json_member_add_uint64(wb, "id", s->ingest.id);
  484. buffer_json_member_add_uint64(wb, "hops", s->ingest.hops);
  485. buffer_json_member_add_string(wb, "type", rrdhost_ingest_type_to_string(s->ingest.type));
  486. buffer_json_member_add_string(wb, "status", rrdhost_ingest_status_to_string(s->ingest.status));
  487. buffer_json_member_add_time_t(wb, "since", s->ingest.since);
  488. buffer_json_member_add_time_t(wb, "age", s->now - s->ingest.since);
  489. if(s->ingest.type == RRDHOST_INGEST_TYPE_CHILD) {
  490. if(s->ingest.status == RRDHOST_INGEST_STATUS_OFFLINE)
  491. buffer_json_member_add_string(wb, "reason", stream_handshake_error_to_string(s->ingest.reason));
  492. if(s->ingest.status == RRDHOST_INGEST_STATUS_REPLICATING) {
  493. buffer_json_member_add_object(wb, "replication");
  494. {
  495. buffer_json_member_add_boolean(wb, "in_progress", s->ingest.replication.in_progress);
  496. buffer_json_member_add_double(wb, "completion", s->ingest.replication.completion);
  497. buffer_json_member_add_uint64(wb, "instances", s->ingest.replication.instances);
  498. }
  499. buffer_json_object_close(wb); // replication
  500. }
  501. if(s->ingest.status == RRDHOST_INGEST_STATUS_REPLICATING || s->ingest.status == RRDHOST_INGEST_STATUS_ONLINE) {
  502. buffer_json_member_add_object(wb, "source");
  503. {
  504. char buf[1024 + 1];
  505. snprintfz(buf, 1024, "[%s]:%d%s", s->ingest.peers.local.ip, s->ingest.peers.local.port, s->ingest.ssl ? ":SSL" : "");
  506. buffer_json_member_add_string(wb, "local", buf);
  507. snprintfz(buf, 1024, "[%s]:%d%s", s->ingest.peers.peer.ip, s->ingest.peers.peer.port, s->ingest.ssl ? ":SSL" : "");
  508. buffer_json_member_add_string(wb, "remote", buf);
  509. stream_capabilities_to_json_array(wb, s->ingest.capabilities, "capabilities");
  510. }
  511. buffer_json_object_close(wb); // source
  512. }
  513. }
  514. }
  515. buffer_json_object_close(wb); // collection
  516. }
  517. static void rrdhost_sender_to_json(BUFFER *wb, RRDHOST_STATUS *s, const char *key) {
  518. if(s->stream.status == RRDHOST_STREAM_STATUS_DISABLED)
  519. return;
  520. buffer_json_member_add_object(wb, key);
  521. {
  522. buffer_json_member_add_uint64(wb, "id", s->stream.id);
  523. buffer_json_member_add_uint64(wb, "hops", s->stream.hops);
  524. buffer_json_member_add_string(wb, "status", rrdhost_streaming_status_to_string(s->stream.status));
  525. buffer_json_member_add_time_t(wb, "since", s->stream.since);
  526. buffer_json_member_add_time_t(wb, "age", s->now - s->stream.since);
  527. if (s->stream.status == RRDHOST_STREAM_STATUS_OFFLINE)
  528. buffer_json_member_add_string(wb, "reason", stream_handshake_error_to_string(s->stream.reason));
  529. if (s->stream.status == RRDHOST_STREAM_STATUS_REPLICATING) {
  530. buffer_json_member_add_object(wb, "replication");
  531. {
  532. buffer_json_member_add_boolean(wb, "in_progress", s->stream.replication.in_progress);
  533. buffer_json_member_add_double(wb, "completion", s->stream.replication.completion);
  534. buffer_json_member_add_uint64(wb, "instances", s->stream.replication.instances);
  535. }
  536. buffer_json_object_close(wb);
  537. }
  538. buffer_json_member_add_object(wb, "destination");
  539. {
  540. char buf[1024 + 1];
  541. snprintfz(buf, 1024, "[%s]:%d%s", s->stream.peers.local.ip, s->stream.peers.local.port, s->stream.ssl ? ":SSL" : "");
  542. buffer_json_member_add_string(wb, "local", buf);
  543. snprintfz(buf, 1024, "[%s]:%d%s", s->stream.peers.peer.ip, s->stream.peers.peer.port, s->stream.ssl ? ":SSL" : "");
  544. buffer_json_member_add_string(wb, "remote", buf);
  545. stream_capabilities_to_json_array(wb, s->stream.capabilities, "capabilities");
  546. buffer_json_member_add_object(wb, "traffic");
  547. {
  548. buffer_json_member_add_boolean(wb, "compression", s->stream.compression);
  549. buffer_json_member_add_uint64(wb, "data", s->stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_DATA]);
  550. buffer_json_member_add_uint64(wb, "metadata", s->stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_METADATA]);
  551. buffer_json_member_add_uint64(wb, "functions", s->stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_FUNCTIONS]);
  552. buffer_json_member_add_uint64(wb, "replication", s->stream.sent_bytes_on_this_connection_per_type[STREAM_TRAFFIC_TYPE_REPLICATION]);
  553. }
  554. buffer_json_object_close(wb); // traffic
  555. buffer_json_member_add_array(wb, "candidates");
  556. struct rrdpush_destinations *d;
  557. for (d = s->host->destinations; d; d = d->next) {
  558. buffer_json_add_array_item_object(wb);
  559. buffer_json_member_add_uint64(wb, "attempts", d->attempts);
  560. {
  561. if (d->ssl) {
  562. snprintfz(buf, 1024, "%s:SSL", string2str(d->destination));
  563. buffer_json_member_add_string(wb, "destination", buf);
  564. }
  565. else
  566. buffer_json_member_add_string(wb, "destination", string2str(d->destination));
  567. buffer_json_member_add_time_t(wb, "since", d->since);
  568. buffer_json_member_add_time_t(wb, "age", s->now - d->since);
  569. buffer_json_member_add_string(wb, "last_handshake", stream_handshake_error_to_string(d->reason));
  570. if(d->postpone_reconnection_until > s->now) {
  571. buffer_json_member_add_time_t(wb, "next_check", d->postpone_reconnection_until);
  572. buffer_json_member_add_time_t(wb, "next_in", d->postpone_reconnection_until - s->now);
  573. }
  574. }
  575. buffer_json_object_close(wb); // each candidate
  576. }
  577. buffer_json_array_close(wb); // candidates
  578. }
  579. buffer_json_object_close(wb); // destination
  580. }
  581. buffer_json_object_close(wb); // streaming
  582. }
  583. static void agent_capabilities_to_json(BUFFER *wb, RRDHOST *host, const char *key) {
  584. buffer_json_member_add_array(wb, key);
  585. struct capability *capas = aclk_get_node_instance_capas(host);
  586. for(struct capability *capa = capas; capa->name ;capa++) {
  587. buffer_json_add_array_item_object(wb);
  588. {
  589. buffer_json_member_add_string(wb, "name", capa->name);
  590. buffer_json_member_add_uint64(wb, "version", capa->version);
  591. buffer_json_member_add_boolean(wb, "enabled", capa->enabled);
  592. }
  593. buffer_json_object_close(wb);
  594. }
  595. buffer_json_array_close(wb);
  596. freez(capas);
  597. }
  598. static inline void rrdhost_health_to_json_v2(BUFFER *wb, const char *key, RRDHOST_STATUS *s) {
  599. buffer_json_member_add_object(wb, key);
  600. {
  601. buffer_json_member_add_string(wb, "status", rrdhost_health_status_to_string(s->health.status));
  602. if (s->health.status == RRDHOST_HEALTH_STATUS_RUNNING) {
  603. buffer_json_member_add_object(wb, "alerts");
  604. {
  605. buffer_json_member_add_uint64(wb, "critical", s->health.alerts.critical);
  606. buffer_json_member_add_uint64(wb, "warning", s->health.alerts.warning);
  607. buffer_json_member_add_uint64(wb, "clear", s->health.alerts.clear);
  608. buffer_json_member_add_uint64(wb, "undefined", s->health.alerts.undefined);
  609. buffer_json_member_add_uint64(wb, "uninitialized", s->health.alerts.uninitialized);
  610. }
  611. buffer_json_object_close(wb); // alerts
  612. }
  613. }
  614. buffer_json_object_close(wb); // health
  615. }
  616. static void rrdcontext_to_json_v2_rrdhost(BUFFER *wb, RRDHOST *host, struct rrdcontext_to_json_v2_data *ctl, size_t node_id) {
  617. buffer_json_add_array_item_object(wb); // this node
  618. buffer_json_node_add_v2(wb, host, node_id, 0,
  619. (ctl->mode & CONTEXTS_V2_AGENTS) && !(ctl->mode & CONTEXTS_V2_NODE_INSTANCES));
  620. if(ctl->mode & (CONTEXTS_V2_NODES_INFO | CONTEXTS_V2_NODE_INSTANCES)) {
  621. RRDHOST_STATUS s;
  622. rrdhost_status(host, ctl->now, &s);
  623. if (ctl->mode & (CONTEXTS_V2_NODES_INFO)) {
  624. buffer_json_member_add_string(wb, "v", rrdhost_program_version(host));
  625. host_labels2json(host, wb, "labels");
  626. if (host->system_info) {
  627. buffer_json_member_add_object(wb, "hw");
  628. {
  629. buffer_json_member_add_string_or_empty(wb, "architecture", host->system_info->architecture);
  630. buffer_json_member_add_string_or_empty(wb, "cpu_frequency", host->system_info->host_cpu_freq);
  631. buffer_json_member_add_string_or_empty(wb, "cpus", host->system_info->host_cores);
  632. buffer_json_member_add_string_or_empty(wb, "memory", host->system_info->host_ram_total);
  633. buffer_json_member_add_string_or_empty(wb, "disk_space", host->system_info->host_disk_space);
  634. buffer_json_member_add_string_or_empty(wb, "virtualization", host->system_info->virtualization);
  635. buffer_json_member_add_string_or_empty(wb, "container", host->system_info->container);
  636. }
  637. buffer_json_object_close(wb);
  638. buffer_json_member_add_object(wb, "os");
  639. {
  640. buffer_json_member_add_string_or_empty(wb, "id", host->system_info->host_os_id);
  641. buffer_json_member_add_string_or_empty(wb, "nm", host->system_info->host_os_name);
  642. buffer_json_member_add_string_or_empty(wb, "v", host->system_info->host_os_version);
  643. buffer_json_member_add_object(wb, "kernel");
  644. buffer_json_member_add_string_or_empty(wb, "nm", host->system_info->kernel_name);
  645. buffer_json_member_add_string_or_empty(wb, "v", host->system_info->kernel_version);
  646. buffer_json_object_close(wb);
  647. }
  648. buffer_json_object_close(wb);
  649. }
  650. // created - the node is created but never connected to cloud
  651. // unreachable - not currently connected
  652. // stale - connected but not having live data
  653. // reachable - connected with live data
  654. // pruned - not connected for some time and has been removed
  655. buffer_json_member_add_string(wb, "state", rrdhost_state_cloud_emulation(host) ? "reachable" : "stale");
  656. rrdhost_health_to_json_v2(wb, "health", &s);
  657. agent_capabilities_to_json(wb, host, "capabilities");
  658. }
  659. if (ctl->mode & (CONTEXTS_V2_NODE_INSTANCES)) {
  660. buffer_json_member_add_array(wb, "instances");
  661. buffer_json_add_array_item_object(wb); // this instance
  662. {
  663. buffer_json_agent_status_id(wb, 0, 0);
  664. buffer_json_member_add_object(wb, "db");
  665. {
  666. buffer_json_member_add_string(wb, "status", rrdhost_db_status_to_string(s.db.status));
  667. buffer_json_member_add_string(wb, "liveness", rrdhost_db_liveness_to_string(s.db.liveness));
  668. buffer_json_member_add_string(wb, "mode", rrd_memory_mode_name(s.db.mode));
  669. buffer_json_member_add_time_t(wb, "first_time", s.db.first_time_s);
  670. buffer_json_member_add_time_t(wb, "last_time", s.db.last_time_s);
  671. buffer_json_member_add_uint64(wb, "metrics", s.db.metrics);
  672. buffer_json_member_add_uint64(wb, "instances", s.db.instances);
  673. buffer_json_member_add_uint64(wb, "contexts", s.db.contexts);
  674. }
  675. buffer_json_object_close(wb);
  676. rrdhost_receiver_to_json(wb, &s, "ingest");
  677. rrdhost_sender_to_json(wb, &s, "stream");
  678. buffer_json_member_add_object(wb, "ml");
  679. buffer_json_member_add_string(wb, "status", rrdhost_ml_status_to_string(s.ml.status));
  680. buffer_json_member_add_string(wb, "type", rrdhost_ml_type_to_string(s.ml.type));
  681. if (s.ml.status == RRDHOST_ML_STATUS_RUNNING) {
  682. buffer_json_member_add_object(wb, "metrics");
  683. {
  684. buffer_json_member_add_uint64(wb, "anomalous", s.ml.metrics.anomalous);
  685. buffer_json_member_add_uint64(wb, "normal", s.ml.metrics.normal);
  686. buffer_json_member_add_uint64(wb, "trained", s.ml.metrics.trained);
  687. buffer_json_member_add_uint64(wb, "pending", s.ml.metrics.pending);
  688. buffer_json_member_add_uint64(wb, "silenced", s.ml.metrics.silenced);
  689. }
  690. buffer_json_object_close(wb); // metrics
  691. }
  692. buffer_json_object_close(wb); // ml
  693. rrdhost_health_to_json_v2(wb, "health", &s);
  694. host_functions2json(host, wb); // functions
  695. agent_capabilities_to_json(wb, host, "capabilities");
  696. }
  697. buffer_json_object_close(wb); // this instance
  698. buffer_json_array_close(wb); // instances
  699. }
  700. }
  701. buffer_json_object_close(wb); // this node
  702. }
  703. static ssize_t rrdcontext_to_json_v2_add_host(void *data, RRDHOST *host, bool queryable_host) {
  704. if(!queryable_host || !host->rrdctx.contexts)
  705. // the host matches the 'scope_host' but does not match the 'host' patterns
  706. // or the host does not have any contexts
  707. return 0; // continue to next host
  708. struct rrdcontext_to_json_v2_data *ctl = data;
  709. if(ctl->window.enabled && !rrdhost_matches_window(host, ctl->window.after, ctl->window.before, ctl->now))
  710. // the host does not have data in the requested window
  711. return 0; // continue to next host
  712. if(ctl->request->timeout_ms && now_monotonic_usec() > ctl->timings.received_ut + ctl->request->timeout_ms * USEC_PER_MS)
  713. // timed out
  714. return -2; // stop the query
  715. if(ctl->request->interrupt_callback && ctl->request->interrupt_callback(ctl->request->interrupt_callback_data))
  716. // interrupted
  717. return -1; // stop the query
  718. bool host_matched = (ctl->mode & CONTEXTS_V2_NODES);
  719. bool do_contexts = (ctl->mode & (CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_ALERTS));
  720. ctl->q.host_match = FTS_MATCHED_NONE;
  721. if((ctl->mode & CONTEXTS_V2_SEARCH)) {
  722. // check if we match the host itself
  723. if(ctl->q.pattern && (
  724. full_text_search_string(&ctl->q.fts, ctl->q.pattern, host->hostname) ||
  725. full_text_search_char(&ctl->q.fts, ctl->q.pattern, host->machine_guid) ||
  726. (ctl->q.pattern && full_text_search_char(&ctl->q.fts, ctl->q.pattern, ctl->q.host_node_id_str)))) {
  727. ctl->q.host_match = FTS_MATCHED_HOST;
  728. do_contexts = true;
  729. }
  730. }
  731. if(do_contexts) {
  732. // save it
  733. SIMPLE_PATTERN *old_q = ctl->q.pattern;
  734. if(ctl->q.host_match == FTS_MATCHED_HOST)
  735. // do not do pattern matching on contexts - we matched the host itself
  736. ctl->q.pattern = NULL;
  737. ssize_t added = query_scope_foreach_context(
  738. host, ctl->request->scope_contexts,
  739. ctl->contexts.scope_pattern, ctl->contexts.pattern,
  740. rrdcontext_to_json_v2_add_context, queryable_host, ctl);
  741. // restore it
  742. ctl->q.pattern = old_q;
  743. if(unlikely(added < 0))
  744. return -1; // stop the query
  745. if(added)
  746. host_matched = true;
  747. }
  748. if(!host_matched)
  749. return 0;
  750. if(ctl->mode & CONTEXTS_V2_FUNCTIONS) {
  751. struct function_v2_entry t = {
  752. .used = 1,
  753. .size = 1,
  754. .node_ids = &ctl->nodes.ni,
  755. .help = NULL,
  756. };
  757. host_functions_to_dict(host, ctl->functions.dict, &t, sizeof(t), &t.help);
  758. }
  759. if(ctl->mode & CONTEXTS_V2_NODES) {
  760. struct contexts_v2_node t = {
  761. .ni = ctl->nodes.ni++,
  762. .host = host,
  763. };
  764. dictionary_set(ctl->nodes.dict, host->machine_guid, &t, sizeof(struct contexts_v2_node));
  765. }
  766. return 1;
  767. }
  768. static void buffer_json_contexts_v2_mode_to_array(BUFFER *wb, const char *key, CONTEXTS_V2_MODE mode) {
  769. buffer_json_member_add_array(wb, key);
  770. if(mode & CONTEXTS_V2_VERSIONS)
  771. buffer_json_add_array_item_string(wb, "versions");
  772. if(mode & CONTEXTS_V2_AGENTS)
  773. buffer_json_add_array_item_string(wb, "agents");
  774. if(mode & CONTEXTS_V2_AGENTS_INFO)
  775. buffer_json_add_array_item_string(wb, "agents-info");
  776. if(mode & CONTEXTS_V2_NODES)
  777. buffer_json_add_array_item_string(wb, "nodes");
  778. if(mode & CONTEXTS_V2_NODES_INFO)
  779. buffer_json_add_array_item_string(wb, "nodes-info");
  780. if(mode & CONTEXTS_V2_NODE_INSTANCES)
  781. buffer_json_add_array_item_string(wb, "nodes-instances");
  782. if(mode & CONTEXTS_V2_CONTEXTS)
  783. buffer_json_add_array_item_string(wb, "contexts");
  784. if(mode & CONTEXTS_V2_SEARCH)
  785. buffer_json_add_array_item_string(wb, "search");
  786. if(mode & CONTEXTS_V2_ALERTS)
  787. buffer_json_add_array_item_string(wb, "alerts");
  788. if(mode & CONTEXTS_V2_ALERT_TRANSITIONS)
  789. buffer_json_add_array_item_string(wb, "alert_transitions");
  790. buffer_json_array_close(wb);
  791. }
  792. void buffer_json_query_timings(BUFFER *wb, const char *key, struct query_timings *timings) {
  793. timings->finished_ut = now_monotonic_usec();
  794. if(!timings->executed_ut)
  795. timings->executed_ut = timings->finished_ut;
  796. if(!timings->preprocessed_ut)
  797. timings->preprocessed_ut = timings->received_ut;
  798. buffer_json_member_add_object(wb, key);
  799. buffer_json_member_add_double(wb, "prep_ms", (NETDATA_DOUBLE)(timings->preprocessed_ut - timings->received_ut) / USEC_PER_MS);
  800. buffer_json_member_add_double(wb, "query_ms", (NETDATA_DOUBLE)(timings->executed_ut - timings->preprocessed_ut) / USEC_PER_MS);
  801. buffer_json_member_add_double(wb, "output_ms", (NETDATA_DOUBLE)(timings->finished_ut - timings->executed_ut) / USEC_PER_MS);
  802. buffer_json_member_add_double(wb, "total_ms", (NETDATA_DOUBLE)(timings->finished_ut - timings->received_ut) / USEC_PER_MS);
  803. buffer_json_member_add_double(wb, "cloud_ms", (NETDATA_DOUBLE)(timings->finished_ut - timings->received_ut) / USEC_PER_MS);
  804. buffer_json_object_close(wb);
  805. }
  806. void build_info_to_json_object(BUFFER *b);
  807. void buffer_json_agents_v2(BUFFER *wb, struct query_timings *timings, time_t now_s, bool info, bool array) {
  808. if(!now_s)
  809. now_s = now_realtime_sec();
  810. if(array) {
  811. buffer_json_member_add_array(wb, "agents");
  812. buffer_json_add_array_item_object(wb);
  813. }
  814. else
  815. buffer_json_member_add_object(wb, "agent");
  816. buffer_json_member_add_string(wb, "mg", localhost->machine_guid);
  817. buffer_json_member_add_uuid(wb, "nd", localhost->node_id);
  818. buffer_json_member_add_string(wb, "nm", rrdhost_hostname(localhost));
  819. buffer_json_member_add_time_t(wb, "now", now_s);
  820. if(array)
  821. buffer_json_member_add_uint64(wb, "ai", 0);
  822. if(info) {
  823. buffer_json_member_add_object(wb, "application");
  824. build_info_to_json_object(wb);
  825. buffer_json_object_close(wb); // netdata
  826. buffer_json_cloud_status(wb, now_s);
  827. buffer_json_member_add_array(wb, "db_size");
  828. for (size_t tier = 0; tier < storage_tiers; tier++) {
  829. STORAGE_ENGINE *eng = localhost->db[tier].eng;
  830. if (!eng) continue;
  831. size_t max = storage_engine_disk_space_max(eng->backend, localhost->db[tier].instance);
  832. size_t used = storage_engine_disk_space_used(eng->backend, localhost->db[tier].instance);
  833. time_t first_time_s = storage_engine_global_first_time_s(eng->backend, localhost->db[tier].instance);
  834. size_t currently_collected_metrics = storage_engine_collected_metrics(eng->backend, localhost->db[tier].instance);
  835. NETDATA_DOUBLE percent;
  836. if (used && max)
  837. percent = (NETDATA_DOUBLE) used * 100.0 / (NETDATA_DOUBLE) max;
  838. else
  839. percent = 0.0;
  840. buffer_json_add_array_item_object(wb);
  841. buffer_json_member_add_uint64(wb, "tier", tier);
  842. if(used || max) {
  843. buffer_json_member_add_uint64(wb, "disk_used", used);
  844. buffer_json_member_add_uint64(wb, "disk_max", max);
  845. buffer_json_member_add_double(wb, "disk_percent", percent);
  846. }
  847. if(first_time_s) {
  848. buffer_json_member_add_time_t(wb, "from", first_time_s);
  849. buffer_json_member_add_time_t(wb, "to", now_s);
  850. buffer_json_member_add_time_t(wb, "retention", now_s - first_time_s);
  851. if(used || max) // we have disk space information
  852. buffer_json_member_add_time_t(wb, "expected_retention",
  853. (time_t) ((NETDATA_DOUBLE) (now_s - first_time_s) * 100.0 / percent));
  854. }
  855. if(currently_collected_metrics)
  856. buffer_json_member_add_uint64(wb, "currently_collected_metrics", currently_collected_metrics);
  857. buffer_json_object_close(wb);
  858. }
  859. buffer_json_array_close(wb); // db_size
  860. }
  861. if(timings)
  862. buffer_json_query_timings(wb, "timings", timings);
  863. buffer_json_object_close(wb);
  864. if(array)
  865. buffer_json_array_close(wb);
  866. }
  867. void buffer_json_cloud_timings(BUFFER *wb, const char *key, struct query_timings *timings) {
  868. if(!timings->finished_ut)
  869. timings->finished_ut = now_monotonic_usec();
  870. buffer_json_member_add_object(wb, key);
  871. buffer_json_member_add_double(wb, "routing_ms", 0.0);
  872. buffer_json_member_add_double(wb, "node_max_ms", 0.0);
  873. buffer_json_member_add_double(wb, "total_ms", (NETDATA_DOUBLE)(timings->finished_ut - timings->received_ut) / USEC_PER_MS);
  874. buffer_json_object_close(wb);
  875. }
  876. static void functions_insert_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
  877. struct function_v2_entry *t = value;
  878. // it is initialized with a static reference - we need to mallocz() the array
  879. size_t *v = t->node_ids;
  880. t->node_ids = mallocz(sizeof(size_t));
  881. *t->node_ids = *v;
  882. t->size = 1;
  883. t->used = 1;
  884. }
  885. static bool functions_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *old_value, void *new_value, void *data __maybe_unused) {
  886. struct function_v2_entry *t = old_value, *n = new_value;
  887. size_t *v = n->node_ids;
  888. if(t->used >= t->size) {
  889. t->node_ids = reallocz(t->node_ids, t->size * 2 * sizeof(size_t));
  890. t->size *= 2;
  891. }
  892. t->node_ids[t->used++] = *v;
  893. return true;
  894. }
  895. static void functions_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
  896. struct function_v2_entry *t = value;
  897. freez(t->node_ids);
  898. }
  899. static bool contexts_conflict_callback(const DICTIONARY_ITEM *item __maybe_unused, void *old_value, void *new_value, void *data __maybe_unused) {
  900. struct context_v2_entry *o = old_value;
  901. struct context_v2_entry *n = new_value;
  902. o->count++;
  903. if(o->family != n->family) {
  904. STRING *m = string_2way_merge(o->family, n->family);
  905. string_freez(o->family);
  906. o->family = m;
  907. }
  908. if(o->priority != n->priority) {
  909. if((o->flags & RRD_FLAG_COLLECTED) && !(n->flags & RRD_FLAG_COLLECTED))
  910. // keep o
  911. ;
  912. else if(!(o->flags & RRD_FLAG_COLLECTED) && (n->flags & RRD_FLAG_COLLECTED))
  913. // keep n
  914. o->priority = n->priority;
  915. else
  916. // keep the min
  917. o->priority = MIN(o->priority, n->priority);
  918. }
  919. if(o->first_time_s && n->first_time_s)
  920. o->first_time_s = MIN(o->first_time_s, n->first_time_s);
  921. else if(!o->first_time_s)
  922. o->first_time_s = n->first_time_s;
  923. if(o->last_time_s && n->last_time_s)
  924. o->last_time_s = MAX(o->last_time_s, n->last_time_s);
  925. else if(!o->last_time_s)
  926. o->last_time_s = n->last_time_s;
  927. o->flags |= n->flags;
  928. o->match = MIN(o->match, n->match);
  929. string_freez(n->family);
  930. return true;
  931. }
  932. static void contexts_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
  933. struct context_v2_entry *z = value;
  934. string_freez(z->family);
  935. }
  936. static void rrdcontext_v2_set_transition_filter(const char *machine_guid, const char *context, time_t alarm_id, void *data) {
  937. struct rrdcontext_to_json_v2_data *ctl = data;
  938. if(machine_guid && *machine_guid) {
  939. if(ctl->nodes.scope_pattern)
  940. simple_pattern_free(ctl->nodes.scope_pattern);
  941. if(ctl->nodes.pattern)
  942. simple_pattern_free(ctl->nodes.pattern);
  943. ctl->nodes.scope_pattern = string_to_simple_pattern(machine_guid);
  944. ctl->nodes.pattern = NULL;
  945. }
  946. if(context && *context) {
  947. if(ctl->contexts.scope_pattern)
  948. simple_pattern_free(ctl->contexts.scope_pattern);
  949. if(ctl->contexts.pattern)
  950. simple_pattern_free(ctl->contexts.pattern);
  951. ctl->contexts.scope_pattern = string_to_simple_pattern(context);
  952. ctl->contexts.pattern = NULL;
  953. }
  954. ctl->alerts.alarm_id_filter = alarm_id;
  955. }
  956. struct alert_instances_callback_data {
  957. BUFFER *wb;
  958. struct rrdcontext_to_json_v2_data *ctl;
  959. bool debug;
  960. };
  961. static void contexts_v2_alert_config_to_json_from_sql_alert_config_data(struct sql_alert_config_data *t, void *data) {
  962. struct alert_transitions_callback_data *d = data;
  963. BUFFER *wb = d->wb;
  964. bool debug = d->debug;
  965. d->configs_added++;
  966. if(d->only_one_config)
  967. buffer_json_add_array_item_object(wb); // alert config
  968. {
  969. buffer_json_member_add_string(wb, "name", t->name);
  970. buffer_json_member_add_uuid(wb, "config_hash_id", t->config_hash_id);
  971. buffer_json_member_add_object(wb, "selectors");
  972. {
  973. bool is_template = t->selectors.on_template && *t->selectors.on_template ? true : false;
  974. buffer_json_member_add_string(wb, "type", is_template ? "template" : "alarm");
  975. buffer_json_member_add_string(wb, "on", is_template ? t->selectors.on_template : t->selectors.on_key);
  976. buffer_json_member_add_string(wb, "os", t->selectors.os);
  977. buffer_json_member_add_string(wb, "hosts", t->selectors.hosts);
  978. buffer_json_member_add_string(wb, "families", t->selectors.families);
  979. buffer_json_member_add_string(wb, "plugin", t->selectors.plugin);
  980. buffer_json_member_add_string(wb, "module", t->selectors.module);
  981. buffer_json_member_add_string(wb, "host_labels", t->selectors.host_labels);
  982. buffer_json_member_add_string(wb, "chart_labels", t->selectors.chart_labels);
  983. buffer_json_member_add_string(wb, "charts", t->selectors.charts);
  984. }
  985. buffer_json_object_close(wb); // selectors
  986. buffer_json_member_add_object(wb, "value"); // value
  987. {
  988. // buffer_json_member_add_string(wb, "every", t->value.every); // does not exist in Netdata Cloud
  989. buffer_json_member_add_string(wb, "units", t->value.units);
  990. buffer_json_member_add_uint64(wb, "update_every", t->value.update_every);
  991. if (t->value.db.after || debug) {
  992. buffer_json_member_add_object(wb, "db");
  993. {
  994. // buffer_json_member_add_string(wb, "lookup", t->value.db.lookup); // does not exist in Netdata Cloud
  995. buffer_json_member_add_time_t(wb, "after", t->value.db.after);
  996. buffer_json_member_add_time_t(wb, "before", t->value.db.before);
  997. buffer_json_member_add_string(wb, "method", t->value.db.method);
  998. buffer_json_member_add_string(wb, "dimensions", t->value.db.dimensions);
  999. web_client_api_request_v1_data_options_to_buffer_json_array(wb, "options",(RRDR_OPTIONS) t->value.db.options);
  1000. }
  1001. buffer_json_object_close(wb); // db
  1002. }
  1003. if (t->value.calc || debug)
  1004. buffer_json_member_add_string(wb, "calc", t->value.calc);
  1005. }
  1006. buffer_json_object_close(wb); // value
  1007. if (t->status.warn || t->status.crit || debug) {
  1008. buffer_json_member_add_object(wb, "status"); // status
  1009. {
  1010. NETDATA_DOUBLE green = t->status.green ? str2ndd(t->status.green, NULL) : NAN;
  1011. NETDATA_DOUBLE red = t->status.red ? str2ndd(t->status.red, NULL) : NAN;
  1012. if (!isnan(green) || debug)
  1013. buffer_json_member_add_double(wb, "green", green);
  1014. if (!isnan(red) || debug)
  1015. buffer_json_member_add_double(wb, "red", red);
  1016. if (t->status.warn || debug)
  1017. buffer_json_member_add_string(wb, "warn", t->status.warn);
  1018. if (t->status.crit || debug)
  1019. buffer_json_member_add_string(wb, "crit", t->status.crit);
  1020. }
  1021. buffer_json_object_close(wb); // status
  1022. }
  1023. buffer_json_member_add_object(wb, "notification");
  1024. {
  1025. buffer_json_member_add_string(wb, "type", "agent");
  1026. buffer_json_member_add_string(wb, "exec", t->notification.exec ? t->notification.exec : NULL);
  1027. buffer_json_member_add_string(wb, "to", t->notification.to_key ? t->notification.to_key : string2str(localhost->health.health_default_recipient));
  1028. buffer_json_member_add_string(wb, "delay", t->notification.delay);
  1029. buffer_json_member_add_string(wb, "repeat", t->notification.repeat);
  1030. buffer_json_member_add_string(wb, "options", t->notification.options);
  1031. }
  1032. buffer_json_object_close(wb); // notification
  1033. buffer_json_member_add_string(wb, "class", t->classification);
  1034. buffer_json_member_add_string(wb, "component", t->component);
  1035. buffer_json_member_add_string(wb, "type", t->type);
  1036. buffer_json_member_add_string(wb, "info", t->info);
  1037. // buffer_json_member_add_string(wb, "source", t->source); // moved to alert instance
  1038. }
  1039. if(d->only_one_config)
  1040. buffer_json_object_close(wb);
  1041. }
  1042. int contexts_v2_alert_config_to_json(struct web_client *w, const char *config_hash_id) {
  1043. struct alert_transitions_callback_data data = {
  1044. .wb = w->response.data,
  1045. .debug = false,
  1046. .only_one_config = false,
  1047. };
  1048. DICTIONARY *configs = dictionary_create(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE);
  1049. dictionary_set(configs, config_hash_id, NULL, 0);
  1050. buffer_flush(w->response.data);
  1051. buffer_json_initialize(w->response.data, "\"", "\"", 0, true, false);
  1052. int added = sql_get_alert_configuration(configs, contexts_v2_alert_config_to_json_from_sql_alert_config_data, &data, false);
  1053. buffer_json_finalize(w->response.data);
  1054. int ret = HTTP_RESP_OK;
  1055. if(added <= 0) {
  1056. buffer_flush(w->response.data);
  1057. w->response.data->content_type = CT_TEXT_PLAIN;
  1058. if(added < 0) {
  1059. buffer_strcat(w->response.data, "Failed to execute SQL query.");
  1060. ret = HTTP_RESP_INTERNAL_SERVER_ERROR;
  1061. }
  1062. else {
  1063. buffer_strcat(w->response.data, "Config is not found.");
  1064. ret = HTTP_RESP_NOT_FOUND;
  1065. }
  1066. }
  1067. return ret;
  1068. }
  1069. static int contexts_v2_alert_instance_to_json_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data) {
  1070. struct sql_alert_instance_v2_entry *t = value;
  1071. struct alert_instances_callback_data *d = data;
  1072. struct rrdcontext_to_json_v2_data *ctl = d->ctl; (void)ctl;
  1073. bool debug = d->debug; (void)debug;
  1074. BUFFER *wb = d->wb;
  1075. buffer_json_add_array_item_object(wb);
  1076. {
  1077. buffer_json_member_add_uint64(wb, "ni", t->ni);
  1078. buffer_json_member_add_string(wb, "nm", string2str(t->name));
  1079. buffer_json_member_add_string(wb, "ch", string2str(t->chart_id));
  1080. buffer_json_member_add_string(wb, "ch_n", string2str(t->chart_name));
  1081. if(ctl->request->options & CONTEXT_V2_OPTION_ALERTS_WITH_SUMMARY)
  1082. buffer_json_member_add_uint64(wb, "ati", t->ati);
  1083. if(ctl->request->options & CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES) {
  1084. buffer_json_member_add_string(wb, "units", string2str(t->units));
  1085. buffer_json_member_add_string(wb, "fami", string2str(t->family));
  1086. buffer_json_member_add_string(wb, "info", string2str(t->info));
  1087. buffer_json_member_add_string(wb, "ctx", string2str(t->context));
  1088. buffer_json_member_add_string(wb, "st", rrdcalc_status2string(t->status));
  1089. buffer_json_member_add_uuid(wb, "tr_i", &t->last_transition_id);
  1090. buffer_json_member_add_double(wb, "tr_v", t->last_status_change_value);
  1091. buffer_json_member_add_time_t(wb, "tr_t", t->last_status_change);
  1092. buffer_json_member_add_uuid(wb, "cfg", &t->config_hash_id);
  1093. buffer_json_member_add_string(wb, "src", string2str(t->source));
  1094. buffer_json_member_add_string(wb, "to", string2str(t->recipient));
  1095. buffer_json_member_add_string(wb, "tp", string2str(t->type));
  1096. buffer_json_member_add_string(wb, "cm", string2str(t->component));
  1097. buffer_json_member_add_string(wb, "cl", string2str(t->classification));
  1098. // Agent specific fields
  1099. buffer_json_member_add_uint64(wb, "gi", t->global_id);
  1100. // rrdcalc_flags_to_json_array (wb, "flags", t->flags);
  1101. }
  1102. if(ctl->request->options & CONTEXT_V2_OPTION_ALERTS_WITH_VALUES) {
  1103. // Netdata Cloud fetched these by querying the agents
  1104. buffer_json_member_add_double(wb, "v", t->value);
  1105. buffer_json_member_add_time_t(wb, "t", t->last_updated);
  1106. }
  1107. }
  1108. buffer_json_object_close(wb); // alert instance
  1109. return 1;
  1110. }
  1111. static void contexts_v2_alert_instances_to_json(BUFFER *wb, const char *key, struct rrdcontext_to_json_v2_data *ctl, bool debug) {
  1112. buffer_json_member_add_array(wb, key);
  1113. {
  1114. struct alert_instances_callback_data data = {
  1115. .wb = wb,
  1116. .ctl = ctl,
  1117. .debug = debug,
  1118. };
  1119. dictionary_walkthrough_rw(ctl->alerts.alert_instances, DICTIONARY_LOCK_READ,
  1120. contexts_v2_alert_instance_to_json_callback, &data);
  1121. }
  1122. buffer_json_array_close(wb); // alerts_instances
  1123. }
  1124. static void contexts_v2_alerts_to_json(BUFFER *wb, struct rrdcontext_to_json_v2_data *ctl, bool debug) {
  1125. if(ctl->request->options & CONTEXT_V2_OPTION_ALERTS_WITH_SUMMARY) {
  1126. buffer_json_member_add_array(wb, "alerts");
  1127. {
  1128. struct alert_v2_entry *t;
  1129. dfe_start_read(ctl->alerts.alerts, t)
  1130. {
  1131. buffer_json_add_array_item_object(wb);
  1132. {
  1133. buffer_json_member_add_uint64(wb, "ati", t->ati);
  1134. buffer_json_member_add_string(wb, "nm", string2str(t->name));
  1135. buffer_json_member_add_uint64(wb, "cr", t->critical);
  1136. buffer_json_member_add_uint64(wb, "wr", t->warning);
  1137. buffer_json_member_add_uint64(wb, "cl", t->clear);
  1138. buffer_json_member_add_uint64(wb, "er", t->error);
  1139. buffer_json_member_add_uint64(wb, "in", t->instances);
  1140. buffer_json_member_add_uint64(wb, "nd", dictionary_entries(t->nodes));
  1141. buffer_json_member_add_uint64(wb, "cfg", dictionary_entries(t->configs));
  1142. }
  1143. buffer_json_object_close(wb); // alert name
  1144. }
  1145. dfe_done(t);
  1146. }
  1147. buffer_json_array_close(wb); // alerts
  1148. }
  1149. if(ctl->request->options & (CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES|CONTEXT_V2_OPTION_ALERTS_WITH_VALUES)) {
  1150. contexts_v2_alert_instances_to_json(wb, "alert_instances", ctl, debug);
  1151. }
  1152. }
  1153. #define SQL_TRANSITION_DATA_SMALL_STRING (6 * 8)
  1154. #define SQL_TRANSITION_DATA_MEDIUM_STRING (12 * 8)
  1155. #define SQL_TRANSITION_DATA_BIG_STRING 512
  1156. struct sql_alert_transition_fixed_size {
  1157. usec_t global_id;
  1158. uuid_t transition_id;
  1159. uuid_t host_id;
  1160. uuid_t config_hash_id;
  1161. uint32_t alarm_id;
  1162. char alert_name[SQL_TRANSITION_DATA_SMALL_STRING];
  1163. char chart[RRD_ID_LENGTH_MAX];
  1164. char chart_name[RRD_ID_LENGTH_MAX];
  1165. char chart_context[SQL_TRANSITION_DATA_MEDIUM_STRING];
  1166. char family[SQL_TRANSITION_DATA_SMALL_STRING];
  1167. char recipient[SQL_TRANSITION_DATA_MEDIUM_STRING];
  1168. char units[SQL_TRANSITION_DATA_SMALL_STRING];
  1169. char exec[SQL_TRANSITION_DATA_BIG_STRING];
  1170. char info[SQL_TRANSITION_DATA_BIG_STRING];
  1171. char classification[SQL_TRANSITION_DATA_SMALL_STRING];
  1172. char type[SQL_TRANSITION_DATA_SMALL_STRING];
  1173. char component[SQL_TRANSITION_DATA_SMALL_STRING];
  1174. time_t when_key;
  1175. time_t duration;
  1176. time_t non_clear_duration;
  1177. uint64_t flags;
  1178. time_t delay_up_to_timestamp;
  1179. time_t exec_run_timestamp;
  1180. int exec_code;
  1181. int new_status;
  1182. int old_status;
  1183. int delay;
  1184. time_t last_repeat;
  1185. NETDATA_DOUBLE new_value;
  1186. NETDATA_DOUBLE old_value;
  1187. char machine_guid[UUID_STR_LEN];
  1188. struct sql_alert_transition_fixed_size *next;
  1189. struct sql_alert_transition_fixed_size *prev;
  1190. };
  1191. static struct sql_alert_transition_fixed_size *contexts_v2_alert_transition_dup(struct sql_alert_transition_data *t, const char *machine_guid, struct sql_alert_transition_fixed_size *dst) {
  1192. struct sql_alert_transition_fixed_size *n = dst ? dst : mallocz(sizeof(*n));
  1193. n->global_id = t->global_id;
  1194. uuid_copy(n->transition_id, *t->transition_id);
  1195. uuid_copy(n->host_id, *t->host_id);
  1196. uuid_copy(n->config_hash_id, *t->config_hash_id);
  1197. n->alarm_id = t->alarm_id;
  1198. strncpyz(n->alert_name, t->alert_name ? t->alert_name : "", sizeof(n->alert_name) - 1);
  1199. strncpyz(n->chart, t->chart ? t->chart : "", sizeof(n->chart) - 1);
  1200. strncpyz(n->chart_name, t->chart_name ? t->chart_name : n->chart, sizeof(n->chart_name) - 1);
  1201. strncpyz(n->chart_context, t->chart_context ? t->chart_context : "", sizeof(n->chart_context) - 1);
  1202. strncpyz(n->family, t->family ? t->family : "", sizeof(n->family) - 1);
  1203. strncpyz(n->recipient, t->recipient ? t->recipient : "", sizeof(n->recipient) - 1);
  1204. strncpyz(n->units, t->units ? t->units : "", sizeof(n->units) - 1);
  1205. strncpyz(n->exec, t->exec ? t->exec : "", sizeof(n->exec) - 1);
  1206. strncpyz(n->info, t->info ? t->info : "", sizeof(n->info) - 1);
  1207. strncpyz(n->classification, t->classification ? t->classification : "", sizeof(n->classification) - 1);
  1208. strncpyz(n->type, t->type ? t->type : "", sizeof(n->type) - 1);
  1209. strncpyz(n->component, t->component ? t->component : "", sizeof(n->component) - 1);
  1210. n->when_key = t->when_key;
  1211. n->duration = t->duration;
  1212. n->non_clear_duration = t->non_clear_duration;
  1213. n->flags = t->flags;
  1214. n->delay_up_to_timestamp = t->delay_up_to_timestamp;
  1215. n->exec_run_timestamp = t->exec_run_timestamp;
  1216. n->exec_code = t->exec_code;
  1217. n->new_status = t->new_status;
  1218. n->old_status = t->old_status;
  1219. n->delay = t->delay;
  1220. n->last_repeat = t->last_repeat;
  1221. n->new_value = t->new_value;
  1222. n->old_value = t->old_value;
  1223. memcpy(n->machine_guid, machine_guid, sizeof(n->machine_guid));
  1224. n->next = n->prev = NULL;
  1225. return n;
  1226. }
  1227. static void contexts_v2_alert_transition_free(struct sql_alert_transition_fixed_size *t) {
  1228. freez(t);
  1229. }
  1230. static inline void contexts_v2_alert_transition_keep(struct alert_transitions_callback_data *d, struct sql_alert_transition_data *t, const char *machine_guid) {
  1231. d->items_matched++;
  1232. if(unlikely(t->global_id <= d->ctl->request->alerts.global_id_anchor)) {
  1233. // this is in our past, we are not interested
  1234. d->operations.skips_before++;
  1235. return;
  1236. }
  1237. if(unlikely(!d->base)) {
  1238. d->last_added = contexts_v2_alert_transition_dup(t, machine_guid, NULL);
  1239. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(d->base, d->last_added, prev, next);
  1240. d->items_to_return++;
  1241. d->operations.first++;
  1242. return;
  1243. }
  1244. struct sql_alert_transition_fixed_size *last = d->last_added;
  1245. while(last->prev != d->base->prev && t->global_id > last->prev->global_id) {
  1246. last = last->prev;
  1247. d->operations.backwards++;
  1248. }
  1249. while(last->next && t->global_id < last->next->global_id) {
  1250. last = last->next;
  1251. d->operations.forwards++;
  1252. }
  1253. if(d->items_to_return >= d->max_items_to_return) {
  1254. if(last == d->base->prev && t->global_id < last->global_id) {
  1255. d->operations.skips_after++;
  1256. return;
  1257. }
  1258. }
  1259. d->items_to_return++;
  1260. if(t->global_id > last->global_id) {
  1261. if(d->items_to_return > d->max_items_to_return) {
  1262. d->items_to_return--;
  1263. d->operations.shifts++;
  1264. d->last_added = d->base->prev;
  1265. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(d->base, d->last_added, prev, next);
  1266. d->last_added = contexts_v2_alert_transition_dup(t, machine_guid, d->last_added);
  1267. }
  1268. DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(d->base, d->last_added, prev, next);
  1269. d->operations.prepend++;
  1270. }
  1271. else {
  1272. d->last_added = contexts_v2_alert_transition_dup(t, machine_guid, NULL);
  1273. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(d->base, d->last_added, prev, next);
  1274. d->operations.append++;
  1275. }
  1276. while(d->items_to_return > d->max_items_to_return) {
  1277. // we have to remove something
  1278. struct sql_alert_transition_fixed_size *tmp = d->base->prev;
  1279. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(d->base, tmp, prev, next);
  1280. d->items_to_return--;
  1281. if(unlikely(d->last_added == tmp))
  1282. d->last_added = d->base;
  1283. contexts_v2_alert_transition_free(tmp);
  1284. d->operations.shifts++;
  1285. }
  1286. }
  1287. static void contexts_v2_alert_transition_callback(struct sql_alert_transition_data *t, void *data) {
  1288. struct alert_transitions_callback_data *d = data;
  1289. d->items_evaluated++;
  1290. char machine_guid[UUID_STR_LEN] = "";
  1291. uuid_unparse_lower(*t->host_id, machine_guid);
  1292. const char *facets[ATF_TOTAL_ENTRIES] = {
  1293. [ATF_STATUS] = rrdcalc_status2string(t->new_status),
  1294. [ATF_CLASS] = t->classification,
  1295. [ATF_TYPE] = t->type,
  1296. [ATF_COMPONENT] = t->component,
  1297. [ATF_ROLE] = t->recipient && *t->recipient ? t->recipient : string2str(localhost->health.health_default_recipient),
  1298. [ATF_NODE] = machine_guid,
  1299. [ATF_ALERT_NAME] = t->alert_name,
  1300. [ATF_CHART_NAME] = t->chart_name,
  1301. [ATF_CONTEXT] = t->chart_context,
  1302. };
  1303. for(size_t i = 0; i < ATF_TOTAL_ENTRIES ;i++) {
  1304. if (!facets[i] || !*facets[i]) facets[i] = "unknown";
  1305. struct facet_entry tmp = {
  1306. .count = 0,
  1307. };
  1308. dictionary_set(d->facets[i].dict, facets[i], &tmp, sizeof(tmp));
  1309. }
  1310. bool selected[ATF_TOTAL_ENTRIES] = { 0 };
  1311. uint32_t selected_by = 0;
  1312. for(size_t i = 0; i < ATF_TOTAL_ENTRIES ;i++) {
  1313. selected[i] = !d->facets[i].pattern || simple_pattern_matches(d->facets[i].pattern, facets[i]);
  1314. if(selected[i])
  1315. selected_by++;
  1316. }
  1317. if(selected_by == ATF_TOTAL_ENTRIES) {
  1318. // this item is selected by all facets
  1319. // put it in our result (if it fits)
  1320. contexts_v2_alert_transition_keep(d, t, machine_guid);
  1321. }
  1322. if(selected_by >= ATF_TOTAL_ENTRIES - 1) {
  1323. // this item is selected by all, or all except one facet
  1324. // in both cases we need to add it to our counters
  1325. for (size_t i = 0; i < ATF_TOTAL_ENTRIES; i++) {
  1326. uint32_t counted_by = selected_by;
  1327. if (counted_by != ATF_TOTAL_ENTRIES) {
  1328. counted_by = 0;
  1329. for (size_t j = 0; j < ATF_TOTAL_ENTRIES; j++) {
  1330. if (i == j || selected[j])
  1331. counted_by++;
  1332. }
  1333. }
  1334. if (counted_by == ATF_TOTAL_ENTRIES) {
  1335. // we need to count it on this facet
  1336. struct facet_entry *x = dictionary_get(d->facets[i].dict, facets[i]);
  1337. internal_fatal(!x, "facet is not found");
  1338. if(x)
  1339. x->count++;
  1340. }
  1341. }
  1342. }
  1343. }
  1344. static void contexts_v2_alert_transitions_to_json(BUFFER *wb, struct rrdcontext_to_json_v2_data *ctl, bool debug) {
  1345. struct alert_transitions_callback_data data = {
  1346. .wb = wb,
  1347. .ctl = ctl,
  1348. .debug = debug,
  1349. .only_one_config = true,
  1350. .max_items_to_return = ctl->request->alerts.last,
  1351. .items_to_return = 0,
  1352. .base = NULL,
  1353. };
  1354. for(size_t i = 0; i < ATF_TOTAL_ENTRIES ;i++) {
  1355. data.facets[i].dict = dictionary_create_advanced(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_FIXED_SIZE | DICT_OPTION_DONT_OVERWRITE_VALUE, NULL, sizeof(struct facet_entry));
  1356. if(ctl->request->alerts.facets[i])
  1357. data.facets[i].pattern = simple_pattern_create(ctl->request->alerts.facets[i], ",|", SIMPLE_PATTERN_EXACT, false);
  1358. }
  1359. sql_alert_transitions(
  1360. ctl->nodes.dict,
  1361. ctl->window.after,
  1362. ctl->window.before,
  1363. ctl->request->contexts,
  1364. ctl->request->alerts.alert,
  1365. ctl->request->alerts.transition,
  1366. contexts_v2_alert_transition_callback,
  1367. &data,
  1368. debug);
  1369. buffer_json_member_add_array(wb, "facets");
  1370. for (size_t i = 0; i < ATF_TOTAL_ENTRIES; i++) {
  1371. buffer_json_add_array_item_object(wb);
  1372. {
  1373. buffer_json_member_add_string(wb, "id", alert_transition_facets[i].id);
  1374. buffer_json_member_add_string(wb, "name", alert_transition_facets[i].name);
  1375. buffer_json_member_add_uint64(wb, "order", alert_transition_facets[i].order);
  1376. buffer_json_member_add_array(wb, "options");
  1377. {
  1378. struct facet_entry *x;
  1379. dfe_start_read(data.facets[i].dict, x) {
  1380. buffer_json_add_array_item_object(wb);
  1381. {
  1382. buffer_json_member_add_string(wb, "id", x_dfe.name);
  1383. if (i == ATF_NODE) {
  1384. RRDHOST *host = rrdhost_find_by_guid(x_dfe.name);
  1385. if (host)
  1386. buffer_json_member_add_string(wb, "name", rrdhost_hostname(host));
  1387. else
  1388. buffer_json_member_add_string(wb, "name", x_dfe.name);
  1389. } else
  1390. buffer_json_member_add_string(wb, "name", x_dfe.name);
  1391. buffer_json_member_add_uint64(wb, "count", x->count);
  1392. }
  1393. buffer_json_object_close(wb);
  1394. }
  1395. dfe_done(x);
  1396. }
  1397. buffer_json_array_close(wb); // options
  1398. }
  1399. buffer_json_object_close(wb); // facet
  1400. }
  1401. buffer_json_array_close(wb); // facets
  1402. buffer_json_member_add_array(wb, "transitions");
  1403. for(struct sql_alert_transition_fixed_size *t = data.base; t ; t = t->next) {
  1404. buffer_json_add_array_item_object(wb);
  1405. {
  1406. RRDHOST *host = rrdhost_find_by_guid(t->machine_guid);
  1407. buffer_json_member_add_uint64(wb, "gi", t->global_id);
  1408. buffer_json_member_add_uuid(wb, "transition_id", &t->transition_id);
  1409. buffer_json_member_add_uuid(wb, "config_hash_id", &t->config_hash_id);
  1410. buffer_json_member_add_string(wb, "machine_guid", t->machine_guid);
  1411. if(host) {
  1412. buffer_json_member_add_string(wb, "hostname", rrdhost_hostname(host));
  1413. if(host->node_id)
  1414. buffer_json_member_add_uuid(wb, "node_id", host->node_id);
  1415. }
  1416. buffer_json_member_add_string(wb, "alert", *t->alert_name ? t->alert_name : NULL);
  1417. buffer_json_member_add_string(wb, "instance", *t->chart ? t->chart : NULL);
  1418. buffer_json_member_add_string(wb, "instance_n", *t->chart_name ? t->chart_name : NULL);
  1419. buffer_json_member_add_string(wb, "context", *t->chart_context ? t->chart_context : NULL);
  1420. // buffer_json_member_add_string(wb, "family", *t->family ? t->family : NULL);
  1421. buffer_json_member_add_string(wb, "component", *t->component ? t->component : NULL);
  1422. buffer_json_member_add_string(wb, "classification", *t->classification ? t->classification : NULL);
  1423. buffer_json_member_add_string(wb, "type", *t->type ? t->type : NULL);
  1424. buffer_json_member_add_time_t(wb, "when", t->when_key);
  1425. buffer_json_member_add_string(wb, "info", *t->info ? t->info : "");
  1426. buffer_json_member_add_string(wb, "units", *t->units ? t->units : NULL);
  1427. buffer_json_member_add_object(wb, "new");
  1428. {
  1429. buffer_json_member_add_string(wb, "status", rrdcalc_status2string(t->new_status));
  1430. buffer_json_member_add_double(wb, "value", t->new_value);
  1431. }
  1432. buffer_json_object_close(wb); // new
  1433. buffer_json_member_add_object(wb, "old");
  1434. {
  1435. buffer_json_member_add_string(wb, "status", rrdcalc_status2string(t->old_status));
  1436. buffer_json_member_add_double(wb, "value", t->old_value);
  1437. buffer_json_member_add_time_t(wb, "duration", t->duration);
  1438. buffer_json_member_add_time_t(wb, "raised_duration", t->non_clear_duration);
  1439. }
  1440. buffer_json_object_close(wb); // old
  1441. buffer_json_member_add_object(wb, "notification");
  1442. {
  1443. buffer_json_member_add_time_t(wb, "when", t->exec_run_timestamp);
  1444. buffer_json_member_add_time_t(wb, "delay", t->delay);
  1445. buffer_json_member_add_time_t(wb, "delay_up_to_time", t->delay_up_to_timestamp);
  1446. health_entry_flags_to_json_array(wb, "flags", t->flags);
  1447. buffer_json_member_add_string(wb, "exec", *t->exec ? t->exec : string2str(localhost->health.health_default_exec));
  1448. buffer_json_member_add_uint64(wb, "exec_code", t->exec_code);
  1449. buffer_json_member_add_string(wb, "to", *t->recipient ? t->recipient : string2str(localhost->health.health_default_recipient));
  1450. }
  1451. buffer_json_object_close(wb); // notification
  1452. }
  1453. buffer_json_object_close(wb); // a transition
  1454. }
  1455. buffer_json_array_close(wb); // all transitions
  1456. if(ctl->options & CONTEXT_V2_OPTION_ALERTS_WITH_CONFIGURATIONS) {
  1457. DICTIONARY *configs = dictionary_create(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE);
  1458. for(struct sql_alert_transition_fixed_size *t = data.base; t ; t = t->next) {
  1459. char guid[UUID_STR_LEN];
  1460. uuid_unparse_lower(t->config_hash_id, guid);
  1461. dictionary_set(configs, guid, NULL, 0);
  1462. }
  1463. buffer_json_member_add_array(wb, "configurations");
  1464. sql_get_alert_configuration(configs, contexts_v2_alert_config_to_json_from_sql_alert_config_data, &data, debug);
  1465. buffer_json_array_close(wb);
  1466. dictionary_destroy(configs);
  1467. }
  1468. while(data.base) {
  1469. struct sql_alert_transition_fixed_size *t = data.base;
  1470. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(data.base, t, prev, next);
  1471. contexts_v2_alert_transition_free(t);
  1472. }
  1473. for(size_t i = 0; i < ATF_TOTAL_ENTRIES ;i++) {
  1474. dictionary_destroy(data.facets[i].dict);
  1475. simple_pattern_free(data.facets[i].pattern);
  1476. }
  1477. buffer_json_member_add_object(wb, "items");
  1478. {
  1479. // all the items in the window, under the scope_nodes, ignoring the facets (filters)
  1480. buffer_json_member_add_uint64(wb, "evaluated", data.items_evaluated);
  1481. // all the items matching the query (if you didn't put anchor_gi and last, these are all the items you would get back)
  1482. buffer_json_member_add_uint64(wb, "matched", data.items_matched);
  1483. // the items included in this response
  1484. buffer_json_member_add_uint64(wb, "returned", data.items_to_return);
  1485. // same as last=X parameter
  1486. buffer_json_member_add_uint64(wb, "max_to_return", data.max_items_to_return);
  1487. // items before the first returned, this should be 0 if anchor_gi is not set
  1488. buffer_json_member_add_uint64(wb, "before", data.operations.skips_before);
  1489. // items after the last returned, when this is zero there aren't any items after the current list
  1490. buffer_json_member_add_uint64(wb, "after", data.operations.skips_after + data.operations.shifts);
  1491. }
  1492. buffer_json_object_close(wb); // items
  1493. if(debug) {
  1494. buffer_json_member_add_object(wb, "stats");
  1495. {
  1496. buffer_json_member_add_uint64(wb, "first", data.operations.first);
  1497. buffer_json_member_add_uint64(wb, "prepend", data.operations.prepend);
  1498. buffer_json_member_add_uint64(wb, "append", data.operations.append);
  1499. buffer_json_member_add_uint64(wb, "backwards", data.operations.backwards);
  1500. buffer_json_member_add_uint64(wb, "forwards", data.operations.forwards);
  1501. buffer_json_member_add_uint64(wb, "shifts", data.operations.shifts);
  1502. buffer_json_member_add_uint64(wb, "skips_before", data.operations.skips_before);
  1503. buffer_json_member_add_uint64(wb, "skips_after", data.operations.skips_after);
  1504. }
  1505. buffer_json_object_close(wb);
  1506. }
  1507. }
  1508. int rrdcontext_to_json_v2(BUFFER *wb, struct api_v2_contexts_request *req, CONTEXTS_V2_MODE mode) {
  1509. int resp = HTTP_RESP_OK;
  1510. bool run = true;
  1511. if(mode & CONTEXTS_V2_SEARCH)
  1512. mode |= CONTEXTS_V2_CONTEXTS;
  1513. if(mode & (CONTEXTS_V2_AGENTS_INFO))
  1514. mode |= CONTEXTS_V2_AGENTS;
  1515. if(mode & (CONTEXTS_V2_FUNCTIONS | CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_SEARCH | CONTEXTS_V2_NODES_INFO | CONTEXTS_V2_NODE_INSTANCES))
  1516. mode |= CONTEXTS_V2_NODES;
  1517. if(mode & CONTEXTS_V2_ALERTS) {
  1518. mode |= CONTEXTS_V2_NODES;
  1519. req->options &= ~CONTEXT_V2_OPTION_ALERTS_WITH_CONFIGURATIONS;
  1520. if(!(req->options & (CONTEXT_V2_OPTION_ALERTS_WITH_SUMMARY|CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES|CONTEXT_V2_OPTION_ALERTS_WITH_VALUES)))
  1521. req->options |= CONTEXT_V2_OPTION_ALERTS_WITH_SUMMARY;
  1522. }
  1523. if(mode & CONTEXTS_V2_ALERT_TRANSITIONS) {
  1524. mode |= CONTEXTS_V2_NODES;
  1525. req->options &= ~CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES;
  1526. }
  1527. struct rrdcontext_to_json_v2_data ctl = {
  1528. .wb = wb,
  1529. .request = req,
  1530. .mode = mode,
  1531. .options = req->options,
  1532. .versions = { 0 },
  1533. .nodes.scope_pattern = string_to_simple_pattern(req->scope_nodes),
  1534. .nodes.pattern = string_to_simple_pattern(req->nodes),
  1535. .contexts.pattern = string_to_simple_pattern(req->contexts),
  1536. .contexts.scope_pattern = string_to_simple_pattern(req->scope_contexts),
  1537. .q.pattern = string_to_simple_pattern_nocase(req->q),
  1538. .alerts.alert_name_pattern = string_to_simple_pattern(req->alerts.alert),
  1539. .window = {
  1540. .enabled = false,
  1541. .relative = false,
  1542. .after = req->after,
  1543. .before = req->before,
  1544. },
  1545. .timings = {
  1546. .received_ut = now_monotonic_usec(),
  1547. }
  1548. };
  1549. bool debug = ctl.options & CONTEXT_V2_OPTION_DEBUG;
  1550. if(mode & CONTEXTS_V2_NODES) {
  1551. ctl.nodes.dict = dictionary_create_advanced(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  1552. NULL, sizeof(struct contexts_v2_node));
  1553. }
  1554. if(mode & CONTEXTS_V2_CONTEXTS) {
  1555. ctl.contexts.dict = dictionary_create_advanced(
  1556. DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE, NULL,
  1557. sizeof(struct context_v2_entry));
  1558. dictionary_register_conflict_callback(ctl.contexts.dict, contexts_conflict_callback, &ctl);
  1559. dictionary_register_delete_callback(ctl.contexts.dict, contexts_delete_callback, &ctl);
  1560. }
  1561. if(mode & CONTEXTS_V2_FUNCTIONS) {
  1562. ctl.functions.dict = dictionary_create_advanced(
  1563. DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE, NULL,
  1564. sizeof(struct function_v2_entry));
  1565. dictionary_register_insert_callback(ctl.functions.dict, functions_insert_callback, &ctl);
  1566. dictionary_register_conflict_callback(ctl.functions.dict, functions_conflict_callback, &ctl);
  1567. dictionary_register_delete_callback(ctl.functions.dict, functions_delete_callback, &ctl);
  1568. }
  1569. if(mode & CONTEXTS_V2_ALERTS) {
  1570. if(req->alerts.transition) {
  1571. ctl.options |= CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES|CONTEXT_V2_OPTION_ALERTS_WITH_VALUES;
  1572. run = sql_find_alert_transition(req->alerts.transition, rrdcontext_v2_set_transition_filter, &ctl);
  1573. if(!run) {
  1574. resp = HTTP_RESP_NOT_FOUND;
  1575. goto cleanup;
  1576. }
  1577. }
  1578. ctl.alerts.alerts = dictionary_create_advanced(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  1579. NULL, sizeof(struct alert_v2_entry));
  1580. dictionary_register_insert_callback(ctl.alerts.alerts, alerts_v2_insert_callback, &ctl);
  1581. dictionary_register_conflict_callback(ctl.alerts.alerts, alerts_v2_conflict_callback, &ctl);
  1582. dictionary_register_delete_callback(ctl.alerts.alerts, alerts_v2_delete_callback, &ctl);
  1583. if(ctl.options & (CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES | CONTEXT_V2_OPTION_ALERTS_WITH_VALUES)) {
  1584. ctl.alerts.alert_instances = dictionary_create_advanced(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  1585. NULL, sizeof(struct sql_alert_instance_v2_entry));
  1586. dictionary_register_insert_callback(ctl.alerts.alert_instances, alert_instances_v2_insert_callback, &ctl);
  1587. dictionary_register_conflict_callback(ctl.alerts.alert_instances, alert_instances_v2_conflict_callback, &ctl);
  1588. dictionary_register_delete_callback(ctl.alerts.alert_instances, alert_instances_delete_callback, &ctl);
  1589. }
  1590. }
  1591. if(req->after || req->before) {
  1592. ctl.window.relative = rrdr_relative_window_to_absolute(&ctl.window.after, &ctl.window.before, &ctl.now);
  1593. ctl.window.enabled = !(mode & CONTEXTS_V2_ALERT_TRANSITIONS);
  1594. }
  1595. else
  1596. ctl.now = now_realtime_sec();
  1597. buffer_json_initialize(wb, "\"", "\"", 0,
  1598. true, (req->options & CONTEXT_V2_OPTION_MINIFY) && !(req->options & CONTEXT_V2_OPTION_DEBUG));
  1599. buffer_json_member_add_uint64(wb, "api", 2);
  1600. if(req->options & CONTEXT_V2_OPTION_DEBUG) {
  1601. buffer_json_member_add_object(wb, "request");
  1602. {
  1603. buffer_json_contexts_v2_mode_to_array(wb, "mode", mode);
  1604. web_client_api_request_v2_contexts_options_to_buffer_json_array(wb, "options", req->options);
  1605. buffer_json_member_add_object(wb, "scope");
  1606. {
  1607. buffer_json_member_add_string(wb, "scope_nodes", req->scope_nodes);
  1608. if (mode & (CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_SEARCH | CONTEXTS_V2_ALERTS))
  1609. buffer_json_member_add_string(wb, "scope_contexts", req->scope_contexts);
  1610. }
  1611. buffer_json_object_close(wb);
  1612. buffer_json_member_add_object(wb, "selectors");
  1613. {
  1614. buffer_json_member_add_string(wb, "nodes", req->nodes);
  1615. if (mode & (CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_SEARCH | CONTEXTS_V2_ALERTS))
  1616. buffer_json_member_add_string(wb, "contexts", req->contexts);
  1617. if(mode & (CONTEXTS_V2_ALERTS | CONTEXTS_V2_ALERT_TRANSITIONS)) {
  1618. buffer_json_member_add_object(wb, "alerts");
  1619. if(mode & CONTEXTS_V2_ALERTS)
  1620. web_client_api_request_v2_contexts_alerts_status_to_buffer_json_array(wb, "status", req->alerts.status);
  1621. if(mode & CONTEXTS_V2_ALERT_TRANSITIONS) {
  1622. buffer_json_member_add_string(wb, "context", req->contexts);
  1623. buffer_json_member_add_uint64(wb, "anchor_gi", req->alerts.global_id_anchor);
  1624. buffer_json_member_add_uint64(wb, "last", req->alerts.last);
  1625. }
  1626. buffer_json_member_add_string(wb, "alert", req->alerts.alert);
  1627. buffer_json_member_add_string(wb, "transition", req->alerts.transition);
  1628. buffer_json_object_close(wb); // alerts
  1629. }
  1630. }
  1631. buffer_json_object_close(wb); // selectors
  1632. buffer_json_member_add_object(wb, "filters");
  1633. {
  1634. if (mode & CONTEXTS_V2_SEARCH)
  1635. buffer_json_member_add_string(wb, "q", req->q);
  1636. buffer_json_member_add_time_t(wb, "after", req->after);
  1637. buffer_json_member_add_time_t(wb, "before", req->before);
  1638. }
  1639. buffer_json_object_close(wb); // filters
  1640. if(mode & CONTEXTS_V2_ALERT_TRANSITIONS) {
  1641. buffer_json_member_add_object(wb, "facets");
  1642. {
  1643. for (int i = 0; i < ATF_TOTAL_ENTRIES; i++) {
  1644. buffer_json_member_add_string(wb, alert_transition_facets[i].query_param, req->alerts.facets[i]);
  1645. }
  1646. }
  1647. buffer_json_object_close(wb); // facets
  1648. }
  1649. }
  1650. buffer_json_object_close(wb);
  1651. }
  1652. ssize_t ret = 0;
  1653. if(run)
  1654. ret = query_scope_foreach_host(ctl.nodes.scope_pattern, ctl.nodes.pattern,
  1655. rrdcontext_to_json_v2_add_host, &ctl,
  1656. &ctl.versions, ctl.q.host_node_id_str);
  1657. if(unlikely(ret < 0)) {
  1658. buffer_flush(wb);
  1659. if(ret == -2) {
  1660. buffer_strcat(wb, "query timeout");
  1661. resp = HTTP_RESP_GATEWAY_TIMEOUT;
  1662. }
  1663. else {
  1664. buffer_strcat(wb, "query interrupted");
  1665. resp = HTTP_RESP_BACKEND_FETCH_FAILED;
  1666. }
  1667. goto cleanup;
  1668. }
  1669. ctl.timings.executed_ut = now_monotonic_usec();
  1670. if(mode & CONTEXTS_V2_ALERT_TRANSITIONS) {
  1671. contexts_v2_alert_transitions_to_json(wb, &ctl, debug);
  1672. }
  1673. else {
  1674. if (mode & CONTEXTS_V2_NODES) {
  1675. buffer_json_member_add_array(wb, "nodes");
  1676. struct contexts_v2_node *t;
  1677. dfe_start_read(ctl.nodes.dict, t) {
  1678. rrdcontext_to_json_v2_rrdhost(wb, t->host, &ctl, t->ni);
  1679. }
  1680. dfe_done(t);
  1681. buffer_json_array_close(wb);
  1682. }
  1683. if (mode & CONTEXTS_V2_FUNCTIONS) {
  1684. buffer_json_member_add_array(wb, "functions");
  1685. {
  1686. struct function_v2_entry *t;
  1687. dfe_start_read(ctl.functions.dict, t) {
  1688. buffer_json_add_array_item_object(wb);
  1689. buffer_json_member_add_string(wb, "name", t_dfe.name);
  1690. buffer_json_member_add_string(wb, "help", string2str(t->help));
  1691. buffer_json_member_add_array(wb, "ni");
  1692. for (size_t i = 0; i < t->used; i++)
  1693. buffer_json_add_array_item_uint64(wb, t->node_ids[i]);
  1694. buffer_json_array_close(wb);
  1695. buffer_json_object_close(wb);
  1696. }
  1697. dfe_done(t);
  1698. }
  1699. buffer_json_array_close(wb);
  1700. }
  1701. if (mode & CONTEXTS_V2_CONTEXTS) {
  1702. buffer_json_member_add_object(wb, "contexts");
  1703. {
  1704. struct context_v2_entry *z;
  1705. dfe_start_read(ctl.contexts.dict, z) {
  1706. bool collected = z->flags & RRD_FLAG_COLLECTED;
  1707. buffer_json_member_add_object(wb, string2str(z->id));
  1708. {
  1709. buffer_json_member_add_string(wb, "family", string2str(z->family));
  1710. buffer_json_member_add_uint64(wb, "priority", z->priority);
  1711. buffer_json_member_add_time_t(wb, "first_entry", z->first_time_s);
  1712. buffer_json_member_add_time_t(wb, "last_entry", collected ? ctl.now : z->last_time_s);
  1713. buffer_json_member_add_boolean(wb, "live", collected);
  1714. if (mode & CONTEXTS_V2_SEARCH)
  1715. buffer_json_member_add_string(wb, "match", fts_match_to_string(z->match));
  1716. }
  1717. buffer_json_object_close(wb);
  1718. }
  1719. dfe_done(z);
  1720. }
  1721. buffer_json_object_close(wb); // contexts
  1722. }
  1723. if (mode & CONTEXTS_V2_ALERTS)
  1724. contexts_v2_alerts_to_json(wb, &ctl, debug);
  1725. if (mode & CONTEXTS_V2_SEARCH) {
  1726. buffer_json_member_add_object(wb, "searches");
  1727. {
  1728. buffer_json_member_add_uint64(wb, "strings", ctl.q.fts.string_searches);
  1729. buffer_json_member_add_uint64(wb, "char", ctl.q.fts.char_searches);
  1730. buffer_json_member_add_uint64(wb, "total", ctl.q.fts.searches);
  1731. }
  1732. buffer_json_object_close(wb);
  1733. }
  1734. if (mode & (CONTEXTS_V2_VERSIONS))
  1735. version_hashes_api_v2(wb, &ctl.versions);
  1736. if (mode & CONTEXTS_V2_AGENTS)
  1737. buffer_json_agents_v2(wb, &ctl.timings, ctl.now, mode & (CONTEXTS_V2_AGENTS_INFO), true);
  1738. }
  1739. buffer_json_cloud_timings(wb, "timings", &ctl.timings);
  1740. buffer_json_finalize(wb);
  1741. cleanup:
  1742. dictionary_destroy(ctl.nodes.dict);
  1743. dictionary_destroy(ctl.contexts.dict);
  1744. dictionary_destroy(ctl.functions.dict);
  1745. dictionary_destroy(ctl.alerts.alerts);
  1746. dictionary_destroy(ctl.alerts.alert_instances);
  1747. simple_pattern_free(ctl.nodes.scope_pattern);
  1748. simple_pattern_free(ctl.nodes.pattern);
  1749. simple_pattern_free(ctl.contexts.pattern);
  1750. simple_pattern_free(ctl.contexts.scope_pattern);
  1751. simple_pattern_free(ctl.q.pattern);
  1752. simple_pattern_free(ctl.alerts.alert_name_pattern);
  1753. return resp;
  1754. }