天天看點

在ubuntu上安裝伺服器監視軟體MRTG

二、安裝CPU負載監視[預設5分鐘采集一次] 

切換到超級使用者: 

sudo -sH 

安裝軟體: 

apt-get install sysstat 

建立CPU腳本: 

mkdir /opt/mrtg 

vim /opt/mrtg/mrtg.cpu 

#!/bin/bash 

cpuusr=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $3}'` 

cpusys=`/usr/bin/sar -u 1 3 | grep Average | awk '{print $5}'` 

UPtime=`/usr/bin/uptime | awk '{print $3""$4""$5}'` 

echo $cpuusr 

echo $cpusys 

echo $UPtime 

hostname 

使腳本可以執行: 

chmod +755 /opt/mrtg/mrtg.cpu 

修改 /etc/mrtg.cfg 在檔案最後加入cpu項目 

Target[cpu]: `/opt/mrtg/mrtg.cpu` 

MaxBytes[cpu]: 100 

Options[cpu]: gauge, nopercent, growright 

YLegend[cpu]: CPU loading (%) 

ShortLegend[cpu]: % 

LegendO[cpu]:   CPU us; 

LegendI[cpu]:   CPU sy; 

Title[cpu]: CPU Loading 

PageTop[cpu]: <H1>CPU Loading</H1> 

重新生成索引頁面: 

indexmaker /etc/mrtg.cfg > /var/www/mrtg/index.html 

通路: 

http://localhost/mrtg/

三、安裝WWW連接配接數監視[預設5分鐘采集一次] 

建立WWW腳本: 

vim /opt/mrtg/mrtg.www 

all=`netstat -a | grep www|awk '{print $5}'|sort | wc -l|awk '{print$1 - 1}'` 

user=`netstat -a | grep www|awk '{print $5}'|cut -d":" -f1|sort| uniq |wc -l | awk '{print $1 - 1}'` 

if [ "$all" = "-1" ]; then 

echo 0 

else 

echo $all 

fi 

if [ "$user" = "-1" ]; then 

echo $user 

UPtime=`/usr/bin/uptime | awk '{print $3 " " $4 " " $5}'` 

chmod +755 /opt/mrtg/mrtg.www 

修改 /etc/mrtg.cfg 在檔案最後加入www項目 

Target[www]: `/opt/mrtg/mrtg.www` 

MaxBytes[www]: 500 

Options[www]: nopercent, growright 

YLegend[www]: Online Users 

ShortLegend[www]: % 

LegendI[www]: &nbsp; Connect : 

LegendO[www]: &nbsp; Online : 

Title[www]: WWW Connect 

PageTop[www]: <H1> WWW Connect </H1> 

四、安裝記憶體使用監視[預設5分鐘采集一次] 

建立RAM腳本: 

vim /opt/mrtg/mrtg.ram 

# run this script to check the mem usage. 

totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'` 

usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'` 

echo $totalmem 

echo $usedmem 

chmod +755 /opt/mrtg/mrtg.ram 

修改 /etc/mrtg.cfg 在檔案最後加入ram項目 

Target[ram]: `/opt/mrtg/mrtg.ram` 

#Unscaled[ram]: dwym 

MaxBytes[ram]: 2048000 

Title[ram]:Memory 

ShortLegend[ram]: & 

kmg[ram]:kB,MB 

kilo[ram]:1024 

YLegend[ram]: &nbsp; Memory Usage : 

Legend1[ram]: &nbsp; Total Memory : 

Legend2[ram]: &nbsp; Used Memory : 

LegendI[ram]: &nbsp; Total Memory : 

LegendO[ram]: &nbsp; Used Memory : 

Options[ram]: growright,gauge,nopercent 

PageTop[ram]:<H1>Memory</H1> 

五、安裝FTP連接配接數監視[預設5分鐘采集一次] 

建立FTP腳本: 

vim /opt/mrtg/mrtg.ftp 

all=`netstat -a | grep ftp|awk '{print $5}'|sort | wc -l|awk '{print$1 - 1}'` 

