但Cacti 的運行要依靠的東西比較太多了,環境不好配是首要,再就要要跑多個PHP動態應用這對於許多站長都只有單臺服務器來說可真費勁。
Options[apache_conn]: gauge,nopercent,growright
Directory[apache_conn]: apache_conn
MaxBytes[apache_conn]: 4000
YLegend[apache_conn]: apache_conn
ShortLegend[apache_conn]:
LegendI[apache_conn]: apache all coonn:
LegendO[apache_conn]: apache now conn
Title[apache_conn]: apache conn
PageTop[apache_conn]:http conn
然後把以下perl代碼創建爲 mrtg.http 存於 mrtg 根目錄中並賜予可執行權限(chmod +x mrtg.http)
$hostname=`hostname`;
$hostname=~s/\s+$//;
$apache_all_conn=`netstat -an|grep 127.0.0.1:80|wc -l`;
$apache_conn=`netstat -an|grep 127.0.0.1|grep ESTABLISHED|wc -l`;
$apache_all_conn=~s/\n$//;
$apache_conn=~s/\n$//;
$apache_all_conn=~s/^\s+|\s+$//;
$apache_conn=~s/^\s+|\s+$//;
$gettime=`uptime|awk '{print \$1" "\$3" "\$4}'`;
$gettime=~s/\,|\n$//g;
print("$apache_all_conn\n");
print("$apache_conn\n");
print("$gettime\n");
print("$hostname\n");
該方法同樣能用在 Nginx 上,因爲一樣都是80端口。
但該方法有個弊端,如果你有另外一個後端。例如 Nginx 反向代理 proxy 到後端 apache。 這時 apache 是 8080 端口的話 這麼數值就有重複了。
然而,就算你使用 8181 這樣的端口號來避開重複,也不是萬全之計。畢竟這不是Nginx 或者 apache 原生的併發數獲知方式。
而 MRTG,也行!!!之前網上找偏東南西北都沒有一篇寫這內容的,最後還是自己研究。今天我就要好好給大家分享分享!
我們需要像 Cacti 那樣,抓取到 NginxStatus 上特定的數值,然後給 MRTG 轉化成圖表。
server accepts handled requests
60085 60085 175930
Reading: 0 Writing: 1 Waiting: 53
好了,內容中,不單有數據,還有解析。我們的構想是! 只要數據,不要解析。而且要的還只是指定的數據。
# curl http://127.0.0.1/NginxStatus | grep Active 這樣! 我們就能緊緊獲取到包含有 Active 字樣的內容:
# curl http://127.0.0.1/NginxStatus |grep Active |awk '{print $3 }' 這樣!我們就能緊緊獲取到內容中第三列的內容。即純數值 :54
於是乎,我們的 shell 腳本就有2行:
curl http://127.0.0.1/NginxStatus |grep Waiting |awk '{print $6 }' > /opt/mrtg/ngx.waiting
`/opt/mrtg/nginx_status`; #調用剛纔的shell腳本從 NginxStatus 中獲取數值到文本。
$hostname=`hostname`;
$hostname=~s/\s+$//;
$apache_all_conn=`tail /opt/mrtg/ngx.active`; #數值文本路徑按照不同配置自行修改
$apache_conn=`tail /opt/mrtg/ngx.waiting`; #數值文本路徑按照不同配置自行修改
$apache_all_conn=~s/\n$//;
$apache_conn=~s/\n$//;
$apache_all_conn=~s/^\s+|\s+$//;
$apache_conn=~s/^\s+|\s+$//;
$gettime=`uptime|awk '{print \$1" "\$3" "\$4}'`;
$gettime=~s/\,|\n$//g;
print("$apache_all_conn\n");
print("$apache_conn\n");
print("$gettime\n");
print("$hostname\n");
Options[apache_conn]: gauge,nopercent,growright
Directory[apache_conn]: nginx_conn # 生成網頁的目錄
MaxBytes[apache_conn]: 4000
YLegend[apache_conn]: nginx_conn # 圖表中的名字
ShortLegend[apache_conn]:
LegendI[apache_conn]: Active connections: # 圖表中綠色曲線所代表的參數
LegendO[apache_conn]: Waiting: # 圖表中藍色曲線所代表的參數
Title[apache_conn]: Nginx # 圖表的擡頭名稱
PageTop[apache_conn]:nginx # 圖表的頁面名稱
至此,MRTG 爲 Nginx (NginxStatus) 生成圖表的配置大功告成!!
Max | Average | Current | |
---|---|---|---|
Active connections: | 188.0 B/s | 51.0 B/s | 132.0 B/s |
Waiting: | 186.0 B/s | 46.0 B/s | 130.0 B/s |