]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4331: main: add CLI command to show snort cpu percentage
authorRishabh Choudhary (rishacho) <rishacho@cisco.com>
Tue, 4 Jun 2024 09:18:01 +0000 (09:18 +0000)
committerShanmugam S (shanms) <shanms@cisco.com>
Tue, 4 Jun 2024 09:18:01 +0000 (09:18 +0000)
Merge in SNORT/snort3 from ~RISHACHO/snort3:snort_cpu_usage to master

Squashed commit of the following:

commit 4c09c864dc8627b02231748978457c87920b86cb
Author: Rishabh Choudhary <rishacho@cisco.com>
Date:   Wed May 8 23:07:25 2024 +0530

    main: add CLI command to show snort cpu percentage

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

index 229235fd51428658e7341fffa1aed35a69c2f9cf..929d28ec791c5eccc32bfbdbd8959e6c19d28759 100644 (file)
@@ -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
 //-------------------------------------------------------------------------
index fe2de28702783bde719fbcf082796918e9bf43c6..6e4dd58f06471371e446feaa9a5c75b27ed881df 100644 (file)
@@ -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);
index 8e5dc8023518c5054599d804b564ff459a3ee172..77a6952cb82a5214e8688685b7516935caf1bae0 100644 (file)
 #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<std::mutex> 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;
+}
index 40f4317d6989ae4b7434863168111615239da23d..b54438d030cd0294b3d6ac4495501b0a7874829b 100644 (file)
 #ifndef ANALYZER_COMMANDS_H
 #define ANALYZER_COMMANDS_H
 
+#include <daq_common.h>
+
 #include <cstdarg>
 #include <vector>
+#include <mutex>
 
 #include "main/snort_types.h"
 
@@ -204,6 +207,24 @@ private:
     std::vector<snort::ScratchAllocator*>& 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
index 1d9032b0b1fdf6f4f939993eaafa352018a3a830..3479104b44d48f034c021adc4b342058277af0da 100644 (file)
@@ -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" },