分享一个服务监控邮件报警shell脚本

原创 浪子丶刘少  2019-06-27 09:20  阅读 1,124 次 评论 1 条 百度未收录

脚本实现原理:

通过ps抓取此服务是否存在进程,如果服务正常运行,将存在进程,提示服务正常,无进程则提示服务错误,并发送邮件通知

#!/bin/bash
#by anthors liushao 2019

echo -e "\033[32m\n+++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m"
echo -e "\033[32m+++++++欢迎使用刘少智能脚本(www.ezliushao.com)+++++++\033[0m"
echo -e "\033[32m+++++++++++++++++++++++++++++++++++++++++++++++++++++\n\033[0m"
MAIL="langziduyou@163.com"
EMAIL_FILE=email.txt
DATE=`date`

if [ -z "$1" ]; then
    echo -e "\033[33m 用法:bash $0 '服务名 | list.txt' \033[0m"
    exit
fi

M_IPADDR=`ifconfig  | grep "broadcast" | awk '{print $2}'`

if [ "$1" == "list.txt" ]; then

for i in `cat list.txt | grep -v "^#"`
do
    count=`ps -ef | grep $i | grep -v "grep" | grep -v "email" | grep -v "$0" | head -1 | wc -l`
    if [ $count -ne 1 ]; then
    cat >$EMAIL_FILE<> /dev/null 2<&1
        echo -e "\033[33m $i 服务存在异常,请检查... \033[0m"
        mail -s "主机($M_IPADDR) 服务($i) 报警" $MAIL < $EMAIL_FILE >> /dev/null 2<&1
    else
        echo  -e "\033[33m $i 服务运行正常... \033[0m"
    fi
done

else
    count=`ps -ef | grep "$1" | grep -v "grep" | grep -v "email" | grep -v "$0" | head -1 | wc -l`
    if [ $count -ne 1 ]; then
    cat >$EMAIL_FILE<> /dev/null 2<&1
        echo -e "\033[33m $1 服务存在异常,请检查... \033[0m"
        mail -s "主机($M_IPADDR) 服务($1) 报警" $MAIL < $$EMAIL_FILE >> /dev/null 2<&1
    else
        echo  -e "\033[33m $1 服务运行正常... \033[0m"
    fi
fi

echo -e "\033[32m\n+++++++++++++++++++++++++++++++++++++++++++++++++++++\n\033[0m"
本文地址:https://www.ezliushao.com/233.html
加入我们:请加入刘少技术博客交流群:扫描二维码刘少技术博客的QQ交流群 | 浪子丶刘少 QQ:1150110267(注:微信暂停添加好友)
版权声明:本文为原创文章,版权归 浪子丶刘少 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

  1. 男人的天堂
    男人的天堂 【新手】 @回复

    学习了