Apache & MySQL Monitoring via MRTG


Setup snmp snmpd
-------------------------

$sudo apt-get install snmpd snmp

Edit snmpd config file
---------------------------

$ sudo nano /etc/snmp/snmpd.conf

#com2sec paranoid  default         public
com2sec readonly  default         public
#com2sec readwrite default         private

SNMP walk get information
---------------------------------

$ snmpwalk -c public -v 1 localhost system

Setup mrtg
--------------

$ sudo apt-get install mrtg

Edit Apache Config
-----------------------

$ sudo nano /etc/apache2/apache2.conf

-------------------------------
Alias /mrtg /var/www/mrtg
<Location /mrtg>
    Order deny,allow
    Deny from all
    Allow from all
</Location>
-------------------------------

$ sudo /etc/init.d/apache2 force-reload


Mkdir dir for myhost.domain
-----------------------------------
$ sudo mkdir -p /var/www/mrtg/myhost

Make dir for mrtg config
--------------------------------

$ sudo mkdir -p /etc/mrtg

Make config for myhost.domain
----------------------------------------

$ sudo cfgmaker  -global "options[_]:bits,growright" --global "workdir: /var/www/mrtg/myhost" [email protected] > /tmp/mrtg-myhost.cfg
$ sudo cp  /tmp/mrtg-myhost.cfg /etc/mrtg/mrtg-myhost.cfg

Make the first result
-------------------------
$ sudo env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg-myhost.cfg

Make index file
-------------------
$ sudo indexmaker --column=1 --output=/var/www/mrtg/myhost/index.html /etc/mrtg/mrtg-myhost.cfg

Save to crontab config
----------------------------
$ sudo nano /etc/cron.d/mrtg-myhost

-------------
*/5 * * * * root  env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg-myhost.cfg
---------------

$ sudo /etc/init.d/cron restart

browse for th result at http://mywebserver/mrtg/myhost

# ตัวอย่างการทำกราฟ Idle CPU and Load
ใช้ข้อมูล ssCpuIdle.0 และ laLoadInt.1 จาก snmp แสดงผลภาพเป็นกราฟสองเส้น

สร้างแฟ้ม /etc/mrtg/myhost-cpu.cfg มีข้อมูลตัวอย่างประมาณว่า


WorkDir: /var/www/mrtg/myhost
LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[myhost-cpu]:ssCpuIdle.0&laLoadInt.1:public@localhost
RouterUptime[myhost-cpu]: public@localhost
MaxBytes[myhost-cpu]: 10000
Title[myhost-cpu]: Idle CPU and Load average
PageTop[myhost-cpu]: <H1>Idle CPU and Load average</H1>
ShortLegend[myhost-cpu]: %
YLegend[myhost-cpu]: CPU and Load Utilization
Legend1[myhost-cpu]: Idle CPU
Legend2[myhost-cpu]: Load average
LegendI[myhost-cpu]:  Idle
LegendO[myhost-cpu]:  Load
Options[myhost-cpu]: gauge, nopercent, growright

ทดสอบสร้างภาพต้นแบบด้วยคำสั่ง sudo env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-cpu.cfg

# ตัวอย่างการทำกราฟ memory และ swap
ใช้ข้อมูล memAvailReal.0 และ memTotalSwap.0 จาก snmp แสดงผลภาพเป็นกราฟสองเส้น

สร้างแฟ้ม /etc/mrtg/myhost-mem.cfg มีข้อมูลตัวอย่างประมาณว่า


WorkDir: /var/www/mrtg/myhost
LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[myhost-mem]: memAvailReal.0&memTotalSwap.0:public@localhost - memIndex.0&memAvailSwap.0:public@localhost
RouterUptime[myhost-mem]: public@localhost
MaxBytes[myhost-mem]: 2000000
Title[myhost-mem]: Free Memory and Swap Used
PageTop[myhost-mem]: <H1>Free Memory and Swap Used</H1>
kMG[myhost-mem]: k,M,G,T,P,X
ShortLegend[myhost-mem]: bytes
YLegend[myhost-mem]: bytes
LegendI[myhost-mem]:  Free Memory:
LegendO[myhost-mem]: Swap Used:
Legend1[myhost-mem]: Free memory, in bytes
Legend2[myhost-mem]: Swap Used, in bytes
Options[myhost-mem]: gauge, nopercent, growright

