Shell脚本如下:

 

#/bin/bash
#environment variable
source /etc/profile
#memory
bu=`free | awk 'NR==2{print $6}'`
ca=`free | awk 'NR==2{print $7}'`
us=`free | awk 'NR==2{print $3}'`
to=`free | awk 'NR==2{print $2}'`
mem=`expr "scale=2;($us-$bu-$ca)/$to" |bc -l | cut -d. -f2`
if(($mem >= 70))
        then
        msg="TIME:$(date +%F_%T)
                 HOSTNAME:$(hostname)
                 IPADDR:$(ifconfig |awk 'NR==2{print $2}')
                 MSG:内存high了high了!已经用了${mem}%"
            echo $msg
        /test/pymail.py $msg
fi

发送邮件脚本 pymail.py 内容如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
import smtplib
import email.mime.multipart
import email.mime.text

server = 'smtp.126.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'] = 'ron@126.com'
    msg['To'] = 'xxx@139.com'
    user = 'ron'
    pwd = 'password'
    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)

 

 

 

 

 

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