rrdcontext.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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 *smh;
  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. uuid_t *transaction;
  235. } QUERY_TARGET_REQUEST;
  236. #define GROUP_BY_MAX_LABEL_KEYS 10
  237. struct query_tier_statistics {
  238. size_t queries;
  239. size_t points;
  240. time_t update_every;
  241. struct {
  242. time_t first_time_s;
  243. time_t last_time_s;
  244. } retention;
  245. };
  246. struct query_versions {
  247. uint64_t contexts_hard_hash;
  248. uint64_t contexts_soft_hash;
  249. uint64_t alerts_hard_hash;
  250. uint64_t alerts_soft_hash;
  251. };
  252. struct query_timings {
  253. usec_t received_ut;
  254. usec_t preprocessed_ut;
  255. usec_t executed_ut;
  256. usec_t finished_ut;
  257. };
  258. #define query_view_update_every(qt) ((qt)->window.group * (qt)->window.query_granularity)
  259. typedef struct query_target {
  260. char id[MAX_QUERY_TARGET_ID_LENGTH + 1]; // query identifier (for logging)
  261. QUERY_TARGET_REQUEST request;
  262. struct {
  263. time_t now; // the current timestamp, the absolute max for any query timestamp
  264. bool relative; // true when the request made with relative timestamps, true if it was absolute
  265. bool aligned;
  266. time_t after; // the absolute timestamp this query is about
  267. time_t before; // the absolute timestamp this query is about
  268. time_t query_granularity;
  269. size_t points; // the number of points the query will return (maybe different from the request)
  270. size_t group;
  271. RRDR_TIME_GROUPING time_group_method;
  272. const char *time_group_options;
  273. size_t resampling_group;
  274. NETDATA_DOUBLE resampling_divisor;
  275. RRDR_OPTIONS options;
  276. size_t tier;
  277. } window;
  278. struct {
  279. size_t queries[RRD_STORAGE_TIERS];
  280. time_t first_time_s; // the combined first_time_t of all metrics in the query, across all tiers
  281. time_t last_time_s; // the combined last_time_T of all metrics in the query, across all tiers
  282. time_t minimum_latest_update_every_s; // the min update every of the metrics in the query
  283. struct query_tier_statistics tiers[RRD_STORAGE_TIERS];
  284. } db;
  285. struct {
  286. QUERY_METRIC *array; // the metrics to be queried (all of them should be queried, no exceptions)
  287. uint32_t used; // how many items of the array are used
  288. uint32_t size; // the size of the array
  289. SIMPLE_PATTERN *pattern;
  290. } query;
  291. struct {
  292. QUERY_DIMENSION *array;
  293. uint32_t used; // how many items of the array are used
  294. uint32_t size; // the size of the array
  295. } dimensions;
  296. struct {
  297. QUERY_INSTANCE *array;
  298. uint32_t used; // how many items of the array are used
  299. uint32_t size; // the size of the array
  300. SIMPLE_PATTERN *pattern;
  301. SIMPLE_PATTERN *labels_pattern;
  302. SIMPLE_PATTERN *alerts_pattern;
  303. SIMPLE_PATTERN *chart_label_key_pattern;
  304. } instances;
  305. struct {
  306. QUERY_CONTEXT *array;
  307. uint32_t used; // how many items of the array are used
  308. uint32_t size; // the size of the array
  309. SIMPLE_PATTERN *pattern;
  310. SIMPLE_PATTERN *scope_pattern;
  311. } contexts;
  312. struct {
  313. QUERY_NODE *array;
  314. uint32_t used; // how many items of the array are used
  315. uint32_t size; // the size of the array
  316. SIMPLE_PATTERN *pattern;
  317. SIMPLE_PATTERN *scope_pattern;
  318. } nodes;
  319. struct {
  320. size_t used;
  321. char *label_keys[GROUP_BY_MAX_LABEL_KEYS * MAX_QUERY_GROUP_BY_PASSES];
  322. } group_by[MAX_QUERY_GROUP_BY_PASSES];
  323. STORAGE_POINT query_points;
  324. struct query_versions versions;
  325. struct query_timings timings;
  326. struct {
  327. SPINLOCK spinlock;
  328. bool used; // when true, this query is currently being used
  329. bool relative; // when true, this query uses relative timestamps
  330. size_t queries; // how many query we have done so far with this QUERY_TARGET - not related to database queries
  331. struct query_target *prev;
  332. struct query_target *next;
  333. } internal;
  334. } QUERY_TARGET;
  335. struct sql_alert_transition_data {
  336. usec_t global_id;
  337. uuid_t *transition_id;
  338. uuid_t *host_id;
  339. uuid_t *config_hash_id;
  340. uint32_t alarm_id;
  341. const char *alert_name;
  342. const char *chart;
  343. const char *chart_name;
  344. const char *chart_context;
  345. const char *family;
  346. const char *recipient;
  347. const char *units;
  348. const char *exec;
  349. const char *info;
  350. const char *summary;
  351. const char *classification;
  352. const char *type;
  353. const char *component;
  354. time_t when_key;
  355. time_t duration;
  356. time_t non_clear_duration;
  357. uint64_t flags;
  358. time_t delay_up_to_timestamp;
  359. time_t exec_run_timestamp;
  360. int exec_code;
  361. int new_status;
  362. int old_status;
  363. int delay;
  364. time_t last_repeat;
  365. NETDATA_DOUBLE new_value;
  366. NETDATA_DOUBLE old_value;
  367. };
  368. struct sql_alert_config_data {
  369. uuid_t *config_hash_id;
  370. const char *name;
  371. struct {
  372. const char *on_template;
  373. const char *on_key;
  374. const char *os;
  375. const char *hosts;
  376. const char *families;
  377. const char *plugin;
  378. const char *module;
  379. const char *host_labels;
  380. const char *chart_labels;
  381. const char *charts;
  382. } selectors;
  383. const char *info;
  384. const char *classification;
  385. const char *component;
  386. const char *type;
  387. const char *summary;
  388. struct {
  389. struct {
  390. const char *dimensions;
  391. const char *method;
  392. uint32_t options;
  393. int32_t after;
  394. int32_t before;
  395. const char *lookup; // the lookup line, unparsed
  396. } db;
  397. const char *calc; // the calculation expression, unparsed
  398. const char *units;
  399. int32_t update_every; // the update frequency of the alert, in seconds
  400. const char *every; // the every line, unparsed
  401. } value;
  402. struct {
  403. const char *green; // the green threshold, unparsed
  404. const char *red; // the red threshold, unparsed
  405. const char *warn; // the warning expression, unparsed
  406. const char *crit; // the critical expression, unparsed
  407. } status;
  408. struct {
  409. const char *exec; // the script to execute, or NULL to execute the default script
  410. const char *to_key; // the recipient, or NULL for the default recipient
  411. const char *delay; // the delay line, unparsed
  412. const char *repeat; // the repeat line, unparsed
  413. const char *options; // FIXME what is this?
  414. } notification;
  415. const char *source; // the configuration file and line this alert came from
  416. };
  417. int contexts_v2_alert_config_to_json(struct web_client *w, const char *config_hash_id);
  418. struct sql_alert_instance_v2_entry {
  419. RRDCALC *tmp;
  420. size_t ati;
  421. STRING *context;
  422. STRING *chart_id;
  423. STRING *chart_name;
  424. STRING *name;
  425. STRING *family;
  426. STRING *units;
  427. STRING *source;
  428. STRING *classification;
  429. STRING *type;
  430. STRING *component;
  431. STRING *recipient;
  432. RRDCALC_STATUS status;
  433. RRDCALC_FLAGS flags;
  434. STRING *info;
  435. STRING *summary;
  436. NETDATA_DOUBLE value;
  437. time_t last_updated;
  438. time_t last_status_change;
  439. NETDATA_DOUBLE last_status_change_value;
  440. uuid_t config_hash_id;
  441. usec_t global_id;
  442. uuid_t last_transition_id;
  443. uint32_t alarm_id;
  444. RRDHOST *host;
  445. size_t ni;
  446. };
  447. static inline NEVERNULL QUERY_NODE *query_node(QUERY_TARGET *qt, size_t id) {
  448. internal_fatal(id >= qt->nodes.used, "QUERY: invalid query host id");
  449. return &qt->nodes.array[id];
  450. }
  451. static inline NEVERNULL QUERY_CONTEXT *query_context(QUERY_TARGET *qt, size_t query_context_id) {
  452. internal_fatal(query_context_id >= qt->contexts.used, "QUERY: invalid query context id");
  453. return &qt->contexts.array[query_context_id];
  454. }
  455. static inline NEVERNULL QUERY_INSTANCE *query_instance(QUERY_TARGET *qt, size_t query_instance_id) {
  456. internal_fatal(query_instance_id >= qt->instances.used, "QUERY: invalid query instance id");
  457. return &qt->instances.array[query_instance_id];
  458. }
  459. static inline NEVERNULL QUERY_DIMENSION *query_dimension(QUERY_TARGET *qt, size_t query_dimension_id) {
  460. internal_fatal(query_dimension_id >= qt->dimensions.used, "QUERY: invalid query dimension id");
  461. return &qt->dimensions.array[query_dimension_id];
  462. }
  463. static inline NEVERNULL QUERY_METRIC *query_metric(QUERY_TARGET *qt, size_t id) {
  464. internal_fatal(id >= qt->query.used, "QUERY: invalid query metric id");
  465. return &qt->query.array[id];
  466. }
  467. static inline const char *query_metric_id(QUERY_TARGET *qt, QUERY_METRIC *qm) {
  468. QUERY_DIMENSION *qd = query_dimension(qt, qm->link.query_dimension_id);
  469. return rrdmetric_acquired_id(qd->rma);
  470. }
  471. static inline const char *query_metric_name(QUERY_TARGET *qt, QUERY_METRIC *qm) {
  472. QUERY_DIMENSION *qd = query_dimension(qt, qm->link.query_dimension_id);
  473. return rrdmetric_acquired_name(qd->rma);
  474. }
  475. struct storage_engine *query_metric_storage_engine(QUERY_TARGET *qt, QUERY_METRIC *qm, size_t tier);
  476. STRING *query_instance_id_fqdn(QUERY_INSTANCE *qi, size_t version);
  477. STRING *query_instance_name_fqdn(QUERY_INSTANCE *qi, size_t version);
  478. void query_target_free(void);
  479. void query_target_release(QUERY_TARGET *qt);
  480. QUERY_TARGET *query_target_create(QUERY_TARGET_REQUEST *qtr);
  481. typedef enum __attribute__((packed)) {
  482. ATF_STATUS = 0,
  483. ATF_CLASS,
  484. ATF_TYPE,
  485. ATF_COMPONENT,
  486. ATF_ROLE,
  487. ATF_NODE,
  488. ATF_ALERT_NAME,
  489. ATF_CHART_NAME,
  490. ATF_CONTEXT,
  491. // total
  492. ATF_TOTAL_ENTRIES,
  493. } ALERT_TRANSITION_FACET;
  494. struct alert_transitions_facets {
  495. const char *name;
  496. const char *query_param;
  497. const char *id;
  498. size_t order;
  499. };
  500. extern struct alert_transitions_facets alert_transition_facets[];
  501. struct api_v2_contexts_request {
  502. char *scope_nodes;
  503. char *scope_contexts;
  504. char *nodes;
  505. char *contexts;
  506. char *q;
  507. CONTEXTS_V2_OPTIONS options;
  508. struct {
  509. CONTEXTS_V2_ALERT_STATUS status;
  510. char *alert;
  511. char *transition;
  512. uint32_t last;
  513. const char *facets[ATF_TOTAL_ENTRIES];
  514. usec_t global_id_anchor;
  515. } alerts;
  516. time_t after;
  517. time_t before;
  518. time_t timeout_ms;
  519. qt_interrupt_callback_t interrupt_callback;
  520. void *interrupt_callback_data;
  521. };
  522. typedef enum __attribute__ ((__packed__)) {
  523. CONTEXTS_V2_SEARCH = (1 << 1),
  524. CONTEXTS_V2_NODES = (1 << 2),
  525. CONTEXTS_V2_NODES_INFO = (1 << 3),
  526. CONTEXTS_V2_NODE_INSTANCES = (1 << 4),
  527. CONTEXTS_V2_CONTEXTS = (1 << 5),
  528. CONTEXTS_V2_AGENTS = (1 << 6),
  529. CONTEXTS_V2_AGENTS_INFO = (1 << 7),
  530. CONTEXTS_V2_VERSIONS = (1 << 8),
  531. CONTEXTS_V2_FUNCTIONS = (1 << 9),
  532. CONTEXTS_V2_ALERTS = (1 << 10),
  533. CONTEXTS_V2_ALERT_TRANSITIONS = (1 << 11),
  534. } CONTEXTS_V2_MODE;
  535. int rrdcontext_to_json_v2(BUFFER *wb, struct api_v2_contexts_request *req, CONTEXTS_V2_MODE mode);
  536. RRDCONTEXT_TO_JSON_OPTIONS rrdcontext_to_json_parse_options(char *o);
  537. void buffer_json_agents_v2(BUFFER *wb, struct query_timings *timings, time_t now_s, bool info, bool array);
  538. void buffer_json_node_add_v2(BUFFER *wb, RRDHOST *host, size_t ni, usec_t duration_ut, bool status);
  539. void buffer_json_query_timings(BUFFER *wb, const char *key, struct query_timings *timings);
  540. void buffer_json_cloud_timings(BUFFER *wb, const char *key, struct query_timings *timings);
  541. // ----------------------------------------------------------------------------
  542. // scope
  543. typedef ssize_t (*foreach_host_cb_t)(void *data, RRDHOST *host, bool queryable);
  544. ssize_t query_scope_foreach_host(SIMPLE_PATTERN *scope_hosts_sp, SIMPLE_PATTERN *hosts_sp,
  545. foreach_host_cb_t cb, void *data,
  546. struct query_versions *versions,
  547. char *host_node_id_str);
  548. typedef ssize_t (*foreach_context_cb_t)(void *data, RRDCONTEXT_ACQUIRED *rca, bool queryable_context);
  549. 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);
  550. // ----------------------------------------------------------------------------
  551. // public API for weights
  552. typedef ssize_t (*weights_add_metric_t)(void *data, RRDHOST *host, RRDCONTEXT_ACQUIRED *rca, RRDINSTANCE_ACQUIRED *ria, RRDMETRIC_ACQUIRED *rma);
  553. ssize_t weights_foreach_rrdmetric_in_context(RRDCONTEXT_ACQUIRED *rca,
  554. SIMPLE_PATTERN *instances_sp,
  555. SIMPLE_PATTERN *chart_label_key_sp,
  556. SIMPLE_PATTERN *labels_sp,
  557. SIMPLE_PATTERN *alerts_sp,
  558. SIMPLE_PATTERN *dimensions_sp,
  559. bool match_ids, bool match_names,
  560. size_t version,
  561. weights_add_metric_t cb,
  562. void *data);
  563. bool rrdcontext_retention_match(RRDCONTEXT_ACQUIRED *rca, time_t after, time_t before);
  564. #define query_matches_retention(after, before, first_entry_s, last_entry_s, update_every_s) \
  565. (((first_entry_s) - ((update_every_s) * 2) <= (before)) && \
  566. ((last_entry_s) + ((update_every_s) * 2) >= (after)))
  567. #define query_target_aggregatable(qt) ((qt)->window.options & RRDR_OPTION_RETURN_RAW)
  568. static inline bool query_has_group_by_aggregation_percentage(QUERY_TARGET *qt) {
  569. // backwards compatibility
  570. // If the request was made with group_by = "percentage-of-instance"
  571. // we need to send back "raw" output with "count"
  572. // otherwise, we need to send back "raw" output with "hidden"
  573. bool last_is_percentage = false;
  574. for(int g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  575. if(qt->request.group_by[g].group_by == RRDR_GROUP_BY_NONE)
  576. break;
  577. if(qt->request.group_by[g].group_by & RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE)
  578. // backwards compatibility
  579. return false;
  580. if(qt->request.group_by[g].aggregation == RRDR_GROUP_BY_FUNCTION_PERCENTAGE)
  581. last_is_percentage = true;
  582. else
  583. last_is_percentage = false;
  584. }
  585. return last_is_percentage;
  586. }
  587. static inline bool query_target_has_percentage_of_group(QUERY_TARGET *qt) {
  588. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  589. if (qt->request.group_by[g].group_by & RRDR_GROUP_BY_PERCENTAGE_OF_INSTANCE)
  590. return true;
  591. if (qt->request.group_by[g].aggregation == RRDR_GROUP_BY_FUNCTION_PERCENTAGE)
  592. return true;
  593. }
  594. return false;
  595. }
  596. static inline bool query_target_needs_all_dimensions(QUERY_TARGET *qt) {
  597. if(qt->request.options & RRDR_OPTION_PERCENTAGE)
  598. return true;
  599. return query_target_has_percentage_of_group(qt);
  600. }
  601. static inline bool query_target_has_percentage_units(QUERY_TARGET *qt) {
  602. if(qt->window.time_group_method == RRDR_GROUPING_CV)
  603. return true;
  604. if((qt->request.options & RRDR_OPTION_PERCENTAGE) && !(qt->window.options & RRDR_OPTION_RETURN_RAW))
  605. return true;
  606. return query_target_has_percentage_of_group(qt);
  607. }
  608. #endif // NETDATA_RRDCONTEXT_H