在Ubuntu下配置MRTG監控Nginx和服務器系統資源

Ubuntu使用apt-get來管理軟件包,很是的好用,能夠自行解決依賴問題。 html

一、安裝mrtgsnmp

apt-get install mrtg snmpd sar curl

二、配置snmpd

編輯/etc/snmp/snmpd.conf文件 nginx

改成以下內容便可: git

com2sec notConfigUser  localhost       public

group   notConfigGroup v1           notConfigUser
group   notConfigGroup v2c           notConfigUser

view    systemview    included   .1.3.6.1.2.1.1
view    systemview    included   .1.3.6.1.2.1.25.1.1

access  notConfigGroup ""      any       noauth    exact all none none
view all    included  .1                               80

syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root

修改完成後重啓snmpd shell

service snmpd restart

snmp功能強大但這裏只須要比較簡單的功能(獲取網卡流量),這麼配置就ok了。 bash

三、準備一些腳本

  • 收集內存使用情況數據腳本

將以下文件存爲/home/mrtg/mrtg.ram 並賦予可執行權限(755) 服務器

#!/bin/bash 
# run this script to check the mem usage. 
swapmem=`/usr/bin/free |grep Swap |awk '{print $3}'`


usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'`
UPtime=`/usr/bin/uptime | awk '{print $3""$4""$5}'`
echo $usedmem 
echo $swapmem 
echo $UPtime 
hostname
  • 收集CPU使用情況數據腳本

將以下文件存爲/home/mrtg/mrtg.cpu 並賦予可執行權限(755) curl

#!/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
  • 收集nginx鏈接數數據腳本

這裏前提是nginx編譯時添加了--with-http_stub_status_module 選項,添加後就能夠監視nginx當前的鏈接數。或者能夠是用淘寶的 Tengine (默認就帶了這個編譯參數) this

在nginx中配置以下: url

location ~ ^/nginx_status {
    stub_status on;
    access_log off;
  }
配置加上這個後,你就能夠經過

http://localhost/nginx_status spa

這種方式來獲取nginx的實時鏈接數信息。

例如訪問http://localhost/nginx_status:

Active connections: 55 
server accepts handled requests
 154905 154905 393798 
Reading: 0 Writing: 1 Waiting: 54

可是這還不夠,由於mrtg須要的是純數據,咱們一樣還須要一個腳本將數據提取出來,

咱們使用curl將數據獲取到並使用awk將數據提取出來存到臨時文件中。

將以下代碼保存爲/home/mrtg/nginx_status,並賦予可執行權限(755)

curl http://localhost/nginx_status | grep Active | awk '{print $3 }'  > /home/mrtg/mrtg-2/ngx.active
curl http://localhost/nginx_status | grep Waiting | awk '{print $6 }' > /home/mrtg/mrtg-2/ngx.waiting
將以下代碼保存爲/home/mrtg/mrtg.ngx,並賦予可執行權限(755)
#!/usr/bin/perl -W
`/home/mrtg/nginx_status`;
$hostname=`hostname`;
$hostname=~s/\s+$//;
$nginx_active_conn=`tail /home/mrtg/ngx.active`;
$nginx_waiting_conn=`tail /home/mrtg/ngx.waiting`;  
$nginx_active_conn=~s/\n$//;
$nginx_waiting_conn=~s/\n$//;
$nginx_active_conn=~s/^\s+|\s+$//;
$nginx_waiting_conn=~s/^\s+|\s+$//;
$gettime=`uptime|awk '{print \$1" "\$3" "\$4}'`;
$gettime=~s/\,|\n$//g;
print("$nginx_active_conn\n");
print("$nginx_waiting_conn\n");
print("$gettime\n");
print("$hostname\n");

四、配置mrtg

默認的mrtg的配置文件是/etc/mrtg.cfg

咱們須要使用命令cfgmaker來生成配置文件,具體命令以下:

cfgmaker public@localhost --output /etc/mrtg.cfg

而後能夠看到文件中已經有了一些配置,網卡流量相關的配置已經生成好了,咱們須要將cpu、內存、nginx鏈接數的配置補進去。

添加以下配置:

WorkDir: /home/mrtg/mrtg
Options[_]: growright, bits

EnableIPv6: no

Target[cpu]: `/home/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>


Target[ram]: `/home/mrtg/mrtg.ram`
#Unscaled[ram]: dwym 
MaxBytes[ram]:  16424724   注意,這裏的數值爲服務器內存的最大值,可經過free命令查看。
Title[ram]:Memory
ShortLegend[ram]: &
kmg[ram]:kB,MB
kilo[ram]:1024
YLegend[ram]:   Memory Usage :
Legend1[ram]:   Swap Memory :
Legend2[ram]:   Used Memory :
LegendI[ram]:   Swap Memory :
LegendO[ram]:   Used Memory :
Options[ram]: growright,gauge,nopercent
PageTop[ram]:<H1>Memory</H1>


Target[nginx_conn]: `/home/mrtg/mrtg.ngx`
Options[nginx_conn]: gauge,nopercent,growright
Directory[nginx_conn]: nginx_conn
MaxBytes[nginx_conn]: 8000
YLegend[nginx_conn]: nginx_conn
ShortLegend[nginx_conn]:
LegendI[nginx_conn]: Active connections:
LegendO[nginx_conn]: Waiting:
Title[nginx_conn]: Nginx
PageTop[nginx_conn]:<h1>nginx</h1>

cfgmaker生成的從snmp獲取網卡流量的部分配置若是不懂的話最好不要動。

最終的mrtg配置看起來像這樣,可是不能照搬,須要靈活根據本身的狀況進行調整:

WorkDir: /home/mrtg/mrtg
Options[_]: growright, bits

EnableIPv6: no

Target[cpu]: `/home/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>


