From 2251c67e560f293434fda7454b5a3d12e19ac06a Mon Sep 17 00:00:00 2001 From: Arne Welzel Date: Fri, 3 Mar 2023 10:48:50 +0100 Subject: [PATCH] get_dns_stats: Expose total cache size and cached text entries It wasn't possible from script land to determine the total size of the cache table held by the DNS_Mgr. Add the total and also also the TEXT entries count. --- scripts/base/init-bare.zeek | 2 ++ src/DNS_Mgr.cc | 1 + src/DNS_Mgr.h | 1 + src/stats.bif | 2 ++ testing/btest/Baseline/bifs.get_dns_stats/out | 2 ++ testing/btest/bifs/get_dns_stats.test | 8 ++++++++ 6 files changed, 16 insertions(+) create mode 100644 testing/btest/Baseline/bifs.get_dns_stats/out create mode 100644 testing/btest/bifs/get_dns_stats.test diff --git a/scripts/base/init-bare.zeek b/scripts/base/init-bare.zeek index cf5291d6cf..7f44475c59 100644 --- a/scripts/base/init-bare.zeek +++ b/scripts/base/init-bare.zeek @@ -757,6 +757,8 @@ type DNSStats: record { pending: count; ##< Current pending queries. cached_hosts: count; ##< Number of cached hosts. cached_addresses: count; ##< Number of cached addresses. + cached_texts: count; ##< Number of cached text entries. + cached_total: count; ##< Total number of cached entries. }; ## Statistics about number of gaps in TCP connections. diff --git a/src/DNS_Mgr.cc b/src/DNS_Mgr.cc index 36118c6bda..7093010421 100644 --- a/src/DNS_Mgr.cc +++ b/src/DNS_Mgr.cc @@ -1485,6 +1485,7 @@ void DNS_Mgr::GetStats(Stats* stats) stats->cached_hosts = 0; stats->cached_addresses = 0; stats->cached_texts = 0; + stats->cached_total = all_mappings.size(); for ( const auto& [key, mapping] : all_mappings ) { diff --git a/src/DNS_Mgr.h b/src/DNS_Mgr.h index 44e3a82464..97ca1c23f8 100644 --- a/src/DNS_Mgr.h +++ b/src/DNS_Mgr.h @@ -209,6 +209,7 @@ public: unsigned long cached_hosts; unsigned long cached_addresses; unsigned long cached_texts; + unsigned long cached_total; }; /** diff --git a/src/stats.bif b/src/stats.bif index 0cf113b921..3dc7a5d567 100644 --- a/src/stats.bif +++ b/src/stats.bif @@ -259,6 +259,8 @@ function get_dns_stats%(%): DNSStats r->Assign(n++, static_cast(dstats.pending)); r->Assign(n++, static_cast(dstats.cached_hosts)); r->Assign(n++, static_cast(dstats.cached_addresses)); + r->Assign(n++, static_cast(dstats.cached_texts)); + r->Assign(n++, static_cast(dstats.cached_total)); return r; %} diff --git a/testing/btest/Baseline/bifs.get_dns_stats/out b/testing/btest/Baseline/bifs.get_dns_stats/out new file mode 100644 index 0000000000..b9309e9cab --- /dev/null +++ b/testing/btest/Baseline/bifs.get_dns_stats/out @@ -0,0 +1,2 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +[requests=0, successful=0, failed=0, pending=0, cached_hosts=0, cached_addresses=0, cached_texts=0, cached_total=0] diff --git a/testing/btest/bifs/get_dns_stats.test b/testing/btest/bifs/get_dns_stats.test new file mode 100644 index 0000000000..994daef364 --- /dev/null +++ b/testing/btest/bifs/get_dns_stats.test @@ -0,0 +1,8 @@ +# @TEST-DOC: With FAKE_DNS mode caching is disabled, so testing is difficult, invoke the bif once at least. +# @TEST-EXEC: zeek -b %INPUT >out +# @TEST-EXEC: btest-diff out + +event zeek_init() + { + print get_dns_stats(); + }