X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fcore%2Fplugin.c;h=0bf785d571a29b57ac9ecba7d9f34dd1dc6d6b5a;hp=6b1560336b311a659c35566333d78a147d8d39c2;hb=bb7b99fec92dc9ea1c7ce70e050f4d97fec47ded;hpb=502bbcd0731ca35b4f3d93653a818f5246f81e0b diff --git a/src/core/plugin.c b/src/core/plugin.c index 6b15603..0bf785d 100644 --- a/src/core/plugin.c +++ b/src/core/plugin.c @@ -406,8 +406,7 @@ plugin_writer_init(sdb_object_t *obj, va_list ap) assert(impl); if ((! impl->store_host) || (! impl->store_service) - || (! impl->store_metric) || (! impl->store_attribute) - || (! impl->store_service_attr) || (! impl->store_metric_attr)) { + || (! impl->store_metric) || (! impl->store_attribute)) { sdb_log(SDB_LOG_ERR, "core: store writer callback '%s' " "does not fully implement the writer interface.", obj->name); @@ -662,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 */ @@ -1423,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; @@ -1433,6 +1553,16 @@ sdb_plugin_query(sdb_ast_node_t *ast, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf) if (! ast) return 0; + if ((ast->type != SDB_AST_TYPE_FETCH) + && (ast->type != SDB_AST_TYPE_LIST) + && (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", + SDB_AST_TYPE_TO_STRING(ast)); + return -1; + } + if (n != 1) { char *msg = (n > 0) ? "Cannot execute query: multiple readers not supported" @@ -1447,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; @@ -1459,6 +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 = SDB_STORE_HOST_INIT; + char *backends[1]; char *cname; sdb_llist_iter_t *iter; @@ -1479,12 +1612,22 @@ sdb_plugin_store_host(const char *name, sdb_time_t last_update) return -1; } + host.name = cname; + 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)) { writer_t *writer = WRITER(sdb_llist_iter_get_next(iter)); int s; assert(writer); - s = writer->impl.store_host(cname, last_update, writer->w_user_data); + s = writer->impl.store_host(&host, writer->w_user_data); if (((s > 0) && (status >= 0)) || (s < 0)) status = s; } @@ -1497,9 +1640,13 @@ int sdb_plugin_store_service(const char *hostname, const char *name, sdb_time_t last_update) { + sdb_store_service_t service = SDB_STORE_SERVICE_INIT; + char *backends[1]; char *cname; sdb_llist_iter_t *iter; + sdb_data_t d; + int status = 0; if ((! hostname) || (! name)) @@ -1517,17 +1664,37 @@ sdb_plugin_store_service(const char *hostname, const char *name, return -1; } + service.hostname = cname; + service.name = name; + 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)) { writer_t *writer = WRITER(sdb_llist_iter_get_next(iter)); int s; assert(writer); - s = writer->impl.store_service(cname, name, last_update, - writer->w_user_data); + s = writer->impl.store_service(&service, writer->w_user_data); if (((s > 0) && (status >= 0)) || (s < 0)) status = s; } sdb_llist_iter_destroy(iter); + + if (! status) { + /* record the hostname as an attribute */ + d.type = SDB_TYPE_STRING; + d.data.string = cname; + if (sdb_plugin_store_service_attribute(cname, name, + "hostname", &d, service.last_update)) + status = -1; + } + free(cname); return status; } /* sdb_plugin_store_service */ @@ -1536,9 +1703,13 @@ 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 = SDB_STORE_METRIC_INIT; + char *backends[1]; char *cname; sdb_llist_iter_t *iter; + sdb_data_t d; + int status = 0; if ((! hostname) || (! name)) @@ -1559,17 +1730,43 @@ sdb_plugin_store_metric(const char *hostname, const char *name, if (store && ((! store->type) || (! store->id))) store = NULL; + metric.hostname = cname; + metric.name = name; + if (store) { + 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.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)) { writer_t *writer = WRITER(sdb_llist_iter_get_next(iter)); int s; assert(writer); - s = writer->impl.store_metric(cname, name, store, last_update, - writer->w_user_data); + s = writer->impl.store_metric(&metric, writer->w_user_data); if (((s > 0) && (status >= 0)) || (s < 0)) status = s; } sdb_llist_iter_destroy(iter); + + if (! status) { + /* record the hostname as an attribute */ + d.type = SDB_TYPE_STRING; + d.data.string = cname; + if (sdb_plugin_store_metric_attribute(cname, name, + "hostname", &d, metric.last_update)) + status = -1; + } + free(cname); return status; } /* sdb_plugin_store_metric */ @@ -1578,6 +1775,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 = SDB_STORE_ATTRIBUTE_INIT; + char *backends[1]; char *cname; sdb_llist_iter_t *iter; @@ -1598,13 +1797,25 @@ sdb_plugin_store_attribute(const char *hostname, const char *key, return -1; } + attr.parent_type = SDB_HOST; + attr.parent = cname; + attr.key = key; + attr.value = *value; + 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)) { writer_t *writer = WRITER(sdb_llist_iter_get_next(iter)); int s; assert(writer); - s = writer->impl.store_attribute(cname, key, value, last_update, - writer->w_user_data); + s = writer->impl.store_attribute(&attr, writer->w_user_data); if (((s > 0) && (status >= 0)) || (s < 0)) status = s; } @@ -1617,6 +1828,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 = SDB_STORE_ATTRIBUTE_INIT; + char *backends[1]; char *cname; sdb_llist_iter_t *iter; @@ -1637,13 +1850,26 @@ sdb_plugin_store_service_attribute(const char *hostname, const char *service, return -1; } + attr.hostname = cname; + attr.parent_type = SDB_SERVICE; + attr.parent = service; + attr.key = key; + attr.value = *value; + 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)) { writer_t *writer = WRITER(sdb_llist_iter_get_next(iter)); int s; assert(writer); - s = writer->impl.store_service_attr(cname, service, - key, value, last_update, writer->w_user_data); + s = writer->impl.store_attribute(&attr, writer->w_user_data); if (((s > 0) && (status >= 0)) || (s < 0)) status = s; } @@ -1656,6 +1882,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 = SDB_STORE_ATTRIBUTE_INIT; + char *backends[1]; char *cname; sdb_llist_iter_t *iter; @@ -1676,13 +1904,26 @@ sdb_plugin_store_metric_attribute(const char *hostname, const char *metric, return -1; } + attr.hostname = cname; + attr.parent_type = SDB_METRIC; + attr.parent = metric; + attr.key = key; + attr.value = *value; + 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)) { writer_t *writer = WRITER(sdb_llist_iter_get_next(iter)); int s; assert(writer); - s = writer->impl.store_metric_attr(cname, metric, - key, value, last_update, writer->w_user_data); + s = writer->impl.store_attribute(&attr, writer->w_user_data); if (((s > 0) && (status >= 0)) || (s < 0)) status = s; }