rrd.h 63 KB

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