简介

Simple Network Management Protocol (SNMP) is a widely used protocol for monitoring the health and welfare of network equipment (eg. routers), computer equipment and even devices like UPSs. Net-SNMP is a suite of applications used to implement SNMP v1SNMP v2c and SNMP v3 using both IPv4 and IPv6. The suite includes:

  • Command-line applications to:
    • retrieve information from an SNMP-capable device, either using single requests (snmpgetsnmpgetnext), or multiple requests (snmpwalksnmptablesnmpdelta).
    • manipulate configuration information on an SNMP-capable device (snmpset).
    • retrieve a fixed collection of information from an SNMP-capable device (snmpdfsnmpnetstatsnmpstatus).
    • convert between numerical and textual forms of MIB OIDs, and display MIB content and structure (snmptranslate).
  • A graphical MIB browser (tkmib), using Tk/perl.
  • A daemon application for receiving SNMP notifications (snmptrapd). Selected notifications can be logged (to syslog, the NT Event Log, or a plain text file), forwarded to another SNMP management system, or passed to an external application.
  • An extensible agent for responding to SNMP queries for management information (snmpd). This includes built-in support for a wide range of MIB information modules, and can be extended using dynamically loaded modules, external scripts and commands, and both the SNMP multiplexing (SMUX) and Agent Extensibility (AgentX) protocols.
  • A library for developing new SNMP applications, with both C and perl APIs.

Net-SNMP is available for many Unix and Unix-like operating systems and also for Microsoft Windows. Note: Functionality can vary depending on the operating system. Please see the README files for information specific to your platform.

安装

安装SNMP服务

1
2
3
4
5
6
# 安装源文件
sudo yum install epel-release -y
sudo yum update -y

# 安装SNMP服务
sudo yum install -y net-snmp.x86_64 net-snmp-utils.x86_64

启动SNMP服务

1
2
3
4
# 启动snmp服务
sudo systemctl start snmpd
sudo systemctl status snmpd
sudo systemctl start snmpd

启动SNMPTRAP服务

修改snmptrapd配置文件,配置OID与钩子命令:

1
2
3
4
5
6
7
8
9
root@k160: ~ # cat /etc/snmp/snmptrapd.conf
# Example configuration file for snmptrapd
#
# No traps are handled by default, you must edit this file!
#
# authCommunity log,execute,net public
# traphandle SNMPv2-MIB::coldStart /usr/bin/bin/my_great_script cold
authCommunity log,execute,net public
traphandle 1.3.6.1.6.3.1.1.5.2 echo

运行服务:

1
2
# 启动snmptrap
snmptrapd -c /etc/snmp/snmptrapd.conf -f -Le -d

使用

命令行操作

1
2
# 发送snmptrap消息
snmptrap -v 2c -c public localhost "" .1.3.6.1.6.3.1.1.5.2 sysLocation.0 s "test"

Python开发

发送snmptrap消息demo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from pysnmp.hlapi import *
import json

iterator = sendNotification(
SnmpEngine(),
CommunityData('public', mpModel=0),
UdpTransportTarget(('111.111.16.160', 162)),
ContextData(),
'trap',
NotificationType(
ObjectIdentity('1.3.6.1.6.3.1.1.5.2')
).addVarBinds(
('1.3.6.1.6.3.1.1.4.3.0', '1.3.6.1.4.1.20408.4.1.1.2'),
('1.3.6.1.2.1.1.1.0', OctetString('Python Client Test.'))
).loadMibs(
'SNMPv2-MIB'
)
)

errorIndication, errorStatus, errorIndex, varBinds = next(iterator)

if errorIndication:
print(errorIndication)

参考文档