linux 系统调用python脚本发送自定义系统邮件

#!/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
%s' %('
'.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

  

 

  

原文地址:https://www.cnblogs.com/wanchenxi/p/7148262.html