]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #3976: main: reset_stats argument type improvement
authorPRATEEK MOHAN PRABHU -X (pratepra - XORIANT CORPORATION at Cisco) <pratepra@cisco.com>
Fri, 15 Sep 2023 15:45:42 +0000 (15:45 +0000)
committerPriyanka Bangalore Gurudev (prbg) <prbg@cisco.com>
Fri, 15 Sep 2023 15:45:42 +0000 (15:45 +0000)
Merge in SNORT/snort3 from ~PRATEPRA/snort3:reset_stats_improve to master

Squashed commit of the following:

commit bf907f9b6fbfef61c5c9363fd67971d793d36de9
Author: PRATEEK MOHAN PRABHU -X (pratepra - XORIANT CORPORATION at Cisco) <pratepra@cisco.com>
Date:   Mon Aug 28 22:46:09 2023 +0530

    main: reset_stats argument type improvement

src/main.cc
src/main.h
src/main/analyzer_command.h
src/main/snort_module.cc
src/managers/module_manager.cc

index 90b86d6fe66d4444a86be84074e553b419943732..0cae62646319b79779f66d9c8e1163185355d924 100644 (file)
@@ -83,6 +83,17 @@ const struct timespec main_sleep = { 0, 1000000 }; // 0.001 sec
 
 static const char* prompt = "o\")~ ";
 
+static const std::map<std::string, clear_counter_type_t> counter_name_to_id =
+{
+       {"daq", clear_counter_type_t::TYPE_DAQ},
+       {"module", clear_counter_type_t::TYPE_MODULE},
+       {"appid", clear_counter_type_t::TYPE_APPID},
+       {"file_id", clear_counter_type_t::TYPE_FILE_ID},
+       {"snort", clear_counter_type_t::TYPE_SNORT},
+       {"ha", clear_counter_type_t::TYPE_HA},
+       {"all", clear_counter_type_t::TYPE_ALL}
+};
+
 const char* get_prompt()
 { return prompt; }
 static bool use_shell(const SnortConfig* sc)
@@ -353,12 +364,29 @@ int main_dump_heap_stats(lua_State* L)
     return 0;
 }
 
+int convert_counter_type(const char* type)
+{
+       auto it = counter_name_to_id.find(type);
+       if ( it != counter_name_to_id.end() )
+               return it->second;
+       else
+               return clear_counter_type_t::TYPE_INVALID;
+}
+
 int main_reset_stats(lua_State* L)
 {
     ControlConn* ctrlcon = ControlConn::query_from_lua(L);
-    int type = luaL_optint(L, 1, 0);
+    const char* command;
+    int type;
+    if ( lua_gettop(L) == 0 )
+       command = "all";
+    else
+       command = luaL_checkstring(L, 1);
     ctrlcon->respond("== clearing stats\n");
-    main_broadcast_command(new ACResetStats(static_cast<clear_counter_type_t>(type)), ctrlcon);
+    if ((type = convert_counter_type(command)) != clear_counter_type_t::TYPE_INVALID)
+       main_broadcast_command(new ACResetStats(static_cast<clear_counter_type_t>(type)), ctrlcon);
+    else
+       LogRespond(ctrlcon, "Available options to use: all, daq, module, appid, file_id, snort, ha\n");
     return 0;
 }
 
index 8b6ca5040587844758c1fc18a61040cf632e2b8b..1fb99ec13fb62ac7165f6656692e7d8af9434cfa 100644 (file)
@@ -43,6 +43,7 @@ int main_pause(lua_State* = nullptr);
 int main_resume(lua_State* = nullptr);
 int main_quit(lua_State* = nullptr);
 int main_help(lua_State* = nullptr);
+int convert_counter_type(const char* type);
 
 #ifdef SHELL
 int main_dump_plugins(lua_State* = nullptr);
index cad3c51735d8a9f03532baa0ae8e4a0153f96bb3..3e8f63744605ad6f89f56fa4c97d3eedf59c0a0d 100644 (file)
@@ -74,13 +74,14 @@ public:
 
 typedef enum clear_counter_type
 {
-    TYPE_UNKNOWN=-1,
+    TYPE_INVALID=-1,
     TYPE_DAQ=0,
     TYPE_MODULE,
     TYPE_APPID,
     TYPE_FILE_ID,
     TYPE_SNORT,
-    TYPE_HA
+    TYPE_HA,
+       TYPE_ALL
 } clear_counter_type_t;
 
 // FIXIT-M Will replace this vector with an unordered map of
index 89c5b8b01b630eebdcb2110c7c34866dc2da9c63..7b154673dcea3423467a5177c168713c4d07a314 100644 (file)
@@ -118,6 +118,12 @@ static const Parameter main_log_command_param[] =
     { nullptr, Parameter::PT_MAX, nullptr, nullptr, nullptr }
 };
 
+static const Parameter reset_stat_param[] =
+{
+       { "type", Parameter::PT_STRING, nullptr, nullptr, "possible type can be: daq|module|appid|file_id|snort|ha|all." },
+       { nullptr, Parameter::PT_MAX, nullptr, nullptr, nullptr }
+};
+
 static const Command snort_cmds[] =
 {
     { "set_watchdog_params", main_set_watchdog_params, s_watchdog, "set watchdog parameters" },
@@ -128,7 +134,8 @@ static const Command snort_cmds[] =
 
     { "dump_stats", main_dump_stats, nullptr, "show summary statistics" },
     { "dump_heap_stats", main_dump_heap_stats, nullptr, "show heap statistics" },
-    { "reset_stats", main_reset_stats, nullptr, "clear summary statistics" },
+    { "reset_stats", main_reset_stats, reset_stat_param, "clear summary statistics. "
+      "Type can be: daq|module|appid|file_id|snort|ha|all. reset_stats() without a parameter clears all statistics."},
     { "rotate_stats", main_rotate_stats, nullptr, "roll perfmonitor log files" },
     { "reload_config", main_reload_config, s_reload_w_path, "load new configuration" },
     { "reload_policy", main_reload_policy, s_reload, "reload part or all of the default policy" },
index 5768038c06e75a2ac7205e0a01bb7fd9171416f0..2ef30a6cc88ce8c755acc6613254a493af229e4d 100644 (file)
@@ -1504,7 +1504,7 @@ void ModuleManager::clear_global_active_counters()
 
 void ModuleManager::reset_stats(clear_counter_type_t type)
 {
-    if ( type != TYPE_MODULE and type != TYPE_UNKNOWN )
+    if ( type != TYPE_MODULE and type != TYPE_ALL )
     {
         ModHook* mh = get_hook(clear_counter_type_string_map[type]);
         if ( mh and mh->mod )
@@ -1531,14 +1531,14 @@ void ModuleManager::reset_stats(clear_counter_type_t type)
                 }
             }
 
-            if ( type == TYPE_UNKNOWN or !ignore )
+            if ( type == TYPE_ALL or !ignore )
             {
                 lock_guard<mutex> lock(stats_mutex);
                 mh->mod->reset_stats();
             }
         }
     }
-    if ( type == TYPE_DAQ or type == TYPE_UNKNOWN )
+    if ( type == TYPE_DAQ or type == TYPE_ALL )
     {
         lock_guard<mutex> lock(stats_mutex);
         PacketManager::reset_stats();