]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #3558: file_api: corrected the formatting of File Statistics output
authorBhargava Jandhyala (bjandhya) <bjandhya@cisco.com>
Fri, 26 Aug 2022 15:19:08 +0000 (15:19 +0000)
committerBhargava Jandhyala (bjandhya) <bjandhya@cisco.com>
Fri, 26 Aug 2022 15:19:08 +0000 (15:19 +0000)
Merge in SNORT/snort3 from ~UMUNNIKR/snort3:file_stats_dump to master

Squashed commit of the following:

commit 9855f3c445eefa957649c76a9ed4426fba042a43
Author: Unnikrishnan M <umunnikr@cisco.com>
Date:   Mon Aug 15 15:35:57 2022 +0530

    file_api: corrected the formatting of File Statistics output

src/file_api/file_stats.cc
src/framework/file_policy.cc

index 4209b92a4e2d30457d4893d93124d3992d6a03e2..5c1e88c9badf29fbde3cc84737c39224739324f2 100644 (file)
@@ -35,6 +35,7 @@
 #include "utils/util.h"
 
 #include "file_capture.h"
+#include "file_service.h"
 
 using namespace snort;
 
@@ -81,7 +82,9 @@ void file_stats_print()
 {
     uint64_t processed_total[2];
     uint64_t processed_data_total[2];
+    uint64_t processed_sig_total[2]{ 0, 0 };
     uint64_t check_total = 0;
+    uint64_t check_sig_total{ 0 };
 
     for (unsigned i = 0; i < FILE_ID_MAX; i++)
     {
@@ -92,97 +95,97 @@ void file_stats_print()
     if ( !check_total )
         return;
 
+    for (unsigned i = 0; i < FILE_ID_MAX; i++)
+    {
+        check_sig_total += file_totals.signatures_processed[i][0];
+        check_sig_total += file_totals.signatures_processed[i][1];
+    }
+
     LogLabel("File Statistics");
-    LogLabel("file type stats (files)");
+    LogLabel("file stats");
 
-    LogLabel("         Type              Download   Upload ");
+    char buff[128];
+    if (FileService::is_file_signature_enabled() && check_sig_total)
+        snprintf(buff, sizeof(buff), "%25.25s %-10s %-10s %-10s %-10s %-10s %-10s", "Type",
+            "TypeDn", "TypeUp", "SigDn", "SigUp", "BytesDn", "BytesUp");
+    else
+        snprintf(buff, sizeof(buff), "%25.25s %-10s %-10s %-10s %-10s", "Type", "TypeDn", "TypeUp",
+            "BytesDn", "BytesUp");
+    LogText(buff);
 
     processed_total[0] = 0;
     processed_total[1] = 0;
     processed_data_total[0] = 0;
     processed_data_total[1] = 0;
-
-    char buff[128];
-    for (unsigned i = 0; i < FILE_ID_MAX; i++)
-    {
-        std::string type_name = file_type_name(i);
-        if (type_name.length() &&
-            (file_totals.files_processed[i][0] || file_totals.files_processed[i][1] ))
-        {
-            snprintf(buff, sizeof(buff), "%12s(%3d)          " FMTu64("-10") " " FMTu64("-10") " ",
-                type_name.c_str(), i,
-                file_totals.files_processed[i][0],
-                file_totals.files_processed[i][1]);
-            LogText(buff);
-            processed_total[0]+= file_totals.files_processed[i][0];
-            processed_total[1]+= file_totals.files_processed[i][1];
-        }
-    }
-
-    snprintf(buff, sizeof(buff), "            Total          " FMTu64("-10") " " FMTu64("-10") " ",
-        processed_total[0], processed_total[1]);
-    LogText(buff);
-
-    LogLabel("file type stats (bytes)");
-
-    LogLabel("         Type              Download   Upload ");
+    processed_sig_total[0]  = 0;
+    processed_sig_total[1]  = 0;
 
     for (unsigned i = 0; i < FILE_ID_MAX; i++)
     {
-        std::string type_name = file_type_name(i);
-        if (type_name.length() &&
-            (file_totals.files_processed[i][0] || file_totals.files_processed[i][1] ))
+        std::string type_name  = file_type_name(i);
+        bool status = false;
+
+        if (FileService::is_file_signature_enabled() && check_sig_total)
+            status = ( file_totals.files_processed[i][0] ||
+                file_totals.files_processed[i][1] ||
+                file_totals.signatures_processed[i][0] ||
+                file_totals.signatures_processed[i][1] );
+        else
+            status = ( file_totals.files_processed[i][0] ||
+                file_totals.files_processed[i][1] );
+
+        if (type_name.length() && status)
         {
-            snprintf(buff, sizeof(buff), "%12s(%3d)          " FMTu64("-10") " " FMTu64("-10") " ",
-                type_name.c_str(), i,
-                file_totals.data_processed[i][0],
-                file_totals.data_processed[i][1]);
+            type_name.append("(");
+            type_name.append(std::to_string(i));
+            type_name.append(")");
+            if (FileService::is_file_signature_enabled() && check_sig_total)
+            {
+                snprintf(buff, sizeof(buff), "%25.25s " FMTu64("-10") " " FMTu64("-10") " "
+                    FMTu64("-10") " " FMTu64("-10") " " FMTu64("-10") " " FMTu64("-10"),
+                    type_name.c_str(), file_totals.files_processed[i][0],
+                    file_totals.files_processed[i][1],
+                    file_totals.signatures_processed[i][0],
+                    file_totals.signatures_processed[i][1],
+                    file_totals.data_processed[i][0],
+                    file_totals.data_processed[i][1]);
+            }
+            else
+            {
+                snprintf(buff, sizeof(buff), "%25.25s " FMTu64("-10") " " FMTu64("-10") " "
+                    FMTu64("-10") " " FMTu64("-10"),
+                    type_name.c_str(),
+                    file_totals.files_processed[i][0],
+                    file_totals.files_processed[i][1],
+                    file_totals.data_processed[i][0],
+                    file_totals.data_processed[i][1]);
+            }
             LogText(buff);
-
-            processed_data_total[0]+= file_totals.data_processed[i][0];
-            processed_data_total[1]+= file_totals.data_processed[i][1];
+            processed_total[0]      += file_totals.files_processed[i][0];
+            processed_total[1]      += file_totals.files_processed[i][1];
+            processed_data_total[0] += file_totals.data_processed[i][0];
+            processed_data_total[1] += file_totals.data_processed[i][1];
+            if (FileService::is_file_signature_enabled() && check_sig_total)
+            {
+                processed_sig_total[0]  += file_totals.signatures_processed[i][0];
+                processed_sig_total[1]  += file_totals.signatures_processed[i][1];
+            }
         }
     }
 
-    snprintf(buff, sizeof(buff), "            Total          " FMTu64("-10") " " FMTu64("-10") " ",
-        processed_data_total[0], processed_data_total[1]);
-    LogText(buff);
-
-    check_total = 0;
-
-    for (unsigned i = 0; i < FILE_ID_MAX; i++)
+    if (FileService::is_file_signature_enabled() && check_sig_total)
     {
-        check_total += file_totals.signatures_processed[i][0];
-        check_total += file_totals.signatures_processed[i][1];
+        snprintf(buff, sizeof(buff), "%25.25s " FMTu64("-10") " " FMTu64("-10") " "
+            FMTu64("-10") " " FMTu64("-10") " " FMTu64("-10") " " FMTu64("-10"),
+            "Total", processed_total[0], processed_total[1], processed_sig_total[0],
+            processed_sig_total[1], processed_data_total[0], processed_data_total[1]);
     }
-
-    if ( !check_total )
+    else
     {
-        return;
-    }
-
-    LogLabel("file signature stats");
-
-    LogLabel("         Type              Download   Upload ");
-
-    processed_total[0] = 0;
-    processed_total[1] = 0;
-    for (unsigned i = 0; i < FILE_ID_MAX; i++)
-    {
-        std::string type_name = file_type_name(i);
-        if (type_name.length() &&
-            (file_totals.signatures_processed[i][0] || file_totals.signatures_processed[i][1] ))
-        {
-            snprintf(buff, sizeof(buff), "%12s(%3d)          " FMTu64("-10") " " FMTu64("-10") " ",
-                type_name.c_str(), i,
-                file_totals.signatures_processed[i][0], file_totals.signatures_processed[i][1]);
-            LogText(buff);
-            processed_total[0]+= file_totals.signatures_processed[i][0];
-            processed_total[1]+= file_totals.signatures_processed[i][1];
-        }
+        snprintf(buff, sizeof(buff), "%25.25s " FMTu64("-10") " " FMTu64("-10") " "
+            FMTu64("-10") " " FMTu64("-10"),"Total", processed_total[0],
+            processed_total[1], processed_data_total[0], processed_data_total[1]);
     }
-    snprintf(buff, sizeof(buff), "            Total          " FMTu64("-10") " " FMTu64("-10") " ",
-        processed_total[0], processed_total[1]);
     LogText(buff);
 
 #if 0
index 717b16703a3625f0b5e4ef47a1f1d2644a1b9215..ae311f8edaefb62828b11f3c6414ff7e443de83e 100644 (file)
@@ -112,8 +112,11 @@ void FilePolicy::add_file_id(FileRule& rule)
 
 void FilePolicy::load()
 {
-    if (type_enabled)
+    if (type_enabled or signature_enabled)
+    {
+        type_enabled = true;
         FileService::enable_file_type();
+    }
 
     if (signature_enabled)
         FileService::enable_file_signature();