rrdcontext.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_RRDCONTEXT_H
  3. #define NETDATA_RRDCONTEXT_H 1
  4. // ----------------------------------------------------------------------------
  5. // RRDMETRIC
  6. typedef struct rrdmetric_acquired RRDMETRIC_ACQUIRED;
  7. // ----------------------------------------------------------------------------
  8. // RRDINSTANCE
  9. typedef struct rrdinstance_acquired RRDINSTANCE_ACQUIRED;
  10. // ----------------------------------------------------------------------------
  11. // RRDCONTEXT
  12. typedef struct rrdcontext_acquired RRDCONTEXT_ACQUIRED;
  13. // ----------------------------------------------------------------------------
  14. #include "../rrd.h"
  15. bool rrdinstance_acquired_id_and_name_are_same(RRDINSTANCE_ACQUIRED *ria);
  16. const char *rrdmetric_acquired_id(RRDMETRIC_ACQUIRED *rma);
  17. const char *rrdmetric_acquired_name(RRDMETRIC_ACQUIRED *rma);
  18. bool rrdmetric_acquired_has_name(RRDMETRIC_ACQUIRED *rma);
  19. STRING *rrdmetric_acquired_id_dup(RRDMETRIC_ACQUIRED *rma);
  20. STRING *rrdmetric_acquired_name_dup(RRDMETRIC_ACQUIRED *rma);
  21. NETDATA_DOUBLE rrdmetric_acquired_last_stored_value(RRDMETRIC_ACQUIRED *rma);
  22. time_t rrdmetric_acquired_first_entry(RRDMETRIC_ACQUIRED *rma);
  23. time_t rrdmetric_acquired_last_entry(RRDMETRIC_ACQUIRED *rma);
  24. bool rrdmetric_acquired_belongs_to_instance(RRDMETRIC_ACQUIRED *rma, RRDINSTANCE_ACQUIRED *ria);
  25. const char *rrdinstance_acquired_id(RRDINSTANCE_ACQUIRED *ria);
  26. const char *rrdinstance_acquired_name(RRDINSTANCE_ACQUIRED *ria);
  27. bool rrdinstance_acquired_has_name(RRDINSTANCE_ACQUIRED *ria);
  28. const char *rrdinstance_acquired_units(RRDINSTANCE_ACQUIRED *ria);
  29. STRING *rrdinstance_acquired_units_dup(RRDINSTANCE_ACQUIRED *ria);
  30. RRDLABELS *rrdinstance_acquired_labels(RRDINSTANCE_ACQUIRED *ria);
  31. DICTIONARY *rrdinstance_acquired_functions(RRDINSTANCE_ACQUIRED *ria);
  32. RRDHOST *rrdinstance_acquired_rrdhost(RRDINSTANCE_ACQUIRED *ria);
  33. RRDSET *rrdinstance_acquired_rrdset(RRDINSTANCE_ACQUIRED *ria);
  34. bool rrdinstance_acquired_belongs_to_context(RRDINSTANCE_ACQUIRED *ria, RRDCONTEXT_ACQUIRED *rca);
  35. time_t rrdinstance_acquired_update_every(RRDINSTANCE_ACQUIRED *ria);
  36. const char *rrdcontext_acquired_units(RRDCONTEXT_ACQUIRED *rca);
  37. const char *rrdcontext_acquired_title(RRDCONTEXT_ACQUIRED *rca);
  38. RRDSET_TYPE rrdcontext_acquired_chart_type(RRDCONTEXT_ACQUIRED *rca);
  39. // ----------------------------------------------------------------------------
  40. // public API for rrdhost
  41. void rrdhost_load_rrdcontext_data(RRDHOST *host);
  42. void rrdhost_create_rrdcontexts(RRDHOST *host);
  43. void rrdhost_destroy_rrdcontexts(RRDHOST *host);
  44. void rrdcontext_host_child_connected(RRDHOST *host);
  45. void rrdcontext_host_child_disconnected(RRDHOST *host);
  46. int rrdcontext_foreach_instance_with_rrdset_in_context(RRDHOST *host, const char *context, int (*callback)(RRDSET *st, void *data), void *data);
  47. typedef enum {
  48. RRDCONTEXT_OPTION_NONE = 0,
  49. RRDCONTEXT_OPTION_SHOW_METRICS = (1 << 0),
  50. RRDCONTEXT_OPTION_SHOW_INSTANCES = (1 << 1),
  51. RRDCONTEXT_OPTION_SHOW_LABELS = (1 << 2),
  52. RRDCONTEXT_OPTION_SHOW_QUEUED = (1 << 3),
  53. RRDCONTEXT_OPTION_SHOW_FLAGS = (1 << 4),
  54. RRDCONTEXT_OPTION_SHOW_DELETED = (1 << 5),
  55. RRDCONTEXT_OPTION_DEEPSCAN = (1 << 6),
  56. RRDCONTEXT_OPTION_SHOW_UUIDS = (1 << 7),
  57. RRDCONTEXT_OPTION_SHOW_HIDDEN = (1 << 8),
  58. RRDCONTEXT_OPTION_SKIP_ID = (1 << 31), // internal use
  59. } RRDCONTEXT_TO_JSON_OPTIONS;
  60. #define RRDCONTEXT_OPTIONS_ALL (RRDCONTEXT_OPTION_SHOW_METRICS|RRDCONTEXT_OPTION_SHOW_INSTANCES|RRDCONTEXT_OPTION_SHOW_LABELS|RRDCONTEXT_OPTION_SHOW_QUEUED|RRDCONTEXT_OPTION_SHOW_FLAGS|RRDCONTEXT_OPTION_SHOW_DELETED|RRDCONTEXT_OPTION_SHOW_UUIDS|RRDCONTEXT_OPTION_SHOW_HIDDEN)
  61. int rrdcontext_to_json(RRDHOST *host, BUFFER *wb, time_t after, time_t before, RRDCONTEXT_TO_JSON_OPTIONS options, const char *context, SIMPLE_PATTERN *chart_label_key, SIMPLE_PATTERN *chart_labels_filter, SIMPLE_PATTERN *chart_dimensions);
  62. int rrdcontexts_to_json(RRDHOST *host, BUFFER *wb, time_t after, time_t before, RRDCONTEXT_TO_JSON_OPTIONS options, SIMPLE_PATTERN *chart_label_key, SIMPLE_PATTERN *chart_labels_filter, SIMPLE_PATTERN *chart_dimensions);
  63. // ----------------------------------------------------------------------------
  64. // public API for rrdcontexts
  65. const char *rrdcontext_acquired_id(RRDCONTEXT_ACQUIRED *rca);
  66. bool rrdcontext_acquired_belongs_to_host(RRDCONTEXT_ACQUIRED *rca, RRDHOST *host);
  67. // ----------------------------------------------------------------------------
  68. // public API for rrddims
  69. void rrdcontext_updated_rrddim(RRDDIM *rd);
  70. void rrdcontext_removed_rrddim(RRDDIM *rd);
  71. void rrdcontext_updated_rrddim_algorithm(RRDDIM *rd);
  72. void rrdcontext_updated_rrddim_multiplier(RRDDIM *rd);
  73. void rrdcontext_updated_rrddim_divisor(RRDDIM *rd);
  74. void rrdcontext_updated_rrddim_flags(RRDDIM *rd);
  75. void rrdcontext_collected_rrddim(RRDDIM *rd);
  76. int rrdcontext_find_dimension_uuid(RRDSET *st, const char *id, uuid_t *store_uuid);
  77. // ----------------------------------------------------------------------------
  78. // public API for rrdsets
  79. void rrdcontext_updated_rrdset(RRDSET *st);
  80. void rrdcontext_removed_rrdset(RRDSET *st);
  81. void rrdcontext_updated_rrdset_name(RRDSET *st);
  82. void rrdcontext_updated_rrdset_flags(RRDSET *st);
  83. void rrdcontext_updated_retention_rrdset(RRDSET *st);
  84. void rrdcontext_collected_rrdset(RRDSET *st);
  85. int rrdcontext_find_chart_uuid(RRDSET *st, uuid_t *store_uuid);
  86. // ----------------------------------------------------------------------------
  87. // public API for ACLK
  88. void rrdcontext_hub_checkpoint_command(void *cmd);
  89. void rrdcontext_hub_stop_streaming_command(void *cmd);
  90. // ----------------------------------------------------------------------------
  91. // public API for threads
  92. void rrdcontext_db_rotation(void);
  93. void *rrdcontext_main(void *);
  94. // ----------------------------------------------------------------------------
  95. // public API for queries
  96. typedef enum __attribute__ ((__packed__)) {
  97. QUERY_STATUS_NONE = 0,
  98. QUERY_STATUS_QUERIED = (1 << 0),
  99. QUERY_STATUS_DIMENSION_HIDDEN = (1 << 1),
  100. QUERY_STATUS_EXCLUDED = (1 << 2),
  101. QUERY_STATUS_FAILED = (1 << 3),
  102. } QUERY_STATUS;
  103. typedef struct query_plan_entry {
  104. size_t tier;
  105. time_t after;
  106. time_t before;
  107. } QUERY_PLAN_ENTRY;
  108. #define QUERY_PLANS_MAX (RRD_STORAGE_TIERS)
  109. typedef struct query_metrics_counts { // counts the number of metrics related to an object
  110. size_t selected; // selected to be queried
  111. size_t excluded; // not selected to be queried
  112. size_t queried; // successfully queried
  113. size_t failed; // failed to be queried
  114. } QUERY_METRICS_COUNTS;
  115. typedef struct query_instances_counts { // counts the number of instances related to an object
  116. size_t selected; // selected to be queried
  117. size_t excluded; // not selected to be queried
  118. size_t queried; // successfully queried
  119. size_t failed; // failed to be queried
  120. } QUERY_INSTANCES_COUNTS;
  121. typedef struct query_alerts_counts { // counts the number of alerts related to an object
  122. size_t clear; // number of alerts in clear state
  123. size_t warning; // number of alerts in warning state
  124. size_t critical; // number of alerts in critical state
  125. size_t other; // number of alerts in any other state
  126. } QUERY_ALERTS_COUNTS;
  127. typedef struct query_node {
  128. uint32_t slot;
  129. RRDHOST *rrdhost;
  130. char node_id[UUID_STR_LEN];
  131. usec_t duration_ut;
  132. STORAGE_POINT query_points;
  133. QUERY_INSTANCES_COUNTS instances;
  134. QUERY_METRICS_COUNTS metrics;
  135. QUERY_ALERTS_COUNTS alerts;
  136. } QUERY_NODE;
  137. typedef struct query_context {
  138. uint32_t slot;
  139. RRDCONTEXT_ACQUIRED *rca;
  140. STORAGE_POINT query_points;
  141. QUERY_INSTANCES_COUNTS instances;
  142. QUERY_METRICS_COUNTS metrics;
  143. QUERY_ALERTS_COUNTS alerts;
  144. } QUERY_CONTEXT;
  145. typedef struct query_instance {
  146. uint32_t slot;
  147. uint32_t query_host_id;
  148. RRDINSTANCE_ACQUIRED *ria;
  149. STRING *id_fqdn; // never access this directly - it is created on demand via query_instance_id_fqdn()
  150. STRING *name_fqdn; // never access this directly - it is created on demand via query_instance_name_fqdn()
  151. STORAGE_POINT query_points;
  152. QUERY_METRICS_COUNTS metrics;
  153. QUERY_ALERTS_COUNTS alerts;
  154. } QUERY_INSTANCE;
  155. typedef struct query_dimension {
  156. uint32_t slot;
  157. uint32_t priority;
  158. RRDMETRIC_ACQUIRED *rma;
  159. QUERY_STATUS status;
  160. } QUERY_DIMENSION;
  161. typedef struct query_metric {
  162. RRDR_DIMENSION_FLAGS status;
  163. struct query_metric_tier {
  164. STORAGE_METRIC_HANDLE *db_metric_handle;
  165. time_t db_first_time_s; // the oldest timestamp available for this tier
  166. time_t db_last_time_s; // the latest timestamp available for this tier
  167. time_t db_update_every_s; // latest update every for this tier
  168. long weight;
  169. } tiers[RRD_STORAGE_TIERS];
  170. struct {
  171. size_t used;
  172. QUERY_PLAN_ENTRY array[QUERY_PLANS_MAX];
  173. } plan;
  174. struct {
  175. uint32_t query_node_id;
  176. uint32_t query_context_id;
  177. uint32_t query_instance_id;
  178. uint32_t query_dimension_id;
  179. } link;
  180. STORAGE_POINT query_points;
  181. struct {
  182. uint32_t slot;
  183. uint32_t first_slot;
  184. STRING *id;
  185. STRING *name;
  186. STRING *units;
  187. } grouped_as;
  188. usec_t duration_ut;
  189. } QUERY_METRIC;
  190. #define MAX_QUERY_TARGET_ID_LENGTH 255
  191. #define MAX_QUERY_GROUP_BY_PASSES 2
  192. typedef bool (*qt_interrupt_callback_t)(void *data);
  193. struct group_by_pass {
  194. RRDR_GROUP_BY group_by;
  195. char *group_by_label;
  196. RRDR_GROUP_BY_FUNCTION aggregation;
  197. };
  198. typedef struct query_target_request {
  199. size_t version;
  200. const char *scope_nodes;
  201. const char *scope_contexts;
  202. // selecting / filtering metrics to be queried
  203. RRDHOST *host; // the host to be queried (can be NULL, hosts will be used)
  204. RRDCONTEXT_ACQUIRED *rca; // the context to be queried (can be NULL)
  205. RRDINSTANCE_ACQUIRED *ria; // the instance to be queried (can be NULL)
  206. RRDMETRIC_ACQUIRED *rma; // the metric to be queried (can be NULL)
  207. RRDSET *st; // the chart to be queried (NULL, for context queries)
  208. const char *nodes; // hosts simple pattern
  209. const char *contexts; // contexts simple pattern (context queries)
  210. const char *instances; // charts simple pattern (for context queries)
  211. const char *dimensions; // dimensions simple pattern
  212. const char *chart_label_key; // select only the chart having this label key
  213. const char *labels; // select only the charts having this combo of label key:value
  214. const char *alerts; // select only the charts having this combo of alert name:status
  215. time_t after; // the requested timeframe
  216. time_t before; // the requested timeframe
  217. size_t points; // the requested number of points to be returned
  218. uint32_t format; // DATASOURCE_FORMAT
  219. RRDR_OPTIONS options;
  220. time_t timeout_ms; // the timeout of the query in milliseconds
  221. size_t tier;
  222. QUERY_SOURCE query_source;
  223. STORAGE_PRIORITY priority;
  224. // resampling metric values across time
  225. time_t resampling_time;
  226. // grouping metric values across time
  227. RRDR_TIME_GROUPING time_group_method;
  228. const char *time_group_options;
  229. // group by across multiple time-series
  230. struct group_by_pass group_by[MAX_QUERY_GROUP_BY_PASSES];
  231. usec_t received_ut;
  232. qt_interrupt_callback_t interrupt_callback;
  233. void *interrupt_callback_data;
  234. } QUERY_TARGET_REQUEST;
  235. #define GROUP_BY_MAX_LABEL_KEYS 10
  236. struct query_tier_statistics {
  237. size_t queries;
  238. size_t points;
  239. time_t update_every;
  240. struct {
  241. time_t first_time_s;
  242. time_t last_time_s;
  243. } retention;
  244. };
  245. struct query_versions {
  246. uint64_t contexts_hard_hash;
  247. uint64_t contexts_soft_hash;
  248. uint64_t alerts_hard_hash;
  249. uint64_t alerts_soft_hash;
  250. };
  251. struct query_timings {
  252. usec_t received_ut;
  253. usec_t preprocessed_ut;
  254. usec_t executed_ut;
  255. usec_t finished_ut;
  256. };
  257. #define query_view_update_every(qt) ((qt)->window.group * (qt)->window.query_granularity)
  258. typedef struct query_target {
  259. char id[MAX_QUERY_TARGET_ID_LENGTH + 1]; // query identifier (for logging)
  260. QUERY_TARGET_REQUEST request;
  261. struct {
  262. time_t now; // the current timestamp, the absolute max for any query timestamp
  263. bool relative; // true when the request made with relative timestamps, true if it was absolute
  264. bool aligned;
  265. time_t after; // the absolute timestamp this query is about
  266. time_t before; // the absolute timestamp this query is about
  267. time_t query_granularity;
  268. size_t points; // the number of points the query will return (maybe different from the request)
  269. size_t group;
  270. RRDR_TIME_GROUPING time_group_method;
  271. const char *time_group_options;
  272. size_t resampling_group;
  273. NETDATA_DOUBLE resampling_divisor;
  274. RRDR_OPTIONS options;
  275. size_t tier;
  276. } window;
  277. struct {
  278. size_t queries[RRD_STORAGE_TIERS];
  279. time_t first_time_s; // the combined first_time_t of all metrics in the query, across all tiers
  280. time_t last_time_s; // the combined last_time_T of all metrics in the query, across all tiers
  281. time_t minimum_latest_update_every_s; // the min update every of the metrics in the query
  282. struct query_tier_statistics tiers[RRD_STORAGE_TIERS];
  283. } db;
  284. struct {
  285. QUERY_METRIC *array; // the metrics to be queried (all of them should be queried, no exceptions)
  286. uint32_t used; // how many items of the array are used
  287. uint32_t size; // the size of the array
  288. SIMPLE_PATTERN *pattern;
  289. } query;
  290. struct {
  291. QUERY_DIMENSION *array;
  292. uint32_t used; // how many items of the array are used
  293. uint32_t size; // the size of the array
  294. } dimensions;
  295. struct {
  296. QUERY_INSTANCE *array;
  297. uint32_t used; // how many items of the array are used
  298. uint32_t size; // the size of the array
  299. SIMPLE_PATTERN *pattern;
  300. SIMPLE_PATTERN *labels_pattern;
  301. SIMPLE_PATTERN *alerts_pattern;
  302. SIMPLE_PATTERN *chart_label_key_pattern;
  303. } instances;
  304. struct {
  305. QUERY_CONTEXT *array;
  306. uint32_t used; // how many items of the array are used
  307. uint32_t size; // the size of the array
  308. SIMPLE_PATTERN *pattern;
  309. SIMPLE_PATTERN *scope_pattern;
  310. } contexts;
  311. struct {
  312. QUERY_NODE *array;
  313. uint32_t used; // how many items of the array are used
  314. uint32_t size; // the size of the array
  315. SIMPLE_PATTERN *pattern;
  316. SIMPLE_PATTERN *scope_pattern;
  317. } nodes;
  318. struct {
  319. size_t used;
  320. char *label_keys[GROUP_BY_MAX_LABEL_KEYS * MAX_QUERY_GROUP_BY_PASSES];
  321. } group_by[MAX_QUERY_GROUP_BY_PASSES];
  322. STORAGE_POINT query_points;
  323. struct query_versions versions;
  324. struct query_timings timings;
  325. struct {
  326. SPINLOCK spinlock;
  327. bool used; // when true, this query is currently being used
  328. bool relative; // when true, this query uses relative timestamps
  329. size_t queries; // how many query we have done so far with this QUERY_TARGET - not related to database queries
  330. struct query_target *prev;
  331. struct query_target *next;
  332. } internal;
  333. } QUERY_TARGET;
  334. struct sql_alert_transition_data {
  335. usec_t global_id;
  336. uuid_t *transition_id;
  337. uuid_t *host_id;
  338. uuid_t *config_hash_id;
  339. uint32_t alarm_id;
  340. const char *alert_name;
  341. const char *chart;
  342. const char *chart_name;
  343. const char *chart_context;
  344. const char *family;
  345. const char *recipient;
  346. const char *units;
  347. const char *exec;
  348. const char *info;
  349. const char *summary;
  350. const char *classification;
  351. const char *type;
  352. const char *component;
  353. time_t when_key;
  354. time_t duration;
  355. time_t non_clear_duration;
  356. uint64_t flags;
  357. time_t delay_up_to_timestamp;
  358. time_t exec_run_timestamp;
  359. int exec_code;
  360. int new_status;
  361. int old_status;
  362. int delay;
  363. time_t last_repeat;
  364. NETDATA_DOUBLE new_value;
  365. NETDATA_DOUBLE old_value;
  366. };
  367. struct sql_alert_config_data {
  368. uuid_t *config_hash_id;
  369. const char *name;
  370. struct {
  371. const char *on_template;
  372. const char *on_key;
  373. const char *os;
  374. const char *hosts;
  375. const char *families;
  376. const char *plugin;
  377. const char *module;
  378. const char *host_labels;
  379. const char *chart_labels;
  380. const char *charts;
  381. } selectors;
  382. const char *info;
  383. const char *classification;
  384. const char *component;
  385. const char *type;
  386. const char *summary;
  387. struct {
  388. struct {
  389. const char *dimensions;
  390. const char *method;
  391. uint32_t options;
  392. int32_t after;
  393. int32_t before;
  394. const char *lookup; // the lookup line, unparsed
  395. } db;
  396. const char *calc; // the calculation expression, unparsed
  397. const char *units;
  398. int32_t update_every; // the update frequency of the alert, in seconds
  399. const char *every; // the every line, unparsed
  400. } value;
  401. struct {
  402. const char *green; // the green threshold, unparsed
  403. const char *red; // the red threshold, unparsed
  404. const char *warn; // the warning expression, unparsed
  405. const char *crit; // the critical expression, unparsed
  406. } status;
  407. struct {
  408. const char *exec; // the script to execute, or NULL to execute the default script
  409. const char *to_key; // the recipient, or NULL for the default recipient
  410. const char *delay; // the delay line, unparsed
  411. const char *repeat; // the repeat line, unparsed
  412. const char *options; // FIXME what is this?
  413. } notification;
  414. const char *source; // the configuration file and line this alert came from
  415. };
  416. int contexts_v2_alert_config_to_json(struct web_client *w, const char *config_hash_id);
  417. struct sql_alert_instance_v2_entry {
  418. RRDCALC *tmp;
  419. size_t ati;
  420. STRING *context;
  421. STRING *chart_id;
  422. STRING *chart_name;
  423. STRING *name;
  424. STRING *family;
  425. STRING *units;
  426. STRING *source;
  427. STRING *classification;
  428. STRING *type;
  429. STRING *component;
  430. STRING *recipient;
  431. RRDCALC_STATUS status;
  432. RRDCALC_FLAGS flags;
  433. STRING *info;
  434. STRING *summary;
  435. NETDATA_DOUBLE value;
  436. time_t last_updated;
  437. time_t last_status_change;
  438. NETDATA_DOUBLE last_status_change_value;
  439. uuid_t config_hash_id;
  440. usec_t global_id;
  441. uuid_t last_transition_id;
  442. uint32_t alarm_id;
  443. RRDHOST *host;
  444. size_t ni;
  445. };
  446. static inline NEVERNULL QUERY_NODE *query_node(QUERY_TARGET *qt, size_t id) {
  447. internal_fatal(id >= qt->nodes.used, "QUERY: invalid query host id");
  448. return &qt->nodes.array[id];
  449. }
  450. static inline NEVERNULL QUERY_CONTEXT *query_context(QUERY_TARGET *qt, size_t query_context_id) {
  451. internal_fatal(query_context_id >= qt->contexts.used, "QUERY: invalid query context id");
  452. return &qt->contexts.array[query_context_id];
  453. }
  454. static inline NEVERNULL QUERY_INSTANCE *query_instance(QUERY_TARGET *qt, size_t query_instance_id) {
  455. internal_fatal(query_instance_id >= qt->instances.used, "QUERY: invalid query instance id");
  456. return &qt->instances.array[query_instance_id];
  457. }
  458. static inline NEVERNULL QUERY_DIMENSION *query_dimension(QUERY_TARGET *qt, size_t query_dimension_id) {
  459. internal_fatal(query_dimension_id >= qt->dimensions.used, "QUERY: invalid query dimension id");
  460. return &qt->dimensions.array[query_dimension_id];
  461. }
  462. static inline NEVERNULL QUERY_METRIC *query_metric(QUERY_TARGET *qt, size_t id) {
  463. internal_fatal(id >= qt->query.used, "QUERY: invalid query metric id");
  464. return &qt->query.array[id];
  465. }
  466. static inline const char *query_metric_id(QUERY_TARGET *qt, QUERY_METRIC *qm) {
  467. QUERY_DIMENSION *qd = query_dimension(qt, qm->link.query_dimension_id);
  468. return rrdmetric_acquired_id(qd->rma);
  469. }
  470. static inline const char *query_metric_name(QUERY_TARGET *qt, QUERY_METRIC *qm) {
  471. QUERY_DIMENSION *qd = query_dimension(qt, qm->link.query_dimension_id);
  472. return rrdmetric_acquired_name(qd->rma);
  473. }
  474. struct storage_engine *query_metric_storage_engine(QUERY_TARGET *qt, QUERY_METRIC *qm, size_t tier);
  475. STRING *query_instance_id_fqdn(QUERY_INSTANCE *qi, size_t version);
  476. STRING *query_instance_name_fqdn(QUERY_INSTANCE *qi, size_t version);
  477. void query_target_free(void);
  478. void query_target_release(QUERY_TARGET *qt);
  479. QUERY_TARGET *query_target_create(QUERY_TARGET_REQUEST *qtr);
  480. typedef enum __attribute__((packed)) {
  481. ATF_STATUS = 0,
  482. ATF_CLASS,
  483. ATF_TYPE,
  484. ATF_COMPONENT,
  485. ATF_ROLE,
  486. ATF_NODE,
  487. ATF_ALERT_NAME,
  488. ATF_CHART_NAME,
  489. ATF_CONTEXT,
  490. // total
  491. ATF_TOTAL_ENTRIES,
  492. } ALERT_TRANSITION_FACET;
  493. struct alert_transitions_facets {
  494. const char *name;
  495. const char *query_param;
  496. const char *id;
  497. size_t order;
  498. };
  499. extern struct alert_transitions_facets alert_transition_facets[];
  500. struct api_v2_contexts_request {
  501. char *scope_nodes;
  502. char *scope_contexts;
  503. char *nodes;
  504. char *contexts;
  505. char *q;
  506. CONTEXTS_V2_OPTIONS options;
  507. struct {
  508. CONTEXTS_V2_ALERT_STATUS status;
  509. char *alert;
  510. char *transition;
  511. uint32_t last;
  512. const char *facets[ATF_TOTAL_ENTRIES];
  513. usec_t global_id_anchor;
  514. } alerts;
  515. time_t after;
  516. time_t before;
  517. time_t timeout_ms;
  518. qt_interrupt_callback_t interrupt_callback;
  519. void *interrupt_callback_data;
  520. };
  521. typedef enum __attribute__ ((__packed__)) {
  522. CONTEXTS_V2_SEARCH = (1 << 1),
  523. CONTEXTS_V2_NODES = (1 << 2),
  524. CONTEXTS_V2_NODES_INFO = (1 << 3),
  525. CONTEXTS_V2_NODE_INSTANCES = (1 << 4),
  526. CONTEXTS_V2_CONTEXTS = (1 << 5),
  527. CONTEXTS_V2_AGENTS = (1 << 6),
  528. CONTEXTS_V2_AGENTS_INFO = (1 << 7),
  529. CONTEXTS_V2_VERSIONS = (1 << 8),
  530. CONTEXTS_V2_FUNCTIONS = (1 << 9),
  531. CONTEXTS_V2_ALERTS = (1 << 10),
  532. CONTEXTS_V2_ALERT_TRANSITIONS = (1 << 11),
  533. } CONTEXTS_V2_MODE;
  534. int rrdcontext_to_json_v2(BUFFER *wb, struct api_v2_contexts_request *req, CONTEXTS_V2_MODE mode);
  535. RRDCONTEXT_TO_JSON_OPTIONS rrdcontext_to_json_parse_options(char *o);
  536. void buffer_json_agents_v2(BUFFER *wb, struct query_timings *timings, time_t now_s, bool info, bool array);
  537. void buffer_json_node_add_v2(BUFFER *wb, RRDHOST *host, size_t ni, usec_t duration_ut, bool status);
  538. void buffer_json_query_timings(BUFFER *wb, const char *key, struct query_timings *timings);
  539. void buffer_json_cloud_timings(BUFFER *wb, const char *key, struct query_timings *timings);
  540. // ----------------------------------------------------------------------------
  541. // scope
  542. typedef ssize_t (*foreach_host_cb_t)(void *data, RRDHOST *host, bool queryable);
  543. ssize_t query_scope_foreach_host(SIMPLE_PATTERN *scope_hosts_sp, SIMPLE_PATTERN *hosts_sp,
  544. foreach_host_cb_t cb, void *data,
  545. struct query_versions *versions,
  546. char *host_node_id_str);
  547. typedef ssize_t (*foreach_context_cb_t)(void *data, RRDCONTEXT_ACQUIRED *rca, bool queryable_context);
  548. ssize_t query_scope_foreach_context(RRDHOST *host, const char *scope_contexts, SIMPLE_PATTERN *scope_contexts_sp, SIMPLE_PATTERN *contexts_sp, foreach_context_cb_t cb, bool queryable_host, void *data);
  549. // ----------------------------------------------------------------------------
  550. // public API for weights
  551. typedef ssize_t (*weights_add_metric_t)(void *data, RRDHOST *host, RRDCONTEXT_ACQUIRED *rca, RRDINSTANCE_ACQUIRED *ria, RRDMETRIC_ACQUIRED *rma);
  552. ssize_t weights_foreach_rrdmetric_in_context(RRDCONTEXT_ACQUIRED *rca,
  553. SIMPLE_PATTERN *instances_sp,
  554. SIMPLE_PATTERN *chart_label_key_sp,
  555. SIMPLE_PATTERN *labels_sp,
  556. SIMPLE_PATTERN *alerts_sp,
  557. SIMPLE_PATTERN *dimensions_sp,
  558. bool match_ids, bool match_names,
  559. size_t version,
  560. weights_add_metric_t cb,
  561. void *data);
  562. bool rrdcontext_retention_match(RRDCONTEXT_ACQUIRED *rca, time_t after, time_t before);
  563. #define query_matches_retention(after, before, first_entry_s, last_entry_s, update_every_s) \
  564. (((first_entry_s) - ((update_every_s) * 2) <= (before)) && \
  565. ((last_entry_s) + ((update_every_s) * 2) >= (after)))
  566. #define query_target_aggregatable(qt) ((qt)->window.options & RRDR_OPTION_RETURN_RAW)
  567. static inline bool query_has_group_by_aggregation_percentage(QUERY_TARGET *qt) {
  568. // backwards compatibility
  569. // If the request was made with group_by = "percentage-of-instance"
  570. // we need to send back "raw" output with "count"
  571. // otherwise, we need to send back "raw" output with "hidden"
  572. bool last_is_percentage = false;
  573. for(int g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  574. if(qt->request.group_by[g].group_by == RRDR_GROUP_BY_NONE)
  575. break;
  576. if(qt->request.group_by[g].group_by & RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE)
  577. // backwards compatibility
  578. return false;
  579. if(qt->request.group_by[g].aggregation == RRDR_GROUP_BY_FUNCTION_PERCENTAGE)
  580. last_is_percentage = true;
  581. else
  582. last_is_percentage = false;
  583. }
  584. return last_is_percentage;
  585. }
  586. static inline bool query_target_has_percentage_of_group(QUERY_TARGET *qt) {
  587. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  588. if (qt->request.group_by[g].group_by & RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE)
  589. return true;
  590. if (qt->request.group_by[g].aggregation == RRDR_GROUP_BY_FUNCTION_PERCENTAGE)
  591. return true;
  592. }
  593. return false;
  594. }
  595. static inline bool query_target_needs_all_dimensions(QUERY_TARGET *qt) {
  596. if(qt->request.options & RRDR_OPTION_PERCENTAGE)
  597. return true;
  598. return query_target_has_percentage_of_group(qt);
  599. }
  600. static inline bool query_target_has_percentage_units(QUERY_TARGET *qt) {
  601. if(qt->window.time_group_method == RRDR_GROUPING_CV)
  602. return true;
  603. if((qt->request.options & RRDR_OPTION_PERCENTAGE) && !(qt->window.options & RRDR_OPTION_RETURN_RAW))
  604. return true;
  605. return query_target_has_percentage_of_group(qt);
  606. }
  607. #endif // NETDATA_RRDCONTEXT_H