user=`netstat -a | grep ftp|awk '{print $5}'|cut -d":" -f1|sort| uniq |wc -l | awk '{print $1 - 1}'` 

chmod +755 /opt/mrtg/mrtg.ftp 

修改 /etc/mrtg.cfg 在檔案最後加入ftp項目 

Target[ftp]: `/opt/mrtg/mrtg.ftp` 

MaxBytes[ftp]: 500 

Options[ftp]: nopercent, growright 

YLegend[ftp]: Online Users 

ShortLegend[ftp]: % 

LegendI[ftp]: &nbsp; Connect : 

LegendO[ftp]: &nbsp; Online : 

Title[ftp]: FTP Connect 

PageTop[ftp]: <H1> FTP Connect </H1> 

六、安裝CPU溫度監視[預設5分鐘采集一次] 

apt-get install mbmon 

建立CPU溫度腳本: 

vim /opt/mrtg/mrtg.temp 

/usr/bin/mbmon -c 1 -i -T 4 -u -n 

exit 0 

chmod +755 /opt/mrtg/mrtg.temp 

Target[temp]: `/opt/mrtg/mrtg.temp` 

MaxBytes[temp]: 100 

Title[temp]: CPU Temperature 

PageTop[temp]: <H1>CPU Temperature</H1> 

Options[temp]: gauge,absolute,unknaszero,growright 

YLegend[temp]: Temperature(C) 

ShortLegend[temp]: (C) 

Legend1[temp]: CPU Temperature 

Legend2[temp]: M/B Temperature 

LegendI[temp]: CPU Temp. 

LegendO[temp]: M/B Temp. 

七、硬碟讀寫監視[預設5分鐘采集一次] 

建立硬碟讀寫腳本: 

vim /opt/mrtg/mrtg.disk 

hd=sda 

disk=/dev/$hd 

UPtime=`/usr/bin/uptime |awk '{print $3""$4""$5}'` 

KBread_sec=`iostat -x $disk|grep $hd |awk '{print 8$}'` 

KBwrite_sec=`iostat -x $disk|grep $hd |awk '{print 9$}'` 

echo $KBread_sec 

echo $KBwrite_sec 

chmod +755 /opt/mrtg/mrtg.disk 

修改 /etc/mrtg.cfg 在檔案最後加入硬碟讀寫 

Target[disk]: `/opt/mrtg/mrtg.disk` 

Title[disk]: Disk HDA I/O Utilization Report 

#Unscaled[disk]: dwym 

MaxBytes[disk]: 10240000 

PageTop[disk]: <H1>Disk I/O Utilization Report</H1> 

kmg[disk]: KB,MB,GB 

LegendI[disk]: Disk I/O KBread/sec 

LegendO[disk]: Disk I/O KBwrite/sec 

Legend1[disk]: Disk I/O KBread/sec 

Legend2[disk]: Disk I/O KBwrite/sec 

YLegend[disk]: Megabytes 

ShortLegend[disk]: & 

Options[disk]: growright,gauge,nopercent 

八、交換分區 

建立交換分區腳本: 

vim /opt/mrtg/mrtg.swap 

# This script to monitor the swap usage. 

totalswap=`/usr/bin/free |grep Swap |awk '{print $2}'` 

usedswap=`/usr/bin/free |grep Swap |awk '{print $3}'` 

echo "$totalswap" 

echo "$usedswap" 

chmod +755 /opt/mrtg/mrtg.swap 

修改 /etc/mrtg.cfg 在檔案最後加入 

Target[swap]: `/opt/mrtg/mrtg.swap` 

MaxBytes[swap]: 2048000 

Title[swap]:Memory State of Server 

ShortLegend[swap]: & 

kmg[swap]:kB,MB 

kilo[swap]:1024 

YLegend[swap]: Swap Usage 

Legend1[swap]: Total Swap 

Legend2[swap]: Used Swap 

LegendI[swap]: Total Swap 

LegendO[swap]: Used Swap 

Options[swap]: growright,gauge,nopercent 

PageTop[swap]:<H1>Swap</H1>