rrd.h 58 KB

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