First, write a message reminding the Python file
#!/usr/bin/python#-*-coding:utf-8-*-ImportSYSImportSmtplibImportEmail.mime.multipartImportEmail.mime.textserver='smtp.163.com'Port=' -'defSendMail (server,port,user,pwd,msg): SMTP=Smtplib. SMTP () smtp.connect (server,port) smtp.login (user, PWD) smtp.sendmail (msg[' from'], msg[' to'], msg.as_string ()) Smtp.quit ()Print('Mail sent successfully email has send out!')if __name__=='__main__': Msg=Email.mime.multipart.MIMEMultipart () msg['Subject'] ='Server Alarm PLEASE note! 'msg[' from'] ='[email protected]'msg[' to'] ='[email protected]'User='Python4_mail'pwd='sbalex3714'content='%s\n%s'%('\ n'. Join (Sys.argv[1:4]),' '. Join (Sys.argv[4:]))#format processing, specifically for our message formattxt= Email.mime.text.MIMEText (Content, _charset='Utf-8') Msg.attach (TXT) sendmail (server,port,user,pwd,msg)
and write your own surveillance script.
#/bin/Bashbu=` Free|awk 'nr==2{print $6}'' to=` Free|awk 'Nr==2{print $}'' Mem=`Expr "scale=2, $bu/$to"|bc-l |Cut-D.-F2 'if(($mem >= -)) Thenmsg="time:$ (date +%f_%t)hostname:$ (hostname) ipaddr:$ (ifconfig|awk 'Nr==2{print $}') MSG: High Memory High! Already used the ${mem}%" Echo$msg/usr/bin/pymail.py $msgfisystemctl status Nginxif(($?! =0)) Thenmsg="time:$ (date +%f_%t)hostname:$ (hostname) ipaddr:$ (ifconfig|awk 'Nr==2{print $}'Msg:nginx Process exception please check! " Echo$msg/usr/bin/pymail.py $msgfiSystemctl Status NFSif(($?! =0)) Thenmsg="time:$ (date +%f_%t)hostname:$ (hostname) ipaddr:$ (ifconfig|awk 'Nr==2{print $}'Msg:nfs Process exception please check! " Echo$msg/usr/bin/pymail.py $msgfi
After the scheduled task, the write is executed once per minute.
Shutdown of two Nginx and NFS services
Wait a minute later
Shell scripts-monitoring and email alerts