1. 程式人生 > >告警系統郵件引擎、運行告警系統

告警系統郵件引擎、運行告警系統

告警系統腳本 郵件引擎

告警系統郵件引擎
[root@garytao-01 mon]# cd mail/
[root@garytao-01 mail]# vim mail.py

增加如下內容:

#!/usr/bin/env python
#-*- coding: UTF-8 -*-
import os,sys
reload(sys)
sys.setdefaultencoding(‘utf8‘)
import getopt
import smtplib
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from  subprocess import *
def sendqqmail(username,password,mailfrom,mailto,subject,content):
    gserver = ‘smtp.qq.com‘
    gport = 25
    try:
        msg = MIMEText(unicode(content).encode(‘utf-8‘))
        msg[‘from‘] = mailfrom
        msg[‘to‘] = mailto
        msg[‘Reply-To‘] = mailfrom
        msg[‘Subject‘] = subject
        smtp = smtplib.SMTP(gserver, gport)
        smtp.set_debuglevel(0)
        smtp.ehlo()
        smtp.login(username,password)
        smtp.sendmail(mailfrom, mailto, msg.as_string())
        smtp.close()
    except Exception,err:
        print "Send mail failed. Error: %s" % err
def main():
    to=sys.argv[1]
    subject=sys.argv[2]
    content=sys.argv[3]
##定義QQ郵箱的賬號和密碼,你需要修改成你自己的賬號和密碼(請不要把真實的用戶名和密碼放到網上公開,否則你會死的很慘)
    sendqqmail(‘[email protected]‘,‘aaaaaaaaaa‘,‘[email protected]‘,to,subject,content)
if __name__ == "__main__":
    main()

#####腳本使用說明######
#1. 首先定義好腳本中的郵箱賬號和密碼
#2. 腳本執行命令為:python mail.py 目標郵箱 "郵件主題" "郵件內容"
##在mail目錄下創建腳本mail.sh ,當系統服務異常的時候就會調用mail.sh

[root@garytao-01 mail]# vim mail.sh

增加如下內容:

 log=$1
t_s=`date +%s`
t_s2=`date -d "2 hours ago" +%s`
if [ ! -f /tmp/$log ]
then
    echo $t_s2 > /tmp/$log
fi
t_s2=`tail -1 /tmp/$log|awk ‘{print $1}‘`
echo $t_s>>/tmp/$log
v=$[$t_s-$t_s2]
echo $v
if [ $v -gt 3600 ]
then
    ./mail.py  $1  $2  $3
    echo "0" > /tmp/$log.txt
else
    if [ ! -f /tmp/$log.txt ]
    then
        echo "0" > /tmp/$log.txt
    fi
    nu=`cat /tmp/$log.txt`
    nu2=$[$nu+1]
    echo $nu2>/tmp/$log.txt
    if [ $nu2 -gt 10 ]
    then
         ./mail.py  $1 "trouble continue 10 min $2" "$3"
         echo "0" > /tmp/$log.txt
    fi
fi??

運行告警系統

寫一個腳本進入到目錄自動每分鐘執行一次main.sh腳本
[root@garytao-01 mail]# pwd
/usr/local/sbin/mon/mail
[root@garytao-01 mail]# crontab -e

技術分享圖片

手動執行腳本文件
[root@garytao-01 mail]# cd /usr/local/sbin/mon/bin/
[root@garytao-01 bin]# sh -x main.sh 
+ export send=1
+ send=1
++ /sbin/ifconfig
++ grep -A1 ‘ens33: ‘
++ awk ‘/inet/ {print $2}‘
+ export addr=172.16.111.100
+ addr=172.16.111.100
++ pwd
+ dir=/usr/local/sbin/mon/bin
++ echo /usr/local/sbin/mon/bin
++ awk -F/ ‘{print $NF}‘
+ last_dir=bin
+ ‘[‘ bin == bin ‘]‘
+ conf_file=../conf/mon.conf
+ exec
[root@garytao-01 bin]# cat ../log/err.log    //查看日誌
++ date ‘+%F %T‘
+ echo ‘2018-02-07 17:01:39 load average‘
+ /bin/bash ../shares/load.sh
+ grep -q to_mon_502=1 ../conf/mon.conf
++ grep logfile= ../conf/mon.conf
++ awk -F = ‘{print $2}‘
++ sed ‘s/ //g‘
+ export log=/data/log/xxx.xxx.com/access.log
+ log=/data/log/xxx.xxx.com/access.log
+ $‘/bin/bash\302\240\302\240../shares/502.sh‘
main.sh:行23: /bin/bash??../shares/502.sh: 沒有那個文件或目錄
[root@garytao-01 bin]# vi ../conf/mon.conf  //不需要寫入502
[root@garytao-01 bin]# vi main.sh  //暫時註釋掉日誌

技術分享圖片
技術分享圖片


[root@garytao-01 bin]# sh -x main.sh  //重新執行腳本
+ export send=1
+ send=1
++ grep -A1 ‘ens33: ‘
++ awk ‘/inet/ {print $2}‘
++ /sbin/ifconfig
+ export addr=172.16.111.100
+ addr=172.16.111.100
++ pwd
+ dir=/usr/local/sbin/mon/bin
++ echo /usr/local/sbin/mon/bin
++ awk -F/ ‘{print $NF}‘
+ last_dir=bin
+ ‘[‘ bin == bin ‘]‘
+ conf_file=../conf/mon.conf
++ date ‘+%F %T‘
+ echo ‘2018-02-26 19:02:10 load average‘
2018-02-26 19:02:10 load average
+ /bin/bash ../shares/load.sh
19:02:10 load is 0
+ grep -q to_mon_502=1 ../conf/mon.conf
[root@garytao-01 bin]# bash ../shares/load.sh 
19:03:04 load is 0

告警系統郵件引擎、運行告警系統