Linux服务器进程监控脚本(发送邮件报警)

#Program:
#This is a program that can monitor the Program is activity and how many resource it occupy. 
#History:
#       2012-02-27      Daniel.W|Kernel         First Release 

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
port=""

#declare -i max_cpu 
#declare -i max_mem 
#declare -i cpu
#declare -i men

function com(){
if [ -n "$port" ];then
        result=$(ps aux | grep -w $pro | grep port=$port | grep -v grep)
else 
        result=$(ps aux | grep -w $pro | grep -v grep)
fi
}
read -p "Which path the program is:" path
read -p "The mail address do you want ro send:" send_mailbox
read -p "The mail address do you want to receive the warning message:" mailbox 
read -p "The maximum CPU occupancy rate:" max_cpu 
read -p "The maximum memory occupancy rate:" max_mem 
read -p "How many seconds do you want to run:" time
read -p "Which port do you want to monitor(#not necessary,only for mult port process):" port

pro=$( echo $path | awk 'BEGIN{FS="/"}{print $NF}' ) 

while [ 1 ]
do
        com     
        sleep $time
        echo $result 
        if [ -z "$result" ];then
               echo "The $pro process has down,now i try to restart it" | mail -s "Program Monitor Warning[Err:1]" $mailbox
               $path & 
               sleep 20
        fi      
        cpu=$( ps aux | grep -w $pro | grep -v grep | awk '{print $3}'  ) 
        mem=$( ps aux | grep -w $pro | grep -v grep | awk '{print $4}'  ) 
        echo "CPU:"$cpu
        echo "MEM:"$mem
        #if_over_cpu="no"
        #if_over_mem="no"
        echo $cpu
        echo $max_cpu
        if_over_cpu=$(echo $cpu:$max_cpu | awk 'BEGIN{FS=":"}{if($1>=$2) {print "yes"} else {print "no"}}')
        if_over_mem=$(echo $mem:$max_mem | awk 'BEGIN{FS=":"}{if($1>=$2) {print "yes"} else {print "no"}}')
        echo $if_over_cpu
        echo $if_over_mem
        if [ "$if_over_cpu" == "yes" ];then
                echo "2"
                echo "The $pro process has pass the maximum set for CPU occupancy" | mail -s "Program Monitor Warning[Err:2]" $mailb
ox -- -f$send_mailbox 
        fi      
        if [ "$if_over_mem" == "yes" ];then
                echo "3"
                echo "The $pro process has pass the maximum set for Memory occupancy" | mail -s "Program Monitor Warning[Err:3]" $ma
ilbox -- -f$send_mailbox 
        fi      
done

脚本没有对输入的数据进行判断,大家可以加以改进
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值