最近想做一個tool 來解決機器硬件監控的問題,但是又不想去額外的申請防火牆
我想了一下覺得可以做,記錄一下:
需求就是監控一批機器並且用過web 來展示狀態
如果採用普通方案,勢必引入防火牆問題,web servers 還好可以添加一個api ,其他類型的機器就不得不申請開放防火牆
留意到我們所有的機器都是能在我們的desktop 去訪問的,很自然的想到可以利用22 端口搞點東西,於是
(生產消費模式)
首先在我們在windows 上面搭一個虛擬機,採用橋接的方式從網關獲取一個ip(方便操作,且可被局域網訪問到)
然後我們通過expect 腳本安裝執行的腳本到目標機器開啓定時器就好了。爲了可靠,在輪詢和自查腳本上都要開啓郵件提醒,這樣某臺機器掛掉有提醒,desktop 因爲颱風停電等因素關機,目標機器也還在監控,另外還要注意輪詢腳本要加上定期清理虛擬機的空間。
缺陷就是執行週期長(分鐘級別),機器列表不能太多, 雖然通常我們也不需要那麼強的實時性,當然這也只是鬧着玩的,而且是在極限編程(30h)的情況下寫的,還存在很多問題,個人覺得的大公司會買更加可靠的service 方式的監控產品,小公司可能也只需要自查腳本就好了。
附上一張我們的前端效果圖(兩臺機器cpu 實時使用率圖)
在附上代碼:
expect:
#!/usr/bin/expect
#scp.exp
# host user psw path file1 file2...
set timeout -1
set host [lindex $argv 0]
set user [lindex $argv 1]
set psw [lindex $argv 2]
set path [lindex $argv 3]
set file [lindex $argv 4]
set file1 [lindex $argv 5]
set file2 [lindex $argv 6]
set file3 [lindex $argv 7]
spawn scp $file $file1 $file2 $file3 $user@$host:$path
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*password: " { send "$psw\n"; exp_continue }
}
#!/usr/bin/expect
# host user psw path file
#ssh.exp
set timeout -1
set host [lindex $argv 0]
set user [lindex $argv 1]
set psw [lindex $argv 2]
set path [lindex $argv 3]
set file [lindex $argv 4]
spawn ssh -t -p 22 $user@$host "$path/$file"
expect {
"*yes/no" { send "yes\r"; exp_continue}
"*assword*" { send "$psw\n"; exp_continue }
}
輪詢bash:
#!/bin/bash
#remote.sh
serverlist=`cat ../source/serverlist.txt`
user="xxxxx"
psw="xxxxxx"
path="xxxxx"
scp_file="../bin/smtp.py ../bin/check.sh ../bin/get.sh ../bin/update.sh"
ssh_file="../bin/get.sh"
iserror=0
info=""
if [ $1 == "update" ]
then
for host in $serverlist
do
./scp.exp $host $user $psw $path $scp_file >/dev/null 2>&1
ssh_file="../bin/update.sh"
ssh_reslut=`./ssh.exp $host $user $psw $path $ssh_file |grep success`
done
else
for host in $serverlist
do
ssh_reslut=`./ssh.exp $host $user $psw $path $ssh_file |grep Ana`
if [ -z "$ssh_reslut" ]
then
iserror=1
info=$info" "$host
else
#do something/collect infomation
fi
done
fi
#
#防止郵件轟炸,採用(1+3n)*t 的方式提醒
#counter:基數 計數
#
if [ $iserror -eq 1 ]
then
res=`echo $info`
base=`cut -d " " -f1 counter.txt`
times=`cut -d " " -f2 counter.txt`
if [ $times -eq 0 ]
then
base=$[base+3]
times=$base
./smtp.py -t "[email protected]" -s "servers access fail" -c "$res" >/dev/null 2>&1
else
times=$[times-1]
fi
echo "$base $times" >counter.txt
else
echo "1 0" >counter.txt
fi
#!/bin/bash
#get.sh
status_path="xxxx/status.txt"
info=`cat $status_path`
echo "Analyze " $info
自查bash:
#!/bin/bash
#check.sh
result=""
domain=`hostname | awk -F '.' '{print $1}'`
date=`date +"%Y-%m-%d %H:%M:%S"`
mem_total=`free -m|grep Mem|awk '{print $2}'`
mem_used=`free -m|grep Mem|awk '{print $3}'`
mem_rate=`echo "scale=2;$mem_used*1.0/$mem_total"|bc`
result="'$domain','$date','$mem_total','$mem_used','$mem_rate'"
echo $result >xxxxx/status.txt
#校驗,不正常則發郵件,很remote.sh 一樣
iserror=0
errorinfo="\n"
flag=`echo "$mem_rate > 0.9"|bc`
if [ $flag -eq 1 ]
then
iserror=1
errorinfo=$info" mem usage lager than 90%;"
fi
res=`echo $errorinfo`
#郵件提醒
if [ $iserror -eq 1 ]
then
base=`cut -d " " -f1 counter.txt`
times=`cut -d " " -f2 counter.txt`
if [ $times -eq 0 ]
then
base=$[base+3]
times =$base
./smtp.py -t "[email protected]" -s "servers $domain check fail" -c "$res" >/dev/null 2>&1
else
times=$[times -1]
fi
echo "$base $times" >counter.txt
else
echo "1 0" >counter.txt
fi
另外加兩個安裝腳本:
#!/bin/bash
#install.sh
TMP_OLD_CRON=/tmp/oldcrontab
rm -f $TMP_OLD_CRON
crontab -l > $TMP_OLD_CRON
cnt=`grep "remote.sh check" $TMP_OLD_CRON | wc -l`
if test $cnt -eq 0; then
echo "*/3 * * * * cd /opt/selfcheck/SHELL/bin && ./remote.sh check" >> $TMP_OLD_CRON
cat $TMP_OLD_CRON | crontab
fi
echo "1 0" >counter.txt
cd /opt/selfcheck/SHELL/bin && ./remote.sh update
#!/bin/bash
#update.sh
#once
TMP_OLD_CRON=/tmp/oldcrontab
rm -f $TMP_OLD_CRON
crontab -l > $TMP_OLD_CRON
cnt=`grep "check.sh" $TMP_OLD_CRON | wc -l`
if test $cnt -eq 0; then
echo "*/2 * * * * cd /home/MSDOMAIN1/czhang13 && ./check.sh" >> $TMP_OLD_CRON
cat $TMP_OLD_CRON | crontab
fi
echo "1 0" >counter.txt
echo "Ana $domain success"
運行方式:
在虛擬機執行./install.sh 就行了