]> arthur.barton.de Git - netdata.git/blobdiff - src/rrd.h
added rrd memory mode "none" to allow netdata act as a proxy to forward metrics to...
[netdata.git] / src / rrd.h
index 2beb1433f31f70489eb3353b8c47c9e2a15d1044..46ee8e00f9cb1ac8b2b00524f9c4525b2be398f2 100644 (file)
--- a/src/rrd.h
+++ b/src/rrd.h
@@ -3,11 +3,12 @@
 
 #define UPDATE_EVERY 1
 #define UPDATE_EVERY_MAX 3600
-extern int rrd_update_every;
 
 #define RRD_DEFAULT_HISTORY_ENTRIES 3600
 #define RRD_HISTORY_ENTRIES_MAX (86400*10)
-extern int rrd_default_history_entries;
+
+extern int default_rrd_update_every;
+extern int default_rrd_history_entries;
 
 #define RRD_ID_LENGTH_MAX 200
 
@@ -38,19 +39,21 @@ const char *rrdset_type_name(RRDSET_TYPE chart_type);
 // memory mode
 
 typedef enum rrd_memory_mode {
-    RRD_MEMORY_MODE_RAM  = 0,
-    RRD_MEMORY_MODE_MAP  = 1,
-    RRD_MEMORY_MODE_SAVE = 2
+    RRD_MEMORY_MODE_NONE = 0,
+    RRD_MEMORY_MODE_RAM  = 1,
+    RRD_MEMORY_MODE_MAP  = 2,
+    RRD_MEMORY_MODE_SAVE = 3
 } RRD_MEMORY_MODE;
 
+#define RRD_MEMORY_MODE_NONE_NAME "none"
 #define RRD_MEMORY_MODE_RAM_NAME "ram"
 #define RRD_MEMORY_MODE_MAP_NAME "map"
 #define RRD_MEMORY_MODE_SAVE_NAME "save"
 
-RRD_MEMORY_MODE rrd_memory_mode;
+extern RRD_MEMORY_MODE default_rrd_memory_mode;
 
 extern const char *rrd_memory_mode_name(RRD_MEMORY_MODE id);
-extern int rrd_memory_mode_id(const char *name);
+extern RRD_MEMORY_MODE rrd_memory_mode_id(const char *name);
 
 
 // ----------------------------------------------------------------------------
@@ -77,7 +80,8 @@ extern const char *rrd_algorithm_name(RRD_ALGORITHM algorithm);
 typedef enum rrddim_flags {
     RRDDIM_FLAG_HIDDEN                          = 1 << 0,  // this dimension will not be offered to callers
     RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS = 1 << 1,  // do not offer RESET or OVERFLOW info to callers
-    RRDDIM_FLAG_UPDATED                         = 1 << 31  // the dimension has been updated since the last processing
+    RRDDIM_FLAG_UPDATED                         = 1 << 2,  // the dimension has been updated since the last processing
+    RRDDIM_FLAG_EXPOSED                         = 1 << 3   // when set what have sent this dimension to the central netdata
 } RRDDIM_FLAGS;
 
 #define rrddim_flag_check(rd, flag) ((rd)->flags & flag)
