]> arthur.barton.de Git - netdata.git/blob - src/rrd.h
each host can have its own settings for history, update_every, rrd memory mode, healt...
[netdata.git] / src / rrd.h
1 #ifndef NETDATA_RRD_H
2 #define NETDATA_RRD_H 1
3
4 #define UPDATE_EVERY 1
5 #define UPDATE_EVERY_MAX 3600
6
7 #define RRD_DEFAULT_HISTORY_ENTRIES 3600
8 #define RRD_HISTORY_ENTRIES_MAX (86400*10)
9
10 extern int default_localhost_rrd_update_every;
11 extern int default_localhost_rrd_history_entries;
12
13 #define RRD_ID_LENGTH_MAX 200
14
15 #define RRDSET_MAGIC        "NETDATA RRD SET FILE V019"
16 #define RRDDIMENSION_MAGIC  "NETDATA RRD DIMENSION FILE V019"
17
18 typedef long long total_number;
19 #define TOTAL_NUMBER_FORMAT "%lld"
20
21 // ----------------------------------------------------------------------------
22 // chart types
23
24 typedef enum rrdset_type {
25     RRDSET_TYPE_LINE    = 0,
26     RRDSET_TYPE_AREA    = 1,
27     RRDSET_TYPE_STACKED = 2
28 } RRDSET_TYPE;
29
30 #define RRDSET_TYPE_LINE_NAME "line"
31 #define RRDSET_TYPE_AREA_NAME "area"
32 #define RRDSET_TYPE_STACKED_NAME "stacked"
33
34 RRDSET_TYPE rrdset_type_id(const char *name);
35 const char *rrdset_type_name(RRDSET_TYPE chart_type);
36
37
38 // ----------------------------------------------------------------------------
39 // memory mode
40
41 typedef enum rrd_memory_mode {
42     RRD_MEMORY_MODE_RAM  = 0,
43     RRD_MEMORY_MODE_MAP  = 1,
44     RRD_MEMORY_MODE_SAVE = 2
45 } RRD_MEMORY_MODE;
46
47 #define RRD_MEMORY_MODE_RAM_NAME "ram"
48 #define RRD_MEMORY_MODE_MAP_NAME "map"
49 #define RRD_MEMORY_MODE_SAVE_NAME "save"
50
51 extern RRD_MEMORY_MODE default_localhost_rrd_memory_mode;
52
53 extern const char *rrd_memory_mode_name(RRD_MEMORY_MODE id);
54 extern RRD_MEMORY_MODE rrd_memory_mode_id(const char *name);
55
56
57 // ----------------------------------------------------------------------------
58 // algorithms types
59
60 typedef enum rrd_algorithm {
61     RRD_ALGORITHM_ABSOLUTE              = 0,
62     RRD_ALGORITHM_INCREMENTAL           = 1,
63     RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL = 2,
64     RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL  = 3
65 } RRD_ALGORITHM;
66
67 #define RRD_ALGORITHM_ABSOLUTE_NAME                "absolute"
68 #define RRD_ALGORITHM_INCREMENTAL_NAME             "incremental"
69 #define RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL_NAME   "percentage-of-incremental-row"
70 #define RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL_NAME    "percentage-of-absolute-row"
71
72 extern RRD_ALGORITHM rrd_algorithm_id(const char *name);
73 extern const char *rrd_algorithm_name(RRD_ALGORITHM algorithm);
74
75 // ----------------------------------------------------------------------------
76 // flags
77
78 typedef enum rrddim_flags {
79     RRDDIM_FLAG_HIDDEN                          = 1 << 0,  // this dimension will not be offered to callers
80     RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS = 1 << 1,  // do not offer RESET or OVERFLOW info to callers
81     RRDDIM_FLAG_UPDATED                         = 1 << 31  // the dimension has been updated since the last processing
82 } RRDDIM_FLAGS;
83
84 #define rrddim_flag_check(rd, flag) ((rd)->flags & flag)
85 #define rrddim_flag_set(rd, flag)   (rd)->flags |= flag
86 #define rrddim_flag_clear(rd, flag) (rd)->flags &= ~flag
87
88
89 // ----------------------------------------------------------------------------
90 // RRD FAMILY
91
92 struct rrdfamily {
93     avl avl;
94
95     const char *family;
96     uint32_t hash_family;
97
98     size_t use_count;
99
100     avl_tree_lock variables_root_index;
101 };
102 typedef struct rrdfamily RRDFAMILY;
103
104
105 // ----------------------------------------------------------------------------
106 // RRD DIMENSION - this is a metric
107
108 struct rrddim {
109     // ------------------------------------------------------------------------
110     // binary indexing structures
111
112     avl avl;                                        // the binary index - this has to be first member!
113
114     // ------------------------------------------------------------------------
115     // the dimension definition
116
117     const char *id;                                 // the id of this dimension (for internal identification)
118     const char *name;                               // the name of this dimension (as presented to user)
119                                                     // this is a pointer to the config structure
120                                                     // since the config always has a higher priority
121                                                     // (the user overwrites the name of the charts)
122                                                     // DO NOT FREE THIS - IT IS ALLOCATED IN CONFIG
123
124     RRD_ALGORITHM algorithm;                        // the algorithm that is applied to add new collected values
125     RRD_MEMORY_MODE memory_mode;                    // the memory mode for this dimension
126
127     collected_number multiplier;                    // the multiplier of the collected values
128     collected_number divisor;                       // the divider of the collected values
129
130     uint32_t flags;                                 // options and status options for the dimension
131
132     // ------------------------------------------------------------------------
133     // members for temporary data we need for calculations
134
135     uint32_t hash;                                  // a simple hash of the id, to speed up searching / indexing
136                                                     // instead of strcmp() every item in the binary index
137                                                     // we first compare the hashes
138
139     uint32_t hash_name;                             // a simple hash of the name
140
141     char *cache_filename;                           // the filename we load/save from/to this set
142
143     size_t counter;                                 // the number of times we added values to this rrdim
144
145     struct timeval last_collected_time;             // when was this dimension last updated
146                                                     // this is actual date time we updated the last_collected_value
147                                                     // THIS IS DIFFERENT FROM THE SAME MEMBER OF RRDSET
148
149     calculated_number calculated_value;             // the current calculated value, after applying the algorithm - resets to zero after being used
150     calculated_number last_calculated_value;        // the last calculated value processed
151
152     calculated_number last_stored_value;            // the last value as stored in the database (after interpolation)
153
154     collected_number collected_value;               // the current value, as collected - resets to 0 after being used
155     collected_number last_collected_value;          // the last value that was collected, after being processed
156
157     // the *_volume members are used to calculate the accuracy of the rounding done by the
158     // storage number - they are printed to debug.log when debug is enabled for a set.
159     calculated_number collected_volume;             // the sum of all collected values so far
160     calculated_number stored_volume;                // the sum of all stored values so far
161
162     struct rrddim *next;                            // linking of dimensions within the same data set
163     struct rrdset *rrdset;
164
165     // ------------------------------------------------------------------------
166     // members for checking the data when loading from disk
167
168     long entries;                                   // how many entries this dimension has in ram
169                                                     // this is the same to the entries of the data set
170                                                     // we set it here, to check the data when we load it from disk.
171
172     int update_every;                               // every how many seconds is this updated
173
174     size_t memsize;                                 // the memory allocated for this dimension
175
176     char magic[sizeof(RRDDIMENSION_MAGIC) + 1];     // a string to be saved, used to identify our data file
177
178     struct rrddimvar *variables;
179
180     // ------------------------------------------------------------------------
181     // the values stored in this dimension, using our floating point numbers
182
183     storage_number values[];                        // the array of values - THIS HAS TO BE THE LAST MEMBER
184 };
185 typedef struct rrddim RRDDIM;
186
187
188 // ----------------------------------------------------------------------------
189 // RRDSET - this is a chart
190
191 struct rrdset {
192     // ------------------------------------------------------------------------
193     // binary indexing structures
194
195     avl avl;                                        // the index, with key the id - this has to be first!
196     avl avlname;                                    // the index, with key the name
197
198     // ------------------------------------------------------------------------
199     // the set configuration
200
201     char id[RRD_ID_LENGTH_MAX + 1];                 // id of the data set
202
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
208     char *type;                                     // the type of graph RRD_TYPE_* (a category, for determining graphing options)
209     char *family;                                   // grouping sets under the same family
210     char *title;                                    // title shown to user
211     char *units;                                    // units of measurement
212
213     char *context;                                  // the template of this data set
214     uint32_t hash_context;
215
216     RRDSET_TYPE chart_type;
217
218     int update_every;                               // every how many seconds is this updated?
219
220     long entries;                                   // total number of entries in the data set
221
222     long current_entry;                             // the entry that is currently being updated
223                                                     // it goes around in a round-robin fashion
224
225     int enabled;
226
227     int gap_when_lost_iterations_above;             // after how many lost iterations a gap should be stored
228                                                     // netdata will interpolate values for gaps lower than this
229
230     long priority;
231
232     int isdetail;                                   // if set, the data set should be considered as a detail of another
233                                                     // (the master data set should be the one that has the same family and is not detail)
234
235     // ------------------------------------------------------------------------
236     // members for temporary data we need for calculations
237
238     int mapped;                                     // if set to 1, this is memory mapped
239
240     int debug;
241
242     char *cache_dir;                                // the directory to store dimensions
243     char cache_filename[FILENAME_MAX+1];            // the filename to store this set
244
245     pthread_rwlock_t rwlock;
246
247     unsigned long counter;                          // the number of times we added values to this rrd
248     unsigned long counter_done;                     // the number of times we added values to this rrd
249
250     uint32_t hash;                                  // a simple hash on the id, to speed up searching
251                                                     // we first compare hashes, and only if the hashes are equal we do string comparisons
252
253     uint32_t hash_name;                             // a simple hash on the name
254
255     usec_t usec_since_last_update;                  // the time in microseconds since the last collection of data
256
257     struct timeval last_updated;                    // when this data set was last updated (updated every time the rrd_stats_done() function)
258     struct timeval last_collected_time;             // when did this data set last collected values
259
260     total_number collected_total;                   // used internally to calculate percentages
261     total_number last_collected_total;              // used internally to calculate percentages
262
263     RRDFAMILY *rrdfamily;
264     struct rrdhost *rrdhost;
265
266     struct rrdset *next;                            // linking of rrdsets
267
268     // ------------------------------------------------------------------------
269     // local variables
270
271     calculated_number green;
272     calculated_number red;
273
274     avl_tree_lock variables_root_index;
275     RRDSETVAR *variables;
276     RRDCALC *alarms;
277
278     // ------------------------------------------------------------------------
279     // members for checking the data when loading from disk
280
281     unsigned long memsize;                          // how much mem we have allocated for this (without dimensions)
282
283     char magic[sizeof(RRDSET_MAGIC) + 1];           // our magic
284
285     // ------------------------------------------------------------------------
286     // the dimensions
287
288     avl_tree_lock dimensions_index;                 // the root of the dimensions index
289     RRDDIM *dimensions;                             // the actual data for every dimension
290
291 };
292 typedef struct rrdset RRDSET;
293
294 // ----------------------------------------------------------------------------
295 // RRD HOST
296
297 struct rrdhost {
298     avl avl;                                        // the index of hosts
299
300     char *hostname;                                 // the hostname of this host
301     uint32_t hash_hostname;                         // the hostname hash
302
303     char machine_guid[GUID_LEN + 1];                // the unique ID of this host
304     uint32_t hash_machine_guid;                     // the hash of the unique ID
305
306     int rrd_update_every;                           // the update frequency of the host
307     int rrd_history_entries;                        // the number of history entries for the host's charts
308
309     int health_enabled;                             // 1 when this host has health enabled
310     RRD_MEMORY_MODE rrd_memory_mode;                // the memory more for the charts of this host
311
312     RRDSET *rrdset_root;                            // the host charts
313     pthread_rwlock_t rrdset_root_rwlock;            // lock for the host charts
314
315     avl_tree_lock rrdset_root_index;                // the host's charts index (by id)
316     avl_tree_lock rrdset_root_index_name;           // the host's charts index (by name)
317
318     avl_tree_lock rrdfamily_root_index;             // the host's chart families index
319     avl_tree_lock variables_root_index;             // the host's chart variables index
320
321     // all RRDCALCs are primarily allocated and linked here
322     // RRDCALCs may be linked to charts at any point
323     // (charts may or may not exist when these are loaded)
324     RRDCALC *alarms;
325
326     ALARM_LOG health_log;                           // alarms historical events (event log)
327
328     // templates of alarms
329     // these are used to create alarms when charts
330     // are created or renamed, that match them
331     RRDCALCTEMPLATE *templates;
332
333     // health / alarm settings
334     char *health_default_exec;
335     char *health_default_recipient;
336     char *health_log_filename;
337     size_t health_log_entries_written;
338     FILE *health_log_fp;
339
340     struct rrdhost *next;
341 };
342 typedef struct rrdhost RRDHOST;
343
344 extern RRDHOST *localhost;
345
346 extern void rrd_init(char *hostname);
347
348 extern RRDHOST *rrdhost_find(const char *guid, uint32_t hash);
349 extern RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid);
350
351 #ifdef NETDATA_INTERNAL_CHECKS
352 #define rrdhost_check_wrlock(host) rrdhost_check_wrlock_int(host, __FILE__, __FUNCTION__, __LINE__)
353 #define rrdhost_check_rdlock(host) rrdhost_check_rdlock_int(host, __FILE__, __FUNCTION__, __LINE__)
354 #else
355 #define rrdhost_check_rdlock(host) (void)0
356 #define rrdhost_check_wrlock(host) (void)0
357 #endif
358
359 extern void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
360 extern void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
361
362 extern void rrdhost_rwlock(RRDHOST *host);
363 extern void rrdhost_rdlock(RRDHOST *host);
364 extern void rrdhost_unlock(RRDHOST *host);
365
366 // ----------------------------------------------------------------------------
367 // RRDSET functions
368
369 extern void rrdset_set_name(RRDSET *st, const char *name);
370
371 extern RRDSET *rrdset_create(RRDHOST *host
372                              , const char *type
373                              , const char *id
374                              , const char *name
375                              , const char *family
376                              , const char *context
377                              , const char *title
378                              , const char *units
379                              , long priority
380                              , int update_every
381                              , int chart_type);
382
383 #define rrdset_create_localhost(type, id, name, family, context, title, units, priority, update_every, chart_type) rrdset_create(localhost, type, id, name, family, context, title, units, priority, update_every, chart_type)
384
385 extern void rrdhost_free_all(void);
386 extern void rrdhost_save_all(void);
387
388 extern void rrdhost_free(RRDHOST *host);
389 extern void rrdhost_save(RRDHOST *host);
390
391 extern RRDSET *rrdset_find(RRDHOST *host, const char *id);
392 #define rrdset_find_localhost(id) rrdset_find(localhost, id)
393
394 extern RRDSET *rrdset_find_bytype(RRDHOST *host, const char *type, const char *id);
395 #define rrdset_find_bytype_localhost(type, id) rrdset_find_bytype(localhost, type, id)
396
397 extern RRDSET *rrdset_find_byname(RRDHOST *host, const char *name);
398 #define rrdset_find_byname_localhost(name)  rrdset_find_byname(localhost, name)
399
400 extern void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
401 extern void rrdset_next_usec(RRDSET *st, usec_t microseconds);
402 #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
403
404 extern usec_t rrdset_done(RRDSET *st);
405
406 // get the total duration in seconds of the round robin database
407 #define rrdset_duration(st) ((time_t)( (((st)->counter >= ((unsigned long)(st)->entries))?(unsigned long)(st)->entries:(st)->counter) * (st)->update_every ))
408
409 // get the timestamp of the last entry in the round robin database
410 #define rrdset_last_entry_t(st) ((time_t)(((st)->last_updated.tv_sec)))
411
412 // get the timestamp of first entry in the round robin database
413 #define rrdset_first_entry_t(st) ((time_t)(rrdset_last_entry_t(st) - rrdset_duration(st)))
414
415 // get the last slot updated in the round robin database
416 #define rrdset_last_slot(st) ((unsigned long)(((st)->current_entry == 0) ? (st)->entries - 1 : (st)->current_entry - 1))
417
418 // get the first / oldest slot updated in the round robin database
419 #define rrdset_first_slot(st) ((unsigned long)( (((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) ))
420
421 // get the slot of the round robin database, for the given timestamp (t)
422 // it always returns a valid slot, although may not be for the time requested if the time is outside the round robin database
423 #define rrdset_time2slot(st, t) ( \
424         (  (time_t)(t) >= rrdset_last_entry_t(st))  ? ( rrdset_last_slot(st) ) : \
425         ( ((time_t)(t) <= rrdset_first_entry_t(st)) ?   rrdset_first_slot(st) : \
426         ( (rrdset_last_slot(st) >= (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) ? \
427           (rrdset_last_slot(st) -  (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) ) : \
428           (rrdset_last_slot(st) -  (unsigned long)((rrdset_last_entry_t(st) - (time_t)(t)) / (unsigned long)((st)->update_every)) + (unsigned long)(st)->entries ) \
429         )))
430
431 // get the timestamp of a specific slot in the round robin database
432 #define rrdset_slot2time(st, slot) ( rrdset_last_entry_t(st) - \
433         ((unsigned long)(st)->update_every * ( \
434                 ( (unsigned long)(slot) > rrdset_last_slot(st)) ? \
435                 ( (rrdset_last_slot(st) - (unsigned long)(slot) + (unsigned long)(st)->entries) ) : \
436                 ( (rrdset_last_slot(st) - (unsigned long)(slot)) )) \
437         ))
438
439 // ----------------------------------------------------------------------------
440 // RRD DIMENSION functions
441
442 extern RRDDIM *rrddim_add(RRDSET *st, const char *id, const char *name, collected_number multiplier, collected_number divisor, RRD_ALGORITHM algorithm);
443
444 extern void rrddim_set_name(RRDSET *st, RRDDIM *rd, const char *name);
445 extern RRDDIM *rrddim_find(RRDSET *st, const char *id);
446
447 extern int rrddim_hide(RRDSET *st, const char *id);
448 extern int rrddim_unhide(RRDSET *st, const char *id);
449
450 extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
451 extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
452
453
454 // ----------------------------------------------------------------------------
455 // RRD internal functions
456
457 #ifdef NETDATA_RRD_INTERNALS
458
459 extern avl_tree_lock rrdhost_root_index;
460
461 extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
462 extern char *rrdset_cache_dir(RRDHOST *host, const char *id);
463
464 extern void rrdset_reset(RRDSET *st);
465
466 extern void rrddim_free(RRDSET *st, RRDDIM *rd);
467
468 extern int rrddim_compare(void* a, void* b);
469 extern int rrdset_compare(void* a, void* b);
470 extern int rrdset_compare_name(void* a, void* b);
471 extern int rrdfamily_compare(void *a, void *b);
472
473 extern RRDFAMILY *rrdfamily_create(RRDHOST *host, const char *id);
474 extern void rrdfamily_free(RRDHOST *host, RRDFAMILY *rc);
475
476 #define rrdset_index_add(host, st) (RRDSET *)avl_insert_lock(&((host)->rrdset_root_index), (avl *)(st))
477 #define rrdset_index_del(host, st) (RRDSET *)avl_remove_lock(&((host)->rrdset_root_index), (avl *)(st))
478 extern RRDSET *rrdset_index_del_name(RRDHOST *host, RRDSET *st);
479
480 #endif /* NETDATA_RRD_INTERNALS */
481
482
483 #endif /* NETDATA_RRD_H */