Shell scripts-monitoring and email alerts

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.