From f71f8b9ae070d90e2e39e8a0bc5d12a365952294 Mon Sep 17 00:00:00 2001 From: yaqiangz Date: Wed, 27 Dec 2023 03:59:18 +0000 Subject: [PATCH] [counter] Clear counter table when dhcpmon init --- src/dhcp_device.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/dhcp_device.cpp b/src/dhcp_device.cpp index 8c105f129..9796a14db 100644 --- a/src/dhcp_device.cpp +++ b/src/dhcp_device.cpp @@ -194,6 +194,25 @@ void update_vlan_mapping(std::shared_ptr db_conn) { } } + +/** + * @code clear_counter(std::shared_ptr state_db); + * + * @brief Clear all counter + * + * @param state_db state_db connector pointer + * + */ +void clear_counter(std::shared_ptr state_db) { + std::string match_pattern = DB_COUNTER_TABLE + std::string("*"); + auto keys = state_db->keys(match_pattern); + + for (auto &itr : keys) { + state_db->del(itr); + } +} + + /** update ethernet interface to port-channel map * PORTCHANNEL_MEMBER|PortChannel101|Ethernet112 */ @@ -265,7 +284,6 @@ void initialize_db_counters(std::string &ifname) { auto table_name = DB_COUNTER_TABLE + ifname; auto init_value = gen_counter_json_str(db_counter); - mStateDbPtr->del(table_name); mStateDbPtr->hset(table_name, "RX", init_value); mStateDbPtr->hset(table_name, "TX", init_value); } @@ -924,6 +942,7 @@ int dhcp_device_start_capture(size_t snaplen, struct event_base *base, in_addr_t init_recv_buffers(snaplen); + clear_counter(mStateDbPtr); update_vlan_mapping(mConfigDbPtr); update_portchannel_mapping(mConfigDbPtr); update_mgmt_mapping();