發新話題
打印

如何應用Linux監控遠端主機狀態,建立Alert機制

如何應用Linux監控遠端主機狀態,建立Alert機制

前言
IT人員在公司裡面對使用者反應系統有問題不外兩種反應:
以下為對話情境:
1.User:公司Web不能連線是不是有問題?
IT:是嗎?我檢查一下server,再回覆你。
2.User:mail不能收發,是不是有問題?
IT:因為郵件伺服器有問題,我們正在處理,待兒就可使用了。
兩種情況我想大部分的User都會覺得第二種給他的感覺比較好
面對系統有問題IT人員能第一時間發現,並解決問題,不會讓User總是覺得IT人員慢半拍
因此本文將針對Serve狀態作監控,一有異常馬上通知系統管理者
本文適用:熟悉Linux Shell Script的IT人員
此監控程式將透過Linux主機以ping及namp監控遠端主機Service port的方式,每五鐘監控一次
若主機主機或service沒有回應,則通知管理者,若serivce恢復正常,則再通知管理者。
通知的方式有郵件、手機簡訊、Winpop
環境:
Linux Centos 5.0 + nmap + smbclient
要監控的主機為遠端的
File Server 10.1.1.3
Web Server 10.1.1.2


首先編輯一個script檔
內容如下:
!#/bin/bash

FILESRV=`ping -c1 10.1.1.3 |grep 10.1.1.3 |grep -c ttl`
FILESRV_status=`grep -c down /root/Filesrv-status`
#定義兩個變數,第一個變數為使用ping的方式ping主機,由回應值去判斷主機是否正常
#第二個變數為確認/root/Filesrv-status是否有down字串

if [ $FILESRV -eq 0 ] && [ $FILESRV_status -eq 0 ]
then
mail -s "Fileserver down" [email protected] < /root/test
echo 'Fileserver down' > /root/Filesrv-status

elif [ $FILESRV -eq 1 ] && [ $FILESRV_status -eq 1 ]
then
mail -s "Fileserver live" [email protected] < /root/test
echo > /root/Filesrv-status
fi

#接下來寫一個判斷式,如果第一、二個變數的結果皆為0,則表示主機已經無回應
#那麼就發mail主旨為filesrv down內容為空白的通知信通知管理者,/root/test的內容為空白
#並將 Fileserver down這個字串放到/root/FILESRV-status 若出現第一、二個變數皆為1
#則表示主機已經正常,那麼就發mail通知管理者,並將/root/Filesrv-status檔案清空。
#這樣我們就完成了一台主機的監控。
#接下來我們要監控第二台主機Web Server

WEB=`nmap 10.1.1.2 -p 80 |grep -c '80/tcp open' `
WEB_status=`grep -c down /root/WEB-status`

#同樣定義兩個變數,第一個變數是用nmap去判斷Web Server主機的80 port是否有通,
#第二個變變數是確認/root/WEB-status是否有down字串

if [ $WEB -eq 0 ] && [ $WEB_status -eq 0 ]
then
mail -s "WEB Server web service down" [email protected] < /root/test
echo 'WEB Server web service down' > /root/WEB-status

elif [ $WEB -eq 1 ] && [ $WEB_status -eq 1 ]
then
mail -s "WEB Server web service live" [email protected] < /root/test
echo > /root/WEB-status
fi

#以上的判斷式跟上面的大同小異,只是把發mail的內容換成Web主機


到這邊這個script就好了,我們將他們命名為detect.sh,放於/root/
然後在/root/裡新增兩個檔案
touch Filesrv-status WEB-status,作為detect.sh裡的變數判斷檔案
在/etc/crontab 新增以下字串
*/5 * * * * root /root/detect.sh

剛剛談到的是使用郵件的通知,那像windwos的 net send方式通知如何作
因為Linux裡沒有net send這個指令,不過我們可以使用Samba-client這個套件裡的幾個指令達成
系統需安裝samba-client

yum install samba-client

裝完編輯一個script檔
內容為
#!/bin/bash

