简单网络管理协议(SNMP)

目前snmp定义了三个版本的网络管理协议:SNMP v1,SNMP v2,SNMP v3。SNMP v1,v2有很多共同的特征,SNMP v3 在先前的版本地基础上增加了安全和远程配置能力 。

1.snmp的安装

安装必备套件net-snmp net-snmp-devel net-snmp-utils

net-snmp:提供了一个入口,使得监控服务器可以通过snmp协议从这个入口与被监控机器通信

net-snmp-devel: 是为了使用net-snmp-config, net-snmp-utils是为了使用snmpwalk

net-snmp-libs:  提供了运行需要的库文件

net-snmp-utils: 提供了一套工具,可以利用snmp协议进行通信,snmpget、snmpwalk命令都在该包中

在centos/redhat平台下通过如下命令安装:

#  yum -y install net-snmp net-snmp-devel net-snmp-utils

查看版本

[root@dev032 snmp]# snmpd -v
NET-SNMP version:  5.7.2
Web:               http://www.net-snmp.org/
Email:             net-snmp-coders@lists.sourceforge.net

2.配置

配置文件目录:/etc/snmp 下有两个配置文件 snmpd.conf 和 snmptrap.conf

修改配置

#       sec.name  source          community
#com2sec notConfigUser  default       public
########################## 新定义一个用户myuser,并设置用户的密码为fistforward #################################################
#项目分类名|用户名    |   可访问的来源地址|社区(就是密码)
com2sec myuser          10.117.186.40      passed
com2sec myuser          127.0.0.1          public

##################################################  定义组配置 ################################################################
#       groupName      securityModel securityName
#group   notConfigGroup v2c           notConfigUser
#定义一个组,将我们的用户加入进这个组,同时指定改组可以使用的snmp的协议版本
#项目分类名|组名|协议版本|    添加的用户
group   mygroup v1           myuser
group   mygroup v2c          myuser

##########################################  定义一个view视图,设置视图的可看范围  ################################################
#       name           incl/excl     subtree         mask(optional)
view    systemview    included   .1.3.6.1.2.1.1
view    systemview    included   .1.3.6.1.2.1.25.1.1
#项目名分类|视图名称|包含/排除|范围
view      all       included   .1

######################################################  定义组可以看到的视图权限 #################################################
#项目分类名|组名|描述|sec.model|sec.level|prefix|可读读视图|可写的视图|notif
access    mygroup        ""    any     noauth    exact  all    all     all

添加项

  • view all included .1
  • access mygroup "" any noauth exact all all all 权限都设置为视图 all

重启服务,通过 snmp 查看系统名字命令

snmpwalk -v 2c -c public localhost sysName.0
或者
snmpwalk -v 1 -c public localhost sysName.0

添加自定义OID

首先我们需要找一个oid是否被系统暂用,比如.1.3.6.1.4.1.2021.5000

$ snmpwalk -v 2c -c public www.ttlsa.com .1.3.6.1.4.1.2021.5000
UCD-SNMP-MIB::ucdavis.5000 = No Such Object available on this agent at this OID

增加自定 SNMP OID 编写脚本

# cat /root/scripts/check_nginx.sh
#!/bin/bash
nginxNum=`/bin/ps aux | /bin/grep nginx | wc -l`
echo $nginxNum

修改配置

$ vim /etc/snmp/snmpd.conf
extend .1.3.6.1.4.1.2021.5000 check_nginx /root/scripts/check_nginx.sh // 增加这一行

重启服务再次检查是否被使用,可以有结果返回。

查看OID

snmptranslate -To | head -n 10

.1.3
.1.3.6
.1.3.6.1
...

添加自定义MIB

过于复杂,自行谷歌。

配置snmp v3认证

配置前,先使用 service snmpd stop 命令停止 snmpd 服务。

参数说明
  • -ro:用户读写权限,表示用户fx为只具有读权限
  • fx:用户名
  • -a MD5:认证方式,MD5|SHA散列方式
  • -A auth123456:设置认证密码,密码必须大于8个字符
  • -x DES:加密方式,这边支持DES、AES128、AES192、AES256两种
  • priv123456:加密口令,必须大于8位
  • noAuthNoPriv(不认证也不加密)

    authNoPriv(认证但是不加密)

  • 增加用户 fxa net-snmp-create-v3-user -ro -A auth123456 -a MD5 fxa

    验证: snmpwalk -v 3 -u fxa -a MD5 -A auth123456 -l authNoPriv 127.0.0.1 sysName.0

  • authPriv(既认证又加密)

    增加用户 fx net-snmp-create-v3-user -ro -A auth123456 -X priv123456 -a MD5 -x DES fx

    检查 if 前缀 snmpwalk -v3 -u fx -l authPriv -a MD5 -A auth123456 -X priv123456 127.0.0.1 if

