X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fcore%2Fplugin.c;h=0bf785d571a29b57ac9ecba7d9f34dd1dc6d6b5a;hp=3611be4966b51d296557620e9ed77abda9d7ce48;hb=bb7b99fec92dc9ea1c7ce70e050f4d97fec47ded;hpb=9a96acd759c31211aa512e174339a9c178c4eb83 diff --git a/src/core/plugin.c b/src/core/plugin.c index 3611be4..0bf785d 100644 --- a/src/core/plugin.c +++ b/src/core/plugin.c @@ -661,6 +661,111 @@ plugin_add_callback(sdb_llist_t **list, const char *type, return 0; } /* plugin_add_callback */ +/* + * object meta-data + */ + +typedef struct { + int obj_type; + sdb_time_t last_update; + sdb_time_t interval; +} interval_fetcher_t; + +static int +interval_fetcher_host(sdb_store_host_t *host, sdb_object_t *user_data) +{ + interval_fetcher_t *lu = SDB_OBJ_WRAPPER(user_data)->data; + lu->obj_type = SDB_HOST; + lu->last_update = host->last_update; + return 0; +} /* interval_fetcher_host */ + +static int +interval_fetcher_service(sdb_store_service_t *svc, sdb_object_t *user_data) +{ + interval_fetcher_t *lu = SDB_OBJ_WRAPPER(user_data)->data; + lu->obj_type = SDB_SERVICE; + lu->last_update = svc->last_update; + return 0; +} /* interval_fetcher_service */ + +static int +interval_fetcher_metric(sdb_store_metric_t *metric, sdb_object_t *user_data) +{ + interval_fetcher_t *lu = SDB_OBJ_WRAPPER(user_data)->data; + lu->obj_type = SDB_METRIC; + lu->last_update = metric->last_update; + return 0; +} /* interval_fetcher_metric */ + +static int +interval_fetcher_attr(sdb_store_attribute_t *attr, sdb_object_t *user_data) +{ + interval_fetcher_t *lu = SDB_OBJ_WRAPPER(user_data)->data; + lu->obj_type = SDB_ATTRIBUTE; + lu->last_update = attr->last_update; + return 0; +} /* interval_fetcher_attr */ + +static sdb_store_writer_t interval_fetcher = { + interval_fetcher_host, interval_fetcher_service, + interval_fetcher_metric, interval_fetcher_attr, +}; + +static int +get_interval(int obj_type, const char *hostname, + int parent_type, const char *parent, const char *name, + sdb_time_t last_update, sdb_time_t *interval_out) +{ + sdb_ast_fetch_t fetch = SDB_AST_FETCH_INIT; + char hn[hostname ? strlen(hostname) + 1 : 1]; + char pn[parent ? strlen(parent) + 1 : 1]; + char n[strlen(name) + 1]; + int status; + + interval_fetcher_t lu = { 0, 0, 0 }; + sdb_object_wrapper_t obj = SDB_OBJECT_WRAPPER_STATIC(&lu); + sdb_time_t interval; + + assert(name); + + if (hostname) + strncpy(hn, hostname, sizeof(hn)); + if (parent) + strncpy(pn, parent, sizeof(pn)); + strncpy(n, name, sizeof(n)); + + fetch.obj_type = obj_type; + fetch.hostname = hostname ? hn : NULL; + fetch.parent_type = parent_type; + fetch.parent = parent ? pn : NULL; + fetch.name = n; + + status = sdb_plugin_query(SDB_AST_NODE(&fetch), + &interval_fetcher, SDB_OBJ(&obj), NULL); + if ((status < 0) || (lu.obj_type != obj_type) || (lu.last_update == 0)) { + *interval_out = 0; + return 0; + } + + if (lu.last_update >= last_update) { + if (lu.last_update > last_update) + sdb_log(SDB_LOG_DEBUG, "memstore: Cannot update %s '%s' - " + "value too old (%"PRIsdbTIME" < %"PRIsdbTIME")", + SDB_STORE_TYPE_TO_NAME(obj_type), name, + lu.last_update, last_update); + *interval_out = lu.interval; + return 1; + } + + interval = last_update - lu.last_update; + if (lu.interval && interval) + interval = (sdb_time_t)((0.9 * (double)lu.interval) + + (0.1 * (double)interval)); + *interval_out = interval; + return 0; +} /* get_interval */ + static void get_backend(char **backends, size_t *backends_num) { @@ -1485,7 +1590,7 @@ sdb_plugin_query(sdb_ast_node_t *ast, int sdb_plugin_store_host(const char *name, sdb_time_t last_update) { - sdb_store_host_t host = { 0 }; + sdb_store_host_t host = SDB_STORE_HOST_INIT; char *backends[1]; char *cname; @@ -1508,7 +1613,12 @@ sdb_plugin_store_host(const char *name, sdb_time_t last_update) } host.name = cname; - host.last_update = last_update; + host.last_update = last_update ? last_update : sdb_gettime(); + if (get_interval(SDB_HOST, NULL, -1, NULL, cname, + host.last_update, &host.interval)) { + free(cname); + return 1; + } host.backends = (const char * const *)backends; get_backend(backends, &host.backends_num); @@ -1530,7 +1640,7 @@ int sdb_plugin_store_service(const char *hostname, const char *name, sdb_time_t last_update) { - sdb_store_service_t service = { 0 }; + sdb_store_service_t service = SDB_STORE_SERVICE_INIT; char *backends[1]; char *cname; @@ -1556,7 +1666,12 @@ sdb_plugin_store_service(const char *hostname, const char *name, service.hostname = cname; service.name = name; - service.last_update = last_update; + service.last_update = last_update ? last_update : sdb_gettime(); + if (get_interval(SDB_SERVICE, cname, -1, NULL, name, + service.last_update, &service.interval)) { + free(cname); + return 1; + } service.backends = (const char * const *)backends; get_backend(backends, &service.backends_num); @@ -1576,7 +1691,7 @@ sdb_plugin_store_service(const char *hostname, const char *name, d.type = SDB_TYPE_STRING; d.data.string = cname; if (sdb_plugin_store_service_attribute(cname, name, - "hostname", &d, last_update)) + "hostname", &d, service.last_update)) status = -1; } @@ -1588,7 +1703,7 @@ int sdb_plugin_store_metric(const char *hostname, const char *name, sdb_metric_store_t *store, sdb_time_t last_update) { - sdb_store_metric_t metric = { 0 }; + sdb_store_metric_t metric = SDB_STORE_METRIC_INIT; char *backends[1]; char *cname; @@ -1618,10 +1733,17 @@ sdb_plugin_store_metric(const char *hostname, const char *name, metric.hostname = cname; metric.name = name; if (store) { - metric.store.type = store->type; - metric.store.id = store->id; + if (store->last_update < last_update) + store->last_update = last_update; + metric.stores = store; + metric.stores_num = 1; + } + metric.last_update = last_update ? last_update : sdb_gettime(); + if (get_interval(SDB_METRIC, cname, -1, NULL, name, + metric.last_update, &metric.interval)) { + free(cname); + return 1; } - metric.last_update = last_update; metric.backends = (const char * const *)backends; get_backend(backends, &metric.backends_num); @@ -1641,7 +1763,7 @@ sdb_plugin_store_metric(const char *hostname, const char *name, d.type = SDB_TYPE_STRING; d.data.string = cname; if (sdb_plugin_store_metric_attribute(cname, name, - "hostname", &d, last_update)) + "hostname", &d, metric.last_update)) status = -1; } @@ -1653,7 +1775,7 @@ int sdb_plugin_store_attribute(const char *hostname, const char *key, const sdb_data_t *value, sdb_time_t last_update) { - sdb_store_attribute_t attr = { 0 }; + sdb_store_attribute_t attr = SDB_STORE_ATTRIBUTE_INIT; char *backends[1]; char *cname; @@ -1679,7 +1801,12 @@ sdb_plugin_store_attribute(const char *hostname, const char *key, attr.parent = cname; attr.key = key; attr.value = *value; - attr.last_update = last_update; + attr.last_update = last_update ? last_update : sdb_gettime(); + if (get_interval(SDB_ATTRIBUTE, cname, -1, NULL, key, + attr.last_update, &attr.interval)) { + free(cname); + return 1; + } attr.backends = (const char * const *)backends; get_backend(backends, &attr.backends_num); @@ -1701,7 +1828,7 @@ int sdb_plugin_store_service_attribute(const char *hostname, const char *service, const char *key, const sdb_data_t *value, sdb_time_t last_update) { - sdb_store_attribute_t attr = { 0 }; + sdb_store_attribute_t attr = SDB_STORE_ATTRIBUTE_INIT; char *backends[1]; char *cname; @@ -1728,7 +1855,12 @@ sdb_plugin_store_service_attribute(const char *hostname, const char *service, attr.parent = service; attr.key = key; attr.value = *value; - attr.last_update = last_update; + attr.last_update = last_update ? last_update : sdb_gettime(); + if (get_interval(SDB_ATTRIBUTE, cname, SDB_SERVICE, service, key, + attr.last_update, &attr.interval)) { + free(cname); + return 1; + } attr.backends = (const char * const *)backends; get_backend(backends, &attr.backends_num); @@ -1750,7 +1882,7 @@ int sdb_plugin_store_metric_attribute(const char *hostname, const char *metric, const char *key, const sdb_data_t *value, sdb_time_t last_update) { - sdb_store_attribute_t attr = { 0 }; + sdb_store_attribute_t attr = SDB_STORE_ATTRIBUTE_INIT; char *backends[1]; char *cname; @@ -1777,7 +1909,12 @@ sdb_plugin_store_metric_attribute(const char *hostname, const char *metric, attr.parent = metric; attr.key = key; attr.value = *value; - attr.last_update = last_update; + attr.last_update = last_update ? last_update : sdb_gettime(); + if (get_interval(SDB_ATTRIBUTE, cname, SDB_METRIC, metric, key, + attr.last_update, &attr.interval)) { + free(cname); + return 1; + } attr.backends = (const char * const *)backends; get_backend(backends, &attr.backends_num);