您好,登錄后才能下訂單哦!
一、原理介紹:
1. SNMP簡介
目前網絡中用得最廣泛的網絡管理協議是SNMP(Simple Network Management Protocol,簡單網絡管理協議)。SNMP是被廣泛接受并投入使用的工業標準,用于保證管理信息在網絡中任意兩點間傳送,便于網絡管理員在網絡上的任何節點檢索信息、修改信息、定位故障、完成故障診斷、進行容量規劃和生成報告。
SNMP采用輪詢機制,只提供最基本的功能集,特別適合在小型、快速和低價格的環境中使用。SNMP的實現基于無連接的傳輸層協議UDP,因此可以實現和眾多產品的無障礙連接。
2. SNMP的工作機制
SNMP分為NMS和Agent兩部分:
NMS(Network Management Station,網絡管理站)是運行客戶端程序的工作站,目前常用的網管平臺有QuidView、Sun NetManager和IBM NetView。
Agent是運行在網絡設備上的服務器端軟件。
NMS可以向Agent發出GetRequest、GetNextRequest和SetRequest報文,Agent接收到NMS的這些請求報文后,根據報文類型進行Read或Write操作,生成Response報文,并將報文返回給NMS。
Agent在設備發生異常情況或狀態改變時(如設備重新啟動),也會主動向NMS發送Trap報文,向NMS匯報所發生的事件。
3. SNMP的版本
目前,設備中的SNMP Agent支持SNMP v3版本,兼容SNMP v1版本、SNMP v2C版本。
SNMP v3采用用戶名和密碼認證方式。
SNMP v1、SNMP v2C采用團體名(Community Name)認證,非設備認可團體名的SNMP報文將被丟棄。SNMP團體名用來定義SNMP NMS和SNMP Agent的關系。團體名起到了類似于密碼的作用,可以限制SNMP NMS訪問設備上的SNMP Agent。用戶可以選擇指定以下一個或者多個與團體名相關的特性:
1.定義團體名可以訪問的MIB視圖。
2.設置團體名對MIB對象的訪問權限為讀寫權限(write)或者只讀權限(read)。具有只讀權限的團體名只能對設備信息進行查詢,而具有讀寫權限的團體名還可以對設備進行配置。
3.設置團體名指定的基本訪問控制列表。
4. 設備支持的MIB
在SNMP報文中用管理變量來描述設備中的管理對象。為了唯一標識設備中的管理對象,SNMP用層次結構命名方案來識別管理對象。整個層次結構就像一棵樹,樹的節點表示管理對象,如下圖1-1所示。每一個節點,都可以用從根開始的一條路徑唯一地標識。
MIB(Management Information Base,管理信息庫)的作用就是用來描述樹的層次結構,它是所監控網絡設備的標準變量定義的集合。在圖1-1中,管理對象B可以用一串數字{1.2.1.1}唯一確定,這串數字是管理對象的對象標識符(Object Identifier)。
系統支持的常見MIB如下表1-1所示。
二、RRDtool安裝,分析及使用:
工作原理圖:
整個系統有四部分組成:
1、Cacti系統平臺用于用戶設置與查看圖表;Cacti需要LAMP環境的支持。
2、snmp用于采集數據給rrdtool繪制成圖,使用161.162端口,用MIB數據庫來保持數據信息,根據OID定位數據(類似1.3.6.1.2.1);Linux下使用net-snmp自帶的snmpget和snmpwalk等程序,Windows下使用php的snmp功能。
3、RRDTool繪圖引擎用于性能數據的存儲和繪畫圖像。
4、Mysql數據庫用來存儲RRDTool繪圖所需要的信息。
Cacti常見的監測對象:
1、服務器資源:CPU、內存、磁盤、進程、連接數等
2、服務器類型:WEB、Mail、FTP、數據庫
3、網絡接口:流量、轉發速度、丟包率
Cacti偏重于網絡流量,系統負載方面的監控(網絡流量,cpu,硬盤使用率等)
1、安裝cacti基于lamp環境(apache+mysql+php+net-snmp+rrdtool)
1 |
|
1 2 3 4 5 | service httpd start service mysqld start chkconfig httpd on chkconfig mysqld on mysqladmin -u root password root #設置mysql登錄密碼 |
2、安裝配置cacti
1)、安裝cacti
1 2 3 | wget http: //www .cacti.net /downloads/cacti-0 .8.8b. tar .gz tar zxvf cacti-0.8.8b. tar .gz mv cacti-0.8.8b/* /var/www/html |
2)、導入數據庫結構
1 2 3 | mysql>create database cacti; mysql>use cacti; mysql> source /var/www/html/cacti .sql; |
3)、配置cacti連接數據庫:
1 2 3 4 5 6 7 8 9 | vi /var/www/html/include/config .php $database_type = "mysql" ; $database_default = "cactidb" ; $database_hostname = "localhost" ; $database_username = "root" ; $database_password = "root" ; $database_port = "3306" ; $database_ssl = false ; $url_path = "/" ; #去掉注釋,修改路徑為根 |
4)、圖形安裝cacti,輸入IP開始安裝
默認用戶、密碼都是admin,輸入后會讓你修改密碼
5)、配置snmp監控本地主機
1 2 3 4 5 | vi /etc/snmp/snmpd .conf com2sec notConfigUser 127.0.0.1 public access notConfigGroup "" any noauth exact all none none view all included .1 80 service snmpd restart |
3、cacti其他配置
1)、php設置時區
1 2 3 | vi /etc/php .ini date .timezone = Asia /Shanghai service httpd restart |
2)、手動運行一次,生成初始圖像文件: (編繹安裝路徑/usr/local/apache/htdocs/)
1 | /usr/bin/php /var/www/html/poller .php |
3)、設置計劃任務,每五分鐘生成一次圖表:
1 2 | crontab -e * /5 * * * * /usr/bin/php /var/www/html/poller .php |
4)、修改cacti默認設置:console->setting
rrdtool -v #查看rrdtool版本
snmp version :設置snmp版本,一般是version2
snmp community:設置共同體名,默認是public
5)、設置權限:
1 2 3 4 | chown -R apache.root /var/www/html/ chmod 775 -R /var/www/html chmod 777 /var/www/html/rra /var/www/html/log chown -R root.root /var/www/html/rra /var/www/html/log |
6)、監控本地主機
配置完后,查看localhost發現不出圖都是-nan:
解決:rrdtool fetch -r 300 localhost_mem_buffers_3.rrd AVERAGE #使用rrdtool獲取抓取的信息,結果都是-nan,現在看最后一個日期是1379679000,當date +%s獲取的日期超過最后一個,就該出圖了。
4、監控Linux主機
被監控主機配置SNMP:
1 2 3 4 5 6 | yum install -y net-snmp-* vi /etc/snmp/snmpd .conf com2sec notConfigUser 192.168.0.10 public #改為允許監控主機的IP access notConfigGroup "" any noauth exact all none none view all included .1 80 service snmpd restart #重啟snmp |
監控主機添加設備:
Console->Device->Add->create
添加到圖形樹:
Console->Graph Trees->Add->Tree Item Type->Host->選擇主機->create
5、監控Windows主機
首先主機已經添加snmp服務了,再安裝snmp-informant-std會在服務里面多出SNMP Trap,不需要配置,然后配置Snmp Service:
開始—運行—services.msc,找到“snmp service”,右鍵打開“屬性”,選擇“安全”,在“接受團體名稱”處,點“添加”,在“團體名稱”處寫入你的cacti使用的community,選中“接受來自這些主機的snmp數據包”,默認值為“localhost”,點擊“編輯”,將“localhost”改為cacti監控服務器的實際 ip地址即可!
6、不出圖或不出數據以下入手解決
1.圖像沒有生成,運行 #/usr/bin/php /var/www/html/poller.php
2.權限問題,修改權限 #chmod 777 -R /var/www/html/rra /var/www/html/log/
3.snmp配置問題,檢測是否有數據輸出 #snmpwalk -v2c -c public 127.0.0.1
4.刪除rra下所有文件,清除緩存 #system utilities –>Rebuild poller cache
5.系統時間不準確,重新設置時間 date.timezone = PRC
6.數據庫表有損壞,修復 #mysqlcheck -ao cacti -auto-repair -uroot -p
7.不顯示圖片也不顯示數據 #service snmpd restart
7.嘗試重啟snmpd服務 #service snmpd restart
到此安裝完畢!!!
7、相關詳細配制1 --- 以下為通過RRDtool命令查看數據
[root@localhost ~]# yum install net-snmp
[root@localhost ~]# yum install net-snmp-utils
[root@localhost ~]# snmpwalk -v 2c -c public localhost host
HOST-RESOURCES-MIB::hrSystemUptime.0 = Timeticks: (1618864) 4:29:48.64
HOST-RESOURCES-MIB::hrSystemUptime.0 = No more variables left in this MIB View (It is past the end of the MIB tree)
[root@localhost ~]# snmpwalk -v 2c -c public localhost
SNMPv2-MIB::sysDescr.0 = STRING: Linux localhost.localdomain 2.6.18-164.el5PAE #1 SMP Tue Aug 18 15:59:11 EDT 2009 i686
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (745096) 2:04:10.96
SNMPv2-MIB::sysContact.0 = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
SNMPv2-MIB::sysName.0 = STRING: localhost.localdomain
SNMPv2-MIB::sysLocation.0 = STRING: Unknown (edit /etc/snmp/snmpd.conf)
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (13) 0:00:00.13
SNMPv2-MIB::sysORID.1 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.2 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.3 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.4 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.5 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORID.6 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID.7 = OID: SNMP-MPD-MIB::snmpMPDCompliance
SNMPv2-MIB::sysORID.8 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORDescr.1 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.2 = STRING: The MIB module for ma
[root@localhost ~]# vi /etc/snmp/snmpd.conf
# First, map the community name "public" into a "security name"
# sec.name source community
#com2sec notConfigUser default public
com2sec notConfigUser 127.0.0.1 mypublic
com2sec notConfigUser 10.109.0.0/16 mypublic
# Second, map the security name into a group name:
# groupName securityModel securityName
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
[root@localhost ~]# service snmpd restart
[root@localhost ~]# snmpwalk -v 2c -c mypublic 10.109.131.198
SNMPv2-MIB::sysDescr.0 = STRING: Linux localhost.localdomain 2.6.18-164.el5PAE #1 SMP Tue Aug 18 15:59:11 EDT 2009 i686
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (15379) 0:02:33.79
SNMPv2-MIB::sysContact.0 = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
SNMPv2-MIB::sysName.0 = STRING: localhost.localdomain
SNMPv2-MIB::sysLocation.0 = STRING: Unknown (edit /etc/snmp/snmpd.conf)
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (4) 0:00:00.04
SNMPv2-MIB::sysORID.1 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.2 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.3 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.4 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.5 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORID.6 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
... ...
[root@localhost ~]# chkconfig snmpd on
[root@localhost ~]# chkconfig --list snmpd
snmpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost ~]# yum install ruby
...
Downloading Packages:
(1/2): ruby-libs-1.8.5-5.el5_3.7.i386.rpm | 1.6 MB 00:00
(2/2): ruby-1.8.5-5.el5_3.7.i386.rpm | 274 kB 00:00
...
[root@localhost ~]# rpm -ivh rrdtool-1.2.27-4.el5.i386.rpm
[root@localhost ~]# rpm -ivh rrdtool-perl-1.2.27-4.el5.i386.rpm
[root@localhost ~]# rrdtool create test.rrd --step 5 --start -1 DS:testds:GAUGE:8:0:U RRA:AVERAGE:0.5:1:17280 RRA:AVERAGE:0.5:10:3456 RRA:AVERAGE:0.5:100:1210
[root@localhost ~]# ll -h test.rrd
-rw-r--r-- 1 root root 173K Feb 22 16:14 test.rrd
[root@localhost ~]# rrdtool info test.rrd
filename = "test.rrd"
rrd_version = "0003"
step = 5
last_update = 1487751267
ds[testds].type = "GAUGE"
ds[testds].minimal_heartbeat = 8
ds[testds].min = 0.0000000000e+00
ds[testds].max = NaN
ds[testds].last_ds = "UNKN"
...
[root@localhost ~]# cat insert.sh
#!/bin/bash
#
for I in {1..200000}; do
mysql -e "INSERT INTO testdb.tb1(NAME) VALUES ('stu$I')"
mysql -e "SELECT * FROM testdb.tb1" &> /dev/null
done
[root@localhost ~]# cat getselect.sh
#!/bin/bash
#
while true; do
SELECT=`mysql --batch -e "SHOW GLOBAL STATUS LIKE 'com_select'" | awk '/Com_select/{print $2}'`
rrdtool update mysql.rrd N:$SELECT
sleep 3
done
573 rrdtool fetch -r 5 mysql.rrd AVERAGE
574 rrdtool fetch -r 30 mysql.rrd AVERAGE
575 rrdtool fetch -r 30 mysql.rrd max
576 rrdtool fetch -r 30 mysql.rrd MAX
577 rrdtool fetch -r 30 mysql.rrd LAST
580 rrdtool graph mysql.png -s 1487747370 -t "mysql select" -v "select/3" DEF:select3=mysql.rrd:myselect:AVERAGE:step=3 LINE1:select3#FF0000:"SELECT"
581 rrdtool graph mysql.png -s 1487747370 -t "mysql select" -v "select/30" DEF:select3=mysql.rrd:myselect:AVERAGE:step=30 LINE1:select3#FF0000:"SELECT"
583 rrdtool graph mysql2.png -s 1487747370 -t "mysql select" -v "select/3" DEF:select3=mysql.rrd:myselect:AVERAGE:step=30 DEF:max30=mysql.rrd:myselect:MAX:step=30 DEF:last30=mysql.rrd:myselect:LAST:step=30 LINE1:select3#FF0000:"SELECT"
584 rrdtool graph mysql3.png -s 1487747370 -t "mysql select" -v "select/30" DEF:select3=mysql.rrd:myselect:AVERAGE:step=30 DEF:max30=mysql.rrd:myselect:MAX:step=30 DEF:last30=mysql.rrd:myselect:LAST:step=30 LINE1:select3#FF0000:"SELECT"
589 rrdtool graph mysql4.png -s 1487747370 -t "mysql select" -v "select/30" DEF:select3=mysql.rrd:myselect:AVERAGE:step=30 DEF:max30=mysql.rrd:myselect:MAX:sep=30 DEF:last30=mysql.rrd:myselect:LAST:step=30 LINE1:select3#FF0000:"SELECT" GPRINT:last30:LAST:"CURRENT\: %10.21f" GPRINT:max30:MAX:"MAXIMUM\:%6.21f"
8、相關詳細配制2 --- 創建HTTP虛擬主機及手動查看
[root@localhost ~]# vi /etc/httpd/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerName cacti.jacktest.com
DocumentRoot "/web/vhosts/cacti"
<Directory "/web/vhosts/cacti">
Options Indexes
AllowOverride none
Require all granted
</Directory>
ErrorLog "logs/cacti-error_log"
CustomLog "logs/cacti-access_log" common
</VirtualHost>
[root@localhost ~]# mkdir /web
[root@localhost ~]# mkdir /web/vhosts
[root@localhost ~]# ls /web/vhosts
[root@localhost ~]# tar xf cacti-0.8.8a.tar.gz -C /web/vhosts/
[root@localhost ~]# cd /web/vhosts/
[root@localhost vhosts]# ll
lrwxrwxrwx 1 root root 12 02-23 09:26 cacti -> cacti-0.8.8a
drwxr-xr-x 13 1000 users 4096 2012-04-23 cacti-0.8.8a
[root@localhost vhosts]# service httpd restart
[root@localhost cacti]# mysqladmin create cactidb
[root@localhost cacti]# mysql cactidb < cacti.sql
[root@localhost cacti]# mysql -e "GRANT ALL ON cactidb.* TO cactiuser@localhost IDENTIFIED BY 'cactiuser'"
[root@localhost cacti]# mysqladmin flush-privileges
[root@localhost cacti]# mysql -ucactiuser -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cactidb |
| test |
+--------------------+
3 rows in set (0.01 sec)
[root@localhost cacti]# vi config.php
$database_type = "mysql";
$database_default = "cactidb";
$database_hostname = "localhost";
$database_username = "cactiuser";
$database_password = "cactiuser";
$database_port = "3306";
$database_ssl = false;
/*
Edit this to point to the default URL of your Cacti install
ex: if your cacti install as at http://serverip/cacti/ this
would be set to /cacti/
*/
//$url_path = "/cacti/";
$url_path = "/";
[root@localhost include]# useradd cactiuser
[root@localhost cacti]# pwd
/web/vhosts/cacti
[root@localhost cacti]# chown -R cactiuser:cactiuser rra/ log
poller.php --===>> Spine
[root@localhost cacti]# echo '*/5 * * * * /usr/local/php/bin/php /usr/local/apache/htdocs/cacti/poller.php &>/dev/null' > /var/spool/cron/cactiuser #自動5分鐘執行一次poller.php
[root@localhost cacti]# crontab -u cactiuser -e
[root@localhost vhosts]# vi /etc/php.ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =
date.timezone = PRC
[root@localhost vhosts]# service httpd restart
[cactiuser@localhost ~]$ /usr/local/php/bin/php /web/vhosts/cacti/poller.php
02/24/2017 09:44:13 AM - SYSTEM STATS: Time:0.1567 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:0 RRDsProcessed:0
[root@localhost log]# vi /usr/local/apache/htdocs/cacti/log/cacti.log
02/24/2017 09:40:01 AM - SYSTEM STATS: Time:0.1457 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:0 RRDsProcessed:0
[root@localhost rra]# ls /usr/local/apache/htdocs/cacti/rra/
localhost_load_1min_5.rrd localhost_mem_swap_4.rrd localhost_users_6.rrd
localhost_mem_buffers_3.rrd localhost_proc_7.rrd
9、相關詳細配制3 -- 手動查看監控文檔
1. 測試收集命令:
[cactiuser@localhost ~]$ snmpnetstat -v 2c -c public -Can -Cp tcp 10.109.131.144
2. 顯示一個結果
[root@localhost htdocs]# vim /usr/local/apache/htdocs/scripts/tcpconn.sh
#!/bin/bash
#
# $1: hostname or ip
# $2: snmp comunity
SNMPNETSTAT=/usr/bin/snmpnetstat
ESTABLISHED=`$SNMPNETSTAT -v 2c -c $2 -Can -Cp tcp $1 | grep -i 'established' |wc -l`
echo -n "established:$ESTABLISHED"
執行結果:established:19
3. 顯示多個結果
#!/bin/bash
#
# $1: hostname or ip
# $2: snmp comunity
SNMPNETSTAT=/usr/bin/snmpnetstat
TEMPFILE=`mktemp /tmp/$1_tcpconn.XXXXXXXX`
$SNMPNETSTAT -v 2c -c $2 -Can -Cp tcp $1 > $TEMPFILE
ESTABLISHED=` grep -i '$ESTABLISHED' $TEMPFILE |wc -l `
TIMEWAIT=` grep -i '$TIMEWAIT' $TEMPFILE |wc -l `
SYNRECEIVED=` grep -i '$SYNRECEIVED' $TEMPFILE |wc -l `
echo -n "established:$ESTABLISHED timewait:$TIMEWAIT synreceived:$SYNRECEIVED"
10、相關下載
1) rrdtool 下載:
https://mirrors.tuna.tsinghua.edu.cn/epel//5/i386/
2) cacti-0.8.8a 下載:
http://vdisk.weibo.com/s/ukAFmeXH43dKX
3) cacti-spine-0.8.8a 下載:
http://www.cacti.net/downloads/spine/
4) monitor-v1.3-1.tgz 下載:
http://docs.cacti.net/plugin:monitor
5) Cacti0.8.8a (Spine-Monitor-Setting-thold)安裝與優化
http://damondeng.blog.51cto.com/1038075/1226857
6) thold-v0.5.0.tgz 下載: http://docs.cacti.net/plugin:thold#download
settings-v0.71-1.tgz 下載: http://docs.cacti.net/plugin:thold#download
monitor-v1.3-1.tgz 下載: http://docs.cacti.net/plugin:monitor
9) 用Webmin輕松管理linux服務
http://guojiping.blog.51cto.com/5635432/987817
10) Cnyunwei-Cacti+Nagios最新版本 V11發布,安裝完成即使用。
http://www.cnyunwei.com/thread-5714-1-1.html
---end---
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。