X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fcore%2Fplugin.c;h=92fa8866981f6e5b4623100498c04d7e5567f0d8;hp=2cb852e45112156229a0a22899c20580f254d7ff;hb=1bde17786b9a0b342daae5faa46a54b3eeab6558;hpb=07d956b72b9b0498b5ef720ea647f2df517b7a90 diff --git a/src/core/plugin.c b/src/core/plugin.c index 2cb852e..92fa886 100644 --- a/src/core/plugin.c +++ b/src/core/plugin.c @@ -661,6 +661,126 @@ 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) +{ + const sdb_plugin_info_t *info; + + info = sdb_plugin_current(); + if ((! info) || (! info->plugin_name) || (! *info->plugin_name)) { + *backends_num = 0; + return; + } + + backends[0] = info->plugin_name; + *backends_num = 1; +} /* get_backend */ + /* * public API */ @@ -1422,7 +1542,8 @@ sdb_plugin_fetch_timeseries(const char *type, const char *id, } /* sdb_plugin_fetch_timeseries */ int -sdb_plugin_query(sdb_ast_node_t *ast, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf) +sdb_plugin_query(sdb_ast_node_t *ast, + sdb_store_writer_t *w, sdb_object_t *wd, sdb_strbuf_t *errbuf) { size_t n = sdb_llist_len(reader_list); reader_t *reader; @@ -1434,8 +1555,7 @@ sdb_plugin_query(sdb_ast_node_t *ast, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf) if ((ast->type != SDB_AST_TYPE_FETCH) && (ast->type != SDB_AST_TYPE_LIST) - && (ast->type != SDB_AST_TYPE_LOOKUP) - && (ast->type != SDB_AST_TYPE_TIMESERIES)) { + && (ast->type != SDB_AST_TYPE_LOOKUP)) { sdb_log(SDB_LOG_ERR, "core: Cannot execute query of type %s", SDB_AST_TYPE_TO_STRING(ast)); sdb_strbuf_sprintf(errbuf, "Cannot execute query of type %s", @@ -1457,7 +1577,8 @@ sdb_plugin_query(sdb_ast_node_t *ast, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf) q = reader->impl.prepare_query(ast, errbuf, reader->r_user_data); if (q) - status = reader->impl.execute_query(q, buf, errbuf, reader->r_user_data); + status = reader->impl.execute_query(q, w, SDB_OBJ(wd), + errbuf, reader->r_user_data); else status = -1; @@ -1469,7 +1590,8 @@ sdb_plugin_query(sdb_ast_node_t *ast, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf) 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; sdb_llist_iter_t *iter; @@ -1491,7 +1613,14 @@ 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); iter = sdb_llist_get_iter(writer_list); while (sdb_llist_iter_has_next(iter)) { @@ -1511,7 +1640,8 @@ 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; sdb_llist_iter_t *iter; @@ -1536,7 +1666,14 @@ 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); iter = sdb_llist_get_iter(writer_list); while (sdb_llist_iter_has_next(iter)) { @@ -1554,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; } @@ -1566,7 +1703,8 @@ 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; sdb_llist_iter_t *iter; @@ -1595,10 +1733,20 @@ sdb_plugin_store_metric(const char *hostname, const char *name, metric.hostname = cname; metric.name = name; if (store) { + if (store->last_update < last_update) + store->last_update = last_update; metric.store.type = store->type; metric.store.id = store->id; + metric.store.last_update = store->last_update; + } + 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); iter = sdb_llist_get_iter(writer_list); while (sdb_llist_iter_has_next(iter)) { @@ -1616,7 +1764,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; } @@ -1628,7 +1776,8 @@ 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; sdb_llist_iter_t *iter; @@ -1653,7 +1802,14 @@ 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); iter = sdb_llist_get_iter(writer_list); while (sdb_llist_iter_has_next(iter)) { @@ -1673,7 +1829,8 @@ 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; sdb_llist_iter_t *iter; @@ -1699,7 +1856,14 @@ 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); iter = sdb_llist_get_iter(writer_list); while (sdb_llist_iter_has_next(iter)) { @@ -1719,7 +1883,8 @@ 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; sdb_llist_iter_t *iter; @@ -1745,7 +1910,14 @@ 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); iter = sdb_llist_get_iter(writer_list); while (sdb_llist_iter_has_next(iter)) {