snmptrapd

使用net-snmp提供的 snmptrap 等工具可以实现trap的发送和接收。以 authCommunity 开头的一行配置了 snmptrapd 的安全设置,表示可以接收 community 为 public 的 SNMP Trap,并且本进程可以有 log,net 和 execute 的权限。下面是具体做法。

sudo snmptrapd -d -f -Lo

上面命令中的选项表示:

-C : 表示不使用net-snmp默认路径下的配置文件 snmptrapd.conf;

-c : 指定snmptrapd.conf文件;

-d : 显示收到和发送的数据报,通过这个选项可以看到数据报文;

-f : 默认情况下,snmptrapd是在后台中运行的,加上这个选项,表示在前台运行;

-L : 指定日志记录在哪里,后面的o表示直接输出到屏幕上,如果是跟着f表示日志记录到指定的文件中;

可通过snmptrapd -h查看命令帮助了解该命令的使用。

  • 启动 snmptrapd 监听窗口(可以指定config文件的位置)

在前台运行,将log信息打印到stdout: $ snmptrapd -C -c ./snmptrapd.conf -f -Lo

也可以在后台运行,并将log信息打印到文件中: $ snmptrapd -C -c ./snmptrapd.conf -Lf /tmp/trapd.log

  • 通过snmptrap工具发送一个trap(目标地址是“127.0.0.1:162”)

    snmptrap的命令行格式如下:

snmptrap -v [2c|3] [COMMON OPTIONS] uptime trap-oid [OID TYPE VALUE]

snmptrap -v 2c -c public 127.0.0.1:162 "" .1.3.6.1.4.1.2021.251.1  sysLocation.0 s "test"
snmptrap -v 2c -c public 127.0.0.1:162 "12345678" .1.3.6.1.4.1.2021.251.1  sysLocation.0 s "test"
  • 查看 snmptrapd 的log信息,可以看到我们发送的

trap: 2014-01-14 17:08:13 localhost [UDP: [127.0.0.1]:59609->[127.0.0.1]]: DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (3081118) 8:33:31.18 SNMPv2-MIB::snmpTrapOID.0 = OID: UCD-SNMP-MIB::ucdStart SNMPv2-MIB::sysLocation.0 = STRING: test

SNMP v3 Trap

The command below takes the form of:

snmptrap -v <snmp_version> -e <engine_id> -u <security_username> -a <authentication_protocal> -A <authentication_protocal_pass_phrase> -x <privacy_protocol> -X <privacy_protocol_pass_phrase> <destination_host> <uptime> <OID_or_MIB> <object> <value_type> <value>

Using a MIB:

snmptrap -v 3 -e 0x090807060504030201 -u the_user_name -a SHA -A the_SHA_string -x AES -X the_AES_string localhost '' NET-SNMP-EXAMPLES-MIB::netSnmpExampleHeartbeatNotification netSnmpExampleHeartbeatRate i 123456

Shortening the MIB:

snmptrap -v 3 -e 0x090807060504030201 -u the_user_name -a SHA -A the_SHA_string -x AES -X the_AES_string localhost '' netSnmpExampleHeartbeatNotification netSnmpExampleHeartbeatRate i 123456

Using OID's instead of MIB:

snmptrap -v 3 -e 0x090807060504030201 -u the_user_name -a SHA -A the_SHA_string -x AES -X the_AES_string localhost '' 1.3.6.1.4.1.8072.2.3.0.1 1.3.6.1.4.1.8072.2.3.2.1 i 123456

The commands above required the following settings in /etc/snmp/snmptrapd.conf

disableAuthorization yes
traphandle default /usr/sbin/snmptthandler
createUser -e 0x090807060504030201 the_user_name SHA the_SHA_string AES the_AES_string
authUser log,execute,net the_user_name
上次更新时间: 2024/5/7 05:59:02