snmp+mrtg實現對區域網內的linuxServer的監控(轉)
snmp+mrtg實現對區域網內的linuxServer的監控(轉)[@more@]上回看了platinum的"mrtg能做些什麼"貼子,很受啟發,於是也試著做了一個。但當時對 snmpd不是很熟,所以沒有做成功,後來仔細看了一下snmd的有關文章和RH中的/etc/snmp/snmpd.conf檔案,發現用snmp+ mrtg可以很好的實現對區域網內伺服器狀態的監控。現在就以用snmp+mrtg監控一臺區域網內的redhat機器(IP:192.168.13.103)的網路卡、記憶體、CPU、DISKIO為例子,談一下如何實現。基本的法辦就是用一臺redhat監控機器(IP:192.168.13.105),透過snmpwalk命令去抓目標伺服器的狀態資料,然後用mrtg畫出圖來。1、首先我們要把目標snmpd.conf檔案的配好。這是用snmpwalk命令一抓取資料的關健。下面是目標機器(IP: 192.168.13.103)上的/etc/snmp/snmpd.conf檔案部份內容,紅色的部份是我對snmpd.conf所做的改動。[root@wy1 root]# cat /etc/snmp/snmpd.conf##### First, map the community name "public" into a "security name"# sec.name source communitycom2sec notConfigUser default public #定義community名稱為 public,對映到安全名 notConfigUser。##### Second, map the security name into a group name:# groupName securityModel securityNamegroup notConfigGroup v1 notConfigUser #定義安全使用者名稱notConfigUser對映到notConfigGroup組。group notConfigGroup v2c notConfigUser##### Third, create a view for us to let the group have rights to: #定義一個view,來決定notConfigUser可以操作的範圍。# Make at least snmpwalk -v 1 localhost -c public system fast again. #定義可檢視的snmp的範圍。# name incl/excl subtree mask(optional)view systemview included .1.3.6.1.2.1.1view systemview included .1.3.6.1.2.1.25.1.1view all included .1##### Finally, grant the group read-only access to the systemview view. #給notConfigGroup組所定義view名 all 以只讀許可權。# group context sec.model sec.level prefix read write notifaccess notConfigGroup "" any noauth exact all none none#access notConfigGroup "" any noauth exact mib2 none none# -----------------------------------------------------------------------------# Here is a commented out example configuration that allows less# restrictive access.# YOU SHOULD CHANGE THE "COMMUNITY" TOKEN BELOW TO A NEW KEYWORD ONLY# KNOWN AT YOUR SITE. YOU *MUST* CHANGE THE NETWORK TOKEN BELOW TO# SOMETHING REFLECTING YOUR LOCAL NETWORK ADDRESS SPACE.## sec.name source community#com2sec local localhost COMMUNITY#com2sec mynetwork NETWORK/24 COMMUNITY## group.name sec.model sec.name#group MyRWGroup any local#group MyROGroup any mynetwork##group MyRWGroup any otherv3user#...## incl/excl subtree mask#view all included .1 80## -or just the mib2 tree-#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc#view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc## context sec.model sec.level prefix read write notif#access MyROGroup "" any noauth 0 all none none#access MyRWGroup "" any noauth 0 all all all其實配製一個snmpd.conf檔案不算太難,(1)首選是定義一個共同體名(community),這裡是public,及可以訪問這個public的使用者名稱(sec name),這裡是notConfigUser。Public相當於使用者notConfigUser的密碼:)# sec.name source communitycom2sec notConfigUser default public(2)定義一個組名(groupName)這裡是notConfigGroup,及組的安全級別,把notConfigGroup這個使用者加到這個組中。groupName securityModel securityNamegroup notConfigGroup v1 notConfigUsergroup notConfigGroup v2c notConfigUser(3)定義一個可操作的範圍(view)名, 這裡是all,範圍是 .1# name incl/excl subtree mask(optional)view all included .1(4)定義notConfigUser這個組在all這個view範圍內可做的操作,這時定義了notConfigUser組的成員可對.1這個範圍做只讀操作。# group context sec.model sec.level prefix read write notifaccess notConfigGroup "" any noauth exact all none noneok,這樣我們的snmpd.conf檔案就基本配成了,用service snmpd restart重啟snmpd服務。現在我們做一個測試,在監控機上打下面的命令:[root@wy2 root]# snmpwalk -v 1 192.168.13.103 -c public systemSNMPv2-MIB::sysDescr.0 = STRING: Linux wy1 2.4.20-8smp #1 SMP Thu Mar 13 17:45:54 EST 2003 i686SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10SNMPv2-MIB::sysUpTime.0 = Timeticks: (7565377) 21:00:53.77SNMPv2-MIB::sysContact.0 = STRING: Root (configure /etc/snmp/snmp.local.conf)SNMPv2-MIB::sysName.0 = STRING: wy1SNMPv2-MIB::sysLocation.0 = STRING: wy1.wuying.com (edit /etc/snmp/snmpd.conf)SNMPv2-MIB::sysORLastChange.0 = Timeticks: (10) 0:00:00.10``````````````````````“Linux wy1 2.4.20-8smp”作業系統的資訊已經出來了:)現在我們在目標機上來寫一些指令碼來顯標MEM、CPU、DiskIOMEM資料的抓取指令碼:[root@wy1 root]# cat mfree.sh#!/bin/sh/usr/bin/free -m | grep Mem |awk '{print $4}'/usr/bin/free -m | grep Mem |awk '{print $2}'[root@wy1 root]# sh mfree.sh (上面一個資料是記憶體使用量,下面的是記憶體總量,M)4421006CPU資料的抓取指令碼[root@wy1 root]# cat cpustat.sh#!/bin/shidle=`sar -u 1 3 | grep Average | awk '{print $6}'`used=`echo "101 - $idle" | bc -l -s`echo $usedecho $idleDiskIO資料的抓取指令碼[root@wy1 root]# cat iostat.sh (顯示硬碟IO,k/s)#!/bin/shused1=`sar -d 1 3 | tail -1 | awk '{print $4}'`used2=`echo "$used1 / 2" | bc -l`echo $used2echo $used2好現在我們已經能得到這資料了,怎麼才能讓監控主機透過snmpd得到這些資料呢?可以在目標主機的/etc/snmp/snmpd.conf檔案下面加個這些行:exec .1.3.6.1.4.1.2021.53 mfree /bin/sh /root/mfree.shexec .1.3.6.1.4.1.2021.54 cpustat /bin/sh /root/cpustat.shexec .1.3.6.1.4.1.2021.55 iostat /bin/sh /root/iostat.shservice snmpd restart 重啟目標主機上的snmpd服務。這樣在監控主機上執行:[root@wy2 root]# snmpwalk -v 1 192.168.13.103 -c public .1.3.6.1.4.1.2021.53UCD-SNMP-MIB::ucdavis.53.1.1 = INTEGER: 1UCD-SNMP-MIB::ucdavis.53.2.1 = STRING: "mfree"UCD-SNMP-MIB::ucdavis.53.3.1 = STRING: "/bin/sh /root/mfree.sh"UCD-SNMP-MIB::ucdavis.53.100.1 = INTEGER: 0UCD-SNMP-MIB::ucdavis.53.101.1 = STRING: "442"UCD-SNMP-MIB::ucdavis.53.101.2 = STRING: "1006"UCD-SNMP-MIB::ucdavis.53.102.1 = INTEGER: 0其中UCD-SNMP-MIB::ucdavis.53.101.1 = STRING: "442" 中的442就是mfree.sh輸出的第一個資料,1006是mfree.sh輸出的第二個資料。OK,再做一下加工:[root@wy2 root]# snmpwalk -v 1 192.168.13.103 -c public .1.3.6.1.4.1.2021.53 | grep 53.101 | awk -F" '{print $2}'4431006好,我們已經透過snmpd從監控主機上得到了目標主機上記憶體使用的這兩個資料了:),其它的兩個指令碼也一樣:CPU使用、空閒。[root@wy2 root]# snmpwalk -v 1 192.168.13.103 -c public .1.3.6.1.4.1.2021.54 | grep 54.101 | awk -F" '{print $2}'1.1799.83DISK IO 狀態:[root@wy2 root]# snmpwalk -v 1 192.168.13.103 -c public .1.3.6.1.4.1.2021.55 | grep 55.101 | awk -F" '{print $2}'43.0000000000000000000043.00000000000000000000最後是編輯監控主機上的mrtg.cfg檔案,在Target[xxxx]中加入上面的命令,下面是我的mrtg.cfg檔案,前面的兩個 Target是我用/usr/bin/cfgmaker --global 'WorkDir: /home/httpd/mrtg/net' --global 'Options[_]:growright,bits' --ifref=ip public@192.168.13.103 命令生成的,後面的是我跟據platinum 的"mrtg能做些什麼"的貼子內的mrtg.cfg檔案改的。(在精華里,大家可以看一下)。[root@wy1 root]# cat /home/httpd/mrtg/103/mrtg.cfg# Created by# /usr/bin/cfgmaker --global 'WorkDir: /home/httpd/mrtg/net' --global 'Options[_]:growright,bits' --ifref=ip public@192.168.13.103### Global Config Options# for UNIX# WorkDir: /home/http/mrtg# or for NT# WorkDir: c:mrtgdata### Global Defaults# to get bits instead of bytes and graphs growing to the right# Options[_]: growright, bitsWorkDir: /home/httpd/mrtg/103Options[_]:growright,bitsLanguage: chinese####################################################################### System: wy1# Description: Linux wy1 2.4.20-8smp #1 SMP Thu Mar 13 17:45:54 EST 2003 i686# Contact: Root (configure /etc/snmp/snmp.local.conf)# Location: wy1.wuying.com (edit /etc/snmp/snmpd.conf)######################################################################### Interface 2 >> Descr: 'eth0' | Name: '' | Ip: '192.168.13.103' | Eth: '00-06-5b-19-9d-ea' ###Target[192.168.13.103_192.168.13.103]: /192.168.13.103:public@192.168.13.103:SetEnv[192.168.13.103_192.168.13.103]: MRTG_INT_IP="192.168.13.103" MRTG_INT_DESCR="eth0"MaxBytes[192.168.13.103_192.168.13.103]: 1250000Xsize[192.168.13.103_192.168.13.103]: 300Ysize[192.168.13.103_192.168.13.103]: 100#kmg[192.168.13.103_192.168.13.103]: K/s,M/s#kilo[192.168.13.103_192.168.13.103]: 1024Title[192.168.13.103_192.168.13.103]: Traffic for eth0 192.168.13.103 -- wy1PageTop[192.168.13.103_192.168.13.103]:
Traffic for eth0 192.168.13.103 -- wy1
#Options[192.168.13.103_192.168.13.103]: growright,gauge,nopercent### Interface 3 >> Descr: 'eth1' | Name: '' | Ip: '172.16.0.188' | Eth: '00-06-5b-19-9d-e9' ###Target[192.168.13.103_172.16.0.188]: /172.16.0.188:public@192.168.13.103:SetEnv[192.168.13.103_172.16.0.188]: MRTG_INT_IP="172.16.0.188" MRTG_INT_DESCR="eth1"Xsize[192.168.13.103_172.16.0.188]: 300Ysize[192.168.13.103_172.16.0.188]: 100MaxBytes[192.168.13.103_172.16.0.188]: 1250000#ShortLegend[192.168.13.103_172.16.0.188]:#kmg[192.168.13.103_172.16.0.188]: b/s,Kb/s#kilo[192.168.13.103_172.16.0.188]: 1024Title[192.168.13.103_172.16.0.188]: Traffic for eth1 172.16.0.188 -- wy1PageTop[192.168.13.103_172.16.0.188]:Traffic for eth1 172.16.0.188 -- wy1
#Options[192.168.13.103_172.16.0.188]: growright,gauge,nopercent###MEM statusTarget[wy1_mem]:`snmpwalk -v 1 192.168.13.103 -c public .1.3.6.1.4.1.2021.53 | grep 53.101 | awk -F" '{print $2}'`#Targey[wy1_mem]: memTotalReal.0&memAvailReal.0:holdata@holdata.3322.orgXsize[wy1_mem]: 300Ysize[wy1_mem]: 100Ytics[wy1_mem]: 7MaxBytes[wy1_mem]: 1006Title[wy1_mem]:Memory State of WY1 IP 192.168.13.103 ServerPageTop[wy1_mem]:Memory State of WY1 IP 192.168.13.103 Server
ShortLegend[wy1_mem]: MBkmg[wy1_mem]: MBkilo[wy1_mem]:1024YLegend[wy1_mem]: Memory UsageLegend1[wy1_mem]: 可用記憶體Legend2[wy1_mem]: 總記憶體量Legend3[wy1_mem]: 可用記憶體Legend4[wy1_mem]: 總記憶體量LegendI[wy1_mem]: 可用記憶體LegendO[wy1_mem]: 總記憶體量Options[wy1_mem]: growright,gauge,nopercent###cpu statusTarget[wy1_CPU]:`snmpwalk -v 1 192.168.13.103 -c public .1.3.6.1.4.1.2021.54 | grep 54.101 | awk -F" '{print $2}'`#Targey[wy1_CPU]: memTotalReal.0&memAvailReal.0:holdata@holdata.3322.orgXsize[wy1_CPU]: 300Ysize[wy1_CPU]: 100Ytics[wy1_CPU]: 7MaxBytes[wy1_CPU]: 100Title[wy1_CPU]:CPU State of WY1 IP 192.168.13.103 ServerPageTop[wy1_CPU]:CPU State of WY1 IP 192.168.13.103 Server
ShortLegend[wy1_CPU]:kmg[wy1_CPU]: %#kilo[wy1_CPU]:1024YLegend[wy1_CPU]: CPU UsageLegend1[wy1_CPU]: 已用CPU:Legend2[wy1_CPU]: 可用CPU:LegendI[wy1_CPU]: 已用CPU:LegendO[wy1_CPU]: 可用CPU:Options[wy1_CPU]: growright,gauge,nopercent###iostat###cpu statusTarget[wy1_IO]:`snmpwalk -v 1 192.168.13.103 -c public .1.3.6.1.4.1.2021.55 | grep 55.101 | awk -F" '{print $2}'`#Targey[wy1_IO]: memTotalReal.0&memAvailReal.0:holdata@holdata.3322.orgXsize[wy1_IO]: 300Ysize[wy1_IO]: 100Ytics[wy1_IO]: 7MaxBytes[wy1_IO]: 10000Title[wy1_IO]: DISK IO State of WY1 IP 192.168.13.103 ServerPageTop[wy1_IO]:DISK IO State of WY1 IP 192.168.13.103 Server
ShortLegend[wy1_IO]:kmg[wy1_IO]: K/s,M/skilo[wy1_IO]:1024YLegend[wy1_IO]: DISK IO SPEEDLegend1[wy1_IO]: IO速度:Legend2[wy1_IO]: IO速度:LegendI[wy1_IO]: IO速度:LegendO[wy1_IO]: IO速度:Options[wy1_IO]: growright,gauge,nopercent用indexmaker -o /home/httpd/mrtg/103/index.html /home/httpd/mrtg/103/mrtg.cfg 生成網頁,在crontab 中加入*/5 * * * * mrtg /home/httpd/mrtg/103/mrtg.cfgOK,這樣我們就能透過mrtg生成的圖來監控我們linux的機器的狀態了。在IE中打入http: //192.168.13.105/mrtg/103,mrtg的監控網頁就出來啦(/home/httpd/是我appache的主目錄)。當然我們也能用snmpwalk命令來得到安裝有snmp服務的win2000機器的狀態的資料:如:[root@wy1 103]# snmpwalk -v 1 192.168.1.5 -c public HOST-RESOURCES-MIB::hrStorageHOST-RESOURCES-MIB::hrMemorySize.0 = INTEGER: 3800424 KBytesHOST-RESOURCES-MIB::hrStorageIndex.1 = INTEGER: 1HOST-RESOURCES-MIB::hrStorageIndex.2 = INTEGER: 2HOST-RESOURCES-MIB::hrStorageIndex.3 = INTEGER: 3HOST-RESOURCES-MIB::hrStorageIndex.4 = INTEGER: 4HOST-RESOURCES-MIB::hrStorageIndex.5 = INTEGER: 5HOST-RESOURCES-MIB::hrStorageIndex.6 = INTEGER: 6HOST-RESOURCES-MIB::hrStorageType.1 = OID: HOST-RESOURCES-TYPES::hrStorageRemovableDiskHOST-RESOURCES-MIB::hrStorageType.2 = OID: HOST-RESOURCES-TYPES::hrStorageFixedDiskHOST-RESOURCES-MIB::hrStorageType.3 = OID: HOST-RESOURCES-TYPES::hrStorageFixedDiskHOST-RESOURCES-MIB::hrStorageType.4 = OID: HOST-RESOURCES-TYPES::hrStorageFixedDiskHOST-RESOURCES-MIB::hrStorageType.5 = OID: HOST-RESOURCES-TYPES::hrStorageCompactDiscHOST-RESOURCES-MIB::hrStorageType.6 = OID: HOST-RESOURCES-TYPES::hrStorageVirtualMemoryHOST-RESOURCES-MIB::hrStorageDescr.1 = STRING: A:HOST-RESOURCES-MIB::hrStorageDescr.2 = STRING: C: Label: Serial Number 581e89feHOST-RESOURCES-MIB::hrStorageDescr.3 = STRING: D: Label:New Volume Serial Number 1cde6e55HOST-RESOURCES-MIB::hrStorageDescr.4 = STRING: E: Label:DATA_BAK Serial Number 30d29147HOST-RESOURCES-MIB::hrStorageDescr.5 = STRING: F:HOST-RESOURCES-MIB::hrStorageDescr.6 = STRING: Virtual MemoryHOST-RESOURCES-MIB::hrStorageAllocationUnits.1 = INTEGER: 0 BytesHOST-RESOURCES-MIB::hrStorageAllocationUnits.2 = INTEGER: 4096 BytesHOST-RESOURCES-MIB::hrStorageAllocationUnits.3 = INTEGER: 4096 BytesHOST-RESOURCES-MIB::hrStorageAllocationUnits.4 = INTEGER: 4096 BytesHOST-RESOURCES-MIB::hrStorageAllocationUnits.5 = INTEGER: 0 BytesHOST-RESOURCES-MIB::hrStorageAllocationUnits.6 = INTEGER: 65536 BytesHOST-RESOURCES-MIB::hrStorageSize.1 = INTEGER: 0HOST-RESOURCES-MIB::hrStorageSize.2 = INTEGER: 2050287HOST-RESOURCES-MIB::hrStorageSize.3 = INTEGER: 15703529HOST-RESOURCES-MIB::hrStorageSize.4 = INTEGER: 53263499HOST-RESOURCES-MIB::hrStorageSize.5 = INTEGER: 0HOST-RESOURCES-MIB::hrStorageSize.6 = INTEGER: 89444HOST-RESOURCES-MIB::hrStorageUsed.1 = INTEGER: 0HOST-RESOURCES-MIB::hrStorageUsed.2 = INTEGER: 1233681HOST-RESOURCES-MIB::hrStorageUsed.3 = INTEGER: 591593HOST-RESOURCES-MIB::hrStorageUsed.4 = INTEGER: 23583930HOST-RESOURCES-MIB::hrStorageUsed.5 = INTEGER: 0HOST-RESOURCES-MIB::hrStorageUsed.6 = INTEGER: 50611HOST-RESOURCES-MIB::hrStorageAllocationFailures.1 = Counter32: 0HOST-RESOURCES-MIB::hrStorageAllocationFailures.2 = Counter32: 0HOST-RESOURCES-MIB::hrStorageAllocationFailures.3 = Counter32: 0HOST-RESOURCES-MIB::hrStorageAllocationFailures.4 = Counter32: 0HOST-RESOURCES-MIB::hrStorageAllocationFailures.5 = Counter32: 0HOST-RESOURCES-MIB::hrStorageAllocationFailures.6 = Counter32: 0上面顯示的是192.168.1.5這臺win2000SERVER的磁碟資訊,可以看到磁碟機代號、每個分割槽的空間總量、使用量等。其它的相應建值還有:Diskused: HOST-RESOURCES-MIB::hrStorageUsedCPU: HOST-RESOURCES-MIB::hrProcessorLoadRUN Proc: HOST-RESOURCES-MIB::hrSWRunNameSYS Uptime: HOST-RESOURCES-MIB::hrSystemUptime SNMPv2-MIB::sysUpTimeSYS Date: HOST-RESOURCES-MIB::hrSystemDateSYS Device: HOST-RESOURCES-MIB::hrDeviceDescrSYS Descr: SNMPv2-MIB::sysDescrSYS Name: SNMPv2-MIB::sysNamenetcard speed: IF-MIB::ifSpeednetcard physcal address: IF-MIB::ifPhysAddress這樣我們就可以用一臺linux主機透過snmp+mrtg來監控多臺區域網內的linux/win2000SERVER了:)。現在我最關心的是如何讓snmp包穿過防火牆,(我用snmpwalk試了一下,抓不到公司在IDC防火牆後的SERVER的snmp資料)。不然,我就可以用snmp去監控公司在IDC防火牆後的SERVER的狀態了。聽說perl能實現,但不知用什麼樣的方法來實現來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-945554/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- snmp+mrtg實現對區域網內的linuxServer的監控(zt)LinuxServer
- FREEBSD下snmp+mrtg實現對網路流量、CPU、記憶體等的監視(轉)記憶體
- Java實現區域網內單播Java
- 區域網內實現ARP攻擊
- 用UDP實現區域網內聊天程式UDP
- 使用樹莓派搭建區域網監控樹莓派
- 區域網實現VLAN例項(轉)
- 實現Linux下的區域網遠端接入(轉)Linux
- linux區域網內磁碟對映Linux
- 教你如何使用樹莓派搭建區域網監控樹莓派
- 外網連線內部區域網的方法--ip 埠對映
- 用Delphi在區域網中實現網上影院 (轉)
- 【專案原始碼】- 【區域網聊天】android實現區域網聊天 - UDP實現原始碼AndroidUDP
- 簡單的區域網內通訊
- Java實現ZooKeeper的zNode監控Java
- TiDB監控實現--存活監控TiDB
- 監控網頁中元素的事件 (轉)網頁事件
- 使用IntersectionObserver 實現:自動監聽元素是否進入了裝置的可視區域之內Server
- 區域網內命令大全
- 獨闢蹊徑實現Linux下的區域網遠端接入(轉)Linux
- Linux下如何實現區域網內網路裝置相互通訊Linux內網
- ntp服務在區域網內的搭建
- 藉助sIoT可以通過手機app實現區域網內的裝置操控。APP
- Oracle對錶的監控Oracle
- 企業網盤實現區域網共享
- 國內免費的網站監控工具測評網站
- SNMP+MRTG 網路流量分析(轉)
- 非常實用的區域網內聊天工具Active MessengerMessenger
- OpenTelemetry 實戰:gRPC 監控的實現原理RPC
- 使用FreeBSD的SNMP+MRTG網路流量分析(轉)
- 區域網內部署 Docker RegistryDocker
- [轉]怎樣檢視區域網內其他機器的網路卡實體地址及查詢本機的網路卡實體地址
- 用 Python 指令碼實現對 Linux 伺服器的監控Python指令碼Linux伺服器
- VPC內網服務可用性監控最佳實踐內網
- 遠端影片監控實現的條件
- Docker容器的自動化監控實現Docker
- 如何實現一個IOS網路監控元件iOS元件
- 網路卡流量監控指令碼,python實現指令碼Python