diff options
Diffstat (limited to 'src/rrdhost.c')
-rw-r--r-- | src/rrdhost.c | 154 |
1 files changed, 123 insertions, 31 deletions
diff --git a/src/rrdhost.c b/src/rrdhost.c index a2310330..ff8aa561 100644 --- a/src/rrdhost.c +++ b/src/rrdhost.c @@ -58,15 +58,32 @@ RRDHOST *rrdhost_find_by_hostname(const char *hostname, uint32_t hash) { // ---------------------------------------------------------------------------- // RRDHOST - internal helpers +static inline void rrdhost_init_tags(RRDHOST *host, const char *tags) { + if(host->tags && tags && !strcmp(host->tags, tags)) + return; + + void *old = (void *)host->tags; + host->tags = (tags && *tags)?strdupz(tags):NULL; + freez(old); +} + static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) { - freez(host->hostname); - host->hostname = strdupz(hostname); + if(host->hostname && hostname && !strcmp(host->hostname, hostname)) + return; + + void *old = host->hostname; + host->hostname = strdupz(hostname?hostname:"localhost"); host->hash_hostname = simple_hash(host->hostname); + freez(old); } static inline void rrdhost_init_os(RRDHOST *host, const char *os) { - freez(host->os); + if(host->os && os && !strcmp(host->os, os)) + return; + + void *old = (void *)host->os; host->os = strdupz(os?os:"unknown"); + freez(old); } static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) { @@ -80,16 +97,18 @@ static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_ // RRDHOST - add a host RRDHOST *rrdhost_create(const char *hostname, - const char *guid, - const char *os, - int update_every, - long entries, - RRD_MEMORY_MODE memory_mode, - int health_enabled, - int rrdpush_enabled, - char *rrdpush_destination, - char *rrdpush_api_key, - int is_localhost + const char *registry_hostname, + const char *guid, + const char *os, + const char *tags, + int update_every, + long entries, + RRD_MEMORY_MODE memory_mode, + int health_enabled, + int rrdpush_enabled, + char *rrdpush_destination, + char *rrdpush_api_key, + int is_localhost ) { debug(D_RRDHOST, "Host '%s': adding with guid '%s'", hostname, guid); @@ -115,6 +134,8 @@ RRDHOST *rrdhost_create(const char *hostname, rrdhost_init_hostname(host, hostname); rrdhost_init_machine_guid(host, guid); rrdhost_init_os(host, os); + rrdhost_init_tags(host, tags); + host->registry_hostname = strdupz((registry_hostname && *registry_hostname)?registry_hostname:hostname); avl_init_lock(&(host->rrdset_root_index), rrdset_compare); avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name); @@ -122,10 +143,10 @@ RRDHOST *rrdhost_create(const char *hostname, avl_init_lock(&(host->variables_root_index), rrdvar_compare); if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete obsolete charts files", 1)) - rrdhost_flag_set(host, RRDHOST_DELETE_OBSOLETE_FILES); + rrdhost_flag_set(host, RRDHOST_DELETE_OBSOLETE_CHARTS); if(config_get_boolean(CONFIG_SECTION_GLOBAL, "delete orphan hosts files", 1) && !is_localhost) - rrdhost_flag_set(host, RRDHOST_DELETE_ORPHAN_FILES); + rrdhost_flag_set(host, RRDHOST_DELETE_ORPHAN_HOST); // ------------------------------------------------------------------------ @@ -229,8 +250,9 @@ RRDHOST *rrdhost_create(const char *hostname, host = NULL; } else { - info("Host '%s' with guid '%s' initialized" + info("Host '%s' (at registry as '%s') with guid '%s' initialized" ", os %s" + ", tags '%s'" ", update every %d" ", memory mode %s" ", history entries %ld" @@ -243,8 +265,10 @@ RRDHOST *rrdhost_create(const char *hostname, ", alarms default handler '%s'" ", alarms default recipient '%s'" , host->hostname + , host->registry_hostname , host->machine_guid , host->os + , (host->tags)?host->tags:"" , host->rrd_update_every , rrd_memory_mode_name(host->rrd_memory_mode) , host->rrd_history_entries @@ -267,8 +291,10 @@ RRDHOST *rrdhost_create(const char *hostname, RRDHOST *rrdhost_find_or_create( const char *hostname + , const char *registry_hostname , const char *guid , const char *os + , const char *tags , int update_every , long history , RRD_MEMORY_MODE mode @@ -284,8 +310,10 @@ RRDHOST *rrdhost_find_or_create( if(!host) { host = rrdhost_create( hostname + , registry_hostname , guid , os + , tags , update_every , history , mode @@ -307,22 +335,25 @@ RRDHOST *rrdhost_find_or_create( } if(host->rrd_update_every != update_every) - error("Host '%s' has an update frequency of %d seconds, but the wanted one is %d seconds.", host->hostname, host->rrd_update_every, update_every); + error("Host '%s' has an update frequency of %d seconds, but the wanted one is %d seconds. Restart netdata here to apply the new settings.", host->hostname, host->rrd_update_every, update_every); - if(host->rrd_history_entries != history) - error("Host '%s' has history of %ld entries, but the wanted one is %ld entries.", host->hostname, host->rrd_history_entries, history); + if(host->rrd_history_entries < history) + error("Host '%s' has history of %ld entries, but the wanted one is %ld entries. Restart netdata here to apply the new settings.", host->hostname, host->rrd_history_entries, history); if(host->rrd_memory_mode != mode) - error("Host '%s' has memory mode '%s', but the wanted one is '%s'.", host->hostname, rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode)); + error("Host '%s' has memory mode '%s', but the wanted one is '%s'. Restart netdata here to apply the new settings.", host->hostname, rrd_memory_mode_name(host->rrd_memory_mode), rrd_memory_mode_name(mode)); + + // update host tags + rrdhost_init_tags(host, tags); } rrd_unlock(); - rrdhost_cleanup_orphan(host); + rrdhost_cleanup_orphan_hosts(host); return host; } -static inline int rrdhost_should_be_deleted(RRDHOST *host, RRDHOST *protected, time_t now) { +static inline int rrdhost_should_be_removed(RRDHOST *host, RRDHOST *protected, time_t now) { if(host != protected && host != localhost && !host->connected_senders @@ -333,7 +364,7 @@ static inline int rrdhost_should_be_deleted(RRDHOST *host, RRDHOST *protected, t return 0; } -void rrdhost_cleanup_orphan(RRDHOST *protected) { +void rrdhost_cleanup_orphan_hosts(RRDHOST *protected) { time_t now = now_realtime_sec(); rrd_wrlock(); @@ -342,10 +373,10 @@ void rrdhost_cleanup_orphan(RRDHOST *protected) { restart_after_removal: rrdhost_foreach_write(host) { - if(rrdhost_should_be_deleted(host, protected, now)) { + if(rrdhost_should_be_removed(host, protected, now)) { info("Host '%s' with machine guid '%s' is obsolete - cleaning up.", host->hostname, host->machine_guid); - if(rrdset_flag_check(host, RRDHOST_ORPHAN)) + if(rrdset_flag_check(host, RRDHOST_DELETE_ORPHAN_HOST) && rrdset_flag_check(host, RRDHOST_ORPHAN)) rrdhost_delete(host); else rrdhost_save(host); @@ -372,8 +403,10 @@ void rrd_init(char *hostname) { rrd_wrlock(); localhost = rrdhost_create( hostname + , registry_get_this_machine_hostname() , registry_get_this_machine_guid() , os_type + , config_get(CONFIG_SECTION_BACKEND, "host tags", "") , default_rrd_update_every , default_rrd_history_entries , default_rrd_memory_mode @@ -473,7 +506,8 @@ void rrdhost_free(RRDHOST *host) { // ------------------------------------------------------------------------ // free it - freez(host->os); + freez((void *)host->tags); + freez((void *)host->os); freez(host->cache_dir); freez(host->varlib_dir); freez(host->rrdpush_api_key); @@ -482,6 +516,7 @@ void rrdhost_free(RRDHOST *host) { freez(host->health_default_recipient); freez(host->health_log_filename); freez(host->hostname); + freez(host->registry_hostname); rrdhost_unlock(host); netdata_rwlock_destroy(&host->health_log.alarm_log_rwlock); netdata_rwlock_destroy(&host->rrdhost_rwlock); @@ -497,12 +532,12 @@ void rrdhost_free_all(void) { } // ---------------------------------------------------------------------------- -// RRDHOST - save +// RRDHOST - save host files void rrdhost_save(RRDHOST *host) { if(!host) return; - info("Saving database of host '%s'...", host->hostname); + info("Saving/Closing database of host '%s'...", host->hostname); RRDSET *st; @@ -520,7 +555,7 @@ void rrdhost_save(RRDHOST *host) { } // ---------------------------------------------------------------------------- -// RRDHOST - delete files +// RRDHOST - delete host files void rrdhost_delete(RRDHOST *host) { if(!host) return; @@ -539,9 +574,43 @@ void rrdhost_delete(RRDHOST *host) { rrdset_unlock(st); } + recursively_delete_dir(host->cache_dir, "left over host"); + rrdhost_unlock(host); } +// ---------------------------------------------------------------------------- +// RRDHOST - cleanup host files + +void rrdhost_cleanup(RRDHOST *host) { + if(!host) return; + + info("Cleaning up database of host '%s'...", host->hostname); + + RRDSET *st; + + // we get a write lock + // to ensure only one thread is saving the database + rrdhost_wrlock(host); + + rrdset_foreach_write(st, host) { + rrdset_rdlock(st); + + if(rrdset_flag_check(st, RRDSET_FLAG_OBSOLETE) && rrdhost_flag_check(host, RRDHOST_DELETE_OBSOLETE_CHARTS)) + rrdset_delete(st); + else + rrdset_save(st); + + rrdset_unlock(st); + } + + rrdhost_unlock(host); +} + + +// ---------------------------------------------------------------------------- +// RRDHOST - save all hosts to disk + void rrdhost_save_all(void) { info("Saving database [%zu hosts(s)]...", rrd_hosts_available); @@ -554,7 +623,30 @@ void rrdhost_save_all(void) { rrd_unlock(); } -void rrdhost_cleanup_obsolete(RRDHOST *host) { +// ---------------------------------------------------------------------------- +// RRDHOST - save or delete all hosts from disk + +void rrdhost_cleanup_all(void) { + info("Cleaning up database [%zu hosts(s)]...", rrd_hosts_available); + + rrd_rdlock(); + + RRDHOST *host; + rrdhost_foreach_read(host) { + if(host != localhost && rrdhost_flag_check(host, RRDHOST_DELETE_OBSOLETE_CHARTS) && !host->connected_senders) + rrdhost_delete(host); + else + rrdhost_cleanup(host); + } + + rrd_unlock(); +} + + +// ---------------------------------------------------------------------------- +// RRDHOST - save or delete all the host charts from disk + +void rrdhost_cleanup_obsolete_charts(RRDHOST *host) { time_t now = now_realtime_sec(); RRDSET *st; @@ -569,7 +661,7 @@ restart_after_removal: rrdset_rdlock(st); - if(rrdhost_flag_check(host, RRDHOST_DELETE_OBSOLETE_FILES)) + if(rrdhost_flag_check(host, RRDHOST_DELETE_OBSOLETE_CHARTS)) rrdset_delete(st); else rrdset_save(st); |