From a781766836ddcb257d47b1005331c9799808809d Mon Sep 17 00:00:00 2001 From: Gleb Smirnoff Date: Wed, 27 Jan 2016 00:50:27 +0000 Subject: [PATCH] Grab value for LEAF_tcpCurrEstab from net.inet.tcp.stats instead of net.inet.tcp.pcblist, which is much cheaper. --- contrib/bsnmp/snmp_mibII/mibII_tcp.c | 34 ++++++++++++++++------------ 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/contrib/bsnmp/snmp_mibII/mibII_tcp.c b/contrib/bsnmp/snmp_mibII/mibII_tcp.c index a61052d2eb92..c798bae1fa17 100644 --- a/contrib/bsnmp/snmp_mibII/mibII_tcp.c +++ b/contrib/bsnmp/snmp_mibII/mibII_tcp.c @@ -45,10 +45,10 @@ struct tcp_index { }; static uint64_t tcp_tick; +static uint64_t tcp_stats_tick; static struct tcpstat tcpstat; static struct xinpgen *xinpgen; static size_t xinpgen_len; -static u_int tcp_count; static u_int tcp_total; static u_int oidnum; @@ -64,13 +64,9 @@ tcp_compare(const void *p1, const void *p2) } static int -fetch_tcp(void) +fetch_tcp_stats(void) { size_t len; - struct xinpgen *ptr; - struct xtcpcb *tp; - struct tcp_index *oid; - in_addr_t inaddr; len = sizeof(tcpstat); if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len, NULL, 0) == -1) { @@ -82,6 +78,20 @@ fetch_tcp(void) return (-1); } + tcp_stats_tick = get_ticks(); + + return (0); +} + +static int +fetch_tcp(void) +{ + size_t len; + struct xinpgen *ptr; + struct xtcpcb *tp; + struct tcp_index *oid; + in_addr_t inaddr; + len = 0; if (sysctlbyname("net.inet.tcp.pcblist", NULL, &len, NULL, 0) == -1) { syslog(LOG_ERR, "net.inet.tcp.pcblist: %m"); @@ -102,7 +112,6 @@ fetch_tcp(void) tcp_tick = get_ticks(); - tcp_count = 0; tcp_total = 0; for (ptr = (struct xinpgen *)(void *)((char *)xinpgen + xinpgen->xig_len); ptr->xig_len > sizeof(struct xinpgen); @@ -114,10 +123,6 @@ fetch_tcp(void) if (tp->xt_inp.inp_vflag & INP_IPV4) tcp_total++; - - if (tp->xt_tp.t_state == TCPS_ESTABLISHED || - tp->xt_tp.t_state == TCPS_CLOSE_WAIT) - tcp_count++; } if (oidnum < tcp_total) { @@ -184,8 +189,8 @@ op_tcp(struct snmp_context *ctx __unused, struct snmp_value *value, abort(); } - if (tcp_tick < this_tick) - if (fetch_tcp() == -1) + if (tcp_stats_tick < this_tick) + if (fetch_tcp_stats() == -1) return (SNMP_ERR_GENERR); switch (value->var.subs[sub - 1]) { @@ -226,7 +231,8 @@ op_tcp(struct snmp_context *ctx __unused, struct snmp_value *value, break; case LEAF_tcpCurrEstab: - value->v.uint32 = tcp_count; + value->v.uint32 = tcpstat.tcps_states[TCPS_ESTABLISHED] + + tcpstat.tcps_states[TCPS_CLOSE_WAIT]; break; case LEAF_tcpInSegs: