First, alarm system mail engine
[[email protected] mon]# lsbin conf log mail shares[[email protected] mon]# cd mail[[email protected] m ail]# vim mail.py//Create send mail script #!/usr/bin/env python#-*-coding:utf-8-*-import os,sysreload (SYS) Sys.setdefaultenco Ding (' UTF8 ') import getoptimport smtplibfrom email. Mimetext Import mimetextfrom Email. Mimemultipart Import mimemultipartfrom subprocess import *def sendqqmail (Username,password,mailfrom,mailto,subject, Content): Gserver = ' smtp.qq.com ' gport = 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.sen DMail (Mailfrom, mailto, msg.as_string ()) Smtp.close () except Exception,err:print "Send Mail failed. Error:%s "% errdef Main (): To=sys.argv[1] Subject=SYS.ARGV[2] content=sys.argv[3]# #定义QQ邮箱的账号和密码, you need to change your own account and password (please do not put the real user name and password on the Internet, otherwise you will die very miserable) sendqqmail (' [email& Nbsp;protected] ', ' aaaaaaaaaa ', ' [email protected] ', to,subject,content) if __name__ = = "__main__": Main () ##### Script usage Instructions ###### #1. First, define the email account number and password # # in the script. Script execution commands are: Python mail.py target Mailbox "message subject" "Message content" [[[email protected] mail]# vim mail.sh//Create script mail.sh when system service is abnormal Will call mail.sh#! /bin/bashlog=$1t_s= ' Date +%s ' t_s2= ' date-d "2 hours ago" +%s ' if [!-f/tmp/$log]then echo $t _s2 >/tmp/$logfit _s2= ' Tail-1/tmp/$log |awk ' {print '} ' echo $t _s>>/tmp/$logv =$[$t _s-$t _s2]echo $vif [$v-gt 3600]then./mail.py $ 1 $0 echo >/tmp/$log. txtelse if [!-f/tmp/$log. txt] then echo "0" >/tmp/$log. txt fi Nu= ' cat/tmp/$log. txt ' nu2=$[$nu +1] echo $nu 2>/tmp/$log. txt if [$nu 2-gt] then./mail.py $ "Trouble Continue min $" "$ $" echo "0" >/tmp/$log. txt Fifi
Second, Operation Alarm system
[[email protected] ~]# crontab -e //创建计划任务* * * * * cd /usr/local/sbin/mon/bin; bash main.sh
Shell Script Basics (vi)