rrd.h 57 KB

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