50 #define MODULE_NAME "JsonStatsLog"
82 json_t *jdata = json_object();
90 json_object_set_new(jdata,
"last_reload", json_string(timebuf));
97 json_object_set_new(jdata,
"rules_loaded",
99 json_object_set_new(jdata,
"rules_failed",
115 json_t *js_tenant_list = json_array();
116 json_t *js_tenant = NULL;
118 if (js_tenant_list == NULL) {
123 js_tenant = json_object();
124 if (js_tenant == NULL) {
127 json_object_set_new(js_tenant,
"id", json_integer(list->
tenant_id));
129 json_t *js_stats = EngineStats2Json(list, output);
130 if (js_stats == NULL) {
133 json_object_update(js_tenant, js_stats);
134 json_array_append_new(js_tenant_list, js_tenant);
135 json_decref(js_stats);
140 *jdata = js_tenant_list;
144 json_object_clear(js_tenant);
145 json_decref(js_tenant);
148 json_object_clear(js_tenant_list);
149 json_decref(js_tenant_list);
155 json_object_set_new(*jdata,
"message", json_string(
"Unable to get info"));
167 static json_t *OutputStats2Json(json_t *js,
const char *key)
171 const char *dot = strchr(key,
'.');
174 if (strlen(dot) > 2) {
175 if (*(dot + 1) ==
'.' && *(dot + 2) !=
'\0')
176 dot = strchr(dot + 2,
'.');
179 size_t predot_len = (dot - key) + 1;
183 iter = json_object_iter_at(js, s);
184 const char *s2 = strchr(dot+1,
'.');
186 json_t *value = json_object_iter_value(iter);
188 value = json_object();
190 if (!strncmp(s,
"detect", 6)) {
191 json_t *js_engine = NULL;
195 json_object_set_new(value,
"engines", js_engine);
198 json_object_set_new(js, s, value);
201 return OutputStats2Json(value, &key[dot-key+1]);
211 const char delta_suffix[] =
"_delta";
213 gettimeofday(&tval, NULL);
215 json_t *js_stats = json_object();
221 double up_time_d = difftime(tval.tv_sec, st->
start_time);
222 json_object_set_new(js_stats,
"uptime",
223 json_integer((
int)up_time_d));
227 for (u = 0; u < st->
nstats; u++) {
230 json_t *js_type = NULL;
240 js_type = OutputStats2Json(js_stats, st->
stats[u].
name);
242 if (js_type != NULL) {
243 json_object_set_new(js_type, stat_name, json_integer(st->
stats[u].
value));
246 char deltaname[strlen(stat_name) + strlen(delta_suffix) + 1];
247 snprintf(deltaname,
sizeof(deltaname),
"%s%s", stat_name, delta_suffix);
248 json_object_set_new(js_type, deltaname,
258 json_t *threads = json_object();
260 json_decref(js_stats);
264 for (x = 0; x < st->
ntstats; x++) {
272 json_t *js_type = NULL;
280 js_type = OutputStats2Json(threads,
str);
283 if (js_type != NULL) {
284 json_object_set_new(js_type, stat_name, json_integer(st->
tstats[u].
value));
287 char deltaname[strlen(stat_name) + strlen(delta_suffix) + 1];
288 snprintf(deltaname,
sizeof(deltaname),
"%s%s", stat_name, delta_suffix);
289 json_object_set_new(js_type, deltaname,
295 json_object_set_new(js_stats,
"threads", threads);
306 gettimeofday(&tval, NULL);
308 json_t *js = json_object();
313 json_object_set_new(js,
"timestamp", json_string(timebuf));
314 json_object_set_new(js,
"event_type", json_string(
"stats"));
317 if (js_stats == NULL) {
322 json_object_set_new(js,
"stats", js_stats);
327 json_object_clear(js_stats);
328 json_object_del(js,
"stats");
329 json_object_clear(js);
335 static TmEcode JsonStatsLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
343 SCLogDebug(
"Error getting context for EveLogStats. \"initdata\" argument NULL");
348 if (aft->
buffer == NULL) {
364 if (aft->
buffer != NULL) {
387 static void OutputStatsLogDeinitSub(
OutputCtx *output_ctx)
400 SCLogError(
"eve.stats: stats are disabled globally: set stats.enabled to true. "
401 "See %s/configuration/suricata-yaml.html#stats",
413 "all decoder events correctly. See ticket #2225. Set a prefix in "
414 "stats.decoder-events-prefix.");
423 SCLogDebug(
"totals %s threads %s deltas %s", totals, threads, deltas);
428 SCLogError(
"Cannot disable both totals and threads in stats logging");
450 SCLogDebug(
"Preparing file context for stats submodule logger");
459 output_ctx->
data = stats_ctx;
460 output_ctx->
DeInit = OutputStatsLogDeinitSub;
462 result.
ctx = output_ctx;
470 "eve-log.stats", OutputStatsLogInitSub, JsonStatsLogger,
471 JsonStatsLogThreadInit, JsonStatsLogThreadDeinit, NULL);