NMS Self-Healing dengan Nagios di CentOS 5.x
Pada mulanya, saya dengan Ayik berencana membangun sebuah mail server pesanan salah satu perguruan tinggi swasta di Yogyakarta. Awal pembahasannya sih, mail server ini tidak memiliki fitur macem-macem. Tapi belakangan Ayik punya ide untuk menanam monitoring server di mesin yang sama, dan dengan tambahan fitur self healing. Ide nya adalah, ketika salah satu service dari mesih tersebut mati, maka step pertama yang dilakukan mesin monitoring tersebut adalah mendeteksi bahwa service tersebut mati, kemudian akan mencoba melakukan restart service tersebut tanpa campur tangan administrator. Setelah dalam sekian kali percobaan dan service tetap terdeteksi mati, maka baru dilakukan alert ke administrator via email atau sms, atau media yang lainnya (denger-denger Nagios bisa kirim alert via Yahoo Messenger).
Jadi rencananya, Nagios ini akan digunakan juga untuk mendeteksi update Clamav, ssh, dan beberapa service yang bisa ditangani oleh Nagios. Tapi yang akan digunakan dalam contoh kali ini adalah service HTTP.
Asumsi
1. Operating system mesin yang digunakan adalah CentOS 5.x. Tetapi perintah-perintah di sini cukup compatible dengan mesin Linux/UNIX yang lainnya.
2. Nagios yang digunakan adalah versi 3.0.1 stable.
3. Instalasi CentOS sudah termasuk Apache2, PHP dan MySQL. Jika menggunakan CentOS versi 5.x, otomatis PHP yang digunakan adalah PHP5 dan versi MySQL juga versi 5.x.
4. Bukan server produksi.
Instalasi
1. Sebelum melakukan instalasi Nagios, yang perlu dipersiapkan adalah beberapa software yang akan diperlukan oleh Nagios. Maka kita tambahkan beberapa software tersebut dengan yum:
root# yum install gcc
root# yum install glibc glibc-common
root# yum install gd gd-devel
2. Setelah itu kita download Nagios dengan plugin-nya ke direktori /usr/local/src:
root# cd /usr/local/src
root# wget http://osdn.dl.sourceforge.net/sourceforge/nagios/nagios-3.0.1.tar.gz
root# wget http://osdn.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.11.tar.gz
3. Kita create beberapa group dan user:
root# /usr/sbin/useradd nagios
root# passwd nagios
root# /usr/sbin/groupadd nagcmd
root# /usr/sbin/usermod -G nagcmd nagios
root# /usr/sbin/usermod -G nagcmd apache
4. Lalu setelah itu proses instalasi Nagios dan plugin-nya:
root# cd /usr/local/src
root# tar -zxvf nagios-3.0.1.tar.gz
root# cd nagios-3.0.1
root# ./configure --with-command-group=nagcmd
root# make all
root# make install
root# make install-init
root# make install-config
root# make install-commandmode
Secara default Nagios akan terinstall di /usr/local/nagios
Edit alamat email yang terdapat di /usr/local/nagios/etc/objects/contacts.cfg, dan kita ganti dengan alamat email kita yang aktif. Setelah itu:
root# make install-webconf
root# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password:
Re-type new password:
Adding password for user nagiosadmin
Perintah make install-webconf di atas akan menambahkan konfigurasi web UI ke dalam Apache secara otomatis. Berbeda dengan langkah instalasi pada versi 2.x, pada versi ini kita tidak perlu menambahkannya secara manual. Lalu dengan htpasswd kita membuat login web Nagios. Semua keterangan tentang account yang memiliki hak akses akan disimpan di file /usr/local/nagios/etc/htpasswd.users.
5. Yang berikutnya adalah instalasi plugin Nagios:
root# cd /usr/local/src
root# tar -zxvf nagios-plugins-1.4.11.tar.gz
root# cd nagios-plugins-1.4.11
root# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
root# make
root# make install
Selesai dengan proses instalasinya. Sekarang tinggal kita tambahkan ke stratup system CentOS.
root# chkconfig --add nagios
root# chkconfig nagios on
Perintah tersebut akan menyebabkan Nagios secara otomatis diload saat CentOS restart.
6. Sebagai tambahan, jika setelah instalasi ini kita mendapati error bahwa ada permasalahan dengan Permission pada saat kita browse halaman Nagios, kita coba check pada log error Apache (biasanya di /var/log/httpd/error.log). Jika kita mendapati log seperti di bawah ini:
=================================================================================================================
[Mon Apr 14 02:07:55 2008] [error] [client 192.168.1.116] Premature end of script headers: status.cgi, referer: http://192.168.1.205/nagios/side.html
[Mon Apr 14 02:07:56 2008] [error] [client 192.168.1.116] (13)Permission denied: exec of '/usr/local/nagios/sbin/status.cgi' failed, referer: http://192.168.1.205/nagios/side.html
[Mon Apr 14 02:07:56 2008] [error] [client 192.168.1.116] Premature end of script headers: status.cgi, referer: http://192.168.1.205/nagios/side.html
[Mon Apr 14 02:07:56 2008] [error] [client 192.168.1.116] (13)Permission denied: exec of '/usr/local/nagios/sbin/statusmap.cgi' failed, referer: http://192.168.1.205/nagios/side.html
=================================================================================================================
kemungkinannya adalah setup SELinux yang ga sengaja secara default sistem keamannya malah menghambat Nagios. Jadi yang kita lakukan adalah:
root# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/
root# chcon -R -t httpd_sys_content_t /usr/local/nagios/share/
catatan tambahan lain:
Kadang-kadang, kita melihat bahwa service port 80 sudah up dan listen di semua ip address/interface, tetapi kita tidak bisa ngebrowse ke halaman web-nya. Ini disebabkan default iptables yang entah bagaimana bisa ngeblok akses ke halaman web. Jadi yang kita lakukan adalah melakukan flush di semua rule nya, dan kita definisikan lagi nanti setelah kita selesai melakukan semua setup yang kita perlukan di Nagios.
Self Healing Service
Sebenernya istilah Self Healing ini bukan didapat dari dokumentasi Nagios. Saya justru membacanya pertama kali di white paper yang ditulis oleh Mikko A.T. Pervilä yang bisa didapat di http://www.cs.helsinki.fi/u/niklande/opetus/SemK07/paper/pervila.pdf. Setelah membaca konsepnya, menurut saya cukup menarik juga karena hal ini akan menghemat waktu dan tenaga sebesar sekian kecil persen. Lumayanlah... daripada tidak sama sekali. Hehehe.
Sedangkan fitur pada Nagios yang bisa digunakan untuk melakukan hal ini adalah fitur Event Handlers. Yang perlu kita lakukan untuk mengaktifkan fitur ini adalah dengan meng-enable-nya dari file konfigurasi utama (nagios.cfg):
enable_event_handlers=1
Lalu mendeklarasikannya di host definition dan service definition. Dalam contoh ini, yang akan digunakan adalah host lokal (localhost). Localhost ini memiliki sebuah file konfigurasi default tersendiri pada instalasi standar Nagios. File tersebut adalah /usr/local/nagios/etc/objects/localhost.cfg. Oke, sekarang kita edit file tersebut pada block deklarasi service HTTP (biasanya paling bawah sendiri). Secara default konfigurasinya akan nampak seperti ini:
# Define a service to check HTTP on the local machine.
# Disable notifications for this service by default, as not all users may have HTTP enabled.
define service{
use generic-service ; Name of service template to use
host_name Mail_Server_Prototype
service_description HTTP
check_command check_http
}
Kita tambahkan pada blok tersebut sehingga nampak seperti di bawah ini:
# Define a service to check HTTP on the local machine.
# Disable notifications for this service by default, as not all users may have HTTP enabled.
define service{
use generic-service ; Name of service template to use
host_name Mail_Server_Prototype
service_description HTTP
check_command check_http
check_interval 1
max_check_attempts 4
event_handler restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
}
Setelah itu kita definisikan service restart-httpd di commands.cfg:
define command{
command_name restart-httpd
command_line /usr/local/nagios/libexec/eventhandlers/restart-httpd $SERVICESTATE$ $SERVICESTATETYPE$ $SERVICEATTEMPT$
}
Lalu restart Nagios:
root# service nagios restart
Untuk melakukan uji coba, buka dua console ke mesin Nagios, pada console pertama jalankan perintah ini:
root# watch -n1 "ps aux|grep httpd"
Perintah di atas akan mengawasi proses background HTTPD, dan akan diupdate setiap 1 detik.
Sedangkan pada console kedua, kita jalankan perintah2 berikut:
root# service httpd stop
root# tail -f /usr/local/nagios/var/nagios.log
Perintah pertama akan mematikan service HTTP untuk mensimulasikan masalah. Ini akan menyebabkan layar pada console pertama tidak mendeteksi keberadaan proses background HTTP. Sedangkan perintah kedua akan melihat update log Nagios. Jika setting kita berjalan dengan baik, pada layar console pertama akan menampilkan kembali proses background HTTP seperti ini:
root 6137 0.0 1.9 24772 9792 ? Ss 17:22 0:00 /usr/sbin/httpd
apache 6139 0.0 1.1 24968 5880 ? S 17:22 0:00 /usr/sbin/httpd
apache 6140 0.0 1.1 24968 5880 ? S 17:22 0:00 /usr/sbin/httpd
apache 6141 0.0 1.1 24968 5876 ? S 17:22 0:00 /usr/sbin/httpd
apache 6142 0.0 1.1 24968 5876 ? S 17:22 0:00 /usr/sbin/httpd
apache 6143 0.0 1.1 24968 5876 ? S 17:22 0:00 /usr/sbin/httpd
apache 6144 0.0 1.1 24968 5876 ? S 17:22 0:00 /usr/sbin/httpd
apache 6145 0.0 1.1 24968 5868 ? S 17:22 0:00 /usr/sbin/httpd
apache 6146 0.0 1.1 24968 5876 ? S 17:22 0:00 /usr/sbin/httpd
root 10113 0.0 0.1 3896 652 pts/0 R+ 18:54 0:00 grep httpd
Sedangkan pada layar di console ke dua akan menampilkan seperti ini:
[1208168516] SERVICE EVENT HANDLER: Mail_Server_Prototype;HTTP;(null);(null);(null);restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
[1208168576] SERVICE ALERT: Mail_Server_Prototype;HTTP;CRITICAL;HARD;4;Connection refused
[1208168576] SERVICE FLAPPING ALERT: Mail_Server_Prototype;HTTP;STARTED; Service appears to have started flapping (22.6% change >= 20.0% threshold)
[1208168576] SERVICE EVENT HANDLER: Mail_Server_Prototype;HTTP;(null);(null);(null);restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
[1208168636] SERVICE ALERT: Mail_Server_Prototype;HTTP;WARNING;HARD;4;HTTP WARNING: HTTP/1.1 403 Forbidden
[1208168636] SERVICE EVENT HANDLER: Mail_Server_Prototype;HTTP;(null);(null);(null);restart-httpd!$SERVICESTATE$!$SERVICESTATETYPE$!$SERVICEATTEMPT$
[1208169776] SERVICE FLAPPING ALERT: Mail_Server_Prototype;HTTP;STOPPED; Service appears to have stopped flapping (3.8% change < 5.0% threshold)
[1208172076] Auto-save of retention data completed successfully
Okay sementara ini dulu, untuk service lainnya akan saya bahas di posting selanjutnya.
SELAMAT MENCOBA!
Script restart-httpd
Berikut ini adalah isi dari script restart-httpd:
#!/bin/sh
#
# Event handler script for restarting the web server on the local machine
#
# Note: This script will only restart the web server if the service is
# retried 3 times (in a "soft" state) or if the web service somehow
# manages to fall into a "hard" error state.
#
# What state is the HTTP service in?
case "$1" in
OK)
# The service just came back up, so don't do anything...
;;
WARNING)
# We don't really care about warning states, since the service is probably still running...
;;
UNKNOWN)
# We don't know what might be causing an unknown error, so don't do anything...
;;
CRITICAL)
# Aha! The HTTP service appears to have a problem - perhaps we should restart the server...
# Is this a "soft" or a "hard" state?
case "$2" in
# We're in a "soft" state, meaning that Nagios is in the middle of retrying the
# check before it turns into a "hard" state and contacts get notified...
SOFT)
# What check attempt are we on? We don't want to restart the web server on the first
# check, because it may just be a fluke!
case "$3" in
# Wait until the check has been tried 3 times before restarting the web server.
# If the check fails on the 4th time (after we restart the web server), the state
# type will turn to "hard" and contacts will be notified of the problem.
# Hopefully this will restart the web server successfully, so the 4th check will
# result in a "soft" recovery. If that happens no one gets notified because we
# fixed the problem!
3)
echo -n "Restarting HTTP service (3rd soft critical state)..."
# Call the init script to restart the HTTPD server
/etc/rc.d/init.d/httpd restart
;;
esac
;;
# The HTTP service somehow managed to turn into a hard error without getting fixed.
# It should have been restarted by the code above, but for some reason it didn't.
# Let's give it one last try, shall we?
# Note: Contacts have already been notified of a problem with the service at this
# point (unless you disabled notifications for this service)
HARD)
echo -n "Restarting HTTP service..."
# Call the init script to restart the HTTPD server
/etc/rc.d/init.d/httpd restart
;;
esac
;;
esac
exit 0
Tidak ada komentar:
Posting Komentar