@@ -121,7 +125,7 @@ struct rrddim {
                                                     // DO NOT FREE THIS - IT IS ALLOCATED IN CONFIG
 
     RRD_ALGORITHM algorithm;                        // the algorithm that is applied to add new collected values
-    RRD_MEMORY_MODE memory_mode;                    // the memory mode for this dimension
+    RRD_MEMORY_MODE rrd_memory_mode;                // the memory mode for this dimension
 
     collected_number multiplier;                    // the multiplier of the collected values
     collected_number divisor;                       // the divider of the collected values
@@ -183,10 +187,30 @@ struct rrddim {
 };
 typedef struct rrddim RRDDIM;
 
+// ----------------------------------------------------------------------------
+// these loop macros make sure the linked list is accessed with the right lock
+
+#define rrddim_foreach_read(rd, st) \
+    for(rd = st->dimensions, rrdset_check_rdlock(st); rd ; rd = rd->next)
+
+#define rrddim_foreach_write(rd, st) \
+    for(rd = st->dimensions, rrdset_check_wrlock(st); rd ; rd = rd->next)
+
 
 // ----------------------------------------------------------------------------
 // RRDSET - this is a chart
 
+typedef enum rrdset_flags {
+    RRDSET_FLAG_ENABLED = 1 << 0, // enables or disables a chart
+    RRDSET_FLAG_DETAIL  = 1 << 1, // if set, the data set should be considered as a detail of another
+                                  // (the master data set should be the one that has the same family and is not detail)
+    RRDSET_FLAG_DEBUG   = 1 << 2  // enables or disables debugging for a chart
+} RRDSET_FLAGS;
+
+#define rrdset_flag_check(st, flag) ((st)->flags & flag)
+#define rrdset_flag_set(st, flag)   (st)->flags |= flag
+#define rrdset_flag_clear(st, flag) (st)->flags &= ~flag
+
 struct rrdset {
     // ------------------------------------------------------------------------
     // binary indexing structures
@@ -204,6 +228,8 @@ struct rrdset {
                                                     // since the config always has a higher priority
                                                     // (the user overwrites the name of the charts)
 
+    char *config_section;                           // the config section for the chart
+
     char *type;                                     // the type of graph RRD_TYPE_* (a category, for determining graphing options)
     char *family;                                   // grouping sets under the same family
     char *title;                                    // title shown to user
@@ -221,27 +247,23 @@ struct rrdset {
     long current_entry;                             // the entry that is currently being updated
                                                     // it goes around in a round-robin fashion
 
-    int enabled;
+    uint32_t flags;
 
     int gap_when_lost_iterations_above;             // after how many lost iterations a gap should be stored
                                                     // netdata will interpolate values for gaps lower than this
 
     long priority;
 
-    int isdetail;                                   // if set, the data set should be considered as a detail of another
-                                                    // (the master data set should be the one that has the same family and is not detail)
 
     // ------------------------------------------------------------------------
     // members for temporary data we need for calculations
 
-    int mapped;                                     // if set to 1, this is memory mapped
-
-    int debug;
+    RRD_MEMORY_MODE rrd_memory_mode;                // if set to 1, this is memory mapped
 
     char *cache_dir;                                // the directory to store dimensions
     char cache_filename[FILENAME_MAX+1];            // the filename to store this set
 
-    pthread_rwlock_t rwlock;
+    pthread_rwlock_t rrdset_rwlock;                 // protects dimensions linked list
 
     unsigned long counter;                          // the number of times we added values to this rrd
     unsigned long counter_done;                     // the number of times we added values to this rrd
@@ -290,83 +312,151 @@ struct rrdset {
 };
 typedef struct rrdset RRDSET;
 
+#define rrdset_rdlock(st) pthread_rwlock_rdlock(&((st)->rrdset_rwlock))
+#define rrdset_wrlock(st) pthread_rwlock_wrlock(&((st)->rrdset_rwlock))
+#define rrdset_unlock(st) pthread_rwlock_unlock(&((st)->rrdset_rwlock))
+
+// ----------------------------------------------------------------------------
+// these loop macros make sure the linked list is accessed with the right lock
+
+#define rrdset_foreach_read(st, host) \
+    for(st = host->rrdset_root, rrdhost_check_rdlock(host); st ; st = st->next)
+
+#define rrdset_foreach_write(st, host) \
+    for(st = host->rrdset_root, rrdhost_check_wrlock(host); st ; st = st->next)
+
+
 // ----------------------------------------------------------------------------
 // RRD HOST
 
 struct rrdhost {
-    avl avl;
+    avl avl;                                        // the index of hosts
 
-    char *hostname;
-    uint32_t hash_hostname;
+    char *hostname;                                 // the hostname of this host
+    uint32_t hash_hostname;                         // the hostname hash
 
-    char machine_guid[GUID_LEN + 1];
-    uint32_t hash_machine_guid;
+    char machine_guid[GUID_LEN + 1];                // the unique ID of this host
+    uint32_t hash_machine_guid;                     // the hash of the unique ID
 
-    RRDSET *rrdset_root;
-    pthread_rwlock_t rrdset_root_rwlock;
+    int rrd_update_every;                           // the update frequency of the host
+    int rrd_history_entries;                        // the number of history entries for the host's charts
 
-    avl_tree_lock rrdset_root_index;
-    avl_tree_lock rrdset_root_index_name;
+    int health_enabled;                             // 1 when this host has health enabled
+    time_t health_delay_up_to;                      // a timestamp to delay alarms processing up to
+    RRD_MEMORY_MODE rrd_memory_mode;                // the memory more for the charts of this host
 
-    avl_tree_lock rrdfamily_root_index;
-    avl_tree_lock variables_root_index;
+    RRDSET *rrdset_root;                            // the host charts
+
+    pthread_rwlock_t rrdhost_rwlock;                // lock for this RRDHOST (protects rrdset_root linked list)
+
+    avl_tree_lock rrdset_root_index;                // the host's charts index (by id)
+    avl_tree_lock rrdset_root_index_name;           // the host's charts index (by name)
+
+    avl_tree_lock rrdfamily_root_index;             // the host's chart families index
+    avl_tree_lock variables_root_index;             // the host's chart variables index
+
+    char *cache_dir;                                // the directory to save RRD cache files
+    char *varlib_dir;                               // the directory to save health log
 
     // all RRDCALCs are primarily allocated and linked here
     // RRDCALCs may be linked to charts at any point
     // (charts may or may not exist when these are loaded)
     RRDCALC *alarms;
 
-    // alarms historical events
-    ALARM_LOG health_log;
+    ALARM_LOG health_log;                           // alarms historical events (event log)
 
     // templates of alarms
     // these are used to create alarms when charts
     // are created or renamed, that match them
     RRDCALCTEMPLATE *templates;
 
-    uint32_t flags;
+    char *os;                                       // the O/S type of the host
+    volatile size_t use_counter;                    // when remote hosts are streaming to this
+                                                    // host, this is the counter of connected clients
+
+    // health / alarm settings
+    char *health_default_exec;
+    char *health_default_recipient;
+    char *health_log_filename;
+    size_t health_log_entries_written;
+    FILE *health_log_fp;
 
     struct rrdhost *next;
 };
 typedef struct rrdhost RRDHOST;
-
 extern RRDHOST *localhost;
 
+#define rrdhost_rdlock(h) pthread_rwlock_rdlock(&((h)->rrdhost_rwlock))
+#define rrdhost_wrlock(h) pthread_rwlock_wrlock(&((h)->rrdhost_rwlock))
+#define rrdhost_unlock(h) pthread_rwlock_unlock(&((h)->rrdhost_rwlock))
+
+// ----------------------------------------------------------------------------
+// these loop macros make sure the linked list is accessed with the right lock
+
+#define rrdhost_foreach_read(var) \
+    for(var = localhost, rrd_check_rdlock(); var ; var = var->next)
+
+#define rrdhost_foreach_write(var) \
+    for(var = localhost, rrd_check_wrlock(); var ; var = var->next)
+
+
+// ----------------------------------------------------------------------------
+// global lock for all RRDHOSTs
+
+extern pthread_rwlock_t rrd_rwlock;
+#define rrd_rdlock() pthread_rwlock_rdlock(&rrd_rwlock)
+#define rrd_wrlock() pthread_rwlock_wrlock(&rrd_rwlock)
+#define rrd_unlock() pthread_rwlock_unlock(&rrd_rwlock)
+
+// ----------------------------------------------------------------------------
+
 extern void rrd_init(char *hostname);
 
 extern RRDHOST *rrdhost_find(const char *guid, uint32_t hash);
-extern RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid);
+extern RRDHOST *rrdhost_find_or_create(const char *hostname, const char *guid, const char *os, int update_every, int history, RRD_MEMORY_MODE mode, int health_enabled);
 
 #ifdef NETDATA_INTERNAL_CHECKS
-#define rrdhost_check_wrlock(host) rrdhost_check_wrlock_int(host, __FILE__, __FUNCTION__, __LINE__)
+extern void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
+extern void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
+extern void rrdset_check_rdlock_int(RRDSET *st, const char *file, const char *function, const unsigned long line);
+extern void rrdset_check_wrlock_int(RRDSET *st, const char *file, const char *function, const unsigned long line);
+extern void rrd_check_rdlock_int(const char *file, const char *function, const unsigned long line);
+extern void rrd_check_wrlock_int(const char *file, const char *function, const unsigned long line);
+
 #define rrdhost_check_rdlock(host) rrdhost_check_rdlock_int(host, __FILE__, __FUNCTION__, __LINE__)
+#define rrdhost_check_wrlock(host) rrdhost_check_wrlock_int(host, __FILE__, __FUNCTION__, __LINE__)
+#define rrdset_check_rdlock(st) rrdset_check_rdlock_int(st, __FILE__, __FUNCTION__, __LINE__)
+#define rrdset_check_wrlock(st) rrdset_check_wrlock_int(st, __FILE__, __FUNCTION__, __LINE__)
+#define rrd_check_rdlock() rrd_check_rdlock_int(__FILE__, __FUNCTION__, __LINE__)
+#define rrd_check_wrlock() rrd_check_wrlock_int(__FILE__, __FUNCTION__, __LINE__)
+
 #else
 #define rrdhost_check_rdlock(host) (void)0
 #define rrdhost_check_wrlock(host) (void)0
+#define rrdset_check_rdlock(host) (void)0
+#define rrdset_check_wrlock(host) (void)0
+#define rrd_check_rdlock() (void)0
+#define rrd_check_wrlock() (void)0
 #endif
 
-extern void rrdhost_check_wrlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
-extern void rrdhost_check_rdlock_int(RRDHOST *host, const char *file, const char *function, const unsigned long line);
-
-extern void rrdhost_rwlock(RRDHOST *host);
-extern void rrdhost_rdlock(RRDHOST *host);
-extern void rrdhost_unlock(RRDHOST *host);
-
 // ----------------------------------------------------------------------------
 // RRDSET functions
 
 extern void rrdset_set_name(RRDSET *st, const char *name);
 
-extern RRDSET *rrdset_create(const char *type
-        , const char *id
-        , const char *name
-        , const char *family
-        , const char *context
-        , const char *title
-        , const char *units
-        , long priority
-        , int update_every
-        , int chart_type);
+extern RRDSET *rrdset_create(RRDHOST *host
+                             , const char *type
+                             , const char *id
+                             , const char *name
+                             , const char *family
+                             , const char *context
+                             , const char *title
+                             , const char *units
+                             , long priority
+                             , int update_every
+                             , RRDSET_TYPE chart_type);
+
+#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)
 
 extern void rrdhost_free_all(void);
 extern void rrdhost_save_all(void);
@@ -387,7 +477,7 @@ extern void rrdset_next_usec_unfiltered(RRDSET *st, usec_t microseconds);
 extern void rrdset_next_usec(RRDSET *st, usec_t microseconds);
 #define rrdset_next(st) rrdset_next_usec(st, 0ULL)
 
-extern usec_t rrdset_done(RRDSET *st);
+extern void rrdset_done(RRDSET *st);
 
 // get the total duration in seconds of the round robin database
 #define rrdset_duration(st) ((time_t)( (((st)->counter >= ((unsigned long)(st)->entries))?(unsigned long)(st)->entries:(st)->counter) * (st)->update_every ))
@@ -436,16 +526,19 @@ extern int rrddim_unhide(RRDSET *st, const char *id);
 extern collected_number rrddim_set_by_pointer(RRDSET *st, RRDDIM *rd, collected_number value);
 extern collected_number rrddim_set(RRDSET *st, const char *id, collected_number value);
 
+extern long align_entries_to_pagesize(long entries);
+
 
 // ----------------------------------------------------------------------------
 // RRD internal functions
 
 #ifdef NETDATA_RRD_INTERNALS
 
+extern void rrdset_free(RRDSET *st);
 extern avl_tree_lock rrdhost_root_index;
 
 extern char *rrdset_strncpyz_name(char *to, const char *from, size_t length);
-extern char *rrdset_cache_dir(const char *id);
+extern char *rrdset_cache_dir(RRDHOST *host, const char *id, const char *config_section);
 
 extern void rrdset_reset(RRDSET *st);