json-stats: split out json generation

Split out JSON generation logic so the code becomes reusable.
pull/1867/head
Victor Julien 10 years ago
parent c446abeb47
commit ffdfb6a8f0

@ -90,28 +90,18 @@ static json_t *OutputStats2Json(json_t *js, const char *key)
return value; 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"; const char delta_suffix[] = "_delta";
struct timeval tval; struct timeval tval;
gettimeofday(&tval, NULL); 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(); json_t *js_stats = json_object();
if (unlikely(js_stats == NULL)) { if (unlikely(js_stats == NULL)) {
json_decref(js); return NULL;
return 0;
} }
/* Uptime, in seconds. */ /* 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))); json_integer((int)difftime(tval.tv_sec, st->start_time)));
uint32_t u = 0; uint32_t u = 0;
if (aft->statslog_ctx->flags & JSON_STATS_TOTALS) { if (flags & JSON_STATS_TOTALS) {
for (u = 0; u < st->nstats; u++) { for (u = 0; u < st->nstats; u++) {
if (st->stats[u].name == NULL) if (st->stats[u].name == NULL)
continue; continue;
@ -132,7 +122,8 @@ static int JsonStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable *
if (js_type != NULL) { if (js_type != NULL) {
json_object_set_new(js_type, shortname, json_object_set_new(js_type, shortname,
json_integer(st->stats[u].value)); 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]; char deltaname[strlen(shortname) + strlen(delta_suffix) + 1];
snprintf(deltaname, sizeof(deltaname), "%s%s", shortname, snprintf(deltaname, sizeof(deltaname), "%s%s", shortname,
delta_suffix); delta_suffix);
@ -144,12 +135,12 @@ static int JsonStatsLogger(ThreadVars *tv, void *thread_data, const StatsTable *
} }
/* per thread stats - stored in a "threads" object. */ /* 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) */ /* for each thread (store) */
json_t *threads = json_object(); json_t *threads = json_object();
if (unlikely(threads == NULL)) { if (unlikely(threads == NULL)) {
json_decref(js); json_decref(js_stats);
return 0; return NULL;
} }
uint32_t x; uint32_t x;
for (x = 0; x < st->ntstats; 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) { if (js_type != NULL) {
json_object_set_new(js_type, shortname, json_integer(st->tstats[u].value)); 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]; char deltaname[strlen(shortname) + strlen(delta_suffix) + 1];
snprintf(deltaname, sizeof(deltaname), "%s%s", snprintf(deltaname, sizeof(deltaname), "%s%s",
shortname, delta_suffix); 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); 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); json_object_set_new(js, "stats", js_stats);

Loading…
Cancel
Save