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. */
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;
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);
}
/* 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++) {
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);
}
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);