SNMP入门

来源:百度文库 编辑:神马文学网 时间:2024/04/27 14:20:58
☆ SNMP的历史
SNMP是简单网络管理协议的缩写。
上个世纪80年代中期,网络管理通常使用ping、traceroute、tcpdump以及类似的工
具。当时绝大多数网络规模很小,远没有后来变得那么复杂。1987年, Request For
Comments (RFC) 1024提出了"Simple Gateway Monitoring Protocol"(SGMP)。SGMP
定义了一种平台无关的标准,用于监视网关的状态。大约两年后,RFC 1067提出了
SNMP。
☆ SNMP术语和概念
ASN.1: 一个SNMP消息通过抽象语法记法1(Abstract Syntax Notation, version 1)
构造。ASN.1是一种形式语言。
BER  : 将ASN.1描述表示成octet strings的基本编码规则
MIB  : 管理信息库。下面是一个例子
sysUpTime OBJECT-TYPE
SYNTAX  TimeTicks
ACCESS  read-only
STATUS  mandatory
DESCRIPTION
"距最后一次系统重启动的时间"
OID  : 对象标识符是唯一的点分数字串,比如".1.3.6.1.2.1.4.3.0"
PDU  : 协议数据单元是SNMP消息中的数据区。
SMI  : 管理信息结构是一套描述SNMP如何访问信息的标准,它用ASN.1定义了MIB。
SMI要求每个数据项有name、syntax和encoding三部分。名字就是OID。语法
给出了数据类型,比如"integer"、"string of octets"等等。编码则指明为
了进行平台无关网络转输如何进行数据的序列化(RPC/XDR中存在类似概念)。
☆ SNMP基础
SNMP是种网络设备之间客户机/服务器模式的简单通信协议。路由器、交换机、打印
机、HUB等等都可以成为SNMP系统中的服务器方。而SNMP系统中的客户机方往往是单
独的一台计算机,轮询网络设备并记录它们所返回的数据。这里允许一台服务器多个
客户机的情形。
SNMP允许你用很少的网络带宽和内存收集很多有用的系统、网络数据。
SNMP提供了一种统一的、跨平台的设备管理办法。
☆ NET-SNMP
可以从http://net-snmp.sourceforge.net/获取NET-SNMP的RPM包以及源代码。下载
解压后
su -
cd ucd-snmp-4.2.3
./configure --prefix=/usr  <-- 缺省是/usr/local
make clean all
make install
☆ 收集数据
SNMP agent是SNMP系统中的的服务器方。两年前Internet上到处可见SNMP agent,现
在出于安全原因,防火墙配置得到加强,可能你只好在LAN内测试SNMP了。
snmpget public system.sysDescr.0
应该可以看到一个关于系统的简短描述,类似这样
system.sysDescr.0 = Sun SNMP Agent, Ultra-60
上述命令中的public可以理解为SNMP agent的口令,术语叫做"community string"。
许多网络设备、操作系统都用"public"做为缺省"community string",潜在带来安全
问题。应该修改这个缺省"community string"。
上述命令还可以写成
snmpget public .1.3.6.1.2.1.1.1.0
"system.sysDescr.0"只是".1.3.6.1.2.1.1.1.0"的另一种表述方式,最终还是要转
换成数字形式的OID(对象标识符)。类似FQDN和IP地址的关系。
DNS系统负责FQDN与IP之间的转换,SNMP通过管理信息库(MIB)完成这种转换。MIB是
一个文本文件,包含很多SNMP相关信息,其中包括OID转换信息。MIBs有公用的,也
有专用的。RFC 1213介绍了sysDescr和许多其它常见OIDs,比如sysUpTime(距最后一
次系统重启动的时间)和sysLocation(设备所在物理位置).
可以用如下命令检查MIBs
snmptranslate -Td -OS system.sysDescr.0
--------------------------------------------------------------------------
.1.3.6.1.2.1.1.1.0
sysDescr OBJECT-TYPE
-- FROM       SNMPv2-MIB, RFC1213-MIB
-- TEXTUAL CONVENTION DisplayString
SYNTAX        OCTET STRING (0..255)
DISPLAY-HINT  "255a"
MAX-ACCESS    read-only
STATUS        current
DESCRIPTION   "A textual description of the entity.  This value should
include the full name and version identification of the
system‘s hardware type, software operating-system, and
networking software."
::= { iso(1) org(3) dod(6) internet(1) mgmt(2) mib-2(1) system(1) sysDescr(1) 0 }
--------------------------------------------------------------------------
snmptranslate也可以完成OID到文本描述串的转换,比如
snmptranslate -Td -OS .1.3.6.1.2.1.1.1.0
在一台x86/FreeBSD 4.3-RELEASE上
snmpwalk localhost public interfaces.ifTable.ifEntry.ifDescr
snmpwalk localhost public .1.3.6.1.2.1.2.2.1.2
--------------------------------------------------------------------------
interfaces.ifTable.ifEntry.ifDescr.1 = fxp0
interfaces.ifTable.ifEntry.ifDescr.2 = fxp1
interfaces.ifTable.ifEntry.ifDescr.3 = fxp2
interfaces.ifTable.ifEntry.ifDescr.4 = lo0
--------------------------------------------------------------------------
这个显示顺序与/sbin/ifconfig -l看到的一致。
snmptranslate -Td -OS interfaces.ifTable.ifEntry.ifDescr.1
--------------------------------------------------------------------------
.1.3.6.1.2.1.2.2.1.2.1
ifDescr OBJECT-TYPE
-- FROM       IF-MIB, RFC1213-MIB
-- TEXTUAL CONVENTION DisplayString
SYNTAX        OCTET STRING (0..255)
DISPLAY-HINT  "255a"
MAX-ACCESS    read-only
STATUS        current
DESCRIPTION   "A textual string containing information about the
interface.  This string should include the name of the
manufacturer, the product name and the version of the
interface hardware/software."
::= { iso(1) org(3) dod(6) internet(1) mgmt(2) mib-2(1) interfaces(2) ifTable(2) ifEntry(1) ifDescr(2) 1 }
--------------------------------------------------------------------------
☆ SNMP编程
NET-SNMP提供了支持SNMP开发的C库,你可以开发自己的SNMP应用程序。然而从Perl、
Python或者Tcl这样的脚本语言开始SNMP编程更容易,它们都带有SNMP开发支持。
--------------------------------------------------------------------------
#! /usr/local/bin/perl
#
# File   : list_if.pl
# Test   : SPARC/Solaris 7
# Author : Laurent Girod  / Philip Morris Products S.A. / Neuchatel / Switzerland
#
use BER;
use SNMP_util "0.92";
# Uptime in absolute value
$BER::pretty_print_timeticks = 0;
my $host = shift @ARGV || &usage;
my $community = shift @ARGV || ‘public‘;
&usage if $#ARGV >= 0;
$host = "$community\@$host" if !( $host =~ /\@/ );
my $oid_name = ‘interfaces.ifTable.ifEntry.ifDescr‘;
print "\nCollecting [$oid_name]\n";
@ret = &snmpwalk( $host, $oid_name );
foreach $desc ( @ret )
{
( $oid, $desc ) = split( ‘:‘, $desc, 2 );
print "$oid = $desc\n";
}
sub usage
{
die "Usage: $0 hostname [community]\n";
}
--------------------------------------------------------------------------
注: 为了运行这个Perl程序,可能你需要下载安装SNMP_Session-0.92.tar.gz以获取
Perl扩展模块SNMP_util.pm。
这个脚本列举指定主机的网络接口,与/sbin/ifconfig -a看到的顺序一致
# ./list_if.pl public@localhost  <-- 从代码中可以看出这个用法
Collecting [interfaces.ifTable.ifEntry.ifDescr]
1 = lo0
2 = hme0
#
"community@host"是snmpwalk()第一形参要求的格式。
☆ 缺点
SNMP具有灵活、易用特点的同时,存在严重的安全缺陷。此外用SNMP处理现代面向对
象的或者基于XML的数据非常困难。最后,真正理解SNMP如何工作的系统管理员和程
序员实在太少了。
尽管存在诸多问题,SNMP的优势还是存在。今年早些时候,SNMPv3的RFC出台,修正
了最严重的安全缺陷。
☆ 参考资源
1) The NET-SNMP Project Home Page (source code is available from this site)
http://net-snmp.sourceforge.net/
2) MRTG: The Multi Router Traffic Grapher
http://www.mrtg.org/
3) Big Brother System and Network Monitor
http://maclawran.ca/bb-dnld
4) The Distributed Management Task Force, Inc. (DMTF)
http://www.dmtf.org/
5) Web-Based Enterprise Management (WBEM) Initiative
http://www.dmtf.org/standards/standard_wbem.php
6) SNMP4tPC -- SNMP RFC Information and Links
http://www.wtcs.org/snmp4tpc/snmp_rfc.htm
7) Essential SNMP
http://www.oreilly.com/catalog/esnmp
8) Index of the CPAN (Perl) SNMP Library Directory
http://www.cpan.org/modules/by-module/SNMP
9) RFC 1213 -- MIBs
http://www.cis.ohio-state.edu/cgi-bin/rfc/rfc1213.html
<完>