ทดสอบสร้างภาพต้นแบบด้วยคำสั่ง sudo env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-mem.cfg

# ตัวอย่างทำกราฟ CPU wait for IO เพียงเส้นเดียว
ไม่ได้ใช้ข้อมูลจาก snmp แต่ดึงมาจาก shell script ชื่อ /etc/mrtg/get-cpu-io.sh

สร้างแฟ้ม /etc/mrtg/get-cpu-io.sh มีข้อมูลว่า


#!/bin/bash
CPU_WAIT_IO=$(vmstat|tail -1|awk '{print $16}')
TIME=$(uptime)
echo "$CPU_WAIT_IO"
echo "0"
echo "$TIME"
echo "MYHOST"
เสร็จแล้วแก้ไขให้ execute ได้ด้วยคำสั่ง sudo chmod +x /etc/mrtg/get-cpu-io.sh

สร้างแฟ้ม /etc/mrtg/myhost-cpu-io.cfg มีข้อมูลตัวอย่างประมาณว่า
WorkDir: /var/www/mrtg/myhost
Target[myhost-cpu-io]:`/etc/mrtg/get-cpu-io.sh`
MaxBytes[myhost-cpu-io]: 100
Title[myhost-cpu-io]: CPU Time spent waiting for IO
PageTop[myhost-cpu-io]: <H1>CPU Time spent waiting for IO</H1>
ShortLegend[myhost-cpu-io]: %
YLegend[myhost-cpu-io]: wait IO
Legend1[myhost-cpu-io]: current:
Legend2[myhost-cpu-io]:
LegendI[myhost-cpu-io]: current, %
LegendO[myhost-cpu-io]:
Options[myhost-cpu-io]: gauge, nopercent, growright

ทดสอบสร้างภาพต้นแบบด้วยคำสั่ง sudo env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-cpu-io.cfg

# ตัวอย่างทำกราฟ Apache connection เพียงเส้นเดียว
ไม่ได้ใช้ข้อมูลจาก snmp แต่ดึงมาจาก shell script ชื่อ /etc/mrtg/get-apache-session.sh

สร้างแฟ้ม /etc/mrtg/get-apache-session.sh มีข้อมูลว่า
#!/bin/bash
SESSION=$(ps ax|grep apache2|grep -c -v grep)
TIME=$(uptime)
echo "$SESSION"
echo "0"
echo "$TIME"
echo "MYHOST"
เสร็จแล้วแก้ไขให้ execute ได้ด้วยคำสั่ง sudo chmod +x /etc/mrtg/get-apache-session.sh

สร้างแฟ้ม /etc/mrtg/myhost-apache-session.cfg มีข้อมูลตัวอย่างประมาณว่า
WorkDir: /var/www/mrtg/myhost
Target[myhost-apache-session]:`/etc/mrtg/get-apache-session.sh`
MaxBytes[myhost-apache-session]: 1000
Title[myhost-apache-session]: Apache connection
PageTop[myhost-apache-session]: <H1>Apache connection</H1>
ShortLegend[myhost-apache-session]: conns
YLegend[myhost-apache-session]: conns
Legend1[myhost-apache-session]: current:
Legend2[myhost-apache-session]:
LegendI[myhost-apache-session]: current, number of connections
LegendO[myhost-apache-session]:
Options[myhost-apache-session]: gauge, nopercent, growright

ทดสอบสร้างภาพต้นแบบด้วยคำสั่ง sudo env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-apache-session.cfg

