rrd.h 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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. // forward typedefs
  12. typedef struct rrdhost RRDHOST;
  13. typedef struct rrddim RRDDIM;
  14. typedef struct rrdset RRDSET;
  15. typedef struct rrdvar RRDVAR;
  16. typedef struct rrdsetvar RRDSETVAR;
  17. typedef struct rrddimvar RRDDIMVAR;
  18. typedef struct rrdcalc RRDCALC;
  19. typedef struct rrdcalctemplate RRDCALCTEMPLATE;
  20. typedef struct alarm_entry ALARM_ENTRY;
  21. typedef struct context_param CONTEXT_PARAM;
  22. typedef void *ml_host_t;
  23. typedef void *ml_dimension_t;
  24. // forward declarations
  25. struct rrddim_tier;
  26. struct rrdset_volatile;
  27. struct context_param;
  28. #ifdef ENABLE_DBENGINE
  29. struct rrdeng_page_descr;
  30. struct rrdengine_instance;
  31. struct pg_cache_page_index;
  32. #endif
  33. #include "daemon/common.h"
  34. #include "web/api/queries/query.h"
  35. #include "web/api/queries/rrdr.h"
  36. #include "rrdvar.h"
  37. #include "rrdsetvar.h"
  38. #include "rrddimvar.h"
  39. #include "rrdcalc.h"
  40. #include "rrdcalctemplate.h"
  41. #include "streaming/rrdpush.h"
  42. #include "aclk/aclk_rrdhost_state.h"
  43. #include "sqlite/sqlite_health.h"
  44. #include "rrdcontext.h"
  45. extern int storage_tiers;
  46. extern int storage_tiers_grouping_iterations[RRD_STORAGE_TIERS];
  47. typedef enum {
  48. RRD_BACKFILL_NONE,
  49. RRD_BACKFILL_FULL,
  50. RRD_BACKFILL_NEW
  51. } RRD_BACKFILL;
  52. extern RRD_BACKFILL storage_tiers_backfill[RRD_STORAGE_TIERS];
  53. enum {
  54. CONTEXT_FLAGS_ARCHIVE = 0x01,
  55. CONTEXT_FLAGS_CHART = 0x02,
  56. CONTEXT_FLAGS_CONTEXT = 0x04
  57. };
  58. struct context_param {
  59. RRDDIM *rd;
  60. time_t first_entry_t;
  61. time_t last_entry_t;
  62. uint8_t flags;
  63. };
  64. #define META_CHART_UPDATED 1
  65. #define META_PLUGIN_UPDATED 2
  66. #define META_MODULE_UPDATED 4
  67. #define META_CHART_ACTIVATED 8
  68. #define UPDATE_EVERY 1
  69. #define UPDATE_EVERY_MAX 3600
  70. #define RRD_DEFAULT_HISTORY_ENTRIES 3600
  71. #define RRD_HISTORY_ENTRIES_MAX (86400*365)
  72. extern int default_rrd_update_every;
  73. extern int default_rrd_history_entries;
  74. extern int gap_when_lost_iterations_above;
  75. extern time_t rrdset_free_obsolete_time;
  76. #define RRD_ID_LENGTH_MAX 200
  77. typedef long long total_number;
  78. #define TOTAL_NUMBER_FORMAT "%lld"
  79. // ----------------------------------------------------------------------------
  80. // chart types
  81. typedef enum rrdset_type {
  82. RRDSET_TYPE_LINE = 0,
  83. RRDSET_TYPE_AREA = 1,
  84. RRDSET_TYPE_STACKED = 2
  85. } RRDSET_TYPE;
  86. #define RRDSET_TYPE_LINE_NAME "line"
  87. #define RRDSET_TYPE_AREA_NAME "area"
  88. #define RRDSET_TYPE_STACKED_NAME "stacked"
  89. RRDSET_TYPE rrdset_type_id(const char *name);
  90. const char *rrdset_type_name(RRDSET_TYPE chart_type);
  91. // ----------------------------------------------------------------------------
  92. // memory mode
  93. typedef enum rrd_memory_mode {
  94. RRD_MEMORY_MODE_NONE = 0,
  95. RRD_MEMORY_MODE_RAM = 1,
  96. RRD_MEMORY_MODE_MAP = 2,
  97. RRD_MEMORY_MODE_SAVE = 3,
  98. RRD_MEMORY_MODE_ALLOC = 4,
  99. RRD_MEMORY_MODE_DBENGINE = 5
  100. } RRD_MEMORY_MODE;
  101. #define RRD_MEMORY_MODE_NONE_NAME "none"
  102. #define RRD_MEMORY_MODE_RAM_NAME "ram"
  103. #define RRD_MEMORY_MODE_MAP_NAME "map"
  104. #define RRD_MEMORY_MODE_SAVE_NAME "save"
  105. #define RRD_MEMORY_MODE_ALLOC_NAME "alloc"
  106. #define RRD_MEMORY_MODE_DBENGINE_NAME "dbengine"
  107. extern RRD_MEMORY_MODE default_rrd_memory_mode;
  108. extern const char *rrd_memory_mode_name(RRD_MEMORY_MODE id);
  109. extern RRD_MEMORY_MODE rrd_memory_mode_id(const char *name);
  110. // ----------------------------------------------------------------------------
  111. // algorithms types
  112. typedef enum rrd_algorithm {
  113. RRD_ALGORITHM_ABSOLUTE = 0,
  114. RRD_ALGORITHM_INCREMENTAL = 1,
  115. RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL = 2,
  116. RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL = 3
  117. } RRD_ALGORITHM;
  118. #define RRD_ALGORITHM_ABSOLUTE_NAME "absolute"
  119. #define RRD_ALGORITHM_INCREMENTAL_NAME "incremental"
  120. #define RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL_NAME "percentage-of-incremental-row"
  121. #define RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL_NAME "percentage-of-absolute-row"
  122. extern RRD_ALGORITHM rrd_algorithm_id(const char *name);
  123. extern const char *rrd_algorithm_name(RRD_ALGORITHM algorithm);
  124. // ----------------------------------------------------------------------------
  125. // RRD FAMILY
  126. struct rrdfamily {
  127. avl_t avl;
  128. const char *family;
  129. uint32_t hash_family;
  130. size_t use_count;
  131. avl_tree_lock rrdvar_root_index;
  132. };
  133. typedef struct rrdfamily RRDFAMILY;
  134. // ----------------------------------------------------------------------------
  135. // flags
  136. // use this for configuration flags, not for state control
  137. // flags are set/unset in a manner that is not thread safe
  138. // and may lead to missing information.
  139. typedef enum rrddim_flags {
  140. RRDDIM_FLAG_NONE = 0,
  141. RRDDIM_FLAG_HIDDEN = (1 << 0), // this dimension will not be offered to callers
  142. RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS = (1 << 1), // do not offer RESET or OVERFLOW info to callers
  143. RRDDIM_FLAG_OBSOLETE = (1 << 2), // this is marked by the collector/module as obsolete
  144. // No new values have been collected for this dimension since agent start or it was marked RRDDIM_FLAG_OBSOLETE at
  145. // least rrdset_free_obsolete_time seconds ago.
  146. RRDDIM_FLAG_ARCHIVED = (1 << 3),
  147. RRDDIM_FLAG_ACLK = (1 << 4),
  148. RRDDIM_FLAG_PENDING_FOREACH_ALARM = (1 << 5), // set when foreach alarm has not been initialized yet
  149. RRDDIM_FLAG_META_HIDDEN = (1 << 6), // Status of hidden option in the metadata database
  150. } RRDDIM_FLAGS;
  151. #define rrddim_flag_check(rd, flag) (__atomic_load_n(&((rd)->flags), __ATOMIC_SEQ_CST) & (flag))
  152. #define rrddim_flag_set(rd, flag) __atomic_or_fetch(&((rd)->flags), (flag), __ATOMIC_SEQ_CST)
  153. #define rrddim_flag_clear(rd, flag) __atomic_and_fetch(&((rd)->flags), ~(flag), __ATOMIC_SEQ_CST)
  154. typedef enum rrdlabel_source {
  155. RRDLABEL_SRC_AUTO = (1 << 0), // set when Netdata found the label by some automation
  156. RRDLABEL_SRC_CONFIG = (1 << 1), // set when the user configured the label
  157. RRDLABEL_SRC_K8S = (1 << 2), // set when this label is found from k8s (RRDLABEL_SRC_AUTO should also be set)
  158. RRDLABEL_SRC_ACLK = (1 << 3), // set when this label is found from ACLK (RRDLABEL_SRC_AUTO should also be set)
  159. // more sources can be added here
  160. RRDLABEL_FLAG_PERMANENT = (1 << 29), // set when this label should never be removed (can be overwritten though)
  161. RRDLABEL_FLAG_OLD = (1 << 30), // marks for rrdlabels internal use - they are not exposed outside rrdlabels
  162. RRDLABEL_FLAG_NEW = (1 << 31) // marks for rrdlabels internal use - they are not exposed outside rrdlabels
  163. } RRDLABEL_SRC;
  164. #define RRDLABEL_FLAG_INTERNAL (RRDLABEL_FLAG_OLD | RRDLABEL_FLAG_NEW | RRDLABEL_FLAG_PERMANENT)
  165. extern DICTIONARY *rrdlabels_create(void);
  166. extern void rrdlabels_destroy(DICTIONARY *labels_dict);
  167. extern void rrdlabels_add(DICTIONARY *dict, const char *name, const char *value, RRDLABEL_SRC ls);
  168. extern void rrdlabels_add_pair(DICTIONARY *dict, const char *string, RRDLABEL_SRC ls);
  169. extern void rrdlabels_get_value_to_buffer_or_null(DICTIONARY *labels, BUFFER *wb, const char *key, const char *quote, const char *null);
  170. extern void rrdlabels_unmark_all(DICTIONARY *labels);
  171. extern void rrdlabels_remove_all_unmarked(DICTIONARY *labels);
  172. extern int rrdlabels_walkthrough_read(DICTIONARY *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data);
  173. extern int rrdlabels_sorted_walkthrough_read(DICTIONARY *labels, int (*callback)(const char *name, const char *value, RRDLABEL_SRC ls, void *data), void *data);
  174. extern void rrdlabels_log_to_buffer(DICTIONARY *labels, BUFFER *wb);
  175. extern bool rrdlabels_match_simple_pattern(DICTIONARY *labels, const char *simple_pattern_txt);
  176. extern bool rrdlabels_match_simple_pattern_parsed(DICTIONARY *labels, SIMPLE_PATTERN *pattern, char equal);
  177. extern 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));
  178. extern void rrdlabels_migrate_to_these(DICTIONARY *dst, DICTIONARY *src);
  179. extern void rrdlabels_copy(DICTIONARY *dst, DICTIONARY *src);
  180. void reload_host_labels(void);
  181. extern void rrdset_update_rrdlabels(RRDSET *st, DICTIONARY *new_rrdlabels);
  182. extern int rrdlabels_unittest(void);
  183. // unfortunately this break when defined in exporting_engine.h
  184. extern bool exporting_labels_filter_callback(const char *name, const char *value, RRDLABEL_SRC ls, void *data);
  185. // ----------------------------------------------------------------------------
  186. // RRD DIMENSION - this is a metric
  187. struct rrddim {
  188. // ------------------------------------------------------------------------
  189. // binary indexing structures
  190. avl_t avl; // the binary index - this has to be first member!
  191. uuid_t metric_uuid; // global UUID for this metric (unique_across hosts)
  192. // ------------------------------------------------------------------------
  193. // the dimension definition
  194. const char *id; // the id of this dimension (for internal identification)
  195. const char *name; // the name of this dimension (as presented to user)
  196. // this is a pointer to the config structure
  197. // since the config always has a higher priority
  198. // (the user overwrites the name of the charts)
  199. uint32_t hash; // a simple hash of the id, to speed up searching / indexing
  200. // instead of strcmp() every item in the binary index
  201. // we first compare the hashes
  202. uint32_t hash_name; // a simple hash of the name
  203. RRD_ALGORITHM algorithm; // the algorithm that is applied to add new collected values
  204. RRD_MEMORY_MODE rrd_memory_mode; // the memory mode for this dimension
  205. RRDDIM_FLAGS flags; // configuration flags for the dimension
  206. bool updated; // 1 when the dimension has been updated since the last processing
  207. bool exposed; // 1 when set what have sent this dimension to the central netdata
  208. collected_number multiplier; // the multiplier of the collected values
  209. collected_number divisor; // the divider of the collected values
  210. // ------------------------------------------------------------------------
  211. // members for temporary data we need for calculations
  212. struct timeval last_collected_time; // when was this dimension last updated
  213. // this is actual date time we updated the last_collected_value
  214. // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
  215. #ifdef ENABLE_ACLK
  216. int aclk_live_status;
  217. #endif
  218. ml_dimension_t ml_dimension;
  219. struct rrddim_tier *tiers[RRD_STORAGE_TIERS]; // our tiers of databases
  220. size_t collections_counter; // the number of times we added values to this rrddim
  221. collected_number collected_value_max; // the absolute maximum of the collected value
  222. NETDATA_DOUBLE calculated_value; // the current calculated value, after applying the algorithm - resets to zero after being used
  223. NETDATA_DOUBLE last_calculated_value; // the last calculated value processed
  224. NETDATA_DOUBLE last_stored_value; // the last value as stored in the database (after interpolation)
  225. collected_number collected_value; // the current value, as collected - resets to 0 after being used
  226. collected_number last_collected_value; // the last value that was collected, after being processed
  227. // the *_volume members are used to calculate the accuracy of the rounding done by the
  228. // storage number - they are printed to debug.log when debug is enabled for a set.
  229. NETDATA_DOUBLE collected_volume; // the sum of all collected values so far
  230. NETDATA_DOUBLE stored_volume; // the sum of all stored values so far
  231. struct rrddim *next; // linking of dimensions within the same data set
  232. struct rrdset *rrdset;
  233. RRDMETRIC_ACQUIRED *rrdmetric; // the rrdmetric of this dimension
  234. // ------------------------------------------------------------------------
  235. // members for checking the data when loading from disk
  236. long entries; // how many entries this dimension has in ram
  237. // this is the same to the entries of the data set
  238. // we set it here, to check the data when we load it from disk.
  239. int update_every; // every how many seconds is this updated
  240. size_t memsize; // the memory allocated for this dimension (without RRDDIM)
  241. struct rrddimvar *variables;
  242. // ------------------------------------------------------------------------
  243. // the values stored in this dimension, using our floating point numbers
  244. void *rd_on_file; // pointer to the header written on disk
  245. storage_number *db; // the array of values
  246. };
  247. // returns the RRDDIM cache filename, or NULL if it does not exist
  248. extern const char *rrddim_cache_filename(RRDDIM *rd);
  249. // updated the header with the latest RRDDIM value, for memory mode MAP and SAVE
  250. extern void rrddim_memory_file_update(RRDDIM *rd);
  251. // free the memory file structures for memory mode MAP and SAVE
  252. extern void rrddim_memory_file_free(RRDDIM *rd);
  253. extern bool rrddim_memory_load_or_create_map_save(RRDSET *st, RRDDIM *rd, RRD_MEMORY_MODE memory_mode);
  254. // return the v019 header size of RRDDIM files
  255. extern size_t rrddim_memory_file_header_size(void);
  256. extern void rrddim_memory_file_save(RRDDIM *rd);
  257. // ----------------------------------------------------------------------------
  258. // engine-specific iterator state for dimension data collection
  259. typedef struct storage_collect_handle STORAGE_COLLECT_HANDLE;
  260. // ----------------------------------------------------------------------------
  261. // engine-specific iterator state for dimension data queries
  262. typedef struct storage_query_handle STORAGE_QUERY_HANDLE;
  263. // ----------------------------------------------------------------------------
  264. // iterator state for RRD dimension data queries
  265. struct rrddim_query_handle {
  266. RRDDIM *rd;
  267. time_t start_time;
  268. time_t end_time;
  269. TIER_QUERY_FETCH tier_query_fetch_type;
  270. STORAGE_QUERY_HANDLE* handle;
  271. };
  272. typedef struct storage_point {
  273. NETDATA_DOUBLE min; // when count > 1, this is the minimum among them
  274. NETDATA_DOUBLE max; // when count > 1, this is the maximum among them
  275. NETDATA_DOUBLE sum; // the point sum - divided by count gives the average
  276. // end_time - start_time = point duration
  277. time_t start_time; // the time the point starts
  278. time_t end_time; // the time the point ends
  279. unsigned count; // the number of original points aggregated
  280. unsigned anomaly_count; // the number of original points found anomalous
  281. SN_FLAGS flags; // flags stored with the point
  282. } STORAGE_POINT;
  283. #define storage_point_unset(x) do { \
  284. (x).min = (x).max = (x).sum = NAN; \
  285. (x).count = 0; \
  286. (x).anomaly_count = 0; \
  287. (x).flags = SN_FLAG_NONE; \
  288. (x).start_time = 0; \
  289. (x).end_time = 0; \
  290. } while(0)
  291. #define storage_point_empty(x, start_t, end_t) do { \
  292. (x).min = (x).max = (x).sum = NAN; \
  293. (x).count = 1; \
  294. (x).anomaly_count = 0; \
  295. (x).flags = SN_FLAG_NONE; \
  296. (x).start_time = start_t; \
  297. (x).end_time = end_t; \
  298. } while(0)
  299. #define storage_point_is_unset(x) (!(x).count)
  300. #define storage_point_is_empty(x) (!netdata_double_isnumber((x).sum))
  301. // ------------------------------------------------------------------------
  302. // function pointers that handle data collection
  303. struct rrddim_collect_ops {
  304. // an initialization function to run before starting collection
  305. STORAGE_COLLECT_HANDLE *(*init)(STORAGE_METRIC_HANDLE *db_metric_handle);
  306. // run this to store each metric into the database
  307. void (*store_metric)(STORAGE_COLLECT_HANDLE *collection_handle, usec_t point_in_time, NETDATA_DOUBLE number, NETDATA_DOUBLE min_value,
  308. NETDATA_DOUBLE max_value, uint16_t count, uint16_t anomaly_count, SN_FLAGS flags);
  309. // run this to flush / reset the current data collection sequence
  310. void (*flush)(STORAGE_COLLECT_HANDLE *collection_handle);
  311. // an finalization function to run after collection is over
  312. // returns 1 if it's safe to delete the dimension
  313. int (*finalize)(STORAGE_COLLECT_HANDLE *collection_handle);
  314. };
  315. // function pointers that handle database queries
  316. struct rrddim_query_ops {
  317. // run this before starting a series of next_metric() database queries
  318. void (*init)(STORAGE_METRIC_HANDLE *db_metric_handle, struct rrddim_query_handle *handle, time_t start_time, time_t end_time, TIER_QUERY_FETCH tier_query_fetch_type);
  319. // run this to load each metric number from the database
  320. STORAGE_POINT (*next_metric)(struct rrddim_query_handle *handle);
  321. // run this to test if the series of next_metric() database queries is finished
  322. int (*is_finished)(struct rrddim_query_handle *handle);
  323. // run this after finishing a series of load_metric() database queries
  324. void (*finalize)(struct rrddim_query_handle *handle);
  325. // get the timestamp of the last entry of this metric
  326. time_t (*latest_time)(STORAGE_METRIC_HANDLE *db_metric_handle);
  327. // get the timestamp of the first entry of this metric
  328. time_t (*oldest_time)(STORAGE_METRIC_HANDLE *db_metric_handle);
  329. };
  330. // ----------------------------------------------------------------------------
  331. // Storage tier data for every dimension
  332. struct rrddim_tier {
  333. int tier_grouping;
  334. RRD_MEMORY_MODE mode; // the memory mode of this tier
  335. RRD_BACKFILL backfill; // backfilling configuration
  336. STORAGE_METRIC_HANDLE *db_metric_handle; // the metric handle inside the database
  337. STORAGE_COLLECT_HANDLE *db_collection_handle; // the data collection handle
  338. STORAGE_POINT virtual_point;
  339. time_t next_point_time;
  340. usec_t last_collected_ut;
  341. struct rrddim_collect_ops collect_ops;
  342. struct rrddim_query_ops query_ops;
  343. };
  344. extern void rrdr_fill_tier_gap_from_smaller_tiers(RRDDIM *rd, int tier, time_t now);
  345. // ----------------------------------------------------------------------------
  346. // volatile state per chart
  347. struct rrdset_volatile {
  348. char *old_title;
  349. char *old_units;
  350. char *old_context;
  351. uuid_t hash_id;
  352. DICTIONARY *chart_labels;
  353. bool is_ar_chart;
  354. };
  355. // ----------------------------------------------------------------------------
  356. // these loop macros make sure the linked list is accessed with the right lock
  357. #define rrddim_foreach_read(rd, st) \
  358. for((rd) = (st)->dimensions, rrdset_check_rdlock(st); (rd) ; (rd) = (rd)->next)
  359. #define rrddim_foreach_write(rd, st) \
  360. for((rd) = (st)->dimensions, rrdset_check_wrlock(st); (rd) ; (rd) = (rd)->next)
  361. // ----------------------------------------------------------------------------
  362. // RRDSET - this is a chart
  363. // use this for configuration flags, not for state control
  364. // flags are set/unset in a manner that is not thread safe
  365. // and may lead to missing information.
  366. typedef enum rrdset_flags {
  367. RRDSET_FLAG_DETAIL = 1 << 1, // if set, the data set should be considered as a detail of another
  368. // (the master data set should be the one that has the same family and is not detail)
  369. RRDSET_FLAG_DEBUG = 1 << 2, // enables or disables debugging for a chart
  370. RRDSET_FLAG_OBSOLETE = 1 << 3, // this is marked by the collector/module as obsolete
  371. RRDSET_FLAG_EXPORTING_SEND = 1 << 4, // if set, this chart should be sent to Prometheus web API and external databases
  372. RRDSET_FLAG_EXPORTING_IGNORE = 1 << 5, // if set, this chart should not be sent to Prometheus web API and external databases
  373. RRDSET_FLAG_UPSTREAM_SEND = 1 << 6, // if set, this chart should be sent upstream (streaming)
  374. RRDSET_FLAG_UPSTREAM_IGNORE = 1 << 7, // if set, this chart should not be sent upstream (streaming)
  375. RRDSET_FLAG_UPSTREAM_EXPOSED = 1 << 8, // if set, we have sent this chart definition to netdata parent (streaming)
  376. RRDSET_FLAG_STORE_FIRST = 1 << 9, // if set, do not eliminate the first collection during interpolation
  377. RRDSET_FLAG_HETEROGENEOUS = 1 << 10, // if set, the chart is not homogeneous (dimensions in it have multiple algorithms, multipliers or dividers)
  378. RRDSET_FLAG_HOMOGENEOUS_CHECK = 1 << 11, // if set, the chart should be checked to determine if the dimensions are homogeneous
  379. RRDSET_FLAG_HIDDEN = 1 << 12, // if set, do not show this chart on the dashboard, but use it for exporting
  380. RRDSET_FLAG_SYNC_CLOCK = 1 << 13, // if set, microseconds on next data collection will be ignored (the chart will be synced to now)
  381. RRDSET_FLAG_OBSOLETE_DIMENSIONS = 1 << 14, // this is marked by the collector/module when a chart has obsolete dimensions
  382. // No new values have been collected for this chart since agent start or it was marked RRDSET_FLAG_OBSOLETE at
  383. // least rrdset_free_obsolete_time seconds ago.
  384. RRDSET_FLAG_ARCHIVED = 1 << 15,
  385. RRDSET_FLAG_ACLK = 1 << 16,
  386. RRDSET_FLAG_PENDING_FOREACH_ALARMS = 1 << 17, // contains dims with uninitialized foreach alarms
  387. RRDSET_FLAG_ANOMALY_DETECTION = 1 << 18 // flag to identify anomaly detection charts.
  388. } RRDSET_FLAGS;
  389. #define rrdset_flag_check(st, flag) (__atomic_load_n(&((st)->flags), __ATOMIC_SEQ_CST) & (flag))
  390. #define rrdset_flag_set(st, flag) __atomic_or_fetch(&((st)->flags), flag, __ATOMIC_SEQ_CST)
  391. #define rrdset_flag_clear(st, flag) __atomic_and_fetch(&((st)->flags), ~flag, __ATOMIC_SEQ_CST)
  392. struct rrdset {
  393. // ------------------------------------------------------------------------
  394. // binary indexing structures
  395. avl_t avl; // the index, with key the id - this has to be first!
  396. avl_t avlname; // the index, with key the name
  397. // ------------------------------------------------------------------------
  398. // the set configuration
  399. char id[RRD_ID_LENGTH_MAX + 1]; // id of the data set
  400. const char *name; // the name of this dimension (as presented to user)
  401. // this is a pointer to the config structure
  402. // since the config always has a higher priority
  403. // (the user overwrites the name of the charts)
  404. char *type; // the type of graph RRD_TYPE_* (a category, for determining graphing options)
  405. char *family; // grouping sets under the same family
  406. char *title; // title shown to user
  407. char *units; // units of measurement
  408. char *context; // the template of this data set
  409. uint32_t hash_context; // the hash of the chart's context
  410. RRDINSTANCE_ACQUIRED *rrdinstance; // the rrdinstance of this chart
  411. RRDCONTEXT_ACQUIRED *rrdcontext; // the rrdcontext this chart belongs to
  412. RRDSET_TYPE chart_type; // line, area, stacked
  413. int update_every; // every how many seconds is this updated?
  414. long entries; // total number of entries in the data set
  415. long current_entry; // the entry that is currently being updated
  416. // it goes around in a round-robin fashion
  417. RRDSET_FLAGS flags; // configuration flags
  418. RRDSET_FLAGS *exporting_flags; // array of flags for exporting connector instances
  419. int gap_when_lost_iterations_above; // after how many lost iterations a gap should be stored
  420. // netdata will interpolate values for gaps lower than this
  421. long priority; // the sorting priority of this chart
  422. // ------------------------------------------------------------------------
  423. // members for temporary data we need for calculations
  424. RRD_MEMORY_MODE rrd_memory_mode; // if set to 1, this is memory mapped
  425. char *cache_dir; // the directory to store dimensions
  426. netdata_rwlock_t rrdset_rwlock; // protects dimensions linked list
  427. size_t counter; // the number of times we added values to this database
  428. size_t counter_done; // the number of times rrdset_done() has been called
  429. union {
  430. time_t last_accessed_time; // the last time this RRDSET has been accessed
  431. time_t last_entry_t; // the last_entry_t computed for transient RRDSET
  432. };
  433. time_t upstream_resync_time; // the timestamp up to which we should resync clock upstream
  434. char *plugin_name; // the name of the plugin that generated this
  435. char *module_name; // the name of the plugin module that generated this
  436. uuid_t *chart_uuid; // Store the global GUID for this chart
  437. // this object.
  438. struct rrdset_volatile *state; // volatile state that is not persistently stored
  439. size_t rrddim_page_alignment; // keeps metric pages in alignment when using dbengine
  440. uint32_t hash; // a simple hash on the id, to speed up searching
  441. // we first compare hashes, and only if the hashes are equal we do string comparisons
  442. uint32_t hash_name; // a simple hash on the name
  443. usec_t usec_since_last_update; // the time in microseconds since the last collection of data
  444. struct timeval last_updated; // when this data set was last updated (updated every time the rrd_stats_done() function)
  445. struct timeval last_collected_time; // when did this data set last collected values
  446. total_number collected_total; // used internally to calculate percentages
  447. total_number last_collected_total; // used internally to calculate percentages
  448. RRDFAMILY *rrdfamily; // pointer to RRDFAMILY this chart belongs to
  449. RRDHOST *rrdhost; // pointer to RRDHOST this chart belongs to
  450. struct rrdset *next; // linking of rrdsets
  451. // ------------------------------------------------------------------------
  452. // local variables
  453. NETDATA_DOUBLE green; // green threshold for this chart
  454. NETDATA_DOUBLE red; // red threshold for this chart
  455. avl_tree_lock rrdvar_root_index; // RRDVAR index for this chart
  456. RRDSETVAR *variables; // RRDSETVAR linked list for this chart (one RRDSETVAR, many RRDVARs)
  457. RRDCALC *alarms; // RRDCALC linked list for this chart
  458. // ------------------------------------------------------------------------
  459. // members for checking the data when loading from disk
  460. unsigned long memsize; // how much mem we have allocated for this (without dimensions)
  461. void *st_on_file; // compatibility with V019 RRDSET files
  462. // ------------------------------------------------------------------------
  463. // the dimensions
  464. avl_tree_lock dimensions_index; // the root of the dimensions index
  465. RRDDIM *dimensions; // the actual data for every dimension
  466. };
  467. #define rrdset_rdlock(st) netdata_rwlock_rdlock(&((st)->rrdset_rwlock))
  468. #define rrdset_wrlock(st) netdata_rwlock_wrlock(&((st)->rrdset_rwlock))
  469. #define rrdset_unlock(st) netdata_rwlock_unlock(&((st)->rrdset_rwlock))
  470. // ----------------------------------------------------------------------------
  471. // these loop macros make sure the linked list is accessed with the right lock
  472. #define rrdset_foreach_read(st, host) \
  473. for((st) = (host)->rrdset_root, rrdhost_check_rdlock(host); st ; (st) = (st)->next)
  474. #define rrdset_foreach_write(st, host) \
  475. for((st) = (host)->rrdset_root, rrdhost_check_wrlock(host); st ; (st) = (st)->next)
  476. extern void rrdset_memory_file_save(RRDSET *st);
  477. extern void rrdset_memory_file_free(RRDSET *st);
  478. extern void rrdset_memory_file_update(RRDSET *st);
  479. extern const char *rrdset_cache_filename(RRDSET *st);
  480. extern bool rrdset_memory_load_or_create_map_save(RRDSET *st_on_file, RRD_MEMORY_MODE memory_mode);
  481. // ----------------------------------------------------------------------------
  482. // RRDHOST flags
  483. // use this for configuration flags, not for state control
  484. // flags are set/unset in a manner that is not thread safe
  485. // and may lead to missing information.
  486. typedef enum rrdhost_flags {
  487. RRDHOST_FLAG_ORPHAN = (1 << 0), // this host is orphan (not receiving data)
  488. RRDHOST_FLAG_DELETE_OBSOLETE_CHARTS = (1 << 1), // delete files of obsolete charts
  489. RRDHOST_FLAG_DELETE_ORPHAN_HOST = (1 << 2), // delete the entire host when orphan
  490. RRDHOST_FLAG_EXPORTING_SEND = (1 << 3), // send it to external databases
  491. RRDHOST_FLAG_EXPORTING_DONT_SEND = (1 << 4), // don't send it to external databases
  492. RRDHOST_FLAG_ARCHIVED = (1 << 5), // The host is archived, no collected charts yet
  493. RRDHOST_FLAG_MULTIHOST = (1 << 6), // Host belongs to localhost/megadb
  494. RRDHOST_FLAG_PENDING_FOREACH_ALARMS = (1 << 7), // contains dims with uninitialized foreach alarms
  495. RRDHOST_FLAG_STREAM_LABELS_UPDATE = (1 << 8),
  496. RRDHOST_FLAG_STREAM_LABELS_STOP = (1 << 9),
  497. RRDHOST_FLAG_ACLK_STREAM_CONTEXTS = (1 << 10), // when set, we should send ACLK stream context updates
  498. } RRDHOST_FLAGS;
  499. #define rrdhost_flag_check(host, flag) (__atomic_load_n(&((host)->flags), __ATOMIC_SEQ_CST) & (flag))
  500. #define rrdhost_flag_set(host, flag) __atomic_or_fetch(&((host)->flags), flag, __ATOMIC_SEQ_CST)
  501. #define rrdhost_flag_clear(host, flag) __atomic_and_fetch(&((host)->flags), ~flag, __ATOMIC_SEQ_CST)
  502. #ifdef NETDATA_INTERNAL_CHECKS
  503. #define rrdset_debug(st, fmt, args...) do { if(unlikely(debug_flags & D_RRD_STATS && rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) \
  504. debug_int(__FILE__, __FUNCTION__, __LINE__, "%s: " fmt, st->name, ##args); } while(0)
  505. #else
  506. #define rrdset_debug(st, fmt, args...) debug_dummy()
  507. #endif
  508. // ----------------------------------------------------------------------------
  509. // Health data
  510. struct alarm_entry {
  511. uint32_t unique_id;
  512. uint32_t alarm_id;
  513. uint32_t alarm_event_id;
  514. uuid_t config_hash_id;
  515. time_t when;
  516. time_t duration;
  517. time_t non_clear_duration;
  518. char *name;
  519. uint32_t hash_name;
  520. char *chart;
  521. uint32_t hash_chart;
  522. char *chart_context;
  523. char *family;
  524. char *classification;
  525. char *component;
  526. char *type;
  527. char *exec;
  528. char *recipient;
  529. time_t exec_run_timestamp;
  530. int exec_code;
  531. uint64_t exec_spawn_serial;
  532. char *source;
  533. char *units;
  534. char *info;
  535. NETDATA_DOUBLE old_value;
  536. NETDATA_DOUBLE new_value;
  537. char *old_value_string;
  538. char *new_value_string;
  539. RRDCALC_STATUS old_status;
  540. RRDCALC_STATUS new_status;
  541. uint32_t flags;
  542. int delay;
  543. time_t delay_up_to_timestamp;
  544. uint32_t updated_by_id;
  545. uint32_t updates_id;
  546. time_t last_repeat;
  547. struct alarm_entry *next;
  548. struct alarm_entry *next_in_progress;
  549. struct alarm_entry *prev_in_progress;
  550. };
  551. typedef struct alarm_log {
  552. uint32_t next_log_id;
  553. uint32_t next_alarm_id;
  554. unsigned int count;
  555. unsigned int max;
  556. ALARM_ENTRY *alarms;
  557. netdata_rwlock_t alarm_log_rwlock;
  558. } ALARM_LOG;
  559. // ----------------------------------------------------------------------------
  560. // RRD HOST
  561. struct rrdhost_system_info {
  562. char *cloud_provider_type;
  563. char *cloud_instance_type;
  564. char *cloud_instance_region;
  565. char *host_os_name;
  566. char *host_os_id;
  567. char *host_os_id_like;
  568. char *host_os_version;
  569. char *host_os_version_id;
  570. char *host_os_detection;
  571. char *host_cores;
  572. char *host_cpu_freq;
  573. char *host_ram_total;
  574. char *host_disk_space;
  575. char *container_os_name;
  576. char *container_os_id;
  577. char *container_os_id_like;
  578. char *container_os_version;
  579. char *container_os_version_id;
  580. char *container_os_detection;
  581. char *kernel_name;
  582. char *kernel_version;
  583. char *architecture;
  584. char *virtualization;
  585. char *virt_detection;
  586. char *container;
  587. char *container_detection;
  588. char *is_k8s_node;
  589. uint16_t hops;
  590. bool ml_capable;
  591. bool ml_enabled;
  592. char *install_type;
  593. char *prebuilt_arch;
  594. char *prebuilt_dist;
  595. int mc_version;
  596. };
  597. struct rrdhost {
  598. avl_t avl; // the index of hosts
  599. // ------------------------------------------------------------------------
  600. // host information
  601. char *hostname; // the hostname of this host
  602. uint32_t hash_hostname; // the hostname hash
  603. char *registry_hostname; // the registry hostname for this host
  604. char machine_guid[GUID_LEN + 1]; // the unique ID of this host
  605. uint32_t hash_machine_guid; // the hash of the unique ID
  606. const char *os; // the O/S type of the host
  607. const char *tags; // tags for this host
  608. const char *timezone; // the timezone of the host
  609. const char *abbrev_timezone; // the abbriviated timezone of the host
  610. int32_t utc_offset; // the offset in seconds from utc
  611. RRDHOST_FLAGS flags; // flags about this RRDHOST
  612. RRDHOST_FLAGS *exporting_flags; // array of flags for exporting connector instances
  613. int rrd_update_every; // the update frequency of the host
  614. long rrd_history_entries; // the number of history entries for the host's charts
  615. RRD_MEMORY_MODE rrd_memory_mode; // the memory more for the charts of this host
  616. char *cache_dir; // the directory to save RRD cache files
  617. char *varlib_dir; // the directory to save health log
  618. char *program_name; // the program name that collects metrics for this host
  619. char *program_version; // the program version that collects metrics for this host
  620. struct rrdhost_system_info *system_info; // information collected from the host environment
  621. // ------------------------------------------------------------------------
  622. // streaming of data to remote hosts - rrdpush
  623. unsigned int rrdpush_send_enabled; // 1 when this host sends metrics to another netdata
  624. char *rrdpush_send_destination; // where to send metrics to
  625. char *rrdpush_send_api_key; // the api key at the receiving netdata
  626. struct rrdpush_destinations *destinations; // a linked list of possible destinations
  627. struct rrdpush_destinations *destination; // the current destination from the above list
  628. // the following are state information for the threading
  629. // streaming metrics from this netdata to an upstream netdata
  630. struct sender_state *sender;
  631. volatile unsigned int rrdpush_sender_spawn; // 1 when the sender thread has been spawn
  632. netdata_thread_t rrdpush_sender_thread; // the sender thread
  633. void *dbsync_worker;
  634. bool rrdpush_sender_connected; // 1 when the sender is ready to push metrics
  635. int rrdpush_sender_socket; // the fd of the socket to the remote host, or -1
  636. volatile unsigned int rrdpush_sender_error_shown; // 1 when we have logged a communication error
  637. volatile unsigned int rrdpush_sender_join; // 1 when we have to join the sending thread
  638. SIMPLE_PATTERN *rrdpush_send_charts_matching; // pattern to match the charts to be sent
  639. int rrdpush_sender_pipe[2]; // collector to sender thread signaling
  640. //BUFFER *rrdpush_sender_buffer; // collector fills it, sender sends it
  641. //uint32_t stream_version; //Set the current version of the stream.
  642. // ------------------------------------------------------------------------
  643. // streaming of data from remote hosts - rrdpush
  644. volatile size_t connected_senders; // when remote hosts are streaming to this
  645. // host, this is the counter of connected clients
  646. time_t senders_connect_time; // the time the last sender was connected
  647. time_t senders_last_chart_command; // the time of the last CHART streaming command
  648. time_t senders_disconnected_time; // the time the last sender was disconnected
  649. struct receiver_state *receiver;
  650. netdata_mutex_t receiver_lock;
  651. int trigger_chart_obsoletion_check; // set when child connects, will instruct parent to
  652. // trigger a check for obsoleted charts since previous connect
  653. // ------------------------------------------------------------------------
  654. // health monitoring options
  655. unsigned int health_enabled; // 1 when this host has health enabled
  656. time_t health_delay_up_to; // a timestamp to delay alarms processing up to
  657. char *health_default_exec; // the full path of the alarms notifications program
  658. char *health_default_recipient; // the default recipient for all alarms
  659. char *health_log_filename; // the alarms event log filename
  660. size_t health_log_entries_written; // the number of alarm events written to the alarms event log
  661. FILE *health_log_fp; // the FILE pointer to the open alarms event log file
  662. uint32_t health_default_warn_repeat_every; // the default value for the interval between repeating warning notifications
  663. uint32_t health_default_crit_repeat_every; // the default value for the interval between repeating critical notifications
  664. // all RRDCALCs are primarily allocated and linked here
  665. // RRDCALCs may be linked to charts at any point
  666. // (charts may or may not exist when these are loaded)
  667. RRDCALC *alarms;
  668. RRDCALC *alarms_with_foreach;
  669. avl_tree_lock alarms_idx_health_log;
  670. avl_tree_lock alarms_idx_name;
  671. ALARM_LOG health_log; // alarms historical events (event log)
  672. uint32_t health_last_processed_id; // the last processed health id from the log
  673. uint32_t health_max_unique_id; // the max alarm log unique id given for the host
  674. uint32_t health_max_alarm_id; // the max alarm id given for the host
  675. // templates of alarms
  676. // these are used to create alarms when charts
  677. // are created or renamed, that match them
  678. RRDCALCTEMPLATE *templates;
  679. RRDCALCTEMPLATE *alarms_template_with_foreach;
  680. // ------------------------------------------------------------------------
  681. // the charts of the host
  682. RRDSET *rrdset_root; // the host charts
  683. unsigned int obsolete_charts_count;
  684. // ------------------------------------------------------------------------
  685. // locks
  686. netdata_rwlock_t rrdhost_rwlock; // lock for this RRDHOST (protects rrdset_root linked list)
  687. // ------------------------------------------------------------------------
  688. // ML handle
  689. ml_host_t ml_host;
  690. // ------------------------------------------------------------------------
  691. // Support for host-level labels
  692. DICTIONARY *host_labels;
  693. // ------------------------------------------------------------------------
  694. // indexes
  695. avl_tree_lock rrdset_root_index; // the host's charts index (by id)
  696. avl_tree_lock rrdset_root_index_name; // the host's charts index (by name)
  697. avl_tree_lock rrdfamily_root_index; // the host's chart families index
  698. avl_tree_lock rrdvar_root_index; // the host's chart variables index
  699. STORAGE_INSTANCE *storage_instance[RRD_STORAGE_TIERS]; // the database instances of the storage tiers
  700. RRDCONTEXTS *rrdctx_queue;
  701. RRDCONTEXTS *rrdctx;
  702. uuid_t host_uuid; // Global GUID for this host
  703. uuid_t *node_id; // Cloud node_id
  704. #ifdef ENABLE_HTTPS
  705. struct netdata_ssl ssl; //Structure used to encrypt the connection
  706. struct netdata_ssl stream_ssl; //Structure used to encrypt the stream
  707. #endif
  708. netdata_mutex_t aclk_state_lock;
  709. aclk_rrdhost_state aclk_state;
  710. struct rrdhost *next;
  711. };
  712. extern RRDHOST *localhost;
  713. #define rrdhost_rdlock(host) netdata_rwlock_rdlock(&((host)->rrdhost_rwlock))
  714. #define rrdhost_wrlock(host) netdata_rwlock_wrlock(&((host)->rrdhost_rwlock))
  715. #define rrdhost_unlock(host) netdata_rwlock_unlock(&((host)->rrdhost_rwlock))
  716. #define rrdhost_aclk_state_lock(host) netdata_mutex_lock(&((host)->aclk_state_lock))
  717. #define rrdhost_aclk_state_unlock(host) netdata_mutex_unlock(&((host)->aclk_state_lock))
  718. // ----------------------------------------------------------------------------
  719. // these loop macros make sure the linked list is accessed with the right lock
  720. #define rrdhost_foreach_read(var) \
  721. for((var) = localhost, rrd_check_rdlock(); var ; (var) = (var)->next)
  722. #define rrdhost_foreach_write(var) \
  723. for((var) = localhost, rrd_check_wrlock(); var ; (var) = (var)->next)
  724. // ----------------------------------------------------------------------------
  725. // global lock for all RRDHOSTs
  726. extern netdata_rwlock_t rrd_rwlock;
  727. #define rrd_rdlock() netdata_rwlock_rdlock(&rrd_rwlock)
  728. #define rrd_wrlock() netdata_rwlock_wrlock(&rrd_rwlock)
  729. #define rrd_unlock() netdata_rwlock_unlock(&rrd_rwlock)
  730. // ----------------------------------------------------------------------------
  731. extern bool is_storage_engine_shared(STORAGE_INSTANCE *engine);
  732. // ----------------------------------------------------------------------------
  733. extern size_t rrd_hosts_available;
  734. extern time_t rrdhost_free_orphan_time;
  735. extern int rrd_init(char *hostname, struct rrdhost_system_info *system_info);
  736. extern RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash);
  737. extern RRDHOST *rrdhost_find_by_guid(const char *guid, uint32_t hash);
  738. extern RRDHOST *rrdhost_find_or_create(
  739. const char *hostname
  740. , const char *registry_hostname
  741. , const char *guid
  742. , const char *os
  743. , const char *timezone
  744. , const char *abbrev_timezone
  745. , int32_t utc_offset
  746. , const char *tags
  747. , const char *program_name
  748. , const char *program_version
  749. , int update_every
  750. , long history
  751. , RRD_MEMORY_MODE mode
  752. , unsigned int health_enabled
  753. , unsigned int rrdpush_enabled
  754. , char *rrdpush_destination
  755. , char *rrdpush_api_key
  756. , char *rrdpush_send_charts_matching
  757. , struct rrdhost_system_info *system_info
  758. , bool is_archived
  759. );
  760. extern void rrdhost_update(RRDHOST *host
  761. , const char *hostname
  762. , const char *registry_hostname
  763. , const char *guid
  764. , const char *os
  765. , const char *timezone
  766. , const char *abbrev_timezone
  767. , int32_t utc_offset
  768. , const char *tags
  769. , const char *program_name
  770. , const char *program_version
  771. , int update_every
  772. , long history
  773. , RRD_MEMORY_MODE mode
  774. , unsigned int health_enabled
  775. , unsigned int rrdpush_enabled
  776. , char *rrdpush_destination
  777. , char *rrdpush_api_key
  778. , char *rrdpush_send_charts_matching
  779. , struct rrdhost_system_info *system_info
  780. );
  781. extern int rrdhost_set_system_info_variable(struct rrdhost_system_info *system_info, char *name, char *value);
  782. #if defined(NETDATA_INTERNAL_CHECKS) && defined(NETDATA_VERIFY_LOCKS)
  783. extern void __rrdhost_check_wrlock(RRDHOST *host, const char *file, const char *function, const unsigned long line);
  784. extern void __rrdhost_check_rdlock(RRDHOST *host, const char *file, const char *function, const unsigned long line);
  785. extern void __rrdset_check_rdlock(RRDSET *st, const char *file, const char *function, const unsigned long line);
  786. extern void __rrdset_check_wrlock(RRDSET *st, const char *file, const char *function, const unsigned long line);
  787. extern void __rrd_check_rdlock(const char *file, const char *function, const unsigned long line);
  788. extern void __rrd_check_wrlock(const char *file, const char *function, const unsigned long line);
  789. #define rrdhost_check_rdlock(host) __rrdhost_check_rdlock(host, __FILE__, __FUNCTION__, __LINE__)
  790. #define rrdhost_check_wrlock(host) __rrdhost_check_wrlock(host, __FILE__, __FUNCTION__, __LINE__)
  791. #define rrdset_check_rdlock(st) __rrdset_check_rdlock(st, __FILE__, __FUNCTION__, __LINE__)
  792. #define rrdset_check_wrlock(st) __rrdset_check_wrlock(st, __FILE__, __FUNCTION__, __LINE__)
  793. #define rrd_check_rdlock() __rrd_check_rdlock(__FILE__, __FUNCTION__, __LINE__)
  794. #define rrd_check_wrlock() __rrd_check_wrlock(__FILE__, __FUNCTION__, __LINE__)
  795. #else
  796. #define rrdhost_check_rdlock(host) (void)0
  797. #define rrdhost_check_wrlock(host) (void)0
  798. #define rrdset_check_rdlock(st) (void)0
  799. #define rrdset_check_wrlock(st) (void)0
  800. #define rrd_check_rdlock() (void)0
  801. #define rrd_check_wrlock() (void)0
  802. #endif
  803. // ----------------------------------------------------------------------------
  804. // RRDSET functions
  805. extern int rrdset_set_name(RRDSET *st, const char *name);
  806. extern RRDSET *rrdset_create_custom(RRDHOST *host
  807. , const char *type
  808. , const char *id
  809. , const char *name
  810. , const char *family
  811. , const char *context
  812. , const char *title
  813. , const char *units
  814. , const char *plugin
  815. , const char *module
  816. , long priority
  817. , int update_every
  818. , RRDSET_TYPE chart_type
  819. , RRD_MEMORY_MODE memory_mode
  820. , long history_entries);
  821. #define rrdset_create(host, type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type) \
  822. 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)
  823. #define rrdset_create_localhost(type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type) \
  824. rrdset_create(localhost, type, id, name, family, context, title, units, plugin, module, priority, update_every, chart_type)
  825. extern void rrdhost_free_all(void);
  826. extern void rrdhost_save_all(void);
  827. extern void rrdhost_cleanup_all(void);
  828. extern void rrdhost_cleanup_orphan_hosts_nolock(RRDHOST *protected_host);
  829. extern void rrdhost_system_info_free(struct rrdhost_system_info *system_info);
  830. extern void rrdhost_free(RRDHOST *host, bool force);
  831. extern void rrdhost_save_charts(RRDHOST *host);
  832. extern void rrdhost_delete_charts(RRDHOST *host);
  833. extern void rrd_cleanup_obsolete_charts();
  834. extern int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected_host, time_t now);
  835. extern void rrdset_update_heterogeneous_flag(RRDSET *st);
  836. extern RRDSET *rrdset_find(RRDHOST *host, const char *id);
  837. #define rrdset_find_localhost(id) rrdset_find(localhost, id)
  838. /* This will not return charts that are archived */
  839. static inline RRDSET *rrdset_find_active_localhost(const char *id)
  840. {
  841. RRDSET *st = rrdset_find_localhost(id);
  842. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  843. return NULL;
  844. return st;
  845. }
  846. extern RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id);
  847. #define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(localhost, type, id)
  848. /* This will not return charts that are archived */
  849. static inline RRDSET *rrdset_find_active_bytype_localhost(const char *type, const char *id)
  850. {
  851. RRDSET *st = rrdset_find_bytype_localhost(type, id);
  852. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  853. return NULL;
  854. return st;
  855. }
  856. extern RRDSET *rrdset_find_byname(RRDHOST *host, const char *name);
  857. #define rrdset_find_byname_localhost(name) rrdset_find_byname(localhost, name)
  858. /* This will not return charts that are archived */
  859. static inline RRDSET *rrdset_find_active_byname_localhost(const char *name)
  860. {
  861. RRDSET *st = rrdset_find_byname_localhost(name);
  862. if (unlikely(st && rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED)))
  863. return NULL;
  864. return st;
  865. }
  866. extern void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
  867. extern void rrdset_next_usec(RRDSET *st, usec_t microseconds);
  868. #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
  869. extern void rrdset_done(RRDSET *st);
  870. extern void rrdset_is_obsolete(RRDSET *st);
  871. extern void rrdset_isnot_obsolete(RRDSET *st);
  872. // checks if the RRDSET should be offered to viewers
  873. #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) && (st)->dimensions && (st)->rrd_memory_mode != RRD_MEMORY_MODE_NONE)
  874. #define rrdset_is_available_for_exporting_and_alarms(st) (!rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE) && !rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED) && (st)->dimensions)
  875. #define rrdset_is_archived(st) (rrdset_flag_check(st, RRDSET_FLAG_ARCHIVED) && (st)->dimensions)
  876. // get the timestamp of the last entry in the round robin database
  877. static inline time_t rrddim_last_entry_t(RRDDIM *rd) {
  878. time_t latest = rd->tiers[0]->query_ops.latest_time(rd->tiers[0]->db_metric_handle);
  879. for(int tier = 1; tier < storage_tiers ;tier++) {
  880. if(unlikely(!rd->tiers[tier])) continue;
  881. time_t t = rd->tiers[tier]->query_ops.latest_time(rd->tiers[tier]->db_metric_handle);
  882. if(t > latest)
  883. latest = t;
  884. }
  885. return latest;
  886. }
  887. static inline time_t rrddim_first_entry_t(RRDDIM *rd) {
  888. time_t oldest = 0;
  889. for(int tier = 0; tier < storage_tiers ;tier++) {
  890. if(unlikely(!rd->tiers[tier])) continue;
  891. time_t t = rd->tiers[tier]->query_ops.oldest_time(rd->tiers[tier]->db_metric_handle);
  892. if(t != 0 && (oldest == 0 || t < oldest))
  893. oldest = t;
  894. }
  895. return oldest;
  896. }
  897. // get the timestamp of the last entry in the round robin database
  898. static inline time_t rrdset_last_entry_t_nolock(RRDSET *st) {
  899. RRDDIM *rd;
  900. time_t last_entry_t = 0;
  901. rrddim_foreach_read(rd, st) {
  902. time_t t = rrddim_last_entry_t(rd);
  903. if(t > last_entry_t) last_entry_t = t;
  904. }
  905. return last_entry_t;
  906. }
  907. static inline time_t rrdset_last_entry_t(RRDSET *st) {
  908. time_t last_entry_t;
  909. netdata_rwlock_rdlock(&st->rrdset_rwlock);
  910. last_entry_t = rrdset_last_entry_t_nolock(st);
  911. netdata_rwlock_unlock(&st->rrdset_rwlock);
  912. return last_entry_t;
  913. }
  914. // get the timestamp of first entry in the round robin database
  915. static inline time_t rrdset_first_entry_t_nolock(RRDSET *st) {
  916. RRDDIM *rd;
  917. time_t first_entry_t = LONG_MAX;
  918. rrddim_foreach_read(rd, st) {
  919. time_t t = rrddim_first_entry_t(rd);
  920. if(t < first_entry_t)
  921. first_entry_t = t;
  922. }
  923. if (unlikely(LONG_MAX == first_entry_t)) return 0;
  924. return first_entry_t;
  925. }
  926. static inline time_t rrdset_first_entry_t(RRDSET *st)
  927. {
  928. time_t first_entry_t;
  929. netdata_rwlock_rdlock(&st->rrdset_rwlock);
  930. first_entry_t = rrdset_first_entry_t_nolock(st);
  931. netdata_rwlock_unlock(&st->rrdset_rwlock);
  932. return first_entry_t;
  933. }
  934. time_t rrdhost_last_entry_t(RRDHOST *h);
  935. // ----------------------------------------------------------------------------
  936. // RRD DIMENSION functions
  937. extern void rrdcalc_link_to_rrddim(RRDDIM *rd, RRDSET *st, RRDHOST *host);
  938. extern RRDDIM *rrddim_add_custom(RRDSET *st, const char *id, const char *name, collected_number multiplier,
  939. collected_number divisor, RRD_ALGORITHM algorithm, RRD_MEMORY_MODE memory_mode);//,
  940. //int is_archived, uuid_t *dim_uuid);
  941. #define rrddim_add(st, id, name, multiplier, divisor, algorithm) rrddim_add_custom(st, id, name, multiplier, divisor, \
  942. algorithm, (st)->rrd_memory_mode)//, 0, NULL)
  943. extern int rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
  944. extern int rrddim_set_algorithm(RRDSET *st, RRDDIM *rd, RRD_ALGORITHM algorithm);
  945. extern int rrddim_set_multiplier(RRDSET *st, RRDDIM *rd, collected_number multiplier);
  946. extern int rrddim_set_divisor(RRDSET *st, RRDDIM *rd, collected_number divisor);
  947. extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
  948. /* This will not return dimensions that are archived */
  949. static inline RRDDIM *rrddim_find_active(RRDSET *st, const char *id)
  950. {
  951. RRDDIM *rd = rrddim_find(st, id);
  952. if (unlikely(rd && rrddim_flag_check(rd, RRDDIM_FLAG_ARCHIVED)))
  953. return NULL;
  954. return rd;
  955. }
  956. extern int rrddim_hide(RRDSET *st, const char *id);
  957. extern int rrddim_unhide(RRDSET *st, const char *id);
  958. extern void rrddim_is_obsolete(RRDSET *st, RRDDIM *rd);
  959. extern void rrddim_isnot_obsolete(RRDSET *st, RRDDIM *rd);
  960. extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
  961. extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
  962. #ifdef ENABLE_ACLK
  963. extern time_t calc_dimension_liveness(RRDDIM *rd, time_t now);
  964. #endif
  965. extern long align_entries_to_pagesize(RRD_MEMORY_MODE mode, long entries);
  966. // ----------------------------------------------------------------------------
  967. // Miscellaneous functions
  968. extern int alarm_compare_id(void *a, void *b);
  969. extern int alarm_compare_name(void *a, void *b);
  970. // ----------------------------------------------------------------------------
  971. // RRD internal functions
  972. #ifdef NETDATA_RRD_INTERNALS
  973. extern avl_tree_lock rrdhost_root_index;
  974. extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
  975. extern char *rrdset_cache_dir(RRDHOST *host, const char *id);
  976. extern void rrddim_free(RRDSET *st, RRDDIM *rd);
  977. extern int rrddim_compare(void* a, void* b);
  978. extern int rrdset_compare(void* a, void* b);
  979. extern int rrdset_compare_name(void* a, void* b);
  980. extern int rrdfamily_compare(void *a, void *b);
  981. extern RRDFAMILY *rrdfamily_create(RRDHOST *host, const char *id);
  982. extern void rrdfamily_free(RRDHOST *host, RRDFAMILY *rc);
  983. #define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl_t *)(st))
  984. #define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl_t *)(st))
  985. extern RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st);
  986. extern void rrdset_free(RRDSET *st);
  987. extern void rrdset_reset(RRDSET *st);
  988. extern void rrdset_save(RRDSET *st);
  989. extern void rrdset_delete_files(RRDSET *st);
  990. extern void rrdset_delete_obsolete_dimensions(RRDSET *st);
  991. extern RRDHOST *rrdhost_create(
  992. const char *hostname, const char *registry_hostname, const char *guid, const char *os, const char *timezone,
  993. const char *abbrev_timezone, int32_t utc_offset,const char *tags, const char *program_name, const char *program_version,
  994. int update_every, long entries, RRD_MEMORY_MODE memory_mode, unsigned int health_enabled, unsigned int rrdpush_enabled,
  995. char *rrdpush_destination, char *rrdpush_api_key, char *rrdpush_send_charts_matching, struct rrdhost_system_info *system_info,
  996. int is_localhost, bool is_archived);
  997. #endif /* NETDATA_RRD_INTERNALS */
  998. extern void set_host_properties(
  999. RRDHOST *host, int update_every, RRD_MEMORY_MODE memory_mode, const char *hostname, const char *registry_hostname,
  1000. const char *guid, const char *os, const char *tags, const char *tzone, const char *abbrev_tzone, int32_t utc_offset,
  1001. const char *program_name, const char *program_version);
  1002. extern int get_tier_grouping(int tier);
  1003. // ----------------------------------------------------------------------------
  1004. // RRD DB engine declarations
  1005. #ifdef ENABLE_DBENGINE
  1006. #include "database/engine/rrdengineapi.h"
  1007. #endif
  1008. #include "sqlite/sqlite_functions.h"
  1009. #include "sqlite/sqlite_context.h"
  1010. #include "sqlite/sqlite_aclk.h"
  1011. #include "sqlite/sqlite_aclk_chart.h"
  1012. #include "sqlite/sqlite_aclk_alert.h"
  1013. #include "sqlite/sqlite_aclk_node.h"
  1014. #include "sqlite/sqlite_health.h"
  1015. #ifdef __cplusplus
  1016. }
  1017. #endif
  1018. #endif /* NETDATA_RRD_H */