static void populate(void) { sdb_memstore_t *store; sdb_data_t datum; /* the frontend accesses the store via the plugin API */ store = sdb_memstore_create(); ck_assert(store != NULL); ck_assert(sdb_plugin_register_writer("test-writer", &sdb_memstore_writer, SDB_OBJ(store)) == 0); ck_assert(sdb_plugin_register_reader("test-reader", &sdb_memstore_reader, SDB_OBJ(store)) == 0); sdb_object_deref(SDB_OBJ(store)); /* populate the store */ sdb_plugin_store_host("h1", 1 * SDB_INTERVAL_SECOND); sdb_plugin_store_host("h2", 3 * SDB_INTERVAL_SECOND); datum.type = SDB_TYPE_STRING; datum.data.string = "v1"; sdb_plugin_store_attribute("h1", "k1", &datum, 1 * SDB_INTERVAL_SECOND); datum.data.string = "v2"; sdb_plugin_store_attribute("h1", "k2", &datum, 2 * SDB_INTERVAL_SECOND); datum.data.string = "v3"; sdb_plugin_store_attribute("h1", "k3", &datum, 2 * SDB_INTERVAL_SECOND); sdb_plugin_store_metric("h1", "m1", /* store */ NULL, 2 * SDB_INTERVAL_SECOND); sdb_plugin_store_metric("h1", "m2", /* store */ NULL, 1 * SDB_INTERVAL_SECOND); sdb_plugin_store_metric("h2", "m1", /* store */ NULL, 5 * SDB_INTERVAL_SECOND); sdb_plugin_store_metric("h2", "m1", /* store */ NULL, 10 * SDB_INTERVAL_SECOND); datum.type = SDB_TYPE_INTEGER; datum.data.integer = 42; sdb_plugin_store_metric_attribute("h1", "m1", "k3", &datum, 2 * SDB_INTERVAL_SECOND); sdb_plugin_store_service("h2", "s1", 1 * SDB_INTERVAL_SECOND); sdb_plugin_store_service("h2", "s2", 2 * SDB_INTERVAL_SECOND); datum.data.integer = 123; sdb_plugin_store_service_attribute("h2", "s2", "k1", &datum, 2 * SDB_INTERVAL_SECOND); datum.data.integer = 4711; sdb_plugin_store_service_attribute("h2", "s2", "k2", &datum, 1 * SDB_INTERVAL_SECOND); } /* populate */
static int mock_collect(sdb_object_t *user_data) { size_t i; int check; if (SDB_OBJ_WRAPPER(user_data)->data != MAGIC_DATA) { sdb_log(SDB_LOG_ERR, "mock::plugin: Invalid user data %p " "passed to collect", SDB_OBJ_WRAPPER(user_data)->data); exit(1); } for (i = 0; i < SDB_STATIC_ARRAY_LEN(hostnames); ++i) { if ((check = sdb_plugin_store_host(hostnames[i], sdb_gettime()))) { sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to store host: " "status %d", check); exit(1); } } for (i = 0; i < SDB_STATIC_ARRAY_LEN(metrics); ++i) { if ((check = sdb_plugin_store_metric(metrics[i].hostname, metrics[i].metric, &metrics[i].store, sdb_gettime()))) { sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to store metric: " "status %d", check); exit(1); } } for (i = 0; i < SDB_STATIC_ARRAY_LEN(services); ++i) { if ((check = sdb_plugin_store_service(services[i].hostname, services[i].service, sdb_gettime()))) { sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to store service: " "status %d", check); exit(1); } } for (i = 0; i < SDB_STATIC_ARRAY_LEN(attributes); ++i) { sdb_data_t datum = { SDB_TYPE_STRING, { .string = NULL } }; datum.data.string = strdup(attributes[i].value); if ((check = sdb_plugin_store_attribute(attributes[i].hostname, attributes[i].name, &datum, sdb_gettime()))) { sdb_log(SDB_LOG_ERR, "mock::plugin: Failed to store attribute: " "status %d", check); exit(1); } free(datum.data.string); } return 0; } /* mock_collect */
size_t n, sdb_data_t *data, sdb_object_t __attribute__((unused)) *user_data) { const char *hostname; sdb_time_t timestamp; int status; assert(n == 2); assert((data[0].type == SDB_TYPE_STRING) && (data[1].type == SDB_TYPE_DATETIME)); hostname = data[0].data.string; timestamp = data[1].data.datetime; status = sdb_plugin_store_host(hostname, timestamp); if (status < 0) { sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to " "store/update host '%s'.", hostname); return -1; } else if (status > 0) /* value too old */ return 0; sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated " "host '%s' (last update timestamp = %"PRIsdbTIME").", hostname, timestamp); return 0; } /* sdb_livestatus_get_host */