rrd.h 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_RRD_H
  3. #define NETDATA_RRD_H 1
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. // non-existing structs instead of voids
  8. // to enable type checking at compile time
  9. typedef struct storage_instance STORAGE_INSTANCE;
  10. typedef struct storage_metric_handle STORAGE_METRIC_HANDLE;
  11. typedef struct storage_alignment STORAGE_METRICS_GROUP;
  12. // forward typedefs
  13. typedef struct rrdhost RRDHOST;
  14. typedef struct rrddim RRDDIM;
  15. typedef struct rrdset RRDSET;
  16. typedef struct rrdcalc RRDCALC;
  17. typedef struct rrdcalctemplate RRDCALCTEMPLATE;
  18. typedef struct alarm_entry ALARM_ENTRY;
  19. typedef struct rrdfamily_acquired RRDFAMILY_ACQUIRED;
  20. typedef struct rrdvar_acquired RRDVAR_ACQUIRED;
  21. typedef struct rrdsetvar_acquired RRDSETVAR_ACQUIRED;
  22. typedef struct rrdcalc_acquired RRDCALC_ACQUIRED;
  23. typedef struct rrdhost_acquired RRDHOST_ACQUIRED;
  24. typedef struct rrdset_acquired RRDSET_ACQUIRED;
  25. typedef struct rrddim_acquired RRDDIM_ACQUIRED;
  26. typedef struct ml_host ml_host_t;
  27. typedef struct ml_chart ml_chart_t;
  28. typedef struct ml_dimension ml_dimension_t;
  29. typedef enum __attribute__ ((__packed__)) {
  30. QUERY_SOURCE_UNKNOWN = 0,
  31. QUERY_SOURCE_API_DATA,
  32. QUERY_SOURCE_API_BADGE,
  33. QUERY_SOURCE_API_WEIGHTS,
  34. QUERY_SOURCE_HEALTH,
  35. QUERY_SOURCE_ML,
  36. QUERY_SOURCE_UNITTEST,
  37. } QUERY_SOURCE;
  38. typedef enum __attribute__ ((__packed__)) storage_priority {
  39. STORAGE_PRIORITY_INTERNAL_DBENGINE = 0,
  40. STORAGE_PRIORITY_INTERNAL_QUERY_PREP,
  41. // query priorities
  42. STORAGE_PRIORITY_HIGH,
  43. STORAGE_PRIORITY_NORMAL,
  44. STORAGE_PRIORITY_LOW,
  45. STORAGE_PRIORITY_BEST_EFFORT,
  46. STORAGE_PRIORITY_INTERNAL_MAX_DONT_USE,
  47. } STORAGE_PRIORITY;
  48. // forward declarations
  49. struct rrddim_tier;
  50. #ifdef ENABLE_DBENGINE
  51. struct rrdeng_page_descr;
  52. struct rrdengine_instance;
  53. struct pg_cache_page_index;
  54. #endif
  55. // ----------------------------------------------------------------------------
  56. // memory mode
  57. typedef enum __attribute__ ((__packed__)) rrd_memory_mode {
  58. RRD_MEMORY_MODE_NONE = 0,
  59. RRD_MEMORY_MODE_RAM = 1,
  60. RRD_MEMORY_MODE_MAP = 2,
  61. RRD_MEMORY_MODE_SAVE = 3,
  62. RRD_MEMORY_MODE_ALLOC = 4,
  63. RRD_MEMORY_MODE_DBENGINE = 5,
  64. // this is 8-bit
  65. } RRD_MEMORY_MODE;
  66. #define RRD_MEMORY_MODE_NONE_NAME "none"
  67. #define RRD_MEMORY_MODE_RAM_NAME "ram"
  68. #define RRD_MEMORY_MODE_MAP_NAME "map"
  69. #define RRD_MEMORY_MODE_SAVE_NAME "save"
  70. #define RRD_MEMORY_MODE_ALLOC_NAME "alloc"
  71. #define RRD_MEMORY_MODE_DBENGINE_NAME "dbengine"
  72. extern RRD_MEMORY_MODE default_rrd_memory_mode;
  73. const char *rrd_memory_mode_name(RRD_MEMORY_MODE id);
  74. RRD_MEMORY_MODE rrd_memory_mode_id(const char *name);
  75. #include "daemon/common.h"
  76. #include "web/api/queries/query.h"
  77. #include "web/api/queries/rrdr.h"
  78. #include "rrdvar.h"
  79. #include "rrdsetvar.h"
  80. #include "rrddimvar.h"
  81. #include "rrdcalc.h"
  82. #include "rrdcalctemplate.h"
  83. #include "streaming/rrdpush.h"
  84. #include "aclk/aclk_rrdhost_state.h"
  85. #include "sqlite/sqlite_health.h"
  86. typedef struct storage_query_handle STORAGE_QUERY_HANDLE;
  87. // iterator state for RRD dimension data queries
  88. struct storage_engine_query_handle {
  89. time_t start_time_s;
  90. time_t end_time_s;
  91. STORAGE_PRIORITY priority;
  92. STORAGE_QUERY_HANDLE* handle;
  93. };
  94. typedef struct storage_point {
  95. NETDATA_DOUBLE min; // when count > 1, this is the minimum among them
  96. NETDATA_DOUBLE max; // when count > 1, this is the maximum among them
  97. NETDATA_DOUBLE sum; // the point sum - divided by count gives the average
  98. // end_time - start_time = point duration
  99. time_t start_time_s; // the time the point starts
  100. time_t end_time_s; // the time the point ends
  101. size_t count; // the number of original points aggregated
  102. size_t anomaly_count; // the number of original points found anomalous
  103. SN_FLAGS flags; // flags stored with the point
  104. } STORAGE_POINT;
  105. #include "rrdcontext.h"
  106. extern bool unittest_running;
  107. extern bool dbengine_enabled;
  108. extern size_t storage_tiers;
  109. extern bool use_direct_io;
  110. extern size_t storage_tiers_grouping_iterations[RRD_STORAGE_TIERS];
  111. typedef enum __attribute__ ((__packed__)) {
  112. RRD_BACKFILL_NONE = 0,
  113. RRD_BACKFILL_FULL,
  114. RRD_BACKFILL_NEW
  115. } RRD_BACKFILL;
  116. extern RRD_BACKFILL storage_tiers_backfill[RRD_STORAGE_TIERS];
  117. #define UPDATE_EVERY 1
  118. #define UPDATE_EVERY_MAX 3600
  119. #define RRD_DEFAULT_HISTORY_ENTRIES 3600
  120. #define RRD_HISTORY_ENTRIES_MAX (86400*365)
  121. extern int default_rrd_update_every;
  122. extern int default_rrd_history_entries;
  123. extern int gap_when_lost_iterations_above;
  124. extern time_t rrdset_free_obsolete_time_s;
  125. #if defined(ENV32BIT)
  126. #define MIN_LIBUV_WORKER_THREADS 8
  127. #define MAX_LIBUV_WORKER_THREADS 64
  128. #define RESERVED_LIBUV_WORKER_THREADS 3
  129. #else
  130. #define MIN_LIBUV_WORKER_THREADS 16
  131. #define MAX_LIBUV_WORKER_THREADS 128
  132. #define RESERVED_LIBUV_WORKER_THREADS 6
  133. #endif
  134. extern int libuv_worker_threads;
  135. #define RRD_ID_LENGTH_MAX 200
  136. typedef long long total_number;
  137. #define TOTAL_NUMBER_FORMAT "%lld"
  138. // ----------------------------------------------------------------------------
  139. // chart types
  140. typedef enum __attribute__ ((__packed__)) rrdset_type {
  141. RRDSET_TYPE_LINE = 0,
  142. RRDSET_TYPE_AREA = 1,
  143. RRDSET_TYPE_STACKED = 2,
  144. } RRDSET_TYPE;
  145. #define RRDSET_TYPE_LINE_NAME "line"
  146. #define RRDSET_TYPE_AREA_NAME "area"
  147. #define RRDSET_TYPE_STACKED_NAME "stacked"
  148. RRDSET_TYPE rrdset_type_id(const char *name);
  149. const char *rrdset_type_name(RRDSET_TYPE chart_type);
  150. // ----------------------------------------------------------------------------
  151. // algorithms types
  152. typedef enum __attribute__ ((__packed__)) rrd_algorithm {
  153. RRD_ALGORITHM_ABSOLUTE = 0,
  154. RRD_ALGORITHM_INCREMENTAL = 1,
  155. RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL = 2,
  156. RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL = 3,
  157. // this is 8-bit
  158. } RRD_ALGORITHM;
  159. #define RRD_ALGORITHM_ABSOLUTE_NAME "absolute"
  160. #define RRD_ALGORITHM_INCREMENTAL_NAME "incremental"
  161. #define RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL_NAME "percentage-of-incremental-row"
  162. #define RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL_NAME "percentage-of-absolute-row"
  163. RRD_ALGORITHM rrd_algorithm_id(const char *name);
  164. const char *rrd_algorithm_name(RRD_ALGORITHM algorithm);
  165. // ----------------------------------------------------------------------------
  166. // RRD FAMILY
  167. const RRDFAMILY_ACQUIRED *rrdfamily_add_and_acquire(RRDHOST *host, const char *id);
  168. void rrdfamily_release(RRDHOST *host, const RRDFAMILY_ACQUIRED *rfa);
  169. void rrdfamily_index_init(RRDHOST *host);
  170. void rrdfamily_index_destroy(RRDHOST *host);
  171. DICTIONARY *rrdfamily_rrdvars_dict(const RRDFAMILY_ACQUIRED *rf);
  172. // ----------------------------------------------------------------------------
  173. // flags & options
  174. // options are permanent configuration options (no atomics to alter/access them)
  175. typedef enum __attribute__ ((__packed__)) rrddim_options {
  176. RRDDIM_OPTION_NONE = 0,
  177. RRDDIM_OPTION_HIDDEN = (1 << 0), // this dimension will not be offered to callers
  178. RRDDIM_OPTION_DONT_DETECT_RESETS_OR_OVERFLOWS = (1 << 1), // do not offer RESET or OVERFLOW info to callers
  179. RRDDIM_OPTION_BACKFILLED_HIGH_TIERS = (1 << 2), // when set, we have backfilled higher tiers
  180. // this is 8-bit
  181. } RRDDIM_OPTIONS;
  182. #define rrddim_option_check(rd, option) ((rd)->options & (option))
  183. #define rrddim_option_set(rd, option) (rd)->options |= (option)
  184. #define rrddim_option_clear(rd, option) (rd)->options &= ~(option)
  185. // flags are runtime changing status flags (atomics are required to alter/access them)
  186. typedef enum __attribute__ ((__packed__)) rrddim_flags {
  187. RRDDIM_FLAG_NONE = 0,
  188. RRDDIM_FLAG_PENDING_HEALTH_INITIALIZATION = (1 << 0),
  189. RRDDIM_FLAG_OBSOLETE = (1 << 2), // this is marked by the collector/module as obsolete
  190. // No new values have been collected for this dimension since agent start, or it was marked RRDDIM_FLAG_OBSOLETE at
  191. // least rrdset_free_obsolete_time seconds ago.
  192. RRDDIM_FLAG_ARCHIVED = (1 << 3),
  193. RRDDIM_FLAG_METADATA_UPDATE = (1 << 4), // Metadata needs to go to the database
  194. RRDDIM_FLAG_META_HIDDEN = (1 << 6), // Status of hidden option in the metadata database
  195. // this is 8 bit
  196. } RRDDIM_FLAGS;
  197. #define rrddim_flag_check(rd, flag) (__atomic_load_n(&((rd)->flags), __ATOMIC_SEQ_CST) & (flag))
  198. #define rrddim_flag_set(rd, flag) __atomic_or_fetch(&((rd)->flags), (flag), __ATOMIC_SEQ_CST)
  199. #define rrddim_flag_clear(rd, flag) __atomic_and_fetch(&((rd)->flags), ~(flag), __ATOMIC_SEQ_CST)
  200. typedef enum __attribute__ ((__packed__)) rrdlabel_source {
  201. RRDLABEL_SRC_AUTO = (1 << 0), // set when Netdata found the label by some automation
  202. RRDLABEL_SRC_CONFIG = (1 << 1), // set when the user configured the label
  203. RRDLABEL_SRC_K8S = (1 << 2), // set when this label is found from k8s (RRDLABEL_SRC_AUTO should also be set)
  204. RRDLABEL_SRC_ACLK = (1 << 3), // set when this label is found from ACLK (RRDLABEL_SRC_AUTO should also be set)
  205. // more sources can be added here
  206. RRDLABEL_FLAG_PERMANENT = (1 << 29), // set when this label should never be removed (can be overwritten though)
  207. RRDLABEL_FLAG_OLD = (1 << 30), // marks for rrdlabels internal use - they are not exposed outside rrdlabels
  208. RRDLABEL_FLAG_NEW = (1 << 31) // marks for rrdlabels internal use - they are not exposed outside rrdlabels
  209. } RRDLABEL_SRC;
  210. #define RRDLABEL_FLAG_INTERNAL (RRDLABEL_FLAG_OLD | RRDLABEL_FLAG_NEW | RRDLABEL_FLAG_PERMANENT)
  211. size_t text_sanitize(unsigned char *dst, const unsigned char *src, size_t dst_size, unsigned char *char_map, bool utf, const char *empty, size_t *multibyte_length);
  212. DICTIONARY *rrdlabels_create(void);
  213. void rrdlabels_destroy(DICTIONARY *labels_dict);
  214. void rrdlabels_add(DICTIONARY *dict, const char *name, const char *value, RRDLABEL_SRC ls);
  215. void rrdlabels_add_pair(DICTIONARY *dict, const char *string, RRDLABEL_SRC ls);
  216. void rrdlabels_get_value_to_buffer_or_null(DICTIONARY *labels, BUFFER *wb, const char *key, const char *quote, const char *null);
  217. void rrdlabels_get_value_to_char_or_null(DICTIONARY *labels, char **value, const char *key);
  218. void rrdlabels_flush(DICTIONARY *labels_dict);
  219. void rrdlabels_unmark_all(DICTIONARY *labels);
  220. void rrdlabels_remove_all_unmarked(DICTIONARY *labels);
  221. int rrdlabels_walkthrough_read(DICTIONARY *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data);
  222. int rrdlabels_sorted_walkthrough_read(DICTIONARY *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data);
  223. void rrdlabels_log_to_buffer(DICTIONARY *labels, BUFFER *wb);
  224. bool rrdlabels_match_simple_pattern(DICTIONARY *labels, const char *simple_pattern_txt);
  225. bool rrdlabels_match_simple_pattern_parsed(DICTIONARY *labels, SIMPLE_PATTERN *pattern, char equal);
  226. int rrdlabels_to_buffer(DICTIONARY *labels, BUFFER *wb, const char *before_each, const char *equal, const char *quote, const char *between_them, bool (*filter_callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *filter_data, void (*name_sanitizer)(char *dst, const char *src, size_t dst_size), void (*value_sanitizer)(char *dst, const char *src, size_t dst_size));
  227. void rrdlabels_migrate_to_these(DICTIONARY *dst, DICTIONARY *src);
  228. void rrdlabels_copy(DICTIONARY *dst, DICTIONARY *src);
  229. void reload_host_labels(void);
  230. void rrdset_update_rrdlabels(RRDSET *st, DICTIONARY *new_rrdlabels);
  231. void rrdset_save_rrdlabels_to_sql(RRDSET *st);
  232. void rrdhost_set_is_parent_label(int count);
  233. int rrdlabels_unittest(void);
  234. // unfortunately this break when defined in exporting_engine.h
  235. bool exporting_labels_filter_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data);
  236. // ----------------------------------------------------------------------------
  237. // engine-specific iterator state for dimension data collection
  238. typedef struct storage_collect_handle STORAGE_COLLECT_HANDLE;
  239. // ----------------------------------------------------------------------------
  240. // Storage tier data for every dimension
  241. struct rrddim_tier {
  242. STORAGE_POINT virtual_point;
  243. size_t tier_grouping;
  244. time_t next_point_end_time_s;
  245. STORAGE_METRIC_HANDLE *db_metric_handle; // the metric handle inside the database
  246. STORAGE_COLLECT_HANDLE *db_collection_handle; // the data collection handle
  247. struct storage_engine_collect_ops *collect_ops;
  248. struct storage_engine_query_ops *query_ops;
  249. };
  250. void rrdr_fill_tier_gap_from_smaller_tiers(RRDDIM *rd, size_t tier, time_t now_s);
  251. // ----------------------------------------------------------------------------
  252. // RRD DIMENSION - this is a metric
  253. struct rrddim {
  254. uuid_t metric_uuid; // global UUID for this metric (unique_across hosts)
  255. // ------------------------------------------------------------------------
  256. // dimension definition
  257. STRING *id; // the id of this dimension (for internal identification)
  258. STRING *name; // the name of this dimension (as presented to user)
  259. RRD_ALGORITHM algorithm; // the algorithm that is applied to add new collected values
  260. RRDDIM_OPTIONS options; // permanent configuration options
  261. RRD_MEMORY_MODE rrd_memory_mode; // the memory mode for this dimension
  262. RRDDIM_FLAGS flags; // run time changing status flags
  263. bool updated; // 1 when the dimension has been updated since the last processing
  264. bool exposed; // 1 when set what have sent this dimension to the central netdata
  265. collected_number multiplier; // the multiplier of the collected values
  266. collected_number divisor; // the divider of the collected values
  267. int update_every; // every how many seconds is this updated
  268. // TODO - remove update_every from rrddim
  269. // it is always the same in rrdset
  270. // ------------------------------------------------------------------------
  271. // operational state members
  272. ml_dimension_t *ml_dimension; // machine learning data about this dimension
  273. // ------------------------------------------------------------------------
  274. // linking to siblings and parents
  275. struct rrdset *rrdset;
  276. RRDMETRIC_ACQUIRED *rrdmetric; // the rrdmetric of this dimension
  277. // ------------------------------------------------------------------------
  278. // data collection members
  279. struct rrddim_tier tiers[RRD_STORAGE_TIERS]; // our tiers of databases
  280. struct timeval last_collected_time; // when was this dimension last updated
  281. // this is actual date time we updated the last_collected_value
  282. // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
  283. size_t collections_counter; // the number of times we added values to this rrddim
  284. collected_number collected_value_max; // the absolute maximum of the collected value
  285. NETDATA_DOUBLE calculated_value; // the current calculated value, after applying the algorithm - resets to zero after being used
  286. NETDATA_DOUBLE last_calculated_value; // the last calculated value processed
  287. NETDATA_DOUBLE last_stored_value; // the last value as stored in the database (after interpolation)
  288. collected_number collected_value; // the current value, as collected - resets to 0 after being used
  289. collected_number last_collected_value; // the last value that was collected, after being processed
  290. #ifdef NETDATA_LOG_COLLECTION_ERRORS
  291. usec_t rrddim_store_metric_last_ut; // the timestamp we last called rrddim_store_metric()
  292. size_t rrddim_store_metric_count; // the rrddim_store_metric() counter
  293. const char *rrddim_store_metric_last_caller; // the name of the function that last called rrddim_store_metric()
  294. #endif
  295. // ------------------------------------------------------------------------
  296. // db mode RAM, SAVE, MAP, ALLOC, NONE specifics
  297. // TODO - they should be managed by storage engine
  298. // (RRDDIM_DB_STATE ptr to an undefined structure, and a call to clean this up during destruction)
  299. size_t memsize; // the memory allocated for this dimension (without RRDDIM)
  300. void *rd_on_file; // pointer to the header written on disk
  301. storage_number *db; // the array of values
  302. };
  303. #define rrddim_id(rd) string2str((rd)->id)
  304. #define rrddim_name(rd) string2str((rd) ->name)
  305. // returns the RRDDIM cache filename, or NULL if it does not exist
  306. const char *rrddim_cache_filename(RRDDIM *rd);
  307. // updated the header with the latest RRDDIM value, for memory mode MAP and SAVE
  308. void rrddim_memory_file_update(RRDDIM *rd);
  309. // free the memory file structures for memory mode MAP and SAVE
  310. void rrddim_memory_file_free(RRDDIM *rd);
  311. bool rrddim_memory_load_or_create_map_save(RRDSET *st, RRDDIM *rd, RRD_MEMORY_MODE memory_mode);
  312. // return the v019 header size of RRDDIM files
  313. size_t rrddim_memory_file_header_size(void);
  314. void rrddim_memory_file_save(RRDDIM *rd);
  315. // ----------------------------------------------------------------------------
  316. #define storage_point_unset(x) do { \
  317. (x).min = (x).max = (x).sum = NAN; \
  318. (x).count = 0; \
  319. (x).anomaly_count = 0; \
  320. (x).flags = SN_FLAG_NONE; \
  321. (x).start_time_s = 0; \
  322. (x).end_time_s = 0; \
  323. } while(0)
  324. #define storage_point_empty(x, start_s, end_s) do { \
  325. (x).min = (x).max = (x).sum = NAN; \
  326. (x).count = 1; \
  327. (x).anomaly_count = 0; \
  328. (x).flags = SN_FLAG_NONE; \
  329. (x).start_time_s = start_s; \
  330. (x).end_time_s = end_s; \
  331. } while(0)
  332. #define STORAGE_POINT_UNSET { .min = NAN, .max = NAN, .sum = NAN, .count = 0, .anomaly_count = 0, .flags = SN_FLAG_NONE, .start_time_s = 0, .end_time_s = 0 }
  333. #define storage_point_is_unset(x) (!(x).count)
  334. #define storage_point_is_gap(x) (!netdata_double_isnumber((x).sum))
  335. // ------------------------------------------------------------------------
  336. // function pointers that handle data collection
  337. struct storage_engine_collect_ops {
  338. // an initialization function to run before starting collection
  339. STORAGE_COLLECT_HANDLE *(*init)(STORAGE_METRIC_HANDLE *db_metric_handle, uint32_t update_every, STORAGE_METRICS_GROUP *smg);
  340. // run this to store each metric into the database
  341. void (*store_metric)(STORAGE_COLLECT_HANDLE *collection_handle, usec_t point_in_time, NETDATA_DOUBLE number, NETDATA_DOUBLE min_value,
  342. NETDATA_DOUBLE max_value, uint16_t count, uint16_t anomaly_count, SN_FLAGS flags);
  343. // run this to flush / reset the current data collection sequence
  344. void (*flush)(STORAGE_COLLECT_HANDLE *collection_handle);
  345. // a finalization function to run after collection is over
  346. // returns 1 if it's safe to delete the dimension
  347. int (*finalize)(STORAGE_COLLECT_HANDLE *collection_handle);
  348. void (*change_collection_frequency)(STORAGE_COLLECT_HANDLE *collection_handle, int update_every);
  349. STORAGE_METRICS_GROUP *(*metrics_group_get)(STORAGE_INSTANCE *db_instance, uuid_t *uuid);
  350. void (*metrics_group_release)(STORAGE_INSTANCE *db_instance, STORAGE_METRICS_GROUP *sa);
  351. };
  352. // ----------------------------------------------------------------------------
  353. // function pointers that handle database queries
  354. struct storage_engine_query_ops {
  355. // run this before starting a series of next_metric() database queries
  356. void (*init)(STORAGE_METRIC_HANDLE *db_metric_handle, struct storage_engine_query_handle *handle, time_t start_time_s, time_t end_time_s, STORAGE_PRIORITY priority);
  357. // run this to load each metric number from the database
  358. STORAGE_POINT (*next_metric)(struct storage_engine_query_handle *handle);
  359. // run this to test if the series of next_metric() database queries is finished
  360. int (*is_finished)(struct storage_engine_query_handle *handle);
  361. // run this after finishing a series of load_metric() database queries
  362. void (*finalize)(struct storage_engine_query_handle *handle);
  363. // get the timestamp of the last entry of this metric
  364. time_t (*latest_time_s)(STORAGE_METRIC_HANDLE *db_metric_handle);
  365. // get the timestamp of the first entry of this metric
  366. time_t (*oldest_time_s)(STORAGE_METRIC_HANDLE *db_metric_handle);
  367. // adapt 'before' timestamp to the optimal for the query
  368. // can only move 'before' ahead (to the future)
  369. time_t (*align_to_optimal_before)(struct storage_engine_query_handle *handle);
  370. };
  371. typedef struct storage_engine STORAGE_ENGINE;
  372. // ------------------------------------------------------------------------
  373. // function pointers for all APIs provided by a storage engine
  374. typedef struct storage_engine_api {
  375. // metric management
  376. STORAGE_METRIC_HANDLE *(*metric_get)(STORAGE_INSTANCE *instance, uuid_t *uuid);
  377. STORAGE_METRIC_HANDLE *(*metric_get_or_create)(RRDDIM *rd, STORAGE_INSTANCE *instance);
  378. void (*metric_release)(STORAGE_METRIC_HANDLE *);
  379. STORAGE_METRIC_HANDLE *(*metric_dup)(STORAGE_METRIC_HANDLE *);
  380. bool (*metric_retention_by_uuid)(STORAGE_INSTANCE *db_instance, uuid_t *uuid, time_t *first_entry_s, time_t *last_entry_s);
  381. // operations
  382. struct storage_engine_collect_ops collect_ops;
  383. struct storage_engine_query_ops query_ops;
  384. } STORAGE_ENGINE_API;
  385. struct storage_engine {
  386. RRD_MEMORY_MODE id;
  387. const char* name;
  388. STORAGE_ENGINE_API api;
  389. };
  390. STORAGE_ENGINE* storage_engine_get(RRD_MEMORY_MODE mmode);
  391. STORAGE_ENGINE* storage_engine_find(const char* name);
  392. // ----------------------------------------------------------------------------
  393. // these loop macros make sure the linked list is accessed with the right lock
  394. #define rrddim_foreach_read(rd, st) \
  395. dfe_start_read((st)->rrddim_root_index, rd)
  396. #define rrddim_foreach_write(rd, st) \
  397. dfe_start_write((st)->rrddim_root_index, rd)
  398. #define rrddim_foreach_reentrant(rd, st) \
  399. dfe_start_reentrant((st)->rrddim_root_index, rd)
  400. #define rrddim_foreach_done(rd) \
  401. dfe_done(rd)
  402. // ----------------------------------------------------------------------------
  403. // RRDSET - this is a chart
  404. // use this for configuration flags, not for state control
  405. // flags are set/unset in a manner that is not thread safe
  406. // and may lead to missing information.
  407. typedef enum __attribute__ ((__packed__)) rrdset_flags {
  408. RRDSET_FLAG_DETAIL = (1 << 1), // if set, the data set should be considered as a detail of another
  409. // (the master data set should be the one that has the same family and is not detail)
  410. RRDSET_FLAG_DEBUG = (1 << 2), // enables or disables debugging for a chart
  411. RRDSET_FLAG_OBSOLETE = (1 << 3), // this is marked by the collector/module as obsolete
  412. RRDSET_FLAG_EXPORTING_SEND = (1 << 4), // if set, this chart should be sent to Prometheus web API and external databases
  413. RRDSET_FLAG_EXPORTING_IGNORE = (1 << 5), // if set, this chart should not be sent to Prometheus web API and external databases
  414. RRDSET_FLAG_UPSTREAM_SEND = (1 << 6), // if set, this chart should be sent upstream (streaming)
  415. RRDSET_FLAG_UPSTREAM_IGNORE = (1 << 7), // if set, this chart should not be sent upstream (streaming)
  416. RRDSET_FLAG_UPSTREAM_EXPOSED = (1 << 8), // if set, we have sent this chart definition to netdata parent (streaming)
  417. RRDSET_FLAG_STORE_FIRST = (1 << 9), // if set, do not eliminate the first collection during interpolation
  418. RRDSET_FLAG_HETEROGENEOUS = (1 << 10), // if set, the chart is not homogeneous (dimensions in it have multiple algorithms, multipliers or dividers)
  419. RRDSET_FLAG_HOMOGENEOUS_CHECK = (1 << 11), // if set, the chart should be checked to determine if the dimensions are homogeneous
  420. RRDSET_FLAG_HIDDEN = (1 << 12), // if set, do not show this chart on the dashboard, but use it for exporting
  421. RRDSET_FLAG_SYNC_CLOCK = (1 << 13), // if set, microseconds on next data collection will be ignored (the chart will be synced to now)
  422. RRDSET_FLAG_OBSOLETE_DIMENSIONS = (1 << 14), // this is marked by the collector/module when a chart has obsolete dimensions
  423. // No new values have been collected for this chart since agent start, or it was marked RRDSET_FLAG_OBSOLETE at
  424. // least rrdset_free_obsolete_time seconds ago.
  425. RRDSET_FLAG_ARCHIVED = (1 << 15),
  426. RRDSET_FLAG_METADATA_UPDATE = (1 << 16), // Mark that metadata needs to be stored
  427. RRDSET_FLAG_ANOMALY_DETECTION = (1 << 18), // flag to identify anomaly detection charts.
  428. RRDSET_FLAG_INDEXED_ID = (1 << 19), // the rrdset is indexed by its id
  429. RRDSET_FLAG_INDEXED_NAME = (1 << 20), // the rrdset is indexed by its name
  430. RRDSET_FLAG_PENDING_HEALTH_INITIALIZATION = (1 << 21),
  431. RRDSET_FLAG_SENDER_REPLICATION_IN_PROGRESS = (1 << 22), // the sending side has replication in progress
  432. RRDSET_FLAG_SENDER_REPLICATION_FINISHED = (1 << 23), // the sending side has completed replication
  433. RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS = (1 << 24), // the receiving side has replication in progress
  434. RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED = (1 << 25), // the receiving side has completed replication
  435. RRDSET_FLAG_UPSTREAM_SEND_VARIABLES = (1 << 26), // a custom variable has been updated and needs to be exposed to parent
  436. RRDSET_FLAG_COLLECTION_FINISHED = (1 << 27), // when set, data collection is not available for this chart
  437. } RRDSET_FLAGS;
  438. #define rrdset_flag_check(st, flag) (__atomic_load_n(&((st)->flags), __ATOMIC_SEQ_CST) & (flag))
  439. #define rrdset_flag_set(st, flag) __atomic_or_fetch(&((st)->flags), flag, __ATOMIC_SEQ_CST)
  440. #define rrdset_flag_clear(st, flag) __atomic_and_fetch(&((st)->flags), ~(flag), __ATOMIC_SEQ_CST)
  441. #define rrdset_is_replicating(st) (rrdset_flag_check(st, RRDSET_FLAG_SENDER_REPLICATION_IN_PROGRESS|RRDSET_FLAG_RECEIVER_REPLICATION_IN_PROGRESS) \
  442. && !rrdset_flag_check(st, RRDSET_FLAG_SENDER_REPLICATION_FINISHED|RRDSET_FLAG_RECEIVER_REPLICATION_FINISHED))
  443. struct rrdset {
  444. uuid_t chart_uuid; // the global UUID for this chart
  445. // ------------------------------------------------------------------------
  446. // chart configuration
  447. struct {
  448. STRING *type; // the type of {type}.{id}
  449. STRING *id; // the id of {type}.{id}
  450. STRING *name; // the name of {type}.{name}
  451. } parts;
  452. STRING *id; // the unique ID of the rrdset as {type}.{id}
  453. STRING *name; // the unique name of the rrdset as {type}.{name}
  454. STRING *family; // grouping sets under the same family
  455. STRING *title; // title shown to user
  456. STRING *units; // units of measurement
  457. STRING *context; // the template of this data set
  458. STRING *plugin_name; // the name of the plugin that generated this
  459. STRING *module_name; // the name of the plugin module that generated this
  460. RRDSET_TYPE chart_type; // line, area, stacked
  461. long priority; // the sorting priority of this chart
  462. int update_every; // data collection frequency
  463. DICTIONARY *rrdlabels; // chart labels
  464. DICTIONARY *rrdsetvar_root_index; // chart variables
  465. DICTIONARY *rrddimvar_root_index; // dimension variables
  466. // we use this dictionary to manage their allocation
  467. ml_chart_t *ml_chart;
  468. // ------------------------------------------------------------------------
  469. // operational state members
  470. RRDSET_FLAGS flags; // flags
  471. RRD_MEMORY_MODE rrd_memory_mode; // the db mode of this rrdset
  472. DICTIONARY *rrddim_root_index; // dimensions index
  473. STORAGE_METRICS_GROUP *storage_metrics_groups[RRD_STORAGE_TIERS];
  474. // ------------------------------------------------------------------------
  475. // linking to siblings and parents
  476. RRDHOST *rrdhost; // pointer to RRDHOST this chart belongs to
  477. RRDINSTANCE_ACQUIRED *rrdinstance; // the rrdinstance of this chart
  478. RRDCONTEXT_ACQUIRED *rrdcontext; // the rrdcontext this chart belongs to
  479. // ------------------------------------------------------------------------
  480. // data collection members
  481. SPINLOCK data_collection_lock;
  482. size_t counter; // the number of times we added values to this database
  483. size_t counter_done; // the number of times rrdset_done() has been called
  484. time_t last_accessed_time_s; // the last time this RRDSET has been accessed
  485. usec_t usec_since_last_update; // the time in microseconds since the last collection of data
  486. struct timeval last_updated; // when this data set was last updated (updated every time the rrd_stats_done() function)
  487. struct timeval last_collected_time; // when did this data set last collected values
  488. size_t rrdlabels_last_saved_version;
  489. DICTIONARY *functions_view; // collector functions this rrdset supports, can be NULL
  490. // ------------------------------------------------------------------------
  491. // data collection - streaming to parents, temp variables
  492. time_t upstream_resync_time_s; // the timestamp up to which we should resync clock upstream
  493. // ------------------------------------------------------------------------
  494. // db mode SAVE, MAP specifics
  495. // TODO - they should be managed by storage engine
  496. // (RRDSET_DB_STATE ptr to an undefined structure, and a call to clean this up during destruction)
  497. char *cache_dir; // the directory to store dimensions
  498. void *st_on_file; // compatibility with V019 RRDSET files
  499. // ------------------------------------------------------------------------
  500. // db mode RAM, SAVE, MAP, ALLOC, NONE specifics
  501. // TODO - they should be managed by storage engine
  502. // (RRDSET_DB_STATE ptr to an undefined structure, and a call to clean this up during destruction)
  503. long entries; // total number of entries in the data set
  504. long current_entry; // the entry that is currently being updated
  505. // it goes around in a round-robin fashion
  506. // ------------------------------------------------------------------------
  507. // exporting to 3rd party time-series members
  508. // TODO - they should be managed by exporting engine
  509. // (RRDSET_EXPORTING_STATE ptr to an undefined structure, and a call to clean this up during destruction)
  510. RRDSET_FLAGS *exporting_flags; // array of flags for exporting connector instances
  511. // ------------------------------------------------------------------------
  512. // health monitoring members
  513. // TODO - they should be managed by health
  514. // (RRDSET_HEALTH_STATE ptr to an undefined structure, and a call to clean this up during destruction)
  515. NETDATA_DOUBLE green; // green threshold for this chart
  516. NETDATA_DOUBLE red; // red threshold for this chart
  517. DICTIONARY *rrdvars; // RRDVAR index for this chart
  518. const RRDFAMILY_ACQUIRED *rrdfamily; // pointer to RRDFAMILY dictionary item, this chart belongs to
  519. struct {
  520. netdata_rwlock_t rwlock; // protection for RRDCALC *base
  521. RRDCALC *base; // double linked list of RRDCALC related to this RRDSET
  522. } alerts;
  523. #ifdef NETDATA_LOG_REPLICATION_REQUESTS
  524. struct {
  525. bool log_next_data_collection;
  526. bool start_streaming;
  527. time_t after;
  528. time_t before;
  529. } replay;
  530. #endif // NETDATA_LOG_REPLICATION_REQUESTS
  531. };
  532. #define rrdset_plugin_name(st) string2str((st)->plugin_name)
  533. #define rrdset_module_name(st) string2str((st)->module_name)
  534. #define rrdset_units(st) string2str((st)->units)
  535. #define rrdset_parts_type(st) string2str((st)->parts.type)
  536. #define rrdset_family(st) string2str((st)->family)
  537. #define rrdset_title(st) string2str((st)->title)
  538. #define rrdset_context(st) string2str((st)->context)
  539. #define rrdset_name(st) string2str((st)->name)
  540. #define rrdset_id(st) string2str((st)->id)
  541. STRING *rrd_string_strdupz(const char *s);
  542. // ----------------------------------------------------------------------------
  543. // these loop macros make sure the linked list is accessed with the right lock
  544. #define rrdset_foreach_read(st, host) \
  545. dfe_start_read((host)->rrdset_root_index, st)
  546. #define rrdset_foreach_write(st, host) \
  547. dfe_start_write((host)->rrdset_root_index, st)
  548. #define rrdset_foreach_reentrant(st, host) \
  549. dfe_start_reentrant((host)->rrdset_root_index, st)
  550. #define rrdset_foreach_done(st) \
  551. dfe_done(st)
  552. #define rrdset_number_of_dimensions(st) \
  553. dictionary_entries((st)->rrddim_root_index)
  554. void rrdset_memory_file_save(RRDSET *st);
  555. void rrdset_memory_file_free(RRDSET *st);
  556. void rrdset_memory_file_update(RRDSET *st);
  557. const char *rrdset_cache_filename(RRDSET *st);
  558. bool rrdset_memory_load_or_create_map_save(RRDSET *st_on_file, RRD_MEMORY_MODE memory_mode);
  559. #include "rrdfunctions.h"
  560. // ----------------------------------------------------------------------------
  561. // RRDHOST flags
  562. // use this for configuration flags, not for state control
  563. // flags are set/unset in a manner that is not thread safe
  564. // and may lead to missing information.
  565. typedef enum __attribute__ ((__packed__)) rrdhost_flags {
  566. // Orphan, Archived and Obsolete flags
  567. RRDHOST_FLAG_ORPHAN = (1 << 10), // this host is orphan (not receiving data)
  568. RRDHOST_FLAG_ARCHIVED = (1 << 11), // The host is archived, no collected charts yet
  569. RRDHOST_FLAG_PENDING_OBSOLETE_CHARTS = (1 << 12), // the host has pending chart obsoletions
  570. RRDHOST_FLAG_PENDING_OBSOLETE_DIMENSIONS = (1 << 13), // the host has pending dimension obsoletions
  571. // Streaming sender
  572. RRDHOST_FLAG_RRDPUSH_SENDER_INITIALIZED = (1 << 14), // the host has initialized rrdpush structures
  573. RRDHOST_FLAG_RRDPUSH_SENDER_SPAWN = (1 << 15), // When set, the sender thread is running
  574. RRDHOST_FLAG_RRDPUSH_SENDER_CONNECTED = (1 << 16), // When set, the host is connected to a parent
  575. RRDHOST_FLAG_RRDPUSH_SENDER_READY_4_METRICS = (1 << 17), // when set, rrdset_done() should push metrics to parent
  576. RRDHOST_FLAG_RRDPUSH_SENDER_LOGGED_STATUS = (1 << 18), // when set, we have logged the status of metrics streaming
  577. // Health
  578. RRDHOST_FLAG_PENDING_HEALTH_INITIALIZATION = (1 << 20), // contains charts and dims with uninitialized variables
  579. RRDHOST_FLAG_INITIALIZED_HEALTH = (1 << 21), // the host has initialized health structures
  580. // Exporting
  581. RRDHOST_FLAG_EXPORTING_SEND = (1 << 22), // send it to external databases
  582. RRDHOST_FLAG_EXPORTING_DONT_SEND = (1 << 23), // don't send it to external databases
  583. // ACLK
  584. RRDHOST_FLAG_ACLK_STREAM_CONTEXTS = (1 << 24), // when set, we should send ACLK stream context updates
  585. // Metadata
  586. RRDHOST_FLAG_METADATA_UPDATE = (1 << 25), // metadata needs to be stored in the database
  587. RRDHOST_FLAG_METADATA_LABELS = (1 << 26), // metadata needs to be stored in the database
  588. RRDHOST_FLAG_METADATA_INFO = (1 << 27), // metadata needs to be stored in the database
  589. RRDHOST_FLAG_METADATA_CLAIMID = (1 << 28), // metadata needs to be stored in the database
  590. RRDHOST_FLAG_RRDPUSH_RECEIVER_DISCONNECTED = (1 << 29), // set when the receiver part is disconnected
  591. } RRDHOST_FLAGS;
  592. #define rrdhost_flag_check(host, flag) (__atomic_load_n(&((host)->flags), __ATOMIC_SEQ_CST) & (flag))
  593. #define rrdhost_flag_set(host, flag) __atomic_or_fetch(&((host)->flags), flag, __ATOMIC_SEQ_CST)
  594. #define rrdhost_flag_clear(host, flag) __atomic_and_fetch(&((host)->flags), ~(flag), __ATOMIC_SEQ_CST)
  595. #ifdef NETDATA_INTERNAL_CHECKS
  596. #define rrdset_debug(st, fmt, args...) do { if(unlikely(debug_flags & D_RRD_STATS && rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) \
  597. debug_int(__FILE__, __FUNCTION__, __LINE__, "%s: " fmt, rrdset_name(st), ##args); } while(0)
  598. #else
  599. #define rrdset_debug(st, fmt, args...) debug_dummy()
  600. #endif
  601. typedef enum __attribute__ ((__packed__)) {
  602. // Indexing
  603. RRDHOST_OPTION_INDEXED_MACHINE_GUID = (1 << 0), // when set, we have indexed its machine guid
  604. RRDHOST_OPTION_INDEXED_HOSTNAME = (1 << 1), // when set, we have indexed its hostname
  605. // Streaming configuration
  606. RRDHOST_OPTION_SENDER_ENABLED = (1 << 2), // set when the host is configured to send metrics to a parent
  607. // Configuration options
  608. RRDHOST_OPTION_DELETE_OBSOLETE_CHARTS = (1 << 3), // delete files of obsolete charts
  609. RRDHOST_OPTION_DELETE_ORPHAN_HOST = (1 << 4), // delete the entire host when orphan
  610. RRDHOST_OPTION_REPLICATION = (1 << 5), // when set, we support replication for this host
  611. } RRDHOST_OPTIONS;
  612. #define rrdhost_option_check(host, flag) ((host)->options & (flag))
  613. #define rrdhost_option_set(host, flag) (host)->options |= flag
  614. #define rrdhost_option_clear(host, flag) (host)->options &= ~(flag)
  615. #define rrdhost_has_rrdpush_sender_enabled(host) (rrdhost_option_check(host, RRDHOST_OPTION_SENDER_ENABLED) && (host)->sender)
  616. #define rrdhost_can_send_definitions_to_parent(host) (rrdhost_has_rrdpush_sender_enabled(host) && rrdhost_flag_check(host, RRDHOST_FLAG_RRDPUSH_SENDER_CONNECTED))
  617. // ----------------------------------------------------------------------------
  618. // Health data
  619. struct alarm_entry {
  620. uint32_t unique_id;
  621. uint32_t alarm_id;
  622. uint32_t alarm_event_id;
  623. uuid_t config_hash_id;
  624. time_t when;
  625. time_t duration;
  626. time_t non_clear_duration;
  627. STRING *name;
  628. STRING *chart;
  629. STRING *chart_context;
  630. STRING *family;
  631. STRING *classification;
  632. STRING *component;
  633. STRING *type;
  634. STRING *exec;
  635. STRING *recipient;
  636. time_t exec_run_timestamp;
  637. int exec_code;
  638. uint64_t exec_spawn_serial;
  639. STRING *source;
  640. STRING *units;
  641. STRING *info;
  642. NETDATA_DOUBLE old_value;
  643. NETDATA_DOUBLE new_value;
  644. STRING *old_value_string;
  645. STRING *new_value_string;
  646. RRDCALC_STATUS old_status;
  647. RRDCALC_STATUS new_status;
  648. uint32_t flags;
  649. int delay;
  650. time_t delay_up_to_timestamp;
  651. uint32_t updated_by_id;
  652. uint32_t updates_id;
  653. time_t last_repeat;
  654. struct alarm_entry *next;
  655. struct alarm_entry *next_in_progress;
  656. struct alarm_entry *prev_in_progress;
  657. };
  658. #define ae_name(ae) string2str((ae)->name)
  659. #define ae_chart_name(ae) string2str((ae)->chart)
  660. #define ae_chart_context(ae) string2str((ae)->chart_context)
  661. #define ae_family(ae) string2str((ae)->family)
  662. #define ae_classification(ae) string2str((ae)->classification)
  663. #define ae_component(ae) string2str((ae)->component)
  664. #define ae_type(ae) string2str((ae)->type)
  665. #define ae_exec(ae) string2str((ae)->exec)
  666. #define ae_recipient(ae) string2str((ae)->recipient)
  667. #define ae_source(ae) string2str((ae)->source)
  668. #define ae_units(ae) string2str((ae)->units)
  669. #define ae_info(ae) string2str((ae)->info)
  670. #define ae_old_value_string(ae) string2str((ae)->old_value_string)
  671. #define ae_new_value_string(ae) string2str((ae)->new_value_string)
  672. typedef struct alarm_log {
  673. uint32_t next_log_id;
  674. uint32_t next_alarm_id;
  675. unsigned int count;
  676. unsigned int max;
  677. ALARM_ENTRY *alarms;
  678. netdata_rwlock_t alarm_log_rwlock;
  679. } ALARM_LOG;
  680. typedef struct health {
  681. unsigned int health_enabled; // 1 when this host has health enabled
  682. time_t health_delay_up_to; // a timestamp to delay alarms processing up to
  683. STRING *health_default_exec; // the full path of the alarms notifications program
  684. STRING *health_default_recipient; // the default recipient for all alarms
  685. size_t health_log_entries_written; // the number of alarm events written to the alarms event log
  686. uint32_t health_default_warn_repeat_every; // the default value for the interval between repeating warning notifications
  687. uint32_t health_default_crit_repeat_every; // the default value for the interval between repeating critical notifications
  688. } HEALTH;
  689. // ----------------------------------------------------------------------------
  690. // RRD HOST
  691. struct rrdhost_system_info {
  692. char *cloud_provider_type;
  693. char *cloud_instance_type;
  694. char *cloud_instance_region;
  695. char *host_os_name;
  696. char *host_os_id;
  697. char *host_os_id_like;
  698. char *host_os_version;
  699. char *host_os_version_id;
  700. char *host_os_detection;
  701. char *host_cores;
  702. char *host_cpu_freq;
  703. char *host_ram_total;
  704. char *host_disk_space;
  705. char *container_os_name;
  706. char *container_os_id;
  707. char *container_os_id_like;
  708. char *container_os_version;
  709. char *container_os_version_id;
  710. char *container_os_detection;
  711. char *kernel_name;
  712. char *kernel_version;
  713. char *architecture;
  714. char *virtualization;
  715. char *virt_detection;
  716. char *container;
  717. char *container_detection;
  718. char *is_k8s_node;
  719. uint16_t hops;
  720. bool ml_capable;
  721. bool ml_enabled;
  722. char *install_type;
  723. char *prebuilt_arch;
  724. char *prebuilt_dist;
  725. int mc_version;
  726. };
  727. struct rrdhost {
  728. char machine_guid[GUID_LEN + 1]; // the unique ID of this host
  729. // ------------------------------------------------------------------------
  730. // host information
  731. STRING *hostname; // the hostname of this host
  732. STRING *registry_hostname; // the registry hostname for this host
  733. STRING *os; // the O/S type of the host
  734. STRING *tags; // tags for this host
  735. STRING *timezone; // the timezone of the host
  736. STRING *abbrev_timezone; // the abbriviated timezone of the host
  737. STRING *program_name; // the program name that collects metrics for this host
  738. STRING *program_version; // the program version that collects metrics for this host
  739. int32_t utc_offset; // the offset in seconds from utc
  740. RRDHOST_OPTIONS options; // configuration option for this RRDHOST (no atomics on this)
  741. RRDHOST_FLAGS flags; // runtime flags about this RRDHOST (atomics on this)
  742. RRDHOST_FLAGS *exporting_flags; // array of flags for exporting connector instances
  743. int rrd_update_every; // the update frequency of the host
  744. long rrd_history_entries; // the number of history entries for the host's charts
  745. RRD_MEMORY_MODE rrd_memory_mode; // the configured memory more for the charts of this host
  746. // the actual per tier is at .db[tier].mode
  747. char *cache_dir; // the directory to save RRD cache files
  748. char *varlib_dir; // the directory to save health log
  749. struct {
  750. RRD_MEMORY_MODE mode; // the db mode for this tier
  751. STORAGE_ENGINE *eng; // the storage engine API for this tier
  752. STORAGE_INSTANCE *instance; // the db instance for this tier
  753. size_t tier_grouping; // tier 0 iterations aggregated on this tier
  754. } db[RRD_STORAGE_TIERS];
  755. struct rrdhost_system_info *system_info; // information collected from the host environment
  756. // ------------------------------------------------------------------------
  757. // streaming of data to remote hosts - rrdpush sender
  758. char *rrdpush_send_destination; // where to send metrics to
  759. char *rrdpush_send_api_key; // the api key at the receiving netdata
  760. struct rrdpush_destinations *destinations; // a linked list of possible destinations
  761. struct rrdpush_destinations *destination; // the current destination from the above list
  762. SIMPLE_PATTERN *rrdpush_send_charts_matching; // pattern to match the charts to be sent
  763. time_t rrdpush_seconds_to_replicate; // max time we want to replicate from the child
  764. time_t rrdpush_replication_step; // seconds per replication step
  765. size_t rrdpush_receiver_replicating_charts; // the number of charts currently being replicated from a child
  766. // the following are state information for the threading
  767. // streaming metrics from this netdata to an upstream netdata
  768. struct sender_state *sender;
  769. netdata_thread_t rrdpush_sender_thread; // the sender thread
  770. size_t rrdpush_sender_replicating_charts; // the number of charts currently being replicated to a parent
  771. void *dbsync_worker;
  772. // ------------------------------------------------------------------------
  773. // streaming of data from remote hosts - rrdpush receiver
  774. time_t child_connect_time; // the time the last sender was connected
  775. time_t child_last_chart_command; // the time of the last CHART streaming command
  776. time_t child_disconnected_time; // the time the last sender was disconnected
  777. int connected_children_count; // number of senders currently streaming
  778. struct receiver_state *receiver;
  779. netdata_mutex_t receiver_lock;
  780. int trigger_chart_obsoletion_check; // set when child connects, will instruct parent to
  781. // trigger a check for obsoleted charts since previous connect
  782. // ------------------------------------------------------------------------
  783. // health monitoring options
  784. // health variables
  785. HEALTH health;
  786. // all RRDCALCs are primarily allocated and linked here
  787. DICTIONARY *rrdcalc_root_index;
  788. // templates of alarms
  789. DICTIONARY *rrdcalctemplate_root_index;
  790. ALARM_LOG health_log; // alarms historical events (event log)
  791. uint32_t health_last_processed_id; // the last processed health id from the log
  792. uint32_t health_max_unique_id; // the max alarm log unique id given for the host
  793. uint32_t health_max_alarm_id; // the max alarm id given for the host
  794. // ------------------------------------------------------------------------
  795. // locks
  796. SPINLOCK rrdhost_update_lock;
  797. // ------------------------------------------------------------------------
  798. // ML handle
  799. ml_host_t *ml_host;
  800. // ------------------------------------------------------------------------
  801. // Support for host-level labels
  802. DICTIONARY *rrdlabels;
  803. // ------------------------------------------------------------------------
  804. // Support for functions
  805. DICTIONARY *functions; // collector functions this rrdset supports, can be NULL
  806. // ------------------------------------------------------------------------
  807. // indexes
  808. DICTIONARY *rrdset_root_index; // the host's charts index (by id)
  809. DICTIONARY *rrdset_root_index_name; // the host's charts index (by name)
  810. DICTIONARY *rrdfamily_root_index; // the host's chart families index
  811. DICTIONARY *rrdvars; // the host's chart variables index
  812. // this includes custom host variables
  813. RRDCONTEXTS *rrdctx_hub_queue;
  814. RRDCONTEXTS *rrdctx_post_processing_queue;
  815. RRDCONTEXTS *rrdctx;
  816. uuid_t host_uuid; // Global GUID for this host
  817. uuid_t *node_id; // Cloud node_id
  818. netdata_mutex_t aclk_state_lock;
  819. aclk_rrdhost_state aclk_state;
  820. struct rrdhost *next;
  821. struct rrdhost *prev;
  822. };
  823. extern RRDHOST *localhost;
  824. #define rrdhost_hostname(host) string2str((host)->hostname)
  825. #define rrdhost_registry_hostname(host) string2str((host)->registry_hostname)
  826. #define rrdhost_os(host) string2str((host)->os)
  827. #define rrdhost_tags(host) string2str((host)->tags)
  828. #define rrdhost_timezone(host) string2str((host)->timezone)
  829. #define rrdhost_abbrev_timezone(host) string2str((host)->abbrev_timezone)
  830. #define rrdhost_program_name(host) string2str((host)->program_name)
  831. #define rrdhost_program_version(host) string2str((host)->program_version)
  832. #define rrdhost_aclk_state_lock(host) netdata_mutex_lock(&((host)->aclk_state_lock))
  833. #define rrdhost_aclk_state_unlock(host) netdata_mutex_unlock(&((host)->aclk_state_lock))
  834. #define rrdhost_receiver_replicating_charts(host) (__atomic_load_n(&((host)->rrdpush_receiver_replicating_charts), __ATOMIC_RELAXED))
  835. #define rrdhost_receiver_replicating_charts_plus_one(host) (__atomic_add_fetch(&((host)->rrdpush_receiver_replicating_charts), 1, __ATOMIC_RELAXED))
  836. #define rrdhost_receiver_replicating_charts_minus_one(host) (__atomic_sub_fetch(&((host)->rrdpush_receiver_replicating_charts), 1, __ATOMIC_RELAXED))
  837. #define rrdhost_receiver_replicating_charts_zero(host) (__atomic_store_n(&((host)->rrdpush_receiver_replicating_charts), 0, __ATOMIC_RELAXED))
  838. #define rrdhost_sender_replicating_charts(host) (__atomic_load_n(&((host)->rrdpush_sender_replicating_charts), __ATOMIC_RELAXED))
  839. #define rrdhost_sender_replicating_charts_plus_one(host) (__atomic_add_fetch(&((host)->rrdpush_sender_replicating_charts), 1, __ATOMIC_RELAXED))
  840. #define rrdhost_sender_replicating_charts_minus_one(host) (__atomic_sub_fetch(&((host)->rrdpush_sender_replicating_charts), 1, __ATOMIC_RELAXED))
  841. #define rrdhost_sender_replicating_charts_zero(host) (__atomic_store_n(&((host)->rrdpush_sender_replicating_charts), 0, __ATOMIC_RELAXED))
  842. extern DICTIONARY *rrdhost_root_index;
  843. size_t rrdhost_hosts_available(void);
  844. // ----------------------------------------------------------------------------
  845. #define rrdhost_foreach_read(var) \
  846. for((var) = localhost; var ; (var) = (var)->next)
  847. #define rrdhost_foreach_write(var) \
  848. for((var) = localhost; var ; (var) = (var)->next)
  849. // ----------------------------------------------------------------------------
  850. // global lock for all RRDHOSTs
  851. extern netdata_rwlock_t rrd_rwlock;
  852. #define rrd_rdlock() netdata_rwlock_rdlock(&rrd_rwlock)
  853. #define rrd_wrlock() netdata_rwlock_wrlock(&rrd_rwlock)
  854. #define rrd_unlock() netdata_rwlock_unlock(&rrd_rwlock)
  855. // ----------------------------------------------------------------------------
  856. bool is_storage_engine_shared(STORAGE_INSTANCE *engine);
  857. void rrdset_index_init(RRDHOST *host);
  858. void rrdset_index_destroy(RRDHOST *host);
  859. void rrddim_index_init(RRDSET *st);
  860. void rrddim_index_destroy(RRDSET *st);
  861. // ----------------------------------------------------------------------------
  862. extern time_t rrdhost_free_orphan_time_s;
  863. int rrd_init(char *hostname, struct rrdhost_system_info *system_info, bool unittest);
  864. RRDHOST *rrdhost_find_by_hostname(const char *hostname);
  865. RRDHOST *rrdhost_find_by_guid(const char *guid);
  866. RRDHOST *rrdhost_find_or_create(
  867. const char *hostname
  868. , const char *registry_hostname
  869. , const char *guid
  870. , const char *os
  871. , const char *timezone
  872. , const char *abbrev_timezone
  873. , int32_t utc_offset
  874. , const char *tags
  875. , const char *program_name
  876. , const char *program_version
  877. , int update_every
  878. , long history
  879. , RRD_MEMORY_MODE mode
  880. , unsigned int health_enabled
  881. , unsigned int rrdpush_enabled
  882. , char *rrdpush_destination
  883. , char *rrdpush_api_key
  884. , char *rrdpush_send_charts_matching
  885. , bool rrdpush_enable_replication
  886. , time_t rrdpush_seconds_to_replicate
  887. , time_t rrdpush_replication_step
  888. , struct rrdhost_system_info *system_info
  889. , bool is_archived
  890. );
  891. int rrdhost_set_system_info_variable(struct rrdhost_system_info *system_info, char *name, char *value);
  892. // ----------------------------------------------------------------------------
  893. // RRDSET functions
  894. int rrdset_reset_name(RRDSET *st, const char *name);
  895. RRDSET *rrdset_create_custom(RRDHOST *host
  896. , const char *type
  897. , const char *id
  898. , const char *name
  899. , const char *family
  900. , const char *context
  901. , const char *title
  902. , const char *units
  903. , const char *plugin
  904. , const char *module
  905. , long priority
  906. , int update_every
  907. , RRDSET_TYPE chart_type
  908. , RRD_MEMORY_MODE memory_mode
  909. , long history_entries);
  910. #define rrdset_create(host, type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type) \
  911. rrdset_create_custom(host, type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type, (host)->rrd_memory_mode, (host)->rrd_history_entries)
  912. #define rrdset_create_localhost(type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type) \
  913. rrdset_create(localhost, type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type)
  914. void rrdhost_free_all(void);
  915. void rrdhost_save_all(void);
  916. void rrdhost_cleanup_all(void);
  917. void rrdhost_system_info_free(struct rrdhost_system_info *system_info);
  918. void rrdhost_free___while_having_rrd_wrlock(RRDHOST *host, bool force);
  919. void rrdhost_save_charts(RRDHOST *host);
  920. void rrdhost_delete_charts(RRDHOST *host);
  921. int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected_host, time_t now_s);
  922. void rrdset_update_heterogeneous_flag(RRDSET *st);
  923. time_t rrdset_set_update_every_s(RRDSET *st, time_t update_every_s);
  924. RRDSET *rrdset_find(RRDHOST *host, const char *id);
  925. #define rrdset_find_localhost(id) rrdset_find(localhost, id)
  926. /* This will not return charts that are archived */
  927. static inline RRDSET *rrdset_find_active_localhost(const char *id)
  928. {
  929. RRDSET *st = rrdset_find_localhost(id);
  930. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  931. return NULL;
  932. return st;
  933. }
  934. RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id);
  935. #define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(localhost, type, id)
  936. /* This will not return charts that are archived */
  937. static inline RRDSET *rrdset_find_active_bytype_localhost(const char *type, const char *id)
  938. {
  939. RRDSET *st = rrdset_find_bytype_localhost(type, id);
  940. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  941. return NULL;
  942. return st;
  943. }
  944. RRDSET *rrdset_find_byname(RRDHOST *host, const char *name);
  945. #define rrdset_find_byname_localhost(name) rrdset_find_byname(localhost, name)
  946. /* This will not return charts that are archived */
  947. static inline RRDSET *rrdset_find_active_byname_localhost(const char *name)
  948. {
  949. RRDSET *st = rrdset_find_byname_localhost(name);
  950. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  951. return NULL;
  952. return st;
  953. }
  954. void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
  955. void rrdset_next_usec(RRDSET *st, usec_t microseconds);
  956. void rrdset_timed_next(RRDSET *st, struct timeval now, usec_t microseconds);
  957. #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
  958. void rrdset_timed_done(RRDSET *st, struct timeval now, bool pending_rrdset_next);
  959. void rrdset_done(RRDSET *st);
  960. void rrdset_is_obsolete(RRDSET *st);
  961. void rrdset_isnot_obsolete(RRDSET *st);
  962. // checks if the RRDSET should be offered to viewers
  963. #define rrdset_is_available_for_viewers(st) (!rrdset_flag_check(st, RRDSET_FLAG_HIDDEN) && !rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE) && !rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED) && rrdset_number_of_dimensions(st) && (st)->rrd_memory_mode != RRD_MEMORY_MODE_NONE)
  964. #define rrdset_is_available_for_exporting_and_alarms(st) (!rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE) && !rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED) && rrdset_number_of_dimensions(st))
  965. #define rrdset_is_archived(st) (rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED) && rrdset_number_of_dimensions(st))
  966. time_t rrddim_first_entry_s(RRDDIM *rd);
  967. time_t rrddim_first_entry_s_of_tier(RRDDIM *rd, size_t tier);
  968. time_t rrddim_last_entry_s(RRDDIM *rd);
  969. time_t rrddim_last_entry_s_of_tier(RRDDIM *rd, size_t tier);
  970. time_t rrdset_first_entry_s(RRDSET *st);
  971. time_t rrdset_first_entry_s_of_tier(RRDSET *st, size_t tier);
  972. time_t rrdset_last_entry_s(RRDSET *st);
  973. time_t rrdset_last_entry_s_of_tier(RRDSET *st, size_t tier);
  974. void rrdset_get_retention_of_tier_for_collected_chart(RRDSET *st, time_t *first_time_s, time_t *last_time_s, time_t now_s, size_t tier);
  975. // ----------------------------------------------------------------------------
  976. // RRD DIMENSION functions
  977. RRDDIM *rrddim_add_custom(RRDSET *st
  978. , const char *id
  979. , const char *name
  980. , collected_number multiplier
  981. , collected_number divisor
  982. , RRD_ALGORITHM algorithm
  983. , RRD_MEMORY_MODE memory_mode
  984. );
  985. #define rrddim_add(st, id, name, multiplier, divisor, algorithm) \
  986. rrddim_add_custom(st, id, name, multiplier, divisor, algorithm, (st)->rrd_memory_mode)
  987. int rrddim_reset_name(RRDSET *st, RRDDIM *rd, const char *name);
  988. int rrddim_set_algorithm(RRDSET *st, RRDDIM *rd, RRD_ALGORITHM algorithm);
  989. int rrddim_set_multiplier(RRDSET *st, RRDDIM *rd, collected_number multiplier);
  990. int rrddim_set_divisor(RRDSET *st, RRDDIM *rd, collected_number divisor);
  991. RRDDIM *rrddim_find(RRDSET *st, const char *id);
  992. RRDDIM_ACQUIRED *rrddim_find_and_acquire(RRDSET *st, const char *id);
  993. RRDDIM *rrddim_acquired_to_rrddim(RRDDIM_ACQUIRED *rda);
  994. void rrddim_acquired_release(RRDDIM_ACQUIRED *rda);
  995. RRDDIM *rrddim_find_active(RRDSET *st, const char *id);
  996. int rrddim_hide(RRDSET *st, const char *id);
  997. int rrddim_unhide(RRDSET *st, const char *id);
  998. void rrddim_is_obsolete(RRDSET *st, RRDDIM *rd);
  999. void rrddim_isnot_obsolete(RRDSET *st, RRDDIM *rd);
  1000. collected_number rrddim_timed_set_by_pointer(RRDSET *st, RRDDIM *rd, struct timeval collected_time, collected_number value);
  1001. collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
  1002. collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
  1003. bool rrddim_finalize_collection_and_check_retention(RRDDIM *rd);
  1004. void rrdset_finalize_collection(RRDSET *st, bool dimensions_too);
  1005. void rrdhost_finalize_collection(RRDHOST *host);
  1006. void rrd_finalize_collection_for_all_hosts(void);
  1007. long align_entries_to_pagesize(RRD_MEMORY_MODE mode, long entries);
  1008. #ifdef NETDATA_LOG_COLLECTION_ERRORS
  1009. #define rrddim_store_metric(rd, point_end_time_ut, n, flags) rrddim_store_metric_with_trace(rd, point_end_time_ut, n, flags, __FUNCTION__)
  1010. void rrddim_store_metric_with_trace(RRDDIM *rd, usec_t point_end_time_ut, NETDATA_DOUBLE n, SN_FLAGS flags, const char *function);
  1011. #else
  1012. void rrddim_store_metric(RRDDIM *rd, usec_t point_end_time_ut, NETDATA_DOUBLE n, SN_FLAGS flags);
  1013. #endif
  1014. // ----------------------------------------------------------------------------
  1015. // Miscellaneous functions
  1016. char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
  1017. // ----------------------------------------------------------------------------
  1018. // RRD internal functions
  1019. void rrdset_delete_files(RRDSET *st);
  1020. void rrdset_save(RRDSET *st);
  1021. void rrdset_free(RRDSET *st);
  1022. #ifdef NETDATA_RRD_INTERNALS
  1023. char *rrdhost_cache_dir_for_rrdset_alloc(RRDHOST *host, const char *id);
  1024. const char *rrdset_cache_dir(RRDSET *st);
  1025. void rrddim_free(RRDSET *st, RRDDIM *rd);
  1026. void rrdset_reset(RRDSET *st);
  1027. void rrdset_delete_obsolete_dimensions(RRDSET *st);
  1028. #endif /* NETDATA_RRD_INTERNALS */
  1029. void set_host_properties(
  1030. RRDHOST *host, int update_every, RRD_MEMORY_MODE memory_mode, const char *registry_hostname,
  1031. const char *os, const char *tags, const char *tzone, const char *abbrev_tzone, int32_t utc_offset,
  1032. const char *program_name, const char *program_version);
  1033. size_t get_tier_grouping(size_t tier);
  1034. void store_metric_collection_completed(void);
  1035. // ----------------------------------------------------------------------------
  1036. // RRD DB engine declarations
  1037. #ifdef ENABLE_DBENGINE
  1038. #include "database/engine/rrdengineapi.h"
  1039. #endif
  1040. #include "sqlite/sqlite_functions.h"
  1041. #include "sqlite/sqlite_context.h"
  1042. #include "sqlite/sqlite_metadata.h"
  1043. #include "sqlite/sqlite_aclk.h"
  1044. #include "sqlite/sqlite_aclk_alert.h"
  1045. #include "sqlite/sqlite_aclk_node.h"
  1046. #include "sqlite/sqlite_health.h"
  1047. #ifdef __cplusplus
  1048. }
  1049. #endif
  1050. #endif /* NETDATA_RRD_H */