歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux配置 >> Ubuntu下配置MRTG監控Nginx和服務器系統資源

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

日期:2017/2/27 14:57:35   编辑:Linux配置
Ubuntu使用apt-get來管理軟件包,非常的好用,可以自行解決依賴問題。
1、安裝mrtg和snmp
apt-get install mrtg snmpd sar curl

2、配置snmpd
編輯/etc/snmp/snmpd.conf文件
改為如下內容即可:
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
service snmpd restart
snmp功能強大但這裡只需要比較簡單的功能(獲取網卡流量),這麼配置就ok了。

3、准備一些腳本
  • 收集內存使用狀況數據腳本
將如下文件存為/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)
#!/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 (默認就帶了這個編譯參數)
在nginx中配置如下:
location ~ ^/nginx_status {
  stub_status on;
  access_log off;
}
配置加上這個後,你就可以通過
http://localhost/nginx_status

這種方式來獲取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");

4、配置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><span style="font-size:9pt;line-height:1.5;">192.168.1.123 -- git-osc</h1></span> <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><span style="font-family:'sans serif', tahoma, verdana, helvetica;font-size:10pt;line-height:1.5;"></span>
MaxBytes[localhost_eth0]和MaxBytes[localhost_eth1]的數值分別為兩塊網卡的峰值流量,單位byte。

5、生成mrtg首頁
mkdir /home/mrtg/mrtg
indexmaker /etc/mrtg.cfg > /home/mrtg/mrtg/index.html

6、使用mrtg繪制數據圖表
env LANG=C mrtg /etc/mrtg.cfg
配置crontab定時采集數據,並繪制圖表
1/5 * * * * env LANG=C /usr/bin/mrtg /etc/mrtg.cfg
這裡為每五分鐘采集一次,可活動調整。

7、配置nginx查看結果
在nginx中添加如下配置:
location ~ ^/mrtg {
      root /home/mrtg;
}

8、查看結果
http://localhost/mrtg

紅線刮掉的是網卡流量的title,因為有兩塊網卡所以就有兩個統計結果。

9、注意事項
這裡的http://localhost/nginx_status 和http://localhost/mrtg 沒有加權限控制,你可以通過配置nginx加上http的基礎認證,或者限定只有某些指定的IP可以訪問,具體配置辦法不在本文范疇之內,可查閱相關文檔或者搜索得到結果。
From:http://my.oschina.net/jack230230/blog/150470
Copyright © Linux教程網 All Rights Reserved