49 #define MODULE_NAME "JsonStatsLog"
81 json_t *jdata = json_object();
89 json_object_set_new(jdata,
"last_reload", json_string(timebuf));
96 json_object_set_new(jdata,
"rules_loaded",
98 json_object_set_new(jdata,
"rules_failed",
100 json_object_set_new(jdata,
"rules_skipped", json_integer(sig_stat->
skipped_sigs_total));
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++) {
234 json_t *js_type = NULL;
244 js_type = OutputStats2Json(js_stats, st->
stats[u].
name);
246 if (js_type != NULL) {
247 json_object_set_new(js_type, stat_name, json_integer(st->
stats[u].
value));
251 char deltaname[strlen(stat_name) + strlen(delta_suffix) + 2];
252 snprintf(deltaname,
sizeof(deltaname),
"%s%s", stat_name, delta_suffix);
253 json_object_set_new(js_type, deltaname,
263 json_t *threads = json_object();
265 json_decref(js_stats);
269 for (x = 0; x < st->
ntstats; x++) {
271 const char *tm_name = NULL;
272 json_t *thread = NULL;
284 if (tm_name == NULL) {
288 thread = json_object();
290 json_decref(js_stats);
291 json_decref(threads);
299 json_t *js_type = NULL;
305 js_type = OutputStats2Json(thread, st->
tstats[u].
name);
308 if (js_type != NULL) {
309 json_object_set_new(js_type, stat_name, json_integer(st->
tstats[u].
value));
312 char deltaname[strlen(stat_name) + strlen(delta_suffix) + 2];
313 snprintf(deltaname,
sizeof(deltaname),
"%s%s", stat_name, delta_suffix);
314 json_object_set_new(js_type, deltaname,
319 if (tm_name != NULL) {
321 json_object_set_new(threads, tm_name, thread);
324 json_object_set_new(js_stats,
"threads", threads);
335 gettimeofday(&tval, NULL);
337 json_t *js = json_object();
342 json_object_set_new(js,
"timestamp", json_string(timebuf));
343 json_object_set_new(js,
"event_type", json_string(
"stats"));
346 if (js_stats == NULL) {
351 json_object_set_new(js,
"stats", js_stats);
354 MemBufferReset(aft->
buffer);
356 json_object_clear(js_stats);
357 json_object_del(js,
"stats");
358 json_object_clear(js);
364 static TmEcode JsonStatsLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
372 SCLogDebug(
"Error getting context for EveLogStats. \"initdata\" argument NULL");
377 if (aft->
buffer == NULL) {
393 if (aft->
buffer != NULL) {
416 static void OutputStatsLogDeinitSub(
OutputCtx *output_ctx)
429 SCLogError(
"eve.stats: stats are disabled globally: set stats.enabled to true. "
430 "See %s/configuration/suricata-yaml.html#stats",
442 "all decoder events correctly. See ticket #2225. Set a prefix in "
443 "stats.decoder-events-prefix.");
453 SCLogDebug(
"totals %s threads %s deltas %s", totals, threads, deltas);
458 SCLogError(
"Cannot disable both totals and threads in stats logging");
483 SCLogDebug(
"Preparing file context for stats submodule logger");
492 output_ctx->
data = stats_ctx;
493 output_ctx->
DeInit = OutputStatsLogDeinitSub;
495 result.
ctx = output_ctx;
503 OutputStatsLogInitSub, JsonStatsLogger, JsonStatsLogThreadInit,
504 JsonStatsLogThreadDeinit);