︿
Top

1、前言

本篇實作為利用 Apache ExtendedStatus 功能,讓 Apache 即時顯示伺服器狀態並利用文字模式網頁瀏覽器 lynx 去抓取 BusyServers、IdleServers 的欄位值來畫出 MRTG 圖形;在開始玩本次實作以前請先確定您的 SNMP 及 MRTG 已安裝並正確定設定。




文章目錄

1、前言
2、實作環境
3、安裝及設定
          步驟1.開啟 Apache ExtendedStatus 功能
          步驟2.安裝 lynx 套件
          步驟3.建立 MRTG 設定檔 busy-idle.cfg
          步驟4.產生檔案
          步驟5.寫入排程



2、實作環境

  • FreeBSD 6.2-RELEASE-p5
  • lynx-2.8.6_5,1



3、安裝及設定

步驟1.開啟 Apache ExtendedStatus 功能

Apache 內建 server-status 及 server-info 二種可即時觀看伺服器狀態及模組的方法,Apache 設定檔預設是將這些選項功能關閉 (註解掉),要使用很簡單只要把註解符號 (#) 拿掉,另外記得設定只允許內部的網段去查看即可 (避免安全性問題)。
vi /usr/local/etc/apache/httpd.conf
 ExtendedStatus On              //預設值此行為註解掉請將註解符號 (#) 拿掉
 ###server-status 伺服器狀態資訊
 <Location /server-status>
     SetHandler server-status
     Order deny,allow
     Deny from all
     Allow from 192.168.1.      //僅允許內部網段 192.168.1 能觀看此數值
 </Location>

先測試一下是否可觀看到 Apache Server Status,成果 http://Your_FQDN/server-status。
可在加上如下參數 (ex.http://Your_FQDN/server-status?refresh 為每秒更新一次)
notable   -- 用來支援無法顯示 Table 的 Browser
refresh   -- 每 1 秒更新一次畫面
refresh=n -- 每 n 秒更新一次畫面
auto      -- 顯示 apache 的狀態資料

關於 Apache Server Status 內各欄位的解釋,因為在 twbsd.org 13.5 Apache 伺服器狀態與管理 內已有詳細說明,故不在此多述。



步驟2.安裝 lynx 套件

在進行抓取 Apache Server Status 內 BusyServers、IdleServers 欄位值之前我們先安裝文字模式網頁瀏覽器 lynx 已便屆時可順利抓取需要的欄位值。
cd /usr/ports/www/lynx     //切換至安裝路徑
make install clean          //安裝套件並清除暫存檔案


測試抓取 Apache Server Status 內 BusyServers 欄位值,環境說明如下:
  • Apache Server: http://192.168.1.13
  • 過濾欄位值: BusyServers:
  • 抓取欄位值: 也就是從 BusyServers: 之後開始抓取
lynx -dump 'http://192.168.1.13/server-status?auto' | grep BusyServers: | cut -c 13-15
建立 busy-idle.sh 內容就是抓取 BusyServers、IdleServers 欄位值 (編輯完後記得權限要設為 755 哦)。
vi /usr/local/etc/mrtg/busy-idle.sh
 #!/bin/sh
 BUSY=`lynx -dump 'http://192.168.1.13/server-status?auto' | grep BusyServers: | cut -c 13-15`
 IDLE=`lynx -dump 'http://192.168.1.13/server-status?auto' | grep IdleServers: | cut -c 13-15`
 echo $BUSY
 echo $IDLE
 echo '0'
 echo '0'




步驟3.建立 MRTG 設定檔 busy-idle.cfg

抓取 BusyServers、IdleServers 欄位值後便利用 MRTG 把抓取的數值畫出圖來了,MRTG 設定檔 busy-idle.cfg 內容如下:
Targetapache: `/usr/local/etc/mrtg/busy-idle.sh`                   //抓取欄位值
Optionsapache: growright,gauge
Titleapache: Apache Load analysis -- www.weithenn.org              //網頁的 title
PageTopapache: <H1>Apache Load Analysis -- www.weithenn.org</H1>   //網頁標題
MaxBytesapache: 120                                                //流量圖表的最大值
YLegendapache: Busy Idle Server                                    //流量圖的 Y 軸顯示名稱
ShortLegendapache: Servers
LegendIapache: Busy Servers
LegendOapache: Idle Servers
Legend1apache: Busy Servers
Legend2apache: Idle Servers
WorkDir:/usr/local/www/data/analysis/mrtg/                         //存放產生圖檔的路徑
Language:big5                                                      //使用語系




步驟4.產生檔案

產生檔案,下列指令執行 2 ~ 3 次到沒錯誤訊息後就代表完成了。
/usr/local/bin/mrtg /usr/local/etc/mrtg/busy-idle.cfg  



步驟5.寫入排程

將如下指令寫入排程內以便達成自動產生流量圖表 (每五分鐘執行一次)。
crontab -e
 */5 * * * *   /usr/local/bin/mrtg /usr/local/etc/mrtg/busy-idle.cfg 
文章標籤: