From: PRATEEK MOHAN PRABHU -X (pratepra - XORIANT CORPORATION at Cisco) Date: Fri, 15 Sep 2023 15:45:42 +0000 (+0000) Subject: Pull request #3976: main: reset_stats argument type improvement X-Git-Tag: 3.1.71.0~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b8409e6431da729bce6d7d7c71b3a5da4eea5310;p=thirdparty%2Fsnort3.git Pull request #3976: main: reset_stats argument type improvement 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) Date: Mon Aug 28 22:46:09 2023 +0530 main: reset_stats argument type improvement --- diff --git a/src/main.cc b/src/main.cc index 90b86d6fe..0cae62646 100644 --- a/src/main.cc +++ b/src/main.cc @@ -83,6 +83,17 @@ const struct timespec main_sleep = { 0, 1000000 }; // 0.001 sec static const char* prompt = "o\")~ "; +static const std::map 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(type)), ctrlcon); + if ((type = convert_counter_type(command)) != clear_counter_type_t::TYPE_INVALID) + main_broadcast_command(new ACResetStats(static_cast(type)), ctrlcon); + else + LogRespond(ctrlcon, "Available options to use: all, daq, module, appid, file_id, snort, ha\n"); return 0; } diff --git a/src/main.h b/src/main.h index 8b6ca5040..1fb99ec13 100644 --- a/src/main.h +++ b/src/main.h @@ -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); diff --git a/src/main/analyzer_command.h b/src/main/analyzer_command.h index cad3c5173..3e8f63744 100644 --- a/src/main/analyzer_command.h +++ b/src/main/analyzer_command.h @@ -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 diff --git a/src/main/snort_module.cc b/src/main/snort_module.cc index 89c5b8b01..7b154673d 100644 --- a/src/main/snort_module.cc +++ b/src/main/snort_module.cc @@ -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" }, diff --git a/src/managers/module_manager.cc b/src/managers/module_manager.cc index 5768038c0..2ef30a6cc 100644 --- a/src/managers/module_manager.cc +++ b/src/managers/module_manager.cc @@ -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 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 lock(stats_mutex); PacketManager::reset_stats();