在原来部门做产品的时候,在网管上做了大量的工作,但都是基于电信窄带产品的MML命令开发网管接口,效率很低下,实在令我深恶痛绝。于是后来就规划了一个新版本,准备同SNMP网管对接,也定义了mib,试图通过trap发告警,但最终还是没实现。这几天在用nagios,发现其中监控网络流量的插件都是通过snmpwalk、snmpget等命令搞定的,就顺带看看snmp。
首先保证机器上要安装net-snmp <pre class=php name=code># rpm -qa|grep snmp net-snmp-5.3.0.1-25.25</pre>
被查询的机器上需要开启snmpd服务:
- 编辑/etc/snmp/snmpd.conf,suse 10上该文件默认是不能工作的,将文件替换成下面的内容即可。
<pre class=php name=code># First, map the community name (COMMUNITY) into a security name
(local and mynetwork, depending on where the request is coming
from):
sec.name source community
com2sec notConfigUser default public
####
Second, map the security names into group names:
sec.model sec.name
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
####
Third, create a view for us to let the groups have rights to:
incl/excl subtree mask
view all included .1 80
####
Finally, grant the 2 groups access to the 1 view with different
write permissions:
context sec.model sec.level match read write notif
access notConfigGroup “” any noauth exact all none none</pre>
- 启动snmpd
<pre class=php name=code>#service snmpd restart</pre>
- 配置snmpd自动运行
<pre class=php name=code># chkconfig -e snmpd snmpd 3</pre>
- 通过snmp查询所有网卡名字
<pre class=php name=code># snmpwalk -v 2c -c public localhost IF-MIB::ifDescr IF-MIB::ifDescr.1 = STRING: lo IF-MIB::ifDescr.2 = STRING: eth0</pre>
- 查询指定网卡状态
<pre class=php name=code># snmpwalk -v 2c -c public localhost IF-MIB::ifOperStatus.2 IF-MIB::ifOperStatus.2 = INTEGER: up(1)</pre>
- 查询网卡流量
<pre class=php name=code># snmpwalk -v 2c -c public localhost IF-MIB::ifHCOutOctets.2 //网卡出流量 IF-MIB::ifHCOutOctets.2 = Counter64: 3118479769
snmpwalk -v 2c -c public localhost IF-MIB::ifHCInOctets.2 //入流量
IF-MIB::ifHCInOctets.2 = Counter64: 2352396350</pre>