#!/usr/bin/python # -*- coding: UTF-8 -*- import sys import smtplib import email.mime.multipart import email.mime.text server = 'smtp.163.com' port = '25' def sendmail(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('郵件發送成功email has send out !') if __name__ == '__main__': msg = email.mime.multipart.MIMEMultipart() msg['Subject'] = '你是風兒我是沙,纏纏綿綿回我家' msg['From'] = '1856565020@163.com' msg['To'] = '1829477883@qq.com' user = '1856565020' pwd = 'xxx123456' content='%s\n%s' %('\n'.join(sys.argv[1:4]),' '.join(sys.argv[4:])) #格式處理,專門針對我們的郵件格式 txt = email.mime.text.MIMEText(content, _charset='utf-8') msg.attach(txt) sendmail(server,port,user,pwd,msg)
以上為郵件腳本其中的登陸密碼為客戶端授權碼
把此代碼復制到/usr/bin/mail_test 加個執行權限,讓以下的腳本來調用
#!/bin/bash mem_limit=90 #內存使用超過90%則報警,同上 disk='/dev/sda1' #需要監控的磁盤名 disk_space_limit=90 #磁盤空間使用超過90%則報警,同上 function monitor_mem(){ mem_total=`free |awk 'NR==2{print $2}'` mem_use=`free |awk 'NR==2{print $3}'` mem_per=`echo "scale=2;$mem_use/$mem_total" |bc -l|cut -d. -f2` if [ $mem_per -gt $mem_limit ] then msg="TIME:$(date +%F_%T) HOSTNAME:$(hostname) IPADDR:$(ifconfig |awk 'NR==2{print $2}') MSG:Memory usage exceeds the limit,current value is ${mem_per}%" echo $msg /usr/bin/mail_test $msg fi } function monitor_disk_space(){ space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1` if [ $space_use -gt $disk_space_limit ] then msg="TIME:$(date +%F_%T) HOSTNAME:$(hostname) IPADDR:$(ifconfig |awk 'NR==2{print $2}') MSG:Disk space usage exceeds the limit,current value is ${space_use}%" echo $msg /usr/bin/mail_test $msg fi } monitor_mem &>> /tmp/monitor.log monitor_disk_space &>> /tmp/monitor.log
#!/bin/bash mem_limit=0 #內存使用超過90%則報警,同上 disk='/dev/sda1' #需要監控的磁盤名 disk_space_limit=0 #磁盤空間使用超過90%則報警,同上 function monitor_mem(){ mem_total=`free |awk 'NR==2{print $2}'` mem_use=`free |awk 'NR==2{print $3}'` mem_per=`echo "scale=2;$mem_use/$mem_total" |bc -l|cut -d. -f2` if [ $mem_per -gt $mem_limit ] then msg="TIME:$(date +%F_%T) HOSTNAME:$(hostname) IPADDR:$(ifconfig |awk 'NR==2{print $2}') MSG:Memory usage exceeds the limit,current value is ${mem_per}%" echo $msg /usr/bin/mail_test $msg fi } function monitor_disk_space(){ space_use=`df $disk |awk 'NR==2{print $5}'|cut -d% -f1` if [ $space_use -gt $disk_space_limit ] then msg="TIME:$(date +%F_%T) HOSTNAME:$(hostname) IPADDR:$(ifconfig |awk 'NR==2{print $2}') MSG:Disk space usage exceeds the limit,current value is ${space_use}%" echo $msg /usr/bin/mail_test $msg fi } nginx_monitor () { lsof -i:80 if [ $? -ne 0 ] then msg="TIME:$(date +%F_%T) HOSTNAME:$(hostname) IPADDR:$(ifconfig |awk 'NR==2{print $2}') MSG:nginx is dead" echo $msg /usr/bin/mail_test $msg systemctl start nginx fi } nfs_server_monitor () { systemctl status nfs.service|awk -F: 'NR==3 {print $2}'|grep -q "runnin" if [ $? -ne 0 ] then msg="TIME:$(date +%F_%T) HOSTNAME:$(hostname) IPADDR:$(ifconfig |awk 'NR==2{print $2}') MSG:nfs server is dead,and auto start!" echo $msg /usr/bin/mail_test $msg systemctl status nfs.service fi } nfs_client_monitor() { systemctl status rpcbind.service|awk -F: 'NR==3 {print $2}'|grep -q "runnin" if [ $? -ne 0 ] then msg="TIME:$(date +%F_%T) HOSTNAME:$(hostname) IPADDR:$(ifconfig |awk 'NR==2{print $2}') MSG:nfs client is dead,and auto start!" echo $msg /usr/bin/mail_test $msg systemctl start rpcbind.service fi } monitor_mem &>> /tmp/monitor.log monitor_disk_space &>> /tmp/monitor.log nginx_monitor &>> /tmp/montior.log nfs_server_monitor &>> /tmp/nfs.server.log nfs_client_monitor &>> /tmp/nfs.client.log