Target[ram]: `/home/mrtg/mrtg.ram`
#Unscaled[ram]: dwym 
MaxBytes[ram]:  32687592
Title[ram]:Memory
ShortLegend[ram]: &
kmg[ram]:kB,MB,GB
kilo[ram]:1024
YLegend[ram]:   Memory Usage :
Legend1[ram]:   Swap Memory :
Legend2[ram]:   Used Memory :
LegendI[ram]:   Swap Memory :
LegendO[ram]:   Used Memory :
Options[ram]: growright,gauge,nopercent
PageTop[ram]:<H1>Memory</H1>


Target[nginx_conn]: `/home/mrtg/mrtg.ngx`
Options[nginx_conn]: gauge,nopercent,growright
Directory[nginx_conn]: nginx_conn
MaxBytes[nginx_conn]: 8000
YLegend[nginx_conn]: nginx_conn
ShortLegend[nginx_conn]:
LegendI[nginx_conn]: Active connections:
LegendO[nginx_conn]: Waiting:
Title[nginx_conn]: Nginx
PageTop[nginx_conn]:<h1>nginx</h1>


### Interface 2 >> Descr: 'eth0' | Name: 'eth0' | Ip: '192.168.1.123' | Eth: '00-16-3e-00-0d-71' ###
### The following interface is commented out because:
### * has no ifSpeed property
# 
Target[localhost_eth0]: #eth0:public@localhost:
SetEnv[localhost_eth0]: MRTG_INT_IP="192.168.1.123" MRTG_INT_DESCR="eth0"
MaxBytes[localhost_eth0]: 12500000
Title[localhost_eth0]: 192.168.1.123 -- git-osc
PageTop[localhost_eth0]: <h1>192.168.1.123 -- git-osc</h1> <div id="sysdetails">
 			<table>
 				<tr>
 					<td>System:</td>
 					<td>git-osc in Unknown (edit /etc/snmp/snmpd.conf)</td>
 				</tr>
 				<tr>
 					<td>Maintainer:</td>
 					<td>Root</td>
 				</tr>
 				<tr>
 					<td>Description:</td>
 					<td>eth0  </td>
 				</tr>
 				<tr>
 					<td>ifType:</td>
 					<td>ethernetCsmacd (6)</td>
 				</tr>
 				<tr>
 					<td>ifName:</td>
 					<td>eth0</td>
 				</tr>
 				<tr>
 					<td>Max Speed:</td>
 					<td>0.0 Bytes/s</td>
 				</tr>
 				<tr>
 					<td>Ip:</td>
 					<td>192.168.1.123</td>
 				</tr>
 			</table>
 		</div>


## Interface 3 >> Descr: 'eth1' | Name: 'eth1' | Ip: '192.168.1.134' | Eth: '00-16-3e-00-0d-8e' ###
## The following interface is commented out because:
## * has no ifSpeed property
 
Target[localhost_eth1]: #eth1:public@localhost:
SetEnv[localhost_eth1]: MRTG_INT_IP="192.168.1.134" MRTG_INT_DESCR="eth1"
MaxBytes[localhost_eth1]: 2500000
Title[localhost_eth1]: 192.168.1.134 -- git-osc
PageTop[localhost_eth1]: <h1>192.168.1.134 -- git-osc</h1>
 		<div id="sysdetails">
 			<table>
 				<tr>
 					<td>System:</td>
 					<td>git-osc in Unknown (edit /etc/snmp/snmpd.conf)</td>
 				</tr>
 				<tr>
 					<td>Maintainer:</td>
 					<td>Root</td>
 				</tr>
 				<tr>
 					<td>Description:</td>
 					<td>eth1  </td>
 				</tr>
 				<tr>
 					<td>ifType:</td>
 					<td>ethernetCsmacd (6)</td>
 				</tr>
 				<tr>
 					<td>ifName:</td>
 					<td>eth1</td>
 				</tr>
 				<tr>
 					<td>Max Speed:</td>
 					<td>0.0 Bytes/s</td>
 				</tr>
 				<tr>
 					<td>Ip:</td>
 					<td>192.168.1.134</td>
 				</tr>
 			</table>
 		</div>

MaxBytes[localhost_eth0]和MaxBytes[localhost_eth1]的數值分別爲兩塊網卡的峯值流量,單位byte。

五、生成mrtg首頁

mkdir /home/mrtg/mrtg
indexmaker /etc/mrtg.cfg > /home/mrtg/mrtg/index.html

六、使用mrtg繪製數據圖表

env LANG=C mrtg /etc/mrtg.cfg

    配置crontab定時採集數據,並繪製圖表

1/5 * * * * env LANG=C /usr/bin/mrtg /etc/mrtg.cfg
    這裏爲每五分鐘採集一次,可活動調整。

七、配置nginx查看結果

在nginx中添加以下配置:

location ~ ^/mrtg {
        root /home/mrtg;
  }

八、查看結果

http://localhost/mrtg

紅線刮掉的是網卡流量的title,由於有兩塊網卡因此就有兩個統計結果。

九、注意事項

這裏的http://localhost/nginx_status 和http://localhost/mrtg 沒有加權限控制,你能夠經過配置nginx加上http的基礎認證,或者限定只有某些指定的IP能夠訪問,具體配置辦法不在本文範疇以內,可查閱相關文檔或者搜索獲得結果。

參考了@範堡 的文章:

http://www.oschina.net/question/17_487

相關文章
相關標籤/搜索