rrd.h 58 KB

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