最近想做一个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 "xxxx@xxxx.com" -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 "xxxxxxx@xxx.com" -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 就行了