+#define rrdhost_index_add(rrdhost) (RRDHOST *)avl_insert_lock(&(rrdhost_root_index), (avl *)(rrdhost))
+#define rrdhost_index_del(rrdhost) (RRDHOST *)avl_remove_lock(&(rrdhost_root_index), (avl *)(rrdhost))
+
+
+// ----------------------------------------------------------------------------
+// RRDHOST - internal helpers
+
+static inline void rrdhost_init_hostname(RRDHOST *host, const char *hostname) {
+ freez(host->hostname);
+ host->hostname = strdupz(hostname);
+ host->hash_hostname = simple_hash(host->hostname);
+}
+
+static inline void rrdhost_init_machine_guid(RRDHOST *host, const char *machine_guid) {
+ strncpy(host->machine_guid, machine_guid, GUID_LEN);
+ host->machine_guid[GUID_LEN] = '\0';
+ host->hash_machine_guid = simple_hash(host->machine_guid);
+}
+
+// ----------------------------------------------------------------------------
+// RRDHOST - add a host
+
+RRDHOST *rrdhost_create(const char *hostname, const char *guid) {
+ RRDHOST *host = callocz(1, sizeof(RRDHOST));
+
+ pthread_rwlock_init(&(host->rrdset_root_rwlock), NULL);
+
+ rrdhost_init_hostname(host, hostname);
+ rrdhost_init_machine_guid(host, guid);
+
+ avl_init_lock(&(host->rrdset_root_index), rrdset_compare);
+ avl_init_lock(&(host->rrdset_root_index_name), rrdset_compare_name);
+ avl_init_lock(&(host->rrdfamily_root_index), rrdfamily_compare);
+ avl_init_lock(&(host->variables_root_index), rrdvar_compare);
+
+ host->health_log.next_log_id = 1;
+ host->health_log.next_alarm_id = 1;
+ host->health_log.max = 1000;
+ host->health_log.next_log_id =
+ host->health_log.next_alarm_id = (uint32_t)now_realtime_sec();
+ pthread_rwlock_init(&(host->health_log.alarm_log_rwlock), NULL);
+
+ if(rrdhost_index_add(host) != host)
+ fatal("Cannot add host '%s' to index. It already exists.", hostname);
+
+ debug(D_RRDHOST, "Added host '%s'", host->hostname);
+ return host;
+}
+
+// ----------------------------------------------------------------------------
+// RRDHOST global / startup initialization
+
+RRDHOST *localhost = NULL;
+
+void rrd_init(char *hostname) {
+ localhost = rrdhost_create(hostname, registry_get_this_machine_guid());
+}
+
+// ----------------------------------------------------------------------------
+// RRDHOST - locks
+