#!/bin/bash
#auto monitor 
#write by 2015-8-9
IP=ifconfig |grep "Bcast" |awk '{print $2}'|sed 's/addr://g'
DATE=`date`
EMAIL=email.txt
cat << EOF
########################################################
########        wlecome to  use auto         ###########
########           monitor scripts           ###########
########################################################
EOF
        if [ -z $1 ];then
        echo -e "\033[32mUsage :{sh $0 nginx|http|mysql|ntp|list.txt}\033[0m"
        sleep 2
        exit
        fi
if [ -f "$1" -a "$1" == "list.txt" ];then
for i in `cat list.txt`
do
        count=`ps -ef |grep $i|grep -v grep|grep -v "$0"|wc -l`
if [ $count -eq 0 ];then
cat >$EMAIL << EOF
+++++++++++++++++++SERVER MONITOR++++++++++++++++++++++++
通知类型:故障
服务:$i
主机:$IP
状态:警告
日期:$DATE
额外信息:
CRITTACL + $i server is no running ,please check..
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOF
        echo -e "\033[32mthe monitor $i warning ,please check..\033[0m"
        mail -s "$IP $i warning" 752840649@qq.com < $EMAIL >>/dev/null 2>&1
        else
        echo -e "\033[32mthe monitor $i is ok...\033[0m"
        fi
done

else
        count=`ps -ef |grep $1|grep -v grep|grep -v $0|wc -l`
        
if [ $count -eq 0 ];then
cat >$EMAIL << EOF
+++++++++++++++++++SERVER MONITOR++++++++++++++++++++++++
通知类型:故障
服务:$1
主机:$IP
状态:警告
日期:$DATE
额外信息:
CRITTACL + $1 server is no running ,please check..
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
EOF
        dos2unix $EMAIL
        echo -e "\033[32mthe monitor $1 warning ,please check..\033[0m"
        mail -s "$IP $1 warning" 752840649@qq.com < $EMAIL >>/dev/null 2>&1
        else
        echo -e "\033[32mthe monitor $1 is ok...\033[0m"
        fi
   fi