先看一下系統環境
o@o-pc:~/work/_snmp/net-snmp-5.7.3$ uname -a Linux o-pc 3.16.0-30-generic #40~14.04.1-Ubuntu SMP Thu Jan 15 17:43:14 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
1.下載net-snmp-5.7.3源碼包
net snmp的源碼包可以到 net-snmp官網上去下載。也可以到sourcefroce上去下載。
net-snmp-5.4.4版下載
o@o-pc:~/work/_snmp$ wget http://downloads.sourceforge.net/project/net-snmp/net-snmp/5.4.4/net-snmp-5.4.4.tar.gz
net-snmp-5.7.3版下載
o@o-pc:~/work/_snmp$ wget http://downloads.sourceforge.net/project/net-snmp/net-snmp/5.7.3/net-snmp-5.7.3.tar.gz
2.解壓net-snmp源碼包
o@o-pc:~/work/_snmp$ tar -xzvf net-snmp-5.7.3.tar.gz
3.編譯安裝配置
進入解壓的 net-snmp-5.7.3
文件夾。可以看到configure
文件,現在就使用這個文件來自動配置。
當然,自動配置也可以定製一些內容,比如安裝路徑等。
./configure參數 | 含義 |
---|---|
--prefix=/usr/local/net-snmp | net snmp的安裝路徑 |
--enable-mfd-rewrites | 允許新的mfd重寫可用的mid模塊 |
--with-default-snmp-version="3" | 默認的snmp版本 |
--with-sys-contact="name,E_mail:maileaddress " | 該設備的聯繫人 |
--with-sys-location="China" | 該設備的位置 |
--with-logfile="/var/log/snmpd.log" | 日誌文件路徑 |
--with-persistent-directory="/var/net-snmp" | 不變數據存儲目錄 |
我們這裏只配置安裝路徑,其它的都使用默認設置。
o@o-pc:~/work/_snmp/net-snmp-5.7.3$ ./configure --prefix=/usr/local/net-snmp
配置時的輸出信息
checking what to build and install... agent apps man local mibsusing default persistent mask 077using default temporary file pattern /tmp/snmpdXXXXXXusing default AgentX socket /var/agentx/masterusing default "enterprise.net-snmp"using default enterprise sysOID "NET-SNMP-MIB::netSnmpAgentOIDs..."using default notifications "NET-SNMP-MIB::netSnmpNotifications"using OS default send buffer size for server socketsusing OS default recv buffer size for server socketsusing OS default send buffer size for client socketsusing OS default recv buffer size for client sockets checking whether to prompt for values... configure: ************** Configuration Section **************
注意: 當輸入配置之後按回車鍵之後,系統會有一段時間執行配置。這段時間的執行過程是不會停止的,當出現停止要按某個鍵才能繼續往下執行,說明配置有問題,請取消當前執行重新檢查配置,確保配置正確無誤。 以免以後的安裝出錯,或者安裝完成之後不能正常使用。
執行完配置(沒有中斷,沒有要求按某個鍵)之後會出現如下所示的信息。有可能出現的和下面的不一樣,這取決於配置,但只要出現和這個相似的部分,說明配置是成功的。沒有必要仔細看這個配置摘要。
Net-SNMP configuration summary:--------------------------------------------------------- SNMP Versions Supported: 1 2c 3 Building for: linux Net-SNMP Version: 5.7.3 Network transport support: Callback Unix Alias TCP UDP IPv4Base SocketBase TCPBase UDPIPv4Base UDPBase SNMPv3 Security Modules: usm Agent MIB code: default_modules => snmpv3mibs mibII ucd_snmp notification notification-log-mib target agent_mibs agentx disman/event disman/schedule utilities host MYSQL Trap Logging: unavailable Embedded Perl support: disabled SNMP Perl modules: building -- not embeddable SNMP Python modules: disabled Crypto support from: crypto Authentication support: MD5 SHA1 Encryption support: DES AES Local DNSSEC validation: disabled ---------------------------------------------------------
4.編譯安裝
make編譯
o@o-pc:~/work/_snmp/net-snmp-5.7.3$ make .../usr/bin/ld: cannot find -lperlcollect2: error: ld returned 1 exit status make[1]: *** [libnetsnmpagent.la] 錯誤 1
cannot find -lperl問題
在make編譯的時候遇到了一個錯誤,提示找不到perl
這個庫。原因是因爲我的電腦上沒有安裝perl開發環境,現在裝一個。
o@o-pc:~/work/_snmp/net-snmp-5.7.3$ sudo apt-get install libperl-dev
也許還會碰到缺少libtool
,openssl
,zlib
庫的情況,請直接安裝。
一般遇到這種缺少了什麼庫的問題,都是通過安裝libXXX-dev
或者安裝XXX-dev
來解決。但也不絕對,有的時候是因爲相關的庫不是安裝在系統的默認路徑,也沒用加入到環境變量中。
make install安裝
編譯成功後就可以安裝了,前面設置了安裝路徑是/usr/local/net-snmp
,因爲這個路徑是的所有者(own)和所在組(group)都是root,所有需要sudo來執行。
o@o-pc:~/work/_snmp/net-snmp-5.7.3$ sudo make install
安裝的時候會大量輸出類似於的消息,提示我們安裝了某個配置文件到/usr/local/net-snmp/share/snmp
目錄
install: installed XXX.conf in /usr/local/net-snmp/share/snmp
安裝完成後進入目錄/usr/local/net-snmp/sbin
即可看到可執行文件snmpd,執行輸出一下版本信息。因爲我們這裏還沒有把它的路徑添加到環境變量,所有還不能在任意位置直接輸入snmpd
來運行。
o@o-pc:/usr/local/net-snmp/sbin$ ./snmpd -v NET-SNMP version: 5.7.3Web: http://www.net-snmp.org/Email: [email protected]
5. snmpd.conf配置
snmpd.conf是snmp服務的配置文件。
5.1 snmpd.conf文件生成和修改
生成snmpd.conf
先將EXAMPLE.conf文件複製到/usr/local/net-snmp/share/snmp,並重命名爲snmpd.conf
o@o-pc:~/work/_snmp/net-snmp-5.7.3$ sudo cp EXAMPLE.conf /usr/local/net-snmp/share/snmp/snmpd.conf
上面是一個比較複雜的配置文件,其實可以簡單一點。可以只需要下面說的幾點內容寫到配置文件,其它的都是多餘的
修改snmpd.conf
snmpd.conf文件的修改可以參考這些文章
1.net-snmp的配置文件snmpd.conf配置說明
2.snmpd.conf的基礎配置
這裏按照上面給出的兩篇文章內容來進行的修改(主要是第二篇)。
---
下面的配置中,使用到的com2sec/group/view/access的說明在EXAMPLE.conf文件的註釋中可以看到(大致是65行)。
首先定義一個首共同體名稱(community),這裏是 public。以及可以訪問這個 public 的用戶名(sec name),這裏是notConfigUser 。public 相當於用戶 notConfigUser 的密碼。
在snmpd.conf文件中添加下面的語句。(第一句是註釋)
# sec.name source community com2sec notConfigUser default public
然後定義一個組名(groupName)這裏是 notConfigGroup,以及組的安全級別,並把 notConfigGroup 這個用戶加到這個組中。
在snmpd.conf文件中添加下面的語句。(第一句是註釋)
# groupName securityModel securityName group notConfigGroup v1 notConfigUser group notConfigGroup v2c notConfigUser
接着定義一個可操作的視圖(view)名, 這裏是 all,範圍是 .1。
在snmpd.conf文件中添加下面的語句。(第一句是註釋)
# name incl/excl subtree mask(optional) view all included .1
最後定義 notConfigUser 這個組在 all 這個視圖範圍內可做的操作,這時定義了 notConfigUser 組的成員可對 .1 這個視圖做只讀操作。
# group context sec.model sec.level prefix read write notif access notConfigGroup "" any noauth exact all none none
還可以使用snmpconf
程序來設置,這個程序在安裝目錄的bin
目錄下。它會提示你輸入相關的設置。
o@o-pc:/usr/local/net-snmp/bin$ ./snmpconf -g basic_setup The following installed configuration files were found: 1: /usr/local/net-snmp/share/snmp/snmpd.conf Would you like me to read them in? Their content will be merged with the output files created by this session. Valid answer examples: "all", "none","3","1,2,5" Read in which (default = all): all************************************************ *** Beginning basic system information setup *************************************************** Do you want to configure the information returned in the system MIB group (contact info, etc)? (default = y): ...
啓動netsnmp服務
我們可以使用命令service --sratus-all
來查看當前的服務狀態。可以看到我這臺機器上是沒有netsnmp這個服務的。沒關係,我們並不是特別需要它。具體見下面
o@o-pc:/usr/local/net-snmp/share/snmp$ service --status-all [ + ] acpid [ - ] anacron [ + ] apache [ - ] apparmor [ ? ] apport [ + ] avahi-daemon [ + ] bluetooth [ - ] brltty [ ? ] console-setup [ + ] cron [ + ] cups [ + ] cups-browsed [ - ] dbus [ ? ] dns-clean [ + ] friendly-recovery [ - ] gdm [ - ] grub-common [ ? ] irqbalance [ + ] kerneloops [ ? ] killprocs [ ? ] kmod [ ? ] networking [ ? ] ondemand [ ? ] pppd-dns [ - ] procps [ - ] pulseaudio [ ? ] rc.local [ + ] resolvconf [ - ] rsync [ + ] rsyslog [ + ] saned [ ? ] sendsigs [ - ] spamassassin [ ? ] speech-dispatcher [ - ] sudo [ - ] udev [ ? ] umountfs [ ? ] umountnfs.sh [ ? ] umountroot [ - ] unattended-upgrades [ - ] urandom [ - ] x11-common o@o-pc:/usr/local/net-snmp/share/snmp$
現在還沒有netsnmp這個服務,那麼是不是一定要這個服務呢?不是一定的。因爲linux的服務,可以說就是一個守護進程,我們找到這個進程的可執行文件來執行它就是。而這個文件就在安裝目錄下的sbin
目錄中。
o@o-pc:/usr/local/net-snmp/sbin$ ls snmpd snmptrapd
執行它的時候可指定配置文件(如果不帶,默認就是這個),下面來執行一下試試
o@o-pc:/usr/local/net-snmp/sbin$ ./snmpd -c ../share/snmp/snmpd.conf
如果執行失敗,程序直接退出了,可以使用-f -Le
參數來查看錯誤信息。
其中-f
是do not fork from the shell不從shell派生子進程-L
是 toggle options controlling where to log to切換到相關的日誌控制,它後面需要跟參數。e
:log to standard error 日誌到標準錯誤。
這些相關的參數可以通過./snmpd -h
獲取。
我這邊出現了這樣的錯誤。
o@o-pc:/usr/local/net-snmp/sbin$ ./snmpd -f -Le Turning on AgentX master support. Error opening specified endpoint "udp:127.0.0.1:161"Server Exiting with code 1
這個錯誤的原因很簡單,那就是權限問題。使用sudo
來執行就沒有問題了。linux上很多問題都是權限的問題。
o@o-pc:/usr/local/net-snmp/sbin$ sudo ./snmpd -f -Le [sudo] password for o: NET-SNMP version 5.7.3
這裏沒有加配置文件路徑,是因爲使用的默認配置文件/usr/local/net-snmp/share/snmp/snmpd.conf
,如果還有別的錯誤,那麼就修改配置文件,只保留上面說到的四點,其餘的都刪除掉來試試。
其實配置文件還可以這樣,這也是從網上找的。
#設置區域syslocation "SHANGHAI P.R.China" #設置系統聯繫人syscontact [email protected] #設置一個只讀賬戶 read-only community#下面的 whatever 相當於密碼,後面還可以跟一個IP地址,表示監控點主機rocommunity whatever #設置一個讀寫賬戶 read-write communityrwcommunity whoareyou
執行成功後我們可以進入到bin
目錄,然後執行snmpwalk
,snmpget
等命令來查看一下。
o@o-pc:/usr/local/net-snmp/bin$ ./snmpget -v 1 -c public localhost sysUpTime.0 o@o-pc:/usr/local/net-snmp/bin$ ./snmpwalk -v 1 localhost -c public system o@o-pc:/usr/local/net-snmp/bin$ ./snmpwalk -v 2c -c whatever -m ALL localhost .1.3
o@o-pc:/usr/local/net-snmp/bin$ ./snmpwalk -v 1 172.0.0.1 -c whatever system Timeout: No Response from 172.0.0.1o@o-pc:/usr/local/net-snmp/bin$ ./snmpwalk -v 1 localhost -c whatever system SNMPv2-MIB::sysDescr.0 = STRING: Linux o-pc 3.16.0-30-generic #40~14.04.1-Ubuntu SMP Thu Jan 15 17:43:14 UTC 2015 x86_64SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (21134) 0:03:31.34SNMPv2-MIB::sysContact.0 = STRING: [email protected] SNMPv2-MIB::sysName.0 = STRING: o-pc SNMPv2-MIB::sysLocation.0 = STRING: \"SHANGHAI P.R.China\"SNMPv2-MIB::sysORLastChange.0 = Timeticks: (0) 0:00:00.00SNMPv2-MIB::sysORID.1 = OID: SNMP-MPD-MIB::snmpMPDCompliance SNMPv2-MIB::sysORID.2 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance SNMPv2-MIB::sysORID.3 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance SNMPv2-MIB::sysORID.4 = OID: SNMPv2-MIB::snmpMIB SNMPv2-MIB::sysORID.5 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup SNMPv2-MIB::sysORID.6 = OID: TCP-MIB::tcpMIB SNMPv2-MIB::sysORID.7 = OID: IP-MIB::ip SNMPv2-MIB::sysORID.8 = OID: UDP-MIB::udpMIB SNMPv2-MIB::sysORID.9 = OID: SNMP-NOTIFICATION-MIB::snmpNotifyFullCompliance SNMPv2-MIB::sysORID.10 = OID: NOTIFICATION-LOG-MIB::notificationLogMIB SNMPv2-MIB::sysORDescr.1 = STRING: The MIB for Message Processing and Dispatching. SNMPv2-MIB::sysORDescr.2 = STRING: The management information definitions for the SNMP User-based Security Model. SNMPv2-MIB::sysORDescr.3 = STRING: The SNMP Management Architecture MIB. SNMPv2-MIB::sysORDescr.4 = STRING: The MIB module for SNMPv2 entities SNMPv2-MIB::sysORDescr.5 = STRING: View-based Access Control Model for SNMP. SNMPv2-MIB::sysORDescr.6 = STRING: The MIB module for managing TCP implementations SNMPv2-MIB::sysORDescr.7 = STRING: The MIB module for managing IP and ICMP implementations SNMPv2-MIB::sysORDescr.8 = STRING: The MIB module for managing UDP implementations SNMPv2-MIB::sysORDescr.9 = STRING: The MIB modules for managing SNMP Notification, plus filtering. SNMPv2-MIB::sysORDescr.10 = STRING: The MIB module for logging SNMP Notifications. SNMPv2-MIB::sysORUpTime.1 = Timeticks: (0) 0:00:00.00SNMPv2-MIB::sysORUpTime.2 = Timeticks: (0) 0:00:00.00SNMPv2-MIB::sysORUpTime.3 = Timeticks: (0) 0:00:00.00SNMPv2-MIB::sysORUpTime.4 = Timeticks: (0) 0:00:00.00SNMPv2-MIB::sysORUpTime.5 = Timeticks: (0) 0:00:00.00SNMPv2-MIB::sysORUpTime.6 = Timeticks: (0) 0:00:00.00SNMPv2-MIB::sysORUpTime.7 = Timeticks: (0) 0:00:00.00SNMPv2-MIB::sysORUpTime.8 = Timeticks: (0) 0:00:00.00SNMPv2-MIB::sysORUpTime.9 = Timeticks: (0) 0:00:00.00SNMPv2-MIB::sysORUpTime.10 = Timeticks: (0) 0:00:00.00