From: Rishabh Choudhary (rishacho) Date: Tue, 4 Jun 2024 09:18:01 +0000 (+0000) Subject: Pull request #4331: main: add CLI command to show snort cpu percentage X-Git-Tag: 3.3.0.0~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7abf6d790d961b37bdc658bbab01f191d773c728;p=thirdparty%2Fsnort3.git Pull request #4331: main: add CLI command to show snort cpu percentage Merge in SNORT/snort3 from ~RISHACHO/snort3:snort_cpu_usage to master Squashed commit of the following: commit 4c09c864dc8627b02231748978457c87920b86cb Author: Rishabh Choudhary Date: Wed May 8 23:07:25 2024 +0530 main: add CLI command to show snort cpu percentage --- diff --git a/src/main.cc b/src/main.cc index 229235fd5..929d28ec7 100644 --- a/src/main.cc +++ b/src/main.cc @@ -810,6 +810,14 @@ int main_help(lua_State* L) return 0; } +int show_snort_cpu(lua_State* L) +{ + ControlConn* ctrlconn = ControlConn::query_from_lua(L); + send_response(ctrlconn, "Id \tPid \t30sec \t2min \t5min\n\n"); + main_broadcast_command(new ACShowSnortCPU(ctrlconn), ctrlconn); + return 0; +} + //------------------------------------------------------------------------- // housekeeping foo //------------------------------------------------------------------------- diff --git a/src/main.h b/src/main.h index fe2de2870..6e4dd58f0 100644 --- a/src/main.h +++ b/src/main.h @@ -44,6 +44,7 @@ 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); +int show_snort_cpu(lua_State* = nullptr); #ifdef SHELL int main_dump_plugins(lua_State* = nullptr); diff --git a/src/main/analyzer_command.cc b/src/main/analyzer_command.cc index 8e5dc8023..77a6952cb 100644 --- a/src/main/analyzer_command.cc +++ b/src/main/analyzer_command.cc @@ -32,12 +32,14 @@ #include "protocols/packet_manager.h" #include "target_based/host_attributes.h" #include "utils/stats.h" +#include "packet_io/sfdaq_instance.h" #include "analyzer.h" #include "reload_tracker.h" #include "reload_tuner.h" #include "snort.h" #include "snort_config.h" +#include "thread_config.h" #include "swapper.h" using namespace snort; @@ -269,3 +271,57 @@ SFDAQInstance* AnalyzerCommand::get_daq_instance(Analyzer& analyzer) { return analyzer.get_daq_instance(); } + +ACShowSnortCPU::~ACShowSnortCPU() +{ + if (DAQ_SUCCESS == status) + { + LogRespond(ctrlcon, "\nSummary \t%.1f%% \t%.1f%% \t%.1f%%\n", + cpu_usage_30s/instance_num, + cpu_usage_120s/instance_num, + cpu_usage_300s/instance_num); + } +} + +bool ACShowSnortCPU::execute(Analyzer& analyzer, void**) +{ + DIOCTL_GetCpuProfileData get_data = {}; + do + { + std::lock_guard lock(cpu_usage_mutex); + if (DAQ_SUCCESS != status) + break; + + SFDAQInstance* instance = get_daq_instance(analyzer); + ThreadConfig *thread_config = SnortConfig::get_conf()->thread_config; + int tid = thread_config->get_instance_tid(get_instance_id()); + + status = instance->ioctl( + (DAQ_IoctlCmd)DIOCTL_GET_CPU_PROFILE_DATA, + (void *)(&get_data), + sizeof(DIOCTL_GetCpuProfileData)); + + if (DAQ_SUCCESS != status) + { + LogRespond(ctrlcon, "Fetching profile data failed from DAQ instance\n"); + break; + } + + // Print CPU usage + LogRespond(ctrlcon, "%-3d \t%-6d \t%.1f%% \t%.1f%% \t%.1f%%\n", + instance_num, + tid, + get_data.cpu_usage_percent_30s, + get_data.cpu_usage_percent_120s, + get_data.cpu_usage_percent_300s); + + // Add CPU usage data + cpu_usage_30s += get_data.cpu_usage_percent_30s; + cpu_usage_120s += get_data.cpu_usage_percent_120s; + cpu_usage_300s += get_data.cpu_usage_percent_300s; + instance_num++; + + } while (0); + + return true; +} diff --git a/src/main/analyzer_command.h b/src/main/analyzer_command.h index 40f4317d6..b54438d03 100644 --- a/src/main/analyzer_command.h +++ b/src/main/analyzer_command.h @@ -20,8 +20,11 @@ #ifndef ANALYZER_COMMANDS_H #define ANALYZER_COMMANDS_H +#include + #include #include +#include #include "main/snort_types.h" @@ -204,6 +207,24 @@ private: std::vector& handlers; }; +class ACShowSnortCPU : public snort::AnalyzerCommand +{ +public: + explicit ACShowSnortCPU(ControlConn* conn) : AnalyzerCommand(conn) + { } + bool execute(Analyzer&, void**) override; + const char* stringify() override { return "SHOW_SNORT_CPU"; } + ~ACShowSnortCPU() override; + +private: + int status = DAQ_SUCCESS; + float cpu_usage_30s = 0.0; + float cpu_usage_120s = 0.0; + float cpu_usage_300s = 0.0; + int instance_num = 0; + std::mutex cpu_usage_mutex; +}; + namespace snort { // from main.cc diff --git a/src/main/snort_module.cc b/src/main/snort_module.cc index 1d9032b0b..3479104b4 100644 --- a/src/main/snort_module.cc +++ b/src/main/snort_module.cc @@ -144,6 +144,7 @@ static const Command snort_cmds[] = { "reload_hosts", main_reload_hosts, s_reload, "load a new hosts table" }, { "log_command", main_log_command,main_log_command_param, "enable or disable command logging"}, { "show_config_generation", main_show_config_generation, nullptr, "show loaded configuration ID"}, + { "show_snort_cpu", show_snort_cpu, nullptr, "show snort cpu usage"}, // FIXIT-M rewrite trough to permit updates on the fly //{ "process", main_process, nullptr, "process given pcap" },