case "$1" in
send)
echo "$3"|smbclient -I "$2" -M `nmblookup -A "$2"|sed -e '1d' -e '3,/*/d'|cut -f2|cut -d' ' -f1`
;;
*)
echo "Usage:net send "
exit 1
esac

我們將它存檔為msg
現在我們可以用這個指令加send來送訊息到windows,不過前提是windwos xp 要將messenger這個service開啟
只要輸入 msg send 10.1.1.5 "hello world"就可以將hello world這個訊息送到10.1.1.5這個windows主機
所以運用以下方式我們可以將它加入至detect.sh這個script作為通知使用。

再來我們來談一下手機簡訊如何作
手機簡訊目前我了解是很多種作法,我這提兩種作法
第一種:就是利用郵件發到一特定的email,郵件內容就包含帳號密碼及要送至的手機號碼及簡訊內容
第二種:透過程式跟遠端的主機溝通,將要送的訊息傳給程式
以上兩種方式我都用過,第一種方式請參考http://www.twsms.com/
本文是使用第二種方式,就是hinet企業簡訊
參考網址http://sms.hinet.net/new/sent_program.htm
Linux環境我們使用perl,程式如下:

#!/usr/bin/perl -w

use Time::Local;
use hiairv2;

##----以下資料請自行更改
$account=""; #帳號
$passwd=""; #密碼
$mobile_num=""; #收訊手機號碼
$Message="hiAir-Perl2-簡訊測試"; #傳送簡訊內容
$send_type=1; #傳送形式=1 -> 即時傳送
##----send_type=2,3,4 時才有作用 ----##
$send_time="080110171900"; #預約傳送時間 2008/01/10 17:19
$expire_time="1440"; #重送截止時間 一天


print "Mobile_num = " . $mobile_num . "\n";

##---- 產生 hiairv2物件 ----##
$v2_con = hiairv2->new( host_ip=>"202.39.54.130",
host_port=>"8000",
user_name=>$account,
user_passwd=>$passwd,
);

@ret = $v2_con->init_connect();
if($ret[0]!=0){
print "hiairv2 connecton error! ret_code=" . $ret[0] .",ret_content:" . $ret[1];
$v2_con->close_hiairv2();
exit(0);
}

@ret = $v2_con->send_text($mobile_num,$send_type,$send_time,$expire_time,$Message);
if($ret[0]!=0){
print "hiairv2 send_text error! ret_code=" . $ret[0] .",ret_content:" . $ret[1];
$v2_con->close_hiairv2();
exit(0);
}

##---- close connection ----##
$v2_con->close_hiairv2();
print "ret_code=" . $ret[0] .",ret_content:" . $ret[1] . "\n";

LogFile($mobile_num,$ret[0],$ret[1],$Message);

sub LogFile{
#取得系統時間
(my $sec,my $min,my $hour,my $mday,my $mon,my $year,$_,$_,$_) = localtime(time);
$year = $year+1900;
$mon = $mon+1;
if($mon < 10){
$mon = "0".$mon;
}
if($mday < 10){
$mday = "0".$mday;
}
$now_time = $year . $mon . $mday . " " . $hour . ":" . $min . ":" . $sec ;

open(Log_File,">>send.log");
print Log_File $now_time ." ". $_[0] . " " . $_[1] . " ". $_[2]. " " . $_[3] . "\n";
close Log_File;
}


檔案名稱取為send.pl
我們只要修改上面這兩個地方即可

$mobile_num=""; #收訊手機號碼
$Message="hiAir-Perl2-簡訊測試"; #傳送簡訊內容

執行perl程式在Linux只要使用perl send.pl即可

結論
系統監控Alert在商用軟體裡不乏相關功能,像whatup..等,本文只是基本的應用,實際更多的應用留待讀者去發掘,Linux的優點就是彈性,只是我們要花更多時間去熟悉其系統運作,本文的監控系統希望能讓系統管理者在系統出現異常時能第一時間即時反應,減少down time時間,讓處理效率更好。

發佈網址:http://blog.roodo.com/yungchingman
email:[email protected]

TOP

發新話題