api_v2.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128
  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. if((o->flags & RRD_FLAG_COLLECTED) && !(n->flags & RRD_FLAG_COLLECTED))
  905. // keep old
  906. ;
  907. else if(!(o->flags & RRD_FLAG_COLLECTED) && (n->flags & RRD_FLAG_COLLECTED)) {
  908. // keep new
  909. string_freez(o->family);
  910. o->family = string_dup(n->family);
  911. }
  912. else {
  913. // merge
  914. STRING *old_family = o->family;
  915. o->family = string_2way_merge(o->family, n->family);
  916. string_freez(old_family);
  917. }
  918. }
  919. if(o->priority != n->priority) {
  920. if((o->flags & RRD_FLAG_COLLECTED) && !(n->flags & RRD_FLAG_COLLECTED))
  921. // keep o
  922. ;
  923. else if(!(o->flags & RRD_FLAG_COLLECTED) && (n->flags & RRD_FLAG_COLLECTED))
  924. // keep n
  925. o->priority = n->priority;
  926. else
  927. // keep the min
  928. o->priority = MIN(o->priority, n->priority);
  929. }
  930. if(o->first_time_s && n->first_time_s)
  931. o->first_time_s = MIN(o->first_time_s, n->first_time_s);
  932. else if(!o->first_time_s)
  933. o->first_time_s = n->first_time_s;
  934. if(o->last_time_s && n->last_time_s)
  935. o->last_time_s = MAX(o->last_time_s, n->last_time_s);
  936. else if(!o->last_time_s)
  937. o->last_time_s = n->last_time_s;
  938. o->flags |= n->flags;
  939. o->match = MIN(o->match, n->match);
  940. string_freez(n->family);
  941. return true;
  942. }
  943. static void contexts_delete_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data __maybe_unused) {
  944. struct context_v2_entry *z = value;
  945. string_freez(z->family);
  946. }
  947. static void rrdcontext_v2_set_transition_filter(const char *machine_guid, const char *context, time_t alarm_id, void *data) {
  948. struct rrdcontext_to_json_v2_data *ctl = data;
  949. if(machine_guid && *machine_guid) {
  950. if(ctl->nodes.scope_pattern)
  951. simple_pattern_free(ctl->nodes.scope_pattern);
  952. if(ctl->nodes.pattern)
  953. simple_pattern_free(ctl->nodes.pattern);
  954. ctl->nodes.scope_pattern = string_to_simple_pattern(machine_guid);
  955. ctl->nodes.pattern = NULL;
  956. }
  957. if(context && *context) {
  958. if(ctl->contexts.scope_pattern)
  959. simple_pattern_free(ctl->contexts.scope_pattern);
  960. if(ctl->contexts.pattern)
  961. simple_pattern_free(ctl->contexts.pattern);
  962. ctl->contexts.scope_pattern = string_to_simple_pattern(context);
  963. ctl->contexts.pattern = NULL;
  964. }
  965. ctl->alerts.alarm_id_filter = alarm_id;
  966. }
  967. struct alert_instances_callback_data {
  968. BUFFER *wb;
  969. struct rrdcontext_to_json_v2_data *ctl;
  970. bool debug;
  971. };
  972. static void contexts_v2_alert_config_to_json_from_sql_alert_config_data(struct sql_alert_config_data *t, void *data) {
  973. struct alert_transitions_callback_data *d = data;
  974. BUFFER *wb = d->wb;
  975. bool debug = d->debug;
  976. d->configs_added++;
  977. if(d->only_one_config)
  978. buffer_json_add_array_item_object(wb); // alert config
  979. {
  980. buffer_json_member_add_string(wb, "name", t->name);
  981. buffer_json_member_add_uuid(wb, "config_hash_id", t->config_hash_id);
  982. buffer_json_member_add_object(wb, "selectors");
  983. {
  984. bool is_template = t->selectors.on_template && *t->selectors.on_template ? true : false;
  985. buffer_json_member_add_string(wb, "type", is_template ? "template" : "alarm");
  986. buffer_json_member_add_string(wb, "on", is_template ? t->selectors.on_template : t->selectors.on_key);
  987. buffer_json_member_add_string(wb, "os", t->selectors.os);
  988. buffer_json_member_add_string(wb, "hosts", t->selectors.hosts);
  989. buffer_json_member_add_string(wb, "families", t->selectors.families);
  990. buffer_json_member_add_string(wb, "plugin", t->selectors.plugin);
  991. buffer_json_member_add_string(wb, "module", t->selectors.module);
  992. buffer_json_member_add_string(wb, "host_labels", t->selectors.host_labels);
  993. buffer_json_member_add_string(wb, "chart_labels", t->selectors.chart_labels);
  994. buffer_json_member_add_string(wb, "charts", t->selectors.charts);
  995. }
  996. buffer_json_object_close(wb); // selectors
  997. buffer_json_member_add_object(wb, "value"); // value
  998. {
  999. // buffer_json_member_add_string(wb, "every", t->value.every); // does not exist in Netdata Cloud
  1000. buffer_json_member_add_string(wb, "units", t->value.units);
  1001. buffer_json_member_add_uint64(wb, "update_every", t->value.update_every);
  1002. if (t->value.db.after || debug) {
  1003. buffer_json_member_add_object(wb, "db");
  1004. {
  1005. // buffer_json_member_add_string(wb, "lookup", t->value.db.lookup); // does not exist in Netdata Cloud
  1006. buffer_json_member_add_time_t(wb, "after", t->value.db.after);
  1007. buffer_json_member_add_time_t(wb, "before", t->value.db.before);
  1008. buffer_json_member_add_string(wb, "method", t->value.db.method);
  1009. buffer_json_member_add_string(wb, "dimensions", t->value.db.dimensions);
  1010. web_client_api_request_v1_data_options_to_buffer_json_array(wb, "options",(RRDR_OPTIONS) t->value.db.options);
  1011. }
  1012. buffer_json_object_close(wb); // db
  1013. }
  1014. if (t->value.calc || debug)
  1015. buffer_json_member_add_string(wb, "calc", t->value.calc);
  1016. }
  1017. buffer_json_object_close(wb); // value
  1018. if (t->status.warn || t->status.crit || debug) {
  1019. buffer_json_member_add_object(wb, "status"); // status
  1020. {
  1021. NETDATA_DOUBLE green = t->status.green ? str2ndd(t->status.green, NULL) : NAN;
  1022. NETDATA_DOUBLE red = t->status.red ? str2ndd(t->status.red, NULL) : NAN;
  1023. if (!isnan(green) || debug)
  1024. buffer_json_member_add_double(wb, "green", green);
  1025. if (!isnan(red) || debug)
  1026. buffer_json_member_add_double(wb, "red", red);
  1027. if (t->status.warn || debug)
  1028. buffer_json_member_add_string(wb, "warn", t->status.warn);
  1029. if (t->status.crit || debug)
  1030. buffer_json_member_add_string(wb, "crit", t->status.crit);
  1031. }
  1032. buffer_json_object_close(wb); // status
  1033. }
  1034. buffer_json_member_add_object(wb, "notification");
  1035. {
  1036. buffer_json_member_add_string(wb, "type", "agent");
  1037. buffer_json_member_add_string(wb, "exec", t->notification.exec ? t->notification.exec : NULL);
  1038. buffer_json_member_add_string(wb, "to", t->notification.to_key ? t->notification.to_key : string2str(localhost->health.health_default_recipient));
  1039. buffer_json_member_add_string(wb, "delay", t->notification.delay);
  1040. buffer_json_member_add_string(wb, "repeat", t->notification.repeat);
  1041. buffer_json_member_add_string(wb, "options", t->notification.options);
  1042. }
  1043. buffer_json_object_close(wb); // notification
  1044. buffer_json_member_add_string(wb, "class", t->classification);
  1045. buffer_json_member_add_string(wb, "component", t->component);
  1046. buffer_json_member_add_string(wb, "type", t->type);
  1047. buffer_json_member_add_string(wb, "info", t->info);
  1048. // buffer_json_member_add_string(wb, "source", t->source); // moved to alert instance
  1049. }
  1050. if(d->only_one_config)
  1051. buffer_json_object_close(wb);
  1052. }
  1053. int contexts_v2_alert_config_to_json(struct web_client *w, const char *config_hash_id) {
  1054. struct alert_transitions_callback_data data = {
  1055. .wb = w->response.data,
  1056. .debug = false,
  1057. .only_one_config = false,
  1058. };
  1059. DICTIONARY *configs = dictionary_create(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE);
  1060. dictionary_set(configs, config_hash_id, NULL, 0);
  1061. buffer_flush(w->response.data);
  1062. buffer_json_initialize(w->response.data, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_DEFAULT);
  1063. int added = sql_get_alert_configuration(configs, contexts_v2_alert_config_to_json_from_sql_alert_config_data, &data, false);
  1064. buffer_json_finalize(w->response.data);
  1065. int ret = HTTP_RESP_OK;
  1066. if(added <= 0) {
  1067. buffer_flush(w->response.data);
  1068. w->response.data->content_type = CT_TEXT_PLAIN;
  1069. if(added < 0) {
  1070. buffer_strcat(w->response.data, "Failed to execute SQL query.");
  1071. ret = HTTP_RESP_INTERNAL_SERVER_ERROR;
  1072. }
  1073. else {
  1074. buffer_strcat(w->response.data, "Config is not found.");
  1075. ret = HTTP_RESP_NOT_FOUND;
  1076. }
  1077. }
  1078. return ret;
  1079. }
  1080. static int contexts_v2_alert_instance_to_json_callback(const DICTIONARY_ITEM *item __maybe_unused, void *value, void *data) {
  1081. struct sql_alert_instance_v2_entry *t = value;
  1082. struct alert_instances_callback_data *d = data;
  1083. struct rrdcontext_to_json_v2_data *ctl = d->ctl; (void)ctl;
  1084. bool debug = d->debug; (void)debug;
  1085. BUFFER *wb = d->wb;
  1086. buffer_json_add_array_item_object(wb);
  1087. {
  1088. buffer_json_member_add_uint64(wb, "ni", t->ni);
  1089. buffer_json_member_add_string(wb, "nm", string2str(t->name));
  1090. buffer_json_member_add_string(wb, "ch", string2str(t->chart_id));
  1091. buffer_json_member_add_string(wb, "ch_n", string2str(t->chart_name));
  1092. if(ctl->request->options & CONTEXT_V2_OPTION_ALERTS_WITH_SUMMARY)
  1093. buffer_json_member_add_uint64(wb, "ati", t->ati);
  1094. if(ctl->request->options & CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES) {
  1095. buffer_json_member_add_string(wb, "units", string2str(t->units));
  1096. buffer_json_member_add_string(wb, "fami", string2str(t->family));
  1097. buffer_json_member_add_string(wb, "info", string2str(t->info));
  1098. buffer_json_member_add_string(wb, "ctx", string2str(t->context));
  1099. buffer_json_member_add_string(wb, "st", rrdcalc_status2string(t->status));
  1100. buffer_json_member_add_uuid(wb, "tr_i", &t->last_transition_id);
  1101. buffer_json_member_add_double(wb, "tr_v", t->last_status_change_value);
  1102. buffer_json_member_add_time_t(wb, "tr_t", t->last_status_change);
  1103. buffer_json_member_add_uuid(wb, "cfg", &t->config_hash_id);
  1104. buffer_json_member_add_string(wb, "src", string2str(t->source));
  1105. buffer_json_member_add_string(wb, "to", string2str(t->recipient));
  1106. buffer_json_member_add_string(wb, "tp", string2str(t->type));
  1107. buffer_json_member_add_string(wb, "cm", string2str(t->component));
  1108. buffer_json_member_add_string(wb, "cl", string2str(t->classification));
  1109. // Agent specific fields
  1110. buffer_json_member_add_uint64(wb, "gi", t->global_id);
  1111. // rrdcalc_flags_to_json_array (wb, "flags", t->flags);
  1112. }
  1113. if(ctl->request->options & CONTEXT_V2_OPTION_ALERTS_WITH_VALUES) {
  1114. // Netdata Cloud fetched these by querying the agents
  1115. buffer_json_member_add_double(wb, "v", t->value);
  1116. buffer_json_member_add_time_t(wb, "t", t->last_updated);
  1117. }
  1118. }
  1119. buffer_json_object_close(wb); // alert instance
  1120. return 1;
  1121. }
  1122. static void contexts_v2_alert_instances_to_json(BUFFER *wb, const char *key, struct rrdcontext_to_json_v2_data *ctl, bool debug) {
  1123. buffer_json_member_add_array(wb, key);
  1124. {
  1125. struct alert_instances_callback_data data = {
  1126. .wb = wb,
  1127. .ctl = ctl,
  1128. .debug = debug,
  1129. };
  1130. dictionary_walkthrough_rw(ctl->alerts.alert_instances, DICTIONARY_LOCK_READ,
  1131. contexts_v2_alert_instance_to_json_callback, &data);
  1132. }
  1133. buffer_json_array_close(wb); // alerts_instances
  1134. }
  1135. static void contexts_v2_alerts_to_json(BUFFER *wb, struct rrdcontext_to_json_v2_data *ctl, bool debug) {
  1136. if(ctl->request->options & CONTEXT_V2_OPTION_ALERTS_WITH_SUMMARY) {
  1137. buffer_json_member_add_array(wb, "alerts");
  1138. {
  1139. struct alert_v2_entry *t;
  1140. dfe_start_read(ctl->alerts.alerts, t)
  1141. {
  1142. buffer_json_add_array_item_object(wb);
  1143. {
  1144. buffer_json_member_add_uint64(wb, "ati", t->ati);
  1145. buffer_json_member_add_string(wb, "nm", string2str(t->name));
  1146. buffer_json_member_add_uint64(wb, "cr", t->critical);
  1147. buffer_json_member_add_uint64(wb, "wr", t->warning);
  1148. buffer_json_member_add_uint64(wb, "cl", t->clear);
  1149. buffer_json_member_add_uint64(wb, "er", t->error);
  1150. buffer_json_member_add_uint64(wb, "in", t->instances);
  1151. buffer_json_member_add_uint64(wb, "nd", dictionary_entries(t->nodes));
  1152. buffer_json_member_add_uint64(wb, "cfg", dictionary_entries(t->configs));
  1153. }
  1154. buffer_json_object_close(wb); // alert name
  1155. }
  1156. dfe_done(t);
  1157. }
  1158. buffer_json_array_close(wb); // alerts
  1159. }
  1160. if(ctl->request->options & (CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES|CONTEXT_V2_OPTION_ALERTS_WITH_VALUES)) {
  1161. contexts_v2_alert_instances_to_json(wb, "alert_instances", ctl, debug);
  1162. }
  1163. }
  1164. #define SQL_TRANSITION_DATA_SMALL_STRING (6 * 8)
  1165. #define SQL_TRANSITION_DATA_MEDIUM_STRING (12 * 8)
  1166. #define SQL_TRANSITION_DATA_BIG_STRING 512
  1167. struct sql_alert_transition_fixed_size {
  1168. usec_t global_id;
  1169. uuid_t transition_id;
  1170. uuid_t host_id;
  1171. uuid_t config_hash_id;
  1172. uint32_t alarm_id;
  1173. char alert_name[SQL_TRANSITION_DATA_SMALL_STRING];
  1174. char chart[RRD_ID_LENGTH_MAX];
  1175. char chart_name[RRD_ID_LENGTH_MAX];
  1176. char chart_context[SQL_TRANSITION_DATA_MEDIUM_STRING];
  1177. char family[SQL_TRANSITION_DATA_SMALL_STRING];
  1178. char recipient[SQL_TRANSITION_DATA_MEDIUM_STRING];
  1179. char units[SQL_TRANSITION_DATA_SMALL_STRING];
  1180. char exec[SQL_TRANSITION_DATA_BIG_STRING];
  1181. char info[SQL_TRANSITION_DATA_BIG_STRING];
  1182. char classification[SQL_TRANSITION_DATA_SMALL_STRING];
  1183. char type[SQL_TRANSITION_DATA_SMALL_STRING];
  1184. char component[SQL_TRANSITION_DATA_SMALL_STRING];
  1185. time_t when_key;
  1186. time_t duration;
  1187. time_t non_clear_duration;
  1188. uint64_t flags;
  1189. time_t delay_up_to_timestamp;
  1190. time_t exec_run_timestamp;
  1191. int exec_code;
  1192. int new_status;
  1193. int old_status;
  1194. int delay;
  1195. time_t last_repeat;
  1196. NETDATA_DOUBLE new_value;
  1197. NETDATA_DOUBLE old_value;
  1198. char machine_guid[UUID_STR_LEN];
  1199. struct sql_alert_transition_fixed_size *next;
  1200. struct sql_alert_transition_fixed_size *prev;
  1201. };
  1202. 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) {
  1203. struct sql_alert_transition_fixed_size *n = dst ? dst : mallocz(sizeof(*n));
  1204. n->global_id = t->global_id;
  1205. uuid_copy(n->transition_id, *t->transition_id);
  1206. uuid_copy(n->host_id, *t->host_id);
  1207. uuid_copy(n->config_hash_id, *t->config_hash_id);
  1208. n->alarm_id = t->alarm_id;
  1209. strncpyz(n->alert_name, t->alert_name ? t->alert_name : "", sizeof(n->alert_name) - 1);
  1210. strncpyz(n->chart, t->chart ? t->chart : "", sizeof(n->chart) - 1);
  1211. strncpyz(n->chart_name, t->chart_name ? t->chart_name : n->chart, sizeof(n->chart_name) - 1);
  1212. strncpyz(n->chart_context, t->chart_context ? t->chart_context : "", sizeof(n->chart_context) - 1);
  1213. strncpyz(n->family, t->family ? t->family : "", sizeof(n->family) - 1);
  1214. strncpyz(n->recipient, t->recipient ? t->recipient : "", sizeof(n->recipient) - 1);
  1215. strncpyz(n->units, t->units ? t->units : "", sizeof(n->units) - 1);
  1216. strncpyz(n->exec, t->exec ? t->exec : "", sizeof(n->exec) - 1);
  1217. strncpyz(n->info, t->info ? t->info : "", sizeof(n->info) - 1);
  1218. strncpyz(n->classification, t->classification ? t->classification : "", sizeof(n->classification) - 1);
  1219. strncpyz(n->type, t->type ? t->type : "", sizeof(n->type) - 1);
  1220. strncpyz(n->component, t->component ? t->component : "", sizeof(n->component) - 1);
  1221. n->when_key = t->when_key;
  1222. n->duration = t->duration;
  1223. n->non_clear_duration = t->non_clear_duration;
  1224. n->flags = t->flags;
  1225. n->delay_up_to_timestamp = t->delay_up_to_timestamp;
  1226. n->exec_run_timestamp = t->exec_run_timestamp;
  1227. n->exec_code = t->exec_code;
  1228. n->new_status = t->new_status;
  1229. n->old_status = t->old_status;
  1230. n->delay = t->delay;
  1231. n->last_repeat = t->last_repeat;
  1232. n->new_value = t->new_value;
  1233. n->old_value = t->old_value;
  1234. memcpy(n->machine_guid, machine_guid, sizeof(n->machine_guid));
  1235. n->next = n->prev = NULL;
  1236. return n;
  1237. }
  1238. static void contexts_v2_alert_transition_free(struct sql_alert_transition_fixed_size *t) {
  1239. freez(t);
  1240. }
  1241. static inline void contexts_v2_alert_transition_keep(struct alert_transitions_callback_data *d, struct sql_alert_transition_data *t, const char *machine_guid) {
  1242. d->items_matched++;
  1243. if(unlikely(t->global_id <= d->ctl->request->alerts.global_id_anchor)) {
  1244. // this is in our past, we are not interested
  1245. d->operations.skips_before++;
  1246. return;
  1247. }
  1248. if(unlikely(!d->base)) {
  1249. d->last_added = contexts_v2_alert_transition_dup(t, machine_guid, NULL);
  1250. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(d->base, d->last_added, prev, next);
  1251. d->items_to_return++;
  1252. d->operations.first++;
  1253. return;
  1254. }
  1255. struct sql_alert_transition_fixed_size *last = d->last_added;
  1256. while(last->prev != d->base->prev && t->global_id > last->prev->global_id) {
  1257. last = last->prev;
  1258. d->operations.backwards++;
  1259. }
  1260. while(last->next && t->global_id < last->next->global_id) {
  1261. last = last->next;
  1262. d->operations.forwards++;
  1263. }
  1264. if(d->items_to_return >= d->max_items_to_return) {
  1265. if(last == d->base->prev && t->global_id < last->global_id) {
  1266. d->operations.skips_after++;
  1267. return;
  1268. }
  1269. }
  1270. d->items_to_return++;
  1271. if(t->global_id > last->global_id) {
  1272. if(d->items_to_return > d->max_items_to_return) {
  1273. d->items_to_return--;
  1274. d->operations.shifts++;
  1275. d->last_added = d->base->prev;
  1276. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(d->base, d->last_added, prev, next);
  1277. d->last_added = contexts_v2_alert_transition_dup(t, machine_guid, d->last_added);
  1278. }
  1279. DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(d->base, d->last_added, prev, next);
  1280. d->operations.prepend++;
  1281. }
  1282. else {
  1283. d->last_added = contexts_v2_alert_transition_dup(t, machine_guid, NULL);
  1284. DOUBLE_LINKED_LIST_APPEND_ITEM_UNSAFE(d->base, d->last_added, prev, next);
  1285. d->operations.append++;
  1286. }
  1287. while(d->items_to_return > d->max_items_to_return) {
  1288. // we have to remove something
  1289. struct sql_alert_transition_fixed_size *tmp = d->base->prev;
  1290. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(d->base, tmp, prev, next);
  1291. d->items_to_return--;
  1292. if(unlikely(d->last_added == tmp))
  1293. d->last_added = d->base;
  1294. contexts_v2_alert_transition_free(tmp);
  1295. d->operations.shifts++;
  1296. }
  1297. }
  1298. static void contexts_v2_alert_transition_callback(struct sql_alert_transition_data *t, void *data) {
  1299. struct alert_transitions_callback_data *d = data;
  1300. d->items_evaluated++;
  1301. char machine_guid[UUID_STR_LEN] = "";
  1302. uuid_unparse_lower(*t->host_id, machine_guid);
  1303. const char *facets[ATF_TOTAL_ENTRIES] = {
  1304. [ATF_STATUS] = rrdcalc_status2string(t->new_status),
  1305. [ATF_CLASS] = t->classification,
  1306. [ATF_TYPE] = t->type,
  1307. [ATF_COMPONENT] = t->component,
  1308. [ATF_ROLE] = t->recipient && *t->recipient ? t->recipient : string2str(localhost->health.health_default_recipient),
  1309. [ATF_NODE] = machine_guid,
  1310. [ATF_ALERT_NAME] = t->alert_name,
  1311. [ATF_CHART_NAME] = t->chart_name,
  1312. [ATF_CONTEXT] = t->chart_context,
  1313. };
  1314. for(size_t i = 0; i < ATF_TOTAL_ENTRIES ;i++) {
  1315. if (!facets[i] || !*facets[i]) facets[i] = "unknown";
  1316. struct facet_entry tmp = {
  1317. .count = 0,
  1318. };
  1319. dictionary_set(d->facets[i].dict, facets[i], &tmp, sizeof(tmp));
  1320. }
  1321. bool selected[ATF_TOTAL_ENTRIES] = { 0 };
  1322. uint32_t selected_by = 0;
  1323. for(size_t i = 0; i < ATF_TOTAL_ENTRIES ;i++) {
  1324. selected[i] = !d->facets[i].pattern || simple_pattern_matches(d->facets[i].pattern, facets[i]);
  1325. if(selected[i])
  1326. selected_by++;
  1327. }
  1328. if(selected_by == ATF_TOTAL_ENTRIES) {
  1329. // this item is selected by all facets
  1330. // put it in our result (if it fits)
  1331. contexts_v2_alert_transition_keep(d, t, machine_guid);
  1332. }
  1333. if(selected_by >= ATF_TOTAL_ENTRIES - 1) {
  1334. // this item is selected by all, or all except one facet
  1335. // in both cases we need to add it to our counters
  1336. for (size_t i = 0; i < ATF_TOTAL_ENTRIES; i++) {
  1337. uint32_t counted_by = selected_by;
  1338. if (counted_by != ATF_TOTAL_ENTRIES) {
  1339. counted_by = 0;
  1340. for (size_t j = 0; j < ATF_TOTAL_ENTRIES; j++) {
  1341. if (i == j || selected[j])
  1342. counted_by++;
  1343. }
  1344. }
  1345. if (counted_by == ATF_TOTAL_ENTRIES) {
  1346. // we need to count it on this facet
  1347. struct facet_entry *x = dictionary_get(d->facets[i].dict, facets[i]);
  1348. internal_fatal(!x, "facet is not found");
  1349. if(x)
  1350. x->count++;
  1351. }
  1352. }
  1353. }
  1354. }
  1355. static void contexts_v2_alert_transitions_to_json(BUFFER *wb, struct rrdcontext_to_json_v2_data *ctl, bool debug) {
  1356. struct alert_transitions_callback_data data = {
  1357. .wb = wb,
  1358. .ctl = ctl,
  1359. .debug = debug,
  1360. .only_one_config = true,
  1361. .max_items_to_return = ctl->request->alerts.last,
  1362. .items_to_return = 0,
  1363. .base = NULL,
  1364. };
  1365. for(size_t i = 0; i < ATF_TOTAL_ENTRIES ;i++) {
  1366. 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));
  1367. if(ctl->request->alerts.facets[i])
  1368. data.facets[i].pattern = simple_pattern_create(ctl->request->alerts.facets[i], ",|", SIMPLE_PATTERN_EXACT, false);
  1369. }
  1370. sql_alert_transitions(
  1371. ctl->nodes.dict,
  1372. ctl->window.after,
  1373. ctl->window.before,
  1374. ctl->request->contexts,
  1375. ctl->request->alerts.alert,
  1376. ctl->request->alerts.transition,
  1377. contexts_v2_alert_transition_callback,
  1378. &data,
  1379. debug);
  1380. buffer_json_member_add_array(wb, "facets");
  1381. for (size_t i = 0; i < ATF_TOTAL_ENTRIES; i++) {
  1382. buffer_json_add_array_item_object(wb);
  1383. {
  1384. buffer_json_member_add_string(wb, "id", alert_transition_facets[i].id);
  1385. buffer_json_member_add_string(wb, "name", alert_transition_facets[i].name);
  1386. buffer_json_member_add_uint64(wb, "order", alert_transition_facets[i].order);
  1387. buffer_json_member_add_array(wb, "options");
  1388. {
  1389. struct facet_entry *x;
  1390. dfe_start_read(data.facets[i].dict, x) {
  1391. buffer_json_add_array_item_object(wb);
  1392. {
  1393. buffer_json_member_add_string(wb, "id", x_dfe.name);
  1394. if (i == ATF_NODE) {
  1395. RRDHOST *host = rrdhost_find_by_guid(x_dfe.name);
  1396. if (host)
  1397. buffer_json_member_add_string(wb, "name", rrdhost_hostname(host));
  1398. else
  1399. buffer_json_member_add_string(wb, "name", x_dfe.name);
  1400. } else
  1401. buffer_json_member_add_string(wb, "name", x_dfe.name);
  1402. buffer_json_member_add_uint64(wb, "count", x->count);
  1403. }
  1404. buffer_json_object_close(wb);
  1405. }
  1406. dfe_done(x);
  1407. }
  1408. buffer_json_array_close(wb); // options
  1409. }
  1410. buffer_json_object_close(wb); // facet
  1411. }
  1412. buffer_json_array_close(wb); // facets
  1413. buffer_json_member_add_array(wb, "transitions");
  1414. for(struct sql_alert_transition_fixed_size *t = data.base; t ; t = t->next) {
  1415. buffer_json_add_array_item_object(wb);
  1416. {
  1417. RRDHOST *host = rrdhost_find_by_guid(t->machine_guid);
  1418. buffer_json_member_add_uint64(wb, "gi", t->global_id);
  1419. buffer_json_member_add_uuid(wb, "transition_id", &t->transition_id);
  1420. buffer_json_member_add_uuid(wb, "config_hash_id", &t->config_hash_id);
  1421. buffer_json_member_add_string(wb, "machine_guid", t->machine_guid);
  1422. if(host) {
  1423. buffer_json_member_add_string(wb, "hostname", rrdhost_hostname(host));
  1424. if(host->node_id)
  1425. buffer_json_member_add_uuid(wb, "node_id", host->node_id);
  1426. }
  1427. buffer_json_member_add_string(wb, "alert", *t->alert_name ? t->alert_name : NULL);
  1428. buffer_json_member_add_string(wb, "instance", *t->chart ? t->chart : NULL);
  1429. buffer_json_member_add_string(wb, "instance_n", *t->chart_name ? t->chart_name : NULL);
  1430. buffer_json_member_add_string(wb, "context", *t->chart_context ? t->chart_context : NULL);
  1431. // buffer_json_member_add_string(wb, "family", *t->family ? t->family : NULL);
  1432. buffer_json_member_add_string(wb, "component", *t->component ? t->component : NULL);
  1433. buffer_json_member_add_string(wb, "classification", *t->classification ? t->classification : NULL);
  1434. buffer_json_member_add_string(wb, "type", *t->type ? t->type : NULL);
  1435. buffer_json_member_add_time_t(wb, "when", t->when_key);
  1436. buffer_json_member_add_string(wb, "info", *t->info ? t->info : "");
  1437. buffer_json_member_add_string(wb, "units", *t->units ? t->units : NULL);
  1438. buffer_json_member_add_object(wb, "new");
  1439. {
  1440. buffer_json_member_add_string(wb, "status", rrdcalc_status2string(t->new_status));
  1441. buffer_json_member_add_double(wb, "value", t->new_value);
  1442. }
  1443. buffer_json_object_close(wb); // new
  1444. buffer_json_member_add_object(wb, "old");
  1445. {
  1446. buffer_json_member_add_string(wb, "status", rrdcalc_status2string(t->old_status));
  1447. buffer_json_member_add_double(wb, "value", t->old_value);
  1448. buffer_json_member_add_time_t(wb, "duration", t->duration);
  1449. buffer_json_member_add_time_t(wb, "raised_duration", t->non_clear_duration);
  1450. }
  1451. buffer_json_object_close(wb); // old
  1452. buffer_json_member_add_object(wb, "notification");
  1453. {
  1454. buffer_json_member_add_time_t(wb, "when", t->exec_run_timestamp);
  1455. buffer_json_member_add_time_t(wb, "delay", t->delay);
  1456. buffer_json_member_add_time_t(wb, "delay_up_to_time", t->delay_up_to_timestamp);
  1457. health_entry_flags_to_json_array(wb, "flags", t->flags);
  1458. buffer_json_member_add_string(wb, "exec", *t->exec ? t->exec : string2str(localhost->health.health_default_exec));
  1459. buffer_json_member_add_uint64(wb, "exec_code", t->exec_code);
  1460. buffer_json_member_add_string(wb, "to", *t->recipient ? t->recipient : string2str(localhost->health.health_default_recipient));
  1461. }
  1462. buffer_json_object_close(wb); // notification
  1463. }
  1464. buffer_json_object_close(wb); // a transition
  1465. }
  1466. buffer_json_array_close(wb); // all transitions
  1467. if(ctl->options & CONTEXT_V2_OPTION_ALERTS_WITH_CONFIGURATIONS) {
  1468. DICTIONARY *configs = dictionary_create(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE);
  1469. for(struct sql_alert_transition_fixed_size *t = data.base; t ; t = t->next) {
  1470. char guid[UUID_STR_LEN];
  1471. uuid_unparse_lower(t->config_hash_id, guid);
  1472. dictionary_set(configs, guid, NULL, 0);
  1473. }
  1474. buffer_json_member_add_array(wb, "configurations");
  1475. sql_get_alert_configuration(configs, contexts_v2_alert_config_to_json_from_sql_alert_config_data, &data, debug);
  1476. buffer_json_array_close(wb);
  1477. dictionary_destroy(configs);
  1478. }
  1479. while(data.base) {
  1480. struct sql_alert_transition_fixed_size *t = data.base;
  1481. DOUBLE_LINKED_LIST_REMOVE_ITEM_UNSAFE(data.base, t, prev, next);
  1482. contexts_v2_alert_transition_free(t);
  1483. }
  1484. for(size_t i = 0; i < ATF_TOTAL_ENTRIES ;i++) {
  1485. dictionary_destroy(data.facets[i].dict);
  1486. simple_pattern_free(data.facets[i].pattern);
  1487. }
  1488. buffer_json_member_add_object(wb, "items");
  1489. {
  1490. // all the items in the window, under the scope_nodes, ignoring the facets (filters)
  1491. buffer_json_member_add_uint64(wb, "evaluated", data.items_evaluated);
  1492. // all the items matching the query (if you didn't put anchor_gi and last, these are all the items you would get back)
  1493. buffer_json_member_add_uint64(wb, "matched", data.items_matched);
  1494. // the items included in this response
  1495. buffer_json_member_add_uint64(wb, "returned", data.items_to_return);
  1496. // same as last=X parameter
  1497. buffer_json_member_add_uint64(wb, "max_to_return", data.max_items_to_return);
  1498. // items before the first returned, this should be 0 if anchor_gi is not set
  1499. buffer_json_member_add_uint64(wb, "before", data.operations.skips_before);
  1500. // items after the last returned, when this is zero there aren't any items after the current list
  1501. buffer_json_member_add_uint64(wb, "after", data.operations.skips_after + data.operations.shifts);
  1502. }
  1503. buffer_json_object_close(wb); // items
  1504. if(debug) {
  1505. buffer_json_member_add_object(wb, "stats");
  1506. {
  1507. buffer_json_member_add_uint64(wb, "first", data.operations.first);
  1508. buffer_json_member_add_uint64(wb, "prepend", data.operations.prepend);
  1509. buffer_json_member_add_uint64(wb, "append", data.operations.append);
  1510. buffer_json_member_add_uint64(wb, "backwards", data.operations.backwards);
  1511. buffer_json_member_add_uint64(wb, "forwards", data.operations.forwards);
  1512. buffer_json_member_add_uint64(wb, "shifts", data.operations.shifts);
  1513. buffer_json_member_add_uint64(wb, "skips_before", data.operations.skips_before);
  1514. buffer_json_member_add_uint64(wb, "skips_after", data.operations.skips_after);
  1515. }
  1516. buffer_json_object_close(wb);
  1517. }
  1518. }
  1519. int rrdcontext_to_json_v2(BUFFER *wb, struct api_v2_contexts_request *req, CONTEXTS_V2_MODE mode) {
  1520. int resp = HTTP_RESP_OK;
  1521. bool run = true;
  1522. if(mode & CONTEXTS_V2_SEARCH)
  1523. mode |= CONTEXTS_V2_CONTEXTS;
  1524. if(mode & (CONTEXTS_V2_AGENTS_INFO))
  1525. mode |= CONTEXTS_V2_AGENTS;
  1526. if(mode & (CONTEXTS_V2_FUNCTIONS | CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_SEARCH | CONTEXTS_V2_NODES_INFO | CONTEXTS_V2_NODE_INSTANCES))
  1527. mode |= CONTEXTS_V2_NODES;
  1528. if(mode & CONTEXTS_V2_ALERTS) {
  1529. mode |= CONTEXTS_V2_NODES;
  1530. req->options &= ~CONTEXT_V2_OPTION_ALERTS_WITH_CONFIGURATIONS;
  1531. if(!(req->options & (CONTEXT_V2_OPTION_ALERTS_WITH_SUMMARY|CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES|CONTEXT_V2_OPTION_ALERTS_WITH_VALUES)))
  1532. req->options |= CONTEXT_V2_OPTION_ALERTS_WITH_SUMMARY;
  1533. }
  1534. if(mode & CONTEXTS_V2_ALERT_TRANSITIONS) {
  1535. mode |= CONTEXTS_V2_NODES;
  1536. req->options &= ~CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES;
  1537. }
  1538. struct rrdcontext_to_json_v2_data ctl = {
  1539. .wb = wb,
  1540. .request = req,
  1541. .mode = mode,
  1542. .options = req->options,
  1543. .versions = { 0 },
  1544. .nodes.scope_pattern = string_to_simple_pattern(req->scope_nodes),
  1545. .nodes.pattern = string_to_simple_pattern(req->nodes),
  1546. .contexts.pattern = string_to_simple_pattern(req->contexts),
  1547. .contexts.scope_pattern = string_to_simple_pattern(req->scope_contexts),
  1548. .q.pattern = string_to_simple_pattern_nocase(req->q),
  1549. .alerts.alert_name_pattern = string_to_simple_pattern(req->alerts.alert),
  1550. .window = {
  1551. .enabled = false,
  1552. .relative = false,
  1553. .after = req->after,
  1554. .before = req->before,
  1555. },
  1556. .timings = {
  1557. .received_ut = now_monotonic_usec(),
  1558. }
  1559. };
  1560. bool debug = ctl.options & CONTEXT_V2_OPTION_DEBUG;
  1561. if(mode & CONTEXTS_V2_NODES) {
  1562. ctl.nodes.dict = dictionary_create_advanced(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  1563. NULL, sizeof(struct contexts_v2_node));
  1564. }
  1565. if(mode & CONTEXTS_V2_CONTEXTS) {
  1566. ctl.contexts.dict = dictionary_create_advanced(
  1567. DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE, NULL,
  1568. sizeof(struct context_v2_entry));
  1569. dictionary_register_conflict_callback(ctl.contexts.dict, contexts_conflict_callback, &ctl);
  1570. dictionary_register_delete_callback(ctl.contexts.dict, contexts_delete_callback, &ctl);
  1571. }
  1572. if(mode & CONTEXTS_V2_FUNCTIONS) {
  1573. ctl.functions.dict = dictionary_create_advanced(
  1574. DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE, NULL,
  1575. sizeof(struct function_v2_entry));
  1576. dictionary_register_insert_callback(ctl.functions.dict, functions_insert_callback, &ctl);
  1577. dictionary_register_conflict_callback(ctl.functions.dict, functions_conflict_callback, &ctl);
  1578. dictionary_register_delete_callback(ctl.functions.dict, functions_delete_callback, &ctl);
  1579. }
  1580. if(mode & CONTEXTS_V2_ALERTS) {
  1581. if(req->alerts.transition) {
  1582. ctl.options |= CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES|CONTEXT_V2_OPTION_ALERTS_WITH_VALUES;
  1583. run = sql_find_alert_transition(req->alerts.transition, rrdcontext_v2_set_transition_filter, &ctl);
  1584. if(!run) {
  1585. resp = HTTP_RESP_NOT_FOUND;
  1586. goto cleanup;
  1587. }
  1588. }
  1589. ctl.alerts.alerts = dictionary_create_advanced(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  1590. NULL, sizeof(struct alert_v2_entry));
  1591. dictionary_register_insert_callback(ctl.alerts.alerts, alerts_v2_insert_callback, &ctl);
  1592. dictionary_register_conflict_callback(ctl.alerts.alerts, alerts_v2_conflict_callback, &ctl);
  1593. dictionary_register_delete_callback(ctl.alerts.alerts, alerts_v2_delete_callback, &ctl);
  1594. if(ctl.options & (CONTEXT_V2_OPTION_ALERTS_WITH_INSTANCES | CONTEXT_V2_OPTION_ALERTS_WITH_VALUES)) {
  1595. ctl.alerts.alert_instances = dictionary_create_advanced(DICT_OPTION_SINGLE_THREADED | DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  1596. NULL, sizeof(struct sql_alert_instance_v2_entry));
  1597. dictionary_register_insert_callback(ctl.alerts.alert_instances, alert_instances_v2_insert_callback, &ctl);
  1598. dictionary_register_conflict_callback(ctl.alerts.alert_instances, alert_instances_v2_conflict_callback, &ctl);
  1599. dictionary_register_delete_callback(ctl.alerts.alert_instances, alert_instances_delete_callback, &ctl);
  1600. }
  1601. }
  1602. if(req->after || req->before) {
  1603. ctl.window.relative = rrdr_relative_window_to_absolute(&ctl.window.after, &ctl.window.before, &ctl.now, false);
  1604. ctl.window.enabled = !(mode & CONTEXTS_V2_ALERT_TRANSITIONS);
  1605. }
  1606. else
  1607. ctl.now = now_realtime_sec();
  1608. buffer_json_initialize(wb, "\"", "\"", 0, true,
  1609. ((req->options & CONTEXT_V2_OPTION_MINIFY) && !(req->options & CONTEXT_V2_OPTION_DEBUG)) ? BUFFER_JSON_OPTIONS_MINIFY : BUFFER_JSON_OPTIONS_DEFAULT);
  1610. buffer_json_member_add_uint64(wb, "api", 2);
  1611. if(req->options & CONTEXT_V2_OPTION_DEBUG) {
  1612. buffer_json_member_add_object(wb, "request");
  1613. {
  1614. buffer_json_contexts_v2_mode_to_array(wb, "mode", mode);
  1615. web_client_api_request_v2_contexts_options_to_buffer_json_array(wb, "options", req->options);
  1616. buffer_json_member_add_object(wb, "scope");
  1617. {
  1618. buffer_json_member_add_string(wb, "scope_nodes", req->scope_nodes);
  1619. if (mode & (CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_SEARCH | CONTEXTS_V2_ALERTS))
  1620. buffer_json_member_add_string(wb, "scope_contexts", req->scope_contexts);
  1621. }
  1622. buffer_json_object_close(wb);
  1623. buffer_json_member_add_object(wb, "selectors");
  1624. {
  1625. buffer_json_member_add_string(wb, "nodes", req->nodes);
  1626. if (mode & (CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_SEARCH | CONTEXTS_V2_ALERTS))
  1627. buffer_json_member_add_string(wb, "contexts", req->contexts);
  1628. if(mode & (CONTEXTS_V2_ALERTS | CONTEXTS_V2_ALERT_TRANSITIONS)) {
  1629. buffer_json_member_add_object(wb, "alerts");
  1630. if(mode & CONTEXTS_V2_ALERTS)
  1631. web_client_api_request_v2_contexts_alerts_status_to_buffer_json_array(wb, "status", req->alerts.status);
  1632. if(mode & CONTEXTS_V2_ALERT_TRANSITIONS) {
  1633. buffer_json_member_add_string(wb, "context", req->contexts);
  1634. buffer_json_member_add_uint64(wb, "anchor_gi", req->alerts.global_id_anchor);
  1635. buffer_json_member_add_uint64(wb, "last", req->alerts.last);
  1636. }
  1637. buffer_json_member_add_string(wb, "alert", req->alerts.alert);
  1638. buffer_json_member_add_string(wb, "transition", req->alerts.transition);
  1639. buffer_json_object_close(wb); // alerts
  1640. }
  1641. }
  1642. buffer_json_object_close(wb); // selectors
  1643. buffer_json_member_add_object(wb, "filters");
  1644. {
  1645. if (mode & CONTEXTS_V2_SEARCH)
  1646. buffer_json_member_add_string(wb, "q", req->q);
  1647. buffer_json_member_add_time_t(wb, "after", req->after);
  1648. buffer_json_member_add_time_t(wb, "before", req->before);
  1649. }
  1650. buffer_json_object_close(wb); // filters
  1651. if(mode & CONTEXTS_V2_ALERT_TRANSITIONS) {
  1652. buffer_json_member_add_object(wb, "facets");
  1653. {
  1654. for (int i = 0; i < ATF_TOTAL_ENTRIES; i++) {
  1655. buffer_json_member_add_string(wb, alert_transition_facets[i].query_param, req->alerts.facets[i]);
  1656. }
  1657. }
  1658. buffer_json_object_close(wb); // facets
  1659. }
  1660. }
  1661. buffer_json_object_close(wb);
  1662. }
  1663. ssize_t ret = 0;
  1664. if(run)
  1665. ret = query_scope_foreach_host(ctl.nodes.scope_pattern, ctl.nodes.pattern,
  1666. rrdcontext_to_json_v2_add_host, &ctl,
  1667. &ctl.versions, ctl.q.host_node_id_str);
  1668. if(unlikely(ret < 0)) {
  1669. buffer_flush(wb);
  1670. if(ret == -2) {
  1671. buffer_strcat(wb, "query timeout");
  1672. resp = HTTP_RESP_GATEWAY_TIMEOUT;
  1673. }
  1674. else {
  1675. buffer_strcat(wb, "query interrupted");
  1676. resp = HTTP_RESP_BACKEND_FETCH_FAILED;
  1677. }
  1678. goto cleanup;
  1679. }
  1680. ctl.timings.executed_ut = now_monotonic_usec();
  1681. if(mode & CONTEXTS_V2_ALERT_TRANSITIONS) {
  1682. contexts_v2_alert_transitions_to_json(wb, &ctl, debug);
  1683. }
  1684. else {
  1685. if (mode & CONTEXTS_V2_NODES) {
  1686. buffer_json_member_add_array(wb, "nodes");
  1687. struct contexts_v2_node *t;
  1688. dfe_start_read(ctl.nodes.dict, t) {
  1689. rrdcontext_to_json_v2_rrdhost(wb, t->host, &ctl, t->ni);
  1690. }
  1691. dfe_done(t);
  1692. buffer_json_array_close(wb);
  1693. }
  1694. if (mode & CONTEXTS_V2_FUNCTIONS) {
  1695. buffer_json_member_add_array(wb, "functions");
  1696. {
  1697. struct function_v2_entry *t;
  1698. dfe_start_read(ctl.functions.dict, t) {
  1699. buffer_json_add_array_item_object(wb);
  1700. buffer_json_member_add_string(wb, "name", t_dfe.name);
  1701. buffer_json_member_add_string(wb, "help", string2str(t->help));
  1702. buffer_json_member_add_array(wb, "ni");
  1703. for (size_t i = 0; i < t->used; i++)
  1704. buffer_json_add_array_item_uint64(wb, t->node_ids[i]);
  1705. buffer_json_array_close(wb);
  1706. buffer_json_object_close(wb);
  1707. }
  1708. dfe_done(t);
  1709. }
  1710. buffer_json_array_close(wb);
  1711. }
  1712. if (mode & CONTEXTS_V2_CONTEXTS) {
  1713. buffer_json_member_add_object(wb, "contexts");
  1714. {
  1715. struct context_v2_entry *z;
  1716. dfe_start_read(ctl.contexts.dict, z) {
  1717. bool collected = z->flags & RRD_FLAG_COLLECTED;
  1718. buffer_json_member_add_object(wb, string2str(z->id));
  1719. {
  1720. buffer_json_member_add_string(wb, "family", string2str(z->family));
  1721. buffer_json_member_add_uint64(wb, "priority", z->priority);
  1722. buffer_json_member_add_time_t(wb, "first_entry", z->first_time_s);
  1723. buffer_json_member_add_time_t(wb, "last_entry", collected ? ctl.now : z->last_time_s);
  1724. buffer_json_member_add_boolean(wb, "live", collected);
  1725. if (mode & CONTEXTS_V2_SEARCH)
  1726. buffer_json_member_add_string(wb, "match", fts_match_to_string(z->match));
  1727. }
  1728. buffer_json_object_close(wb);
  1729. }
  1730. dfe_done(z);
  1731. }
  1732. buffer_json_object_close(wb); // contexts
  1733. }
  1734. if (mode & CONTEXTS_V2_ALERTS)
  1735. contexts_v2_alerts_to_json(wb, &ctl, debug);
  1736. if (mode & CONTEXTS_V2_SEARCH) {
  1737. buffer_json_member_add_object(wb, "searches");
  1738. {
  1739. buffer_json_member_add_uint64(wb, "strings", ctl.q.fts.string_searches);
  1740. buffer_json_member_add_uint64(wb, "char", ctl.q.fts.char_searches);
  1741. buffer_json_member_add_uint64(wb, "total", ctl.q.fts.searches);
  1742. }
  1743. buffer_json_object_close(wb);
  1744. }
  1745. if (mode & (CONTEXTS_V2_VERSIONS))
  1746. version_hashes_api_v2(wb, &ctl.versions);
  1747. if (mode & CONTEXTS_V2_AGENTS)
  1748. buffer_json_agents_v2(wb, &ctl.timings, ctl.now, mode & (CONTEXTS_V2_AGENTS_INFO), true);
  1749. }
  1750. buffer_json_cloud_timings(wb, "timings", &ctl.timings);
  1751. buffer_json_finalize(wb);
  1752. cleanup:
  1753. dictionary_destroy(ctl.nodes.dict);
  1754. dictionary_destroy(ctl.contexts.dict);
  1755. dictionary_destroy(ctl.functions.dict);
  1756. dictionary_destroy(ctl.alerts.alerts);
  1757. dictionary_destroy(ctl.alerts.alert_instances);
  1758. simple_pattern_free(ctl.nodes.scope_pattern);
  1759. simple_pattern_free(ctl.nodes.pattern);
  1760. simple_pattern_free(ctl.contexts.pattern);
  1761. simple_pattern_free(ctl.contexts.scope_pattern);
  1762. simple_pattern_free(ctl.q.pattern);
  1763. simple_pattern_free(ctl.alerts.alert_name_pattern);
  1764. return resp;
  1765. }