From: Vincent Bernat Date: Thu, 18 May 2023 14:05:36 +0000 (+0200) Subject: daemon: tentative to fix SNMP indexes depending on sysUpTime X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fheads%2Ffix%2Fmonotonic-clock;p=thirdparty%2Flldpd.git daemon: tentative to fix SNMP indexes depending on sysUpTime --- diff --git a/src/daemon/agent.c b/src/daemon/agent.c index fa10ebfe..75037e16 100644 --- a/src/daemon/agent.c +++ b/src/daemon/agent.c @@ -47,12 +47,12 @@ swap_bits(uint8_t n) return n; }; -extern struct timeval starttime; static long int lastchange(struct lldpd_port *port) { - if (port->p_lastchange > starttime.tv_sec) - return (port->p_lastchange - starttime.tv_sec) * 100; + long int agent_uptime = netsnmp_get_agent_uptime(); + long int age = (monotonic_now() - port->p_lastchange) * 100; + if (age < agent_uptime) return agent_uptime - age; return 0; } @@ -651,7 +651,7 @@ agent_h_scalars(struct variable *vp, oid *name, size_t *length, int exact, long_ret = port->p_lastchange; } } - if (long_ret) long_ret = (long_ret - starttime.tv_sec) * 100; + if (long_ret) long_ret = (netsnmp_get_agent_uptime() - long_ret * 100); return (u_char *)&long_ret; case LLDP_SNMP_STATS_INSERTS: /* We assume this is equal to valid frames received on all ports */ diff --git a/tests/check_snmp.c b/tests/check_snmp.c index e076f4f6..fcef8bf3 100644 --- a/tests/check_snmp.c +++ b/tests/check_snmp.c @@ -26,7 +26,6 @@ #include extern struct lldpd *agent_scfg; -extern struct timeval starttime; extern struct variable8 agent_lldp_vars[]; /* Our test config */ @@ -34,7 +33,6 @@ struct lldpd test_cfg = { .g_config = { .c_tx_interval = 30000, .c_tx_hold = 2, .c_ttl = 60, .c_smart = 0 } }; -struct timeval test_starttime = { .tv_sec = 100, .tv_usec = 0 }; /* First chassis */ struct lldpd_mgmt mgmt1 = { .m_family = LLDPD_AF_IPV4, @@ -110,7 +108,6 @@ struct lldpd_hardware hardware1 = { .h_drop_cnt = 1, .h_lport = { .p_chassis = &chassis1, - .p_lastchange = 200, .p_protocol = LLDPD_MODE_LLDP, .p_id_subtype = LLDP_PORTID_SUBTYPE_LLADDR, .p_id = "AAA012", @@ -193,7 +190,6 @@ struct lldpd_hardware hardware2 = { .h_drop_cnt = 1, .h_lport = { .p_chassis = &chassis1, - .p_lastchange = 50, .p_protocol = LLDPD_MODE_LLDP, .p_id_subtype = LLDP_PORTID_SUBTYPE_IFNAME, .p_id = "eth4", @@ -302,7 +298,6 @@ struct lldpd_pi pi888e01 = { /* First port of second chassis */ struct lldpd_port port2 = { .p_chassis = &chassis2, - .p_lastchange = 180, .p_protocol = LLDPD_MODE_LLDP, .p_id_subtype = LLDP_PORTID_SUBTYPE_IFALIAS, .p_id = "Giga1/7", @@ -313,7 +308,16 @@ struct lldpd_port port2 = { void snmp_config() { - starttime = test_starttime; + time_t now = monotonic_now(); + struct timeval starttime = { + .tv_sec = time(NULL) - 100, + .tv_usec = 0, + }; + netsnmp_set_agent_starttime(&starttime); + hardware1.h_lport.p_lastchange = now - 200; + hardware2.h_lport.p_lastchange = now - 50; + port2.p_lastchange = now - 80; + agent_scfg = &test_cfg; TAILQ_INIT(&test_cfg.g_chassis); TAILQ_INIT(&chassis1.c_mgmt);