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