# ตัวอย่างทำกราฟ Apache memory เพียงเส้นเดียว
ไม่ได้ใช้ข้อมูลจาก snmp แต่ดึงมาจาก shell script ชื่อ /etc/mrtg/get-apache-mem.sh

สร้างแฟ้ม /etc/mrtg/get-apache-mem.sh มีข้อมูลว่า
#!/bin/bash
TEMP1="/tmp/get-apache-mem-temp1.tmp.$$"
ps ax|grep "apache2"|grep -v "grep"|awk '{print $1}' > $TEMP1
MEMORY=0
while read LINE ; do
  PRIVATE=$(pmap -d $LINE|tail -1|awk '{print $4}'|cut -d'K' -f1)
  MEMORY=$(expr $MEMORY + $PRIVATE)
done < $TEMP1
TIME=$(uptime)
echo $MEMORY
echo "0"
echo "$TIME"
echo "MYHOST"
rm $TEMP1

เสร็จแล้วแก้ไขให้ execute ได้ด้วยคำสั่ง sudo chmod +x /etc/mrtg/get-apache-mem.sh

สร้างแฟ้ม /etc/mrtg/myhost-apache-mem.cfg มีข้อมูลตัวอย่างประมาณว่า
WorkDir: /var/www/mrtg/myhost
Target[myhost-apache-mem]: `/etc/mrtg/get-apache-mem.sh`
MaxBytes[myhost-apache-mem]: 10000000
Title[myhost-apache-mem]: Apache memory usage
PageTop[myhost-apache-mem]: <H1>Apache memory usage</H1>
kMG[myhost-apache-mem]: k,M,G,T,P,X
ShortLegend[myhost-apache-mem]: bytes
YLegend[myhost-apache-mem]: bytes
Legend1[myhost-apache-mem]: private memory:
Legend2[myhost-apache-mem]:
LegendI[myhost-apache-mem]: private memory, in bytes
LegendO[myhost-apache-mem]:
Options[myhost-apache-mem]: gauge, nopercent, growright

ทดสอบสร้างภาพต้นแบบด้วยคำสั่ง sudo env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-apache-mem.cfg

# ตัวอย่างทำกราฟ Mysql connection เพียงเส้นเดียว
ไม่ได้ใช้ข้อมูลจาก snmp แต่ดึงมาจาก shell script ชื่อ /etc/mrtg/get-mysql-session.sh

สร้างแฟ้ม /etc/mrtg/get-mysql-session.sh มีข้อมูลว่า
#!/bin/bash
SESSION=$(mysqladmin -uroot -pmysqlpassword extended|grep "Threads_connected"|cut -d'|' -f3)
TIME=$(uptime)
echo "$SESSION"
echo "0"
echo "$TIME"
echo "MYHOST"


เสร็จแล้วแก้ไขให้ execute ได้ด้วยคำสั่ง sudo chmod +x /etc/mrtg/get-mysql-session.sh

สร้างแฟ้ม /etc/mrtg/myhost-mysql-session.cfg มีข้อมูลตัวอย่างประมาณว่า


WorkDir: /var/www/mrtg/myhost
Target[myhost-mysql-session]:`/etc/mrtg/get-mysql-session.sh`
MaxBytes[myhost-mysql-session]: 1000
Title[myhost-mysql-session]: Mysql connection
PageTop[myhost-mysql-session]: <H1>Mysql connection</H1>
ShortLegend[myhost-mysql-session]: conns
YLegend[myhost-mysql-session]: conns
Legend1[myhost-mysql-session]: current:
Legend2[myhost-mysql-session]:
LegendI[myhost-mysql-session]: current, number of connections
LegendO[myhost-mysql-session]:
Options[myhost-mysql-session]: gauge, nopercent, growright

ทดสอบสร้างภาพต้นแบบด้วยคำสั่ง sudo env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-mysql-session.cfg

# ตัวอย่างทำกราฟ Mysql memory เพียงเส้นเดียว
ไม่ได้ใช้ข้อมูลจาก snmp แต่ดึงมาจาก shell script ชื่อ /etc/mrtg/get-mysql-mem.sh

สร้างแฟ้ม /etc/mrtg/get-mysql-mem.sh มีข้อมูลว่า
#!/bin/bash
MYSQL_PID=$(ps ax|grep "mysqld.pid"|grep -v "grep"|awk '{print $1}')
MEMORY=$(pmap -d $MYSQL_PID|tail -1|awk '{print $4}'|cut -d'K' -f1)
TIME=$(uptime)
echo $MEMORY
echo "0"
echo "$TIME"
echo "MYHOST"

เสร็จแล้วแก้ไขให้ execute ได้ด้วยคำสั่ง sudo chmod +x /etc/mrtg/get-mysql-mem.sh

สร้างแฟ้ม /etc/mrtg/myhost-mysql-mem.cfg มีข้อมูลตัวอย่างประมาณว่า
WorkDir: /var/www/mrtg/myhost
Target[myhost-mysql-mem]: `/etc/mrtg/get-mysql-mem.sh`
MaxBytes[myhost-mysql-mem]: 10000000
Title[myhost-mysql-mem]: Mysql memory usage
PageTop[myhost-mysql-mem]: <H1>Mysql memory usage</H1>
kMG[myhost-mysql-mem]: k,M,G,T,P,X
ShortLegend[myhost-mysql-mem]: bytes
YLegend[myhost-mysql-mem]: bytes
Legend1[myhost-mysql-mem]: private memory:
Legend2[myhost-mysql-mem]:
LegendI[myhost-mysql-mem]: private memory, in bytes
LegendO[myhost-mysql-mem]:
Options[myhost-mysql-mem]: gauge, nopercent, growright

ทดสอบสร้างภาพต้นแบบด้วยคำสั่ง sudo env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-mysql-mem.cfg
# ตัวอย่างการทำกราฟ TCP Establish connection
ใช้ข้อมูล tcpCurrEstab.0 จาก snmp แสดงผลภาพเป็นกราฟเพียงเส้นเดียว

สร้างแฟ้ม /etc/mrtg/myhost-tcpestab.cfg มีข้อมูลตัวอย่างประมาณว่า

WorkDir: /var/www/mrtg/myhost
LoadMIBs: /usr/share/snmp/mibs/TCP-MIB.txt
Target[myhost-tcpestab]: tcpCurrEstab.0&tcpCurrEstab.0:public@localhost
RouterUptime[myhost-tcpestab]: public@localhost
MaxBytes[myhost-tcpestab]: 1000000
Title[myhost-tcpestab]: TCP Current Establish
PageTop[myhost-tcpestab]: <H1>TCP Current Establish </H1>
ShortLegend[myhost-tcpestab]: conns
YLegend[myhost-tcpestab]: conns
LegendI[myhost-tcpestab]: Current:
LegendO[myhost-tcpestab]:
Legend1[myhost-tcpestab]: Current, numbers of connections
Legend2[myhost-tcpestab]:
Options[myhost-tcpestab]: nopercent, growright, gauge

ทดสอบสร้างภาพต้นแบบด้วยคำสั่ง sudo env LANG=C /usr/bin/mrtg /etc/mrtg/myhost-tcpestab.cfg

# ตัวอย่างคำสั่งในการแสดง index สำหรับกราฟหลายรูปในหน้าเดียว

sudo indexmaker --column=1 --output=/var/www/mrtg/myhost/index.html /etc/mrtg/myhost-tcpestab.cfg  /etc/mrtg/myhost-cpu.cfg  /etc/mrtg/mrtg-myhost.cfg

คำสำคัญ (Tags): #mrtg#snmp
หมายเลขบันทึก: 234262เขียนเมื่อ 9 มกราคม 2009 11:22 น. ()แก้ไขเมื่อ 13 มิถุนายน 2012 13:37 น. ()สัญญาอนุญาต: จำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (0)

ไม่มีความเห็น

พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท