利用coro、ae::SNMP等实现coro-snmpget,依赖包为:
- use Coro;
- use AnyEvent;
- use AnyEvent::SNMP;
- use Net::SNMP;
Net::SNMP版本低会导致回调函数没有响应,建议安装:
more /usr/local/share/perl5/AnyEvent/SNMP.pm|grep -i version
our $VERSION = '6.0';
more /usr/local/lib64/perl5/Coro.pm | grep -i ver
our $VERSION = 6.511;
package Net::SNMP;
# $Id: SNMP.pm,v 6.1 2010/09/10 00:01:22 dtown Rel $
Benchmarks
- 单台设备进行snmpget没什么差别,这里snmp->varbindlist没有设置多OID,两者都是单MIB交互
[slview@SH_CHK-IP9-Y-1 coro]$ perl ./CoroSnmp.pl
1.1.1.1====================ifDescr=========================
The ifDescr (2) for host '1.1.1.1' is Null0.
The ifDescr (3) for host '1.1.1.1' is MgmtEth0/RSP0/CPU0/0.
The ifDescr (4) for host '1.1.1.1' is MgmtEth0/RSP0/CPU0/1.
The ifDescr (8) for host '1.1.1.1' is MgmtEth0/RSP1/CPU0/0.
The ifDescr (9) for host '1.1.1.1' is MgmtEth0/RSP1/CPU0/1.
...
...
The ifHCInOctets (124) for host '1.1.1.1' is 313116742456.
The ifHCInOctets (125) for host '1.1.1.1' is 653211862.
use 17.4078 sec
[slview@SH_CHK-IP9-Y-1 coro]$ perl ./singlesnmp.pl
1.1.1.1=============ifDescr=========================
The ifDescr (2) for host '1.1.1.1' is Null0.
The ifDescr (3) for host '1.1.1.1' is MgmtEth0/RSP0/CPU0/0.
The ifDescr (4) for host '1.1.1.1' is MgmtEth0/RSP0/CPU0/1.
The ifDescr (8) for host '1.1.1.1' is MgmtEth0/RSP1/CPU0/0.
The ifDescr (9) for host '1.1.1.1' is MgmtEth0/RSP1/CPU0/1.
...
...
The ifHCInOctets (124) for host '1.1.1.1' is 313117798348.
The ifHCInOctets (125) for host '1.1.1.1' is 653218164.
use 17.4182 sec
- 多台设备的性能差距非常大,内核态和用户态时间相似,实际执行时间相差近14倍,50台设备采集所有端口名称、流入流出字节,串行需要18分,coro并发1分15秒,可以充分利用异步事件响应+协程的优势
time nohup ./singlesnmp.pl 2>&1 &
use 1105.2051 sec
real 18m25.261s
user 0m24.943s
sys 0m0.870s
time nohup ./CoroSnmp.pl 2>&1
use 74.9815 sec
real 1m15.046s
user 0m25.167s
sys 0m0.607s