监控脚本

#!/bin/sh
#######################by#yuan#linuxer##########################
############date=2017.01.09#####version=2.0###############
#默认5秒执行一次检查 如果觉得时间太长可改下weile循环的sleep时间
. /etc/init.d/functions
url="127.0.0.1"
url2="127.0.0.1:9200" ###80端口被我加了密码认证暂时不知道curl怎么获取参数
yourmail=young@unknowntech.cn
stmp=smtp.unknowntech.cn
mailpasswd=LCsara210000
monmaill=1352794122@qq.com
acc=`netstat -ant |grep 80|wc -l`
avr=`uptime|cut -d ":" -f5|cut -d "," -f1|cut -d " " -f2|cut -d "." -f1`  #取uptime的第一行
web=`curl -I -s $url2|grep HTTP|cut -d " " -f2` #取HTTP状态吗200
port=`nmap $url -p 80|grep tcp|cut -d " " -f2` #查看端口是否open
#wrmail=`grep "smtp-auth=login" /etc/mail.rc `
################安装mailx邮件发送软件
grep "smtp-auth=login" /etc/mail.rc >/dev/null
if [[ $? == 0 ]]; then
        echo "mail server is ok"
else
        yum -y install mailx
        echo "set from=$yourmail" >>/etc/mail.rc
        echo "set smtp=$stmp" >>/etc/mail.rc
        echo "set smtp-auth-user=$yourmail" >>/etc/mail.rc
        echo "set smtp-auth-password=$mailpasswd" >>/etc/mail.rc
        echo "set smtp-auth=login" >>/etc/mail.rc
        echo  sendmail is ok | mailx -v -s " test mail" $monmaill #测试邮件是否能正常发送
fi

#####################判断模块############################
avrmon(){
        if [[ "$avr" > 3  ]]; then
                echo "$(date +%F%H%M) server avr is too many hig $avr" >>/var/log/monitoravr.log 2>&1
               # echo "$(date +%F%H%M) server avr is too many hig $avr" | mailx -v -s " avrmon" $monmaill
                echo "$(date +%F%H%M) server avr is too many hig $avr"
        else
                echo "now average is $avr"
        fi
}
#######################webmon####################################
webmon(){
        if [[ "$web" != 200 ]]; then
                echo "web server may be down at $(date +%F%H%M)" >>/var/log/monitorweb.log 2>&1
                #echo "web server may be down at $(date +%F%H%M)" | mailx -v -s " webmon" $monmaill
                echo "web server may be down at $(date +%F%H%M)"
        else
                echo "web server not down and access is $acc"
        fi
}         
########################portmon##################################
portmon(){
        if [[ "$port" != "open" ]]; then
                echo "may be server port is not open at $(date +%F%H%M)" >>/var/log/monitorport.log 2>&1
                echo "may be server port is not open at $(date +%F%H%M)"
                #echo "web server may be down at $(date +%F%H%M)" | mailx -v -s " webmon" $monmaill
        else
                echo "port is 正常"
        fi
}
while [[ -e  /etc/init.d/network ]]; do
        sleep 5
        avrmon 
        webmon 
        portmon
done

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值