This article describes the behavior of enabling stats-cache-lifetime option from SNMP configuration. It explains why the result of SNMP queries does not get updated for a certain time from OID.
stats-cache-lifetime
While doing SNMP query for OID, the returned value might not get updated for a certain period.
For example, use SNMP to get interface ingress unicast input byte count. It shows the value is not changed for some time though there are real time traffic.
snmp { stats-cache-lifetime 20; community test { authorization read-only; clients { 10.85.180.92/32; } }
Run the following script on the SNMP server. It will send queries every 5 seconds to get interface (which snmp index is 708) ingress Unicast byte count:
while true; do date; snmpget -v2c -c test 10.85.172.27 1.3.6.1.2.1.2.2.1.11.708; sleep 5; done
Results retrieved from the target device:
[root@localhost ~]# sh snmpget.sh Thu Aug 5 15:04:00 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 2911777945 Thu Aug 5 15:04:05 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 2911777945 Thu Aug 5 15:04:10 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 2911777945 Thu Aug 5 15:04:15 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 2911777945 Thu Aug 5 15:04:20 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 2947538499 Thu Aug 5 15:04:25 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 2947538499 Thu Aug 5 15:04:30 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 2947538499 Thu Aug 5 15:04:35 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 2947538499
Enabling stats-cache-lifetime option :
stats-cache-lifetime option
Junos OS provides an option to configure the length of time (in seconds) that the interface stats is cached. If the NMS queries for the same interface again within the cache time, the same data is returned. If the NMS queries after the cache time, the cache is no longer valid and fresh data is fetched from the lower layers and the cache timestamp is updated. The default value for stats-cache-lifetime option is 5 seconds. This can be tuned as per the polling frequency. Because stats-cache-lifetime is configured as 20 seconds, there is no update for the polling OID during the configured period. Since the poller sends queries every 5 seconds, the same query result is seen 4 times from the above example.
There are two ways to consider whether an updated value needs to be retrieved from the kernel from every SNMP query:
Adjust the stats-cache-lifetime and polling interval to make sure the polling interval value is no less than the stats-cache-lifetime.
stats-cache-lifetime.
For example, modify the polling interval to 30 seconds from the script in the SNMP server side, but keep the stats-cache-lifetime configuration as 20 without change.
while true; do date; snmpget -v2c -c test 10.85.172.27 1.3.6.1.2.1.2.2.1.11.708; sleep 30; done
The following output shows the value is updated every time from each SNMP query:
[root@localhost ~]# sh snmpget.sh Thu Aug 5 15:30:54 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 1488250377 Thu Aug 5 15:31:24 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 1541845712 Thu Aug 5 15:31:54 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 1595415927 Thu Aug 5 15:32:24 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 1649006789 Thu Aug 5 15:32:54 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 1702629916 Thu Aug 5 15:33:24 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 1756252443 Thu Aug 5 15:33:54 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 1809855671 Thu Aug 5 15:34:24 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 1863440112 Thu Aug 5 15:34:54 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 1917046451 Thu Aug 5 15:35:24 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 1970643345 Thu Aug 5 15:35:54 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 2024274768 Thu Aug 5 15:36:24 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 2077904802 Thu Aug 5 15:36:54 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 2131493586
Completely disable caching in mib2d by setting stats-cache-lifetime with zero and keep the polling interval as 5 seconds.
# set snmp stats-cache-lifetime 0
[root@localhost ~]# sh snmpget.sh Thu Aug 5 15:50:37 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3594877215 Thu Aug 5 15:50:42 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3603819112 Thu Aug 5 15:50:47 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3612739986 Thu Aug 5 15:50:52 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3621688060 Thu Aug 5 15:50:57 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3630680026 Thu Aug 5 15:51:02 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3639590514 Thu Aug 5 15:51:07 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3648533124 Thu Aug 5 15:51:12 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3657442836 Thu Aug 5 15:51:17 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3668161876 Thu Aug 5 15:51:22 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3677107158 Thu Aug 5 15:51:27 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3686044631 Thu Aug 5 15:51:32 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3694985254 Thu Aug 5 15:51:37 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3703925890 Thu Aug 5 15:51:42 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3712849523 Thu Aug 5 15:51:47 PDT 2021 IF-MIB::ifInUcastPkts.708 = Counter32: 3721758306