หน้า 1 จากทั้งหมด 1

มอนิเตอร์โปรแกรมบน CentOS ด้วย Monit

โพสต์โพสต์แล้ว: 30 ม.ค. 2010 01:17
โดย voip4share
มาดูวิธีการมอนิเตอร์โปรแกรมบน CentOS ด้วย Monit กันครับ ใครที่เคยลงโปรแกรมแล้วรันทิ้งไว้โดยคิดว่ามันจะทำงานไปตลอด ไม่ดื้อ ไม่อู้ แต่กลายเป็นว่าพอผ่านไปสักหน่อยโปรแกรมมันกลับดื้อ ไม่ทำงานเฉยเลย ไม่ก็หายไปเฉยๆ (ไม่เห็นมีโพรเซสว่ารันอยู่) ลองใช้โปรแกรม Monit ดูครับ มันจะคอยมอนิเตอร์แทนเรา ถ้าพบว่ามีโปรแกรมไหนไม่ทำงาน มันจะสั่งรีสตาร์ทเอง โปรแกรมนั้นก็จะกลับมาทำงานใหม่

เว็บไซต์ http://mmonit.com/monit/download/

1 ดาวน์โหลดและติดตั้งโปรแกรม Monit
โค้ด: เลือกทั้งหมด
cd /tmp
wget http://mmonit.com/monit/dist/monit-5.0.3.tar.gz
tar xzvf monit-5.0.3.tar.gz -C /usr/src
cd /usr/src/monit-5.0.3
./configure --prefix=/usr --exec-prefix=/usr --sysconfdir=/etc --localstatedir=/var
make
make install


ไฟล์ไบนารี่ /usr/bin/monit

2 สร้างไดเร็คตอรี่สำหรับ Monit
โค้ด: เลือกทั้งหมด
mkdir /etc/monitrc


3 แก้ไขไฟล์ /etc/inittab
เพื่อสั่งให้รัน monit ตอนเปิดเครื่อง เอาไว้บรรทัดล่างสุด
โค้ด: เลือกทั้งหมด
vi /etc/inittab

เพิ่มบรรทัดต่อไปนี้ไว้ล่างสุด

#Run Monit in Standard Run-Levels
mo:2345:respawn:/usr/bin/monit -Ic /etc/monitrc/monitrc

4 สร้างไฟล์คอนฟิกของ Monit
โค้ด: เลือกทั้งหมด
cd /etc/monit
vi monitrc


#
# Monit Control File
#
set daemon 120
set logfile syslog facility log_daemon
set alert systemalert@voip4share.com
set httpd port 2812
allow admin:monit
set mailserver mail.voip4share.com port 25 with timeout 15 seconds

check system localhost
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if memory usage > 75% then alert
if cpu usage (user) > 70% then alert
if cpu usage (system) > 30% then alert
if cpu usage (wait) > 20% then alert

include /etc/monitrc/asterisk.monitrc
include /etc/monitrc/httpd.monitrc

โค้ด: เลือกทั้งหมด
vi httpd.monitrc


check process httpd
with pidfile "/var/run/httpd.pid"
start program = "/etc/init.d/httpd start" with timeout 60 seconds
stop program = "/etc/init.d/httpd stop"
if 2 restarts within 3 cycles then timeout
if totalmem > 100 Mb then alert
if children > 255 for 5 cycles then stop
if cpu usage > 95% for 3 cycles then restart
if failed port 80 protocol http then restart
group apache
depends on httpd.conf

check file httpd.conf
with path /etc/httpd/conf/httpd.conf
# Reload httpd if the httpd.conf file was changed
if changed checksum
then exec "/usr/sbin/apachectl graceful"

บันทึกไฟล์

โค้ด: เลือกทั้งหมด
vi asterisk.monitrc


# Asterisk Monitoring

check process asterisk with pidfile /var/run/asterisk/asterisk.pid
group asterisk
start program = "/etc/init.d/asterisk start"
stop program = "/etc/init.d/asterisk stop"
if failed host 127.0.0.1 port 5038 then restart
if 5 restarts within 5 cycles then timeout

check host localhost with address 127.0.0.1
if failed port 5062 type udp protocol sip
with target "localhost:5062" and maxforward 6
then alert

บันทึกไฟล์

5 รีโหลดการทำงานของ Momit
โค้ด: เลือกทั้งหมด
telinit q