Chinaunix首页 | 论坛 | 博客
  • 博客访问: 244368
  • 博文数量: 91
  • 博客积分: 2510
  • 博客等级: 少校
  • 技术积分: 1100
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-15 14:35
文章分类

全部博文(91)

文章存档

2008年(91)

我的朋友

分类: 系统运维

2008-04-06 20:07:54

使用SNMPRFC1213-mib定义进行流量分析

 使用SNMPRFC1213-mib定义进行流量分析

 

  使用snmp管理网络设备,*unix下常用net-snmp的snmpwalk,snmpget等,要得到网络的相关信息,可通过提取RFC1213-mib的定义值得到,例如:要取得远程主机的团体字为"abcd",IP为172.30.1.8的网络端口流入(IN)的数据流量,可以使用如下命令:

           snmpwalk -v 2c -c abcd 172.30.1.8 RFC1213-MIB::ifInOctets

 

返回各端口信息如下:

  IF-MIB::ifInOctets.112 = counter32:165070862

    IF-MIB::ifInOctets是 rfc1213的定义端口流入数据量

    112是查询网络设备的1模块插槽12端口

    counter32后的数值就是该端口的流量,165070862 bits,在*unix下可以通过shell命令取得这两个值

 

# 首先取得 12 接口的 ifIndex

index=$(snmpwalk -v 2c -c abcd -IR 172.30.1.8 RFC1213-MIB::ifDescr |grep IF-MIB::ifInOctets.112 |cut -d '=' -f 1|cut -d '.' -f 2)

 

# 再通过 snmp 协议取得 ififInOctets 和 ifOutOctets 的值

# 也可在 /etc/snmp.conf 中配置了 defVersion 和 defCommunity ,这样 snmpget 命令不用指定这两个参数

 

eth12_in=$(snmpget -v 2c -c abcd -IR -Os 172.30.1.8 ifInOctets.${index}|cut -d ':' -f 2|tr -d '[:blank:]')

eth12_out=$(snmpget -v 2c -c abcd -IR -Os 172.30.1.8 ifOutOctets.${index}|cut -d ':' -f 2 |tr -d '[:blank:]')

echo $eth12_in

echo $eth12_out

 

 

snmp--一般端口流量分析

 

针对普通网络设备的端口,MIB的相关定义是Interface组,主要管理如下信息:

ifIndex            端口索引号

ifDescr            端口描述

ifType            端口类型

ifMtu            最大传输包字节数

ifSpeed            端口速度

ifPhysAddress        物理地址

ifOperStatus        操作状态

ifLastChange        上次状态更新时间

*ifInOctets        输入字节数

*ifInUcastPkts      输入非广播包数

*ifInNUcastPkts      输入广播包数

*ifInDiscards        输入包丢弃数

*ifInErrors        输入包错误数

*ifInUnknownProtos    输入未知协议包数

*ifOutOctets        输出字节数

*ifOutUcastPkts      输出非广播包数

*ifOutNUcastPkts      输出广播包数

*ifOutDiscards      输出包丢弃数

*ifOutErrors        输出包错误数

ifOutQLen          输出队长

其中,*号标识的是与网络流量有关的信息。

 

例如看看网络接口:

输入:

#snmpwalk -v 1 222.90.47.169 -c public ifIndex

输出:

IF-MIB::ifIndex.1 = INTEGER: 1

IF-MIB::ifIndex.2 = INTEGER: 2

IF-MIB::ifIndex.3 = INTEGER: 3

表示有三个网络接口

网络接口明成:

[root@localhost snmp]# snmpwalk -v 1 222.90.47.169 -c public ifDescr

IF-MIB::ifDescr.1 = STRING: lo

IF-MIB::ifDescr.2 = STRING: eth0

IF-MIB::ifDescr.3 = STRING: ppp0

表示;三个接口分别为

1 本地回路

2 以太网卡

3 ADSL连接

 

取本地网卡的数据流量也可以

 

#!/bin/bash

 

# 首先取得 eth0 接口的 ifIndex

index=$(snmpwalk -IR localhost RFC1213-MIB::ifDescr |grep eth0|cut -d '=' -f 1|cut -d '.' -f 2)

 

# 再通过 snmp 协议取得 ififInOctets 和 ifOutOctets 的值

# 由于在 /etc/snmp.conf 中配置了 defVersion 和 defCommunity ,所以 snmpget 命令不用指定这两个参数

 

eth0_in=$(snmpget -IR -Os localhost ifInOctets.${index}|cut -d ':' -f 2|tr -d '[:blank:]')

eth0_out=$(snmpget -IR -Os localhost ifOutOctets.${index}|cut -d ':' -f 2 |tr -d '[:blank:]')

echo $eth0_in

echo $eth0_out

  #!/bin/bash

# 首先取得 eth0 接口的 ifIndex

 

index=$(snmpwalk -IR localhost RFC1213-MIB::ifDescr |grep lo|cut -d '=' -f 1|cut -d '.' -f 2)

lo_in=$(snmpget -IR -Os localhost ifInOctets.${index}|cut -d ':' -f 2|tr -d '[:blank:]')

lo_out=$(snmpget -IR -Os localhost ifOutOctets.${index}|cut -d ':' -f 2 |tr -d '[:blank:]')

echo $lo_in

 

echo $lo_out

 

 

阅读(2647) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~