1. 程式人生 > >利用expect 指令碼(ssh)監控機器狀態

利用expect 指令碼(ssh)監控機器狀態

最近公司有個team 說想做一個tool 來解決機器硬體監控的問題,但是又不想去額外的申請防火牆(因為大公司,限制比較多)

我想了一下覺得可以做,記錄一下:

需求就是監控一批機器並且web 來展示狀態普通方案

如果採用普通方案,勢必引入防火牆問題,web servers 還好可以新增一個api ,其他型別的機器就不得不申請開放防火牆 

留意到我們所有的機器都是能在我們的desktop 去訪問的,很自然的想到可以利用22 埠搞點東西,於是

(生產消費模式)

首先在我們在windows 上面搭一個虛擬機器,採用橋接的方式從閘道器獲取一個ip(方便操作,且可被區域網訪問到)

然後我們通過expect 指令碼安裝執行的指令碼到目標機器開啟定時器就好了。為了可靠,在輪詢和自查指令碼上都要開啟郵件提醒,這樣某臺機器掛掉有提醒,desktop 因為颱風停電等因素關機,目標機器也還在監控,另外還要注意輪詢指令碼要加上定期清理虛擬機器的空間。

缺陷就是執行週期長(分鐘級別),機器列表不能太多, 雖然通常我們也不需要那麼強的實時性,當然這也只是鬧著玩的,而且是在極限程式設計(30h)的情況下寫的,還存在很多問題

監控的話,個人覺得的大公司會買更加可靠的service 方式的監控產品,小公司可能也只需要自查指令碼就好了。 

附上程式碼:

expect:

scp.exp

#!/usr/bin/expect
#to transfer file
# 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]
set file4 [lindex $argv 8]
spawn scp $file $file1 $file2 $file3  
[email protected]
$host:$path expect { "*yes/no" { send "yes\r"; exp_continue} "*password: " { send "$psw\n"; exp_continue } }

ssh.exp

#!/usr/bin/expect
# host user psw path file

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 
[email protected]
$host "$path/$file" expect { "*yes/no" { send "yes\r"; exp_continue} "*assword*" { send "$psw\n"; exp_continue } }

輪詢bash:

remote.sh

#!/bin/bash

serverlist=`cat ../source/serverlist.txt`
user="xxxxx"
psw="xxxxxx"
path="xxxxx"
scp_file="../bin/smtp.py ../bin/check.sh ../bin/get.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
    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

get.sh

#!/bin/bash

status_path="xxxx/status.txt"
info=`cat $status_path`

echo "Analyze " $info

自查bash:

check.sh

#!/bin/bash
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"

#mem
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


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

執行方式:

在虛擬機器執行./install.sh 就行了

再附上一張我們的前端效果圖(兩臺機器cpu 實時使用率圖)