diff options
-rw-r--r-- | main.c | 12 | ||||
-rw-r--r-- | scrape.c | 19 | ||||
-rw-r--r-- | scrape.h | 3 |
3 files changed, 34 insertions, 0 deletions
diff --git a/main.c b/main.c index 0564879..efeba71 100644 --- a/main.c +++ b/main.c @@ -63,10 +63,12 @@ static const struct collector *collectors[] = { struct config { const char *port; + bool print; }; const struct config default_config = { .port = "9100", + .print = false, }; struct collector_ctx { @@ -84,6 +86,11 @@ int main(int argc, char *argv[]) { if (!initialize(argc, argv, &cfg, &ctx)) return 1; + if (cfg.print) { + scrape_print(ctx.enabled, ctx.coll, ctx.coll_ctx); + return 0; + } + scrape_server *server = scrape_listen(cfg.port); if (!server) return 1; @@ -143,6 +150,11 @@ static bool initialize(int argc, char *argv[], struct config *cfg, struct collec goto next_arg; } + if (strncmp(argv[arg], "--stdout", 8) == 0) { + cfg->print = true; + goto next_arg; + } + fprintf(stderr, "unknown argument: %s\n", argv[arg]); return false; next_arg: ; diff --git a/scrape.c b/scrape.c index 68f8c80..f68f9b9 100644 --- a/scrape.c +++ b/scrape.c @@ -218,6 +218,25 @@ void scrape_close(scrape_server *srv) { free(srv); } +void scrape_print(unsigned ncoll, const struct collector *coll[], void *coll_ctx[]) { + bbuf *buf = bbuf_alloc(BUF_INITIAL, BUF_MAX); + for (unsigned i = 0; i < ncoll; i++) { + bbuf_reset(buf); + + struct scrape_req req = { + .state = req_state_write_metrics, + .buf = buf, + }; + coll[i]->collect(&req, coll_ctx[i]); + + size_t len; + char *data = bbuf_get(buf, &len); + write_all(1, data, len); + } + + bbuf_free(buf); +} + // scrape write API implementation void scrape_write(scrape_req *req, const char *metric, const struct label *labels, double value) { diff --git a/scrape.h b/scrape.h index 0c93828..f163e52 100644 --- a/scrape.h +++ b/scrape.h @@ -43,6 +43,9 @@ void scrape_serve(scrape_server *server, unsigned ncoll, const struct collector /** Closes the scrape server sockets and frees any resources. */ void scrape_close(scrape_server *server); +/** Run all scrapers once and print to stdout. */ +void scrape_print(unsigned ncoll, const struct collector *coll[], void *coll_ctx[]); + /** Container for label keys and values. */ struct label { char *key; |