From: Victor Julien Date: Wed, 17 Feb 2016 19:19:20 +0000 (+0100) Subject: json-stats: split out json generation X-Git-Tag: suricata-3.0.1RC1~131 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ffdfb6a8f0151fb778ccb56a506936bc2ccf69c6;p=thirdparty%2Fsuricata.git json-stats: split out json generation Split out JSON generation logic so the code becomes reusable. --- diff --git a/src/output-json-stats.c b/src/output-json-stats.c index f17593235d..68701dd9d9 100644 --- a/src/output-json-stats.c +++ b/src/output-json-stats.c @@ -90,28 +90,18 @@ static json_t *OutputStats2Json(json_t *js, const char *key) return value; } -static int JsonStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable *st) +/** \brief turn StatsTable into a json object + * \param flags JSON_STATS_* flags for controlling output + */ +json_t *StatsToJSON(const StatsTable *st, uint8_t flags) { - SCEnter(); - JsonStatsLogThread *aft = (JsonStatsLogThread *)thread_data; const char delta_suffix[] = "_delta"; - struct timeval tval; gettimeofday(&tval, NULL); - json_t *js = json_object(); - if (unlikely(js == NULL)) - return 0; - - char timebuf[64]; - CreateIsoTimeString(&tval, timebuf, sizeof(timebuf)); - json_object_set_new(js, "timestamp", json_string(timebuf)); - - json_object_set_new(js, "event_type", json_string("stats")); json_t *js_stats = json_object(); if (unlikely(js_stats == NULL)) { - json_decref(js); - return 0; + return NULL; } /* Uptime, in seconds. */ @@ -119,7 +109,7 @@ static int JsonStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable * json_integer((int)difftime(tval.tv_sec, st->start_time))); uint32_t u = 0; - if (aft->statslog_ctx->flags & JSON_STATS_TOTALS) { + if (flags & JSON_STATS_TOTALS) { for (u = 0; u < st->nstats; u++) { if (st->stats[u].name == NULL) continue; @@ -132,7 +122,8 @@ static int JsonStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable * if (js_type != NULL) { json_object_set_new(js_type, shortname, json_integer(st->stats[u].value)); - if (aft->statslog_ctx->flags & JSON_STATS_DELTAS) { + + if (flags & JSON_STATS_DELTAS) { char deltaname[strlen(shortname) + strlen(delta_suffix) + 1]; snprintf(deltaname, sizeof(deltaname), "%s%s", shortname, delta_suffix); @@ -144,12 +135,12 @@ static int JsonStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable * } /* per thread stats - stored in a "threads" object. */ - if (st->tstats != NULL && (aft->statslog_ctx->flags & JSON_STATS_THREADS)) { + if (st->tstats != NULL && (flags & JSON_STATS_THREADS)) { /* for each thread (store) */ json_t *threads = json_object(); if (unlikely(threads == NULL)) { - json_decref(js); - return 0; + json_decref(js_stats); + return NULL; } uint32_t x; for (x = 0; x < st->ntstats; x++) { @@ -168,7 +159,7 @@ static int JsonStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable * if (js_type != NULL) { json_object_set_new(js_type, shortname, json_integer(st->tstats[u].value)); - if (aft->statslog_ctx->flags & JSON_STATS_DELTAS) { + if (flags & JSON_STATS_DELTAS) { char deltaname[strlen(shortname) + strlen(delta_suffix) + 1]; snprintf(deltaname, sizeof(deltaname), "%s%s", shortname, delta_suffix); @@ -180,6 +171,30 @@ static int JsonStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable * } json_object_set_new(js_stats, "threads", threads); } + return js_stats; +} + +static int JsonStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable *st) +{ + SCEnter(); + JsonStatsLogThread *aft = (JsonStatsLogThread *)thread_data; + + struct timeval tval; + gettimeofday(&tval, NULL); + + json_t *js = json_object(); + if (unlikely(js == NULL)) + return 0; + char timebuf[64]; + CreateIsoTimeString(&tval, timebuf, sizeof(timebuf)); + json_object_set_new(js, "timestamp", json_string(timebuf)); + json_object_set_new(js, "event_type", json_string("stats")); + + json_t *js_stats = StatsToJSON(st, aft->statslog_ctx->flags); + if (js_stats == NULL) { + json_decref(js); + return 0; + } json_object_set_new(js, "stats", js_stats);