修改了之前写的《根据文件中定义主机列表批量获取服务器磁盘使用情况脚本》获取到远程服务器信息后,生成指定格式的html文件。然后调用python脚本发送html格式邮件。
1、加入计划任务周期执行的shell脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#!/bin/bash f="/root/imzcy/user.txt" l="/root/imzcy/logs" MyTime=`date "+%Y%m%d_%H%M"` x=`cat /root/imzcy/user.txt |wc -l` MyTime2=`date +"%Y%m%d-%H"` MyHtml="/root/ok/html/mail-${MyTime2}.html" cat >>$MyHtml << ZCY <html> <head> <title>磁盘使用情况</title> <style type="text/css"> .tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #9dcc7a;border-collapse: collapse;} .tftable th {font-size:12px;background-color:#abd28e;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;text-align:left;} .tftable tr {background-color:#ffffff;} .tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;} .tftable tr:hover {background-color:#ffff99;} </style> </head> <body> ZCY disk (){ guazai=`cat ${l}/${MyTime}-disk-${zip}.log |awk 'NR>1{if($2!=""){print $NF}}'` n=`cat ${l}/${MyTime}-disk-${zip}.log |awk 'NR>1{if($2!=""){print $NF}}' |wc -l` echo " <p><font face=\"宋体\" size=\"3\"><b>${zip}</b></font></p>" >>$MyHtml echo " <table class=\"tftable\" border=\"1\">" >>$MyHtml echo " <tr><th>挂载点</th><th>总共</th><th>可用</th><th>使用率</th></tr>" >>$MyHtml for i in ${canshu} ; do x=1 for z in ${guazai} ; do if [ "$i" != "$z" ] ; then if [ $x -lt $n ] ; then x=$(($x+1)) else echo "$i 不存在,请检查" #exit 1 break fi else cat ${l}/${MyTime}-disk-${zip}.log |awk '{if($2!=""){print $0}}' |grep "$i"$ |grep ^" " >/dev/null if [ $? -eq 0 ] ; then eval $(cat ${l}/${MyTime}-disk-${zip}.log |awk '{if($2!=""){print $0}}' |grep "$i"$ |awk '{printf("Ztotal=%s; Zfree=%s; Zbfb=%s",$1,$3,$4)}') echo " <tr><td>${i}</td><td>${Ztotal}</td><td>${Zfree}</td><td>${Zbfb}</td></tr>" >>$MyHtml else eval $(cat ${l}/${MyTime}-disk-${zip}.log |awk '{if($2!=""){print $0}}' |grep "$i"$ |awk '{printf("Ztotal=%s; Zfree=%s; Zbfb=%s",$2,$4,$5)}') echo " <tr><td>${i}</td><td>${Ztotal}</td><td>${Zfree}</td><td>${Zbfb}</td></tr>" >>$MyHtml fi break fi done done echo -e " </table> \n </br>\n" >>$MyHtml } for o in `seq 1 $x`; do eval $(cat $f |head -n $o |tail -n 1 |awk -F'|' '{printf("zip=%s; zuser=%s; zpwd=%s; zport=%s; canshu=%s; geshu=%s",$1,$2,$3,$4,$5,NF)}') /usr/local/bin/sshpass -p $zpwd ssh -o StrictHostKeyChecking=no -p $zport ${zuser}@${zip} "df -h" >${l}/${MyTime}-disk-${zip}.log disk y="" done echo -e " </body>\n</html>" >>$MyHtml python mail.py |
2、生成的html文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
[root@imzcy ok]# cat html/mail-20181009-17.html <html> <head> <title>磁盘使用情况</title> <style type="text/css"> .tftable {font-size:12px;color:#333333;width:100%;border-width: 1px;border-color: #9dcc7a;border-collapse: collapse;} .tftable th {font-size:12px;background-color:#abd28e;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;text-align:left;} .tftable tr {background-color:#ffffff;} .tftable td {font-size:12px;border-width: 1px;padding: 8px;border-style: solid;border-color: #9dcc7a;} .tftable tr:hover {background-color:#ffff99;} </style> </head> <body> <p><font face="宋体" size="3"><b>192.168.1.5</b></font></p> <table class="tftable" border="1"> <tr><th>挂载点</th><th>总共</th><th>可用</th><th>使用率</th></tr> <tr><td>/</td><td>36G</td><td>16G</td><td>54%</td></tr> <tr><td>/data</td><td>394G</td><td>12G</td><td>97%</td></tr> </table> </br> <p><font face="宋体" size="3"><b>192.168.1.3</b></font></p> <table class="tftable" border="1"> <tr><th>挂载点</th><th>总共</th><th>可用</th><th>使用率</th></tr> <tr><td>/</td><td>509G</td><td>197G</td><td>60%</td></tr> <tr><td>/data</td><td>509G</td><td>197G</td><td>99%</td></tr> </table> </br> <p><font face="宋体" size="3"><b>192.168.1.13</b></font></p> <table class="tftable" border="1"> <tr><th>挂载点</th><th>总共</th><th>可用</th><th>使用率</th></tr> <tr><td>/</td><td>541G</td><td>197G</td><td>85%</td></tr> </table> </br> </body> </html> [root@imzcy ok]# |
3、上面shell调用的发送html邮件的python脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#!/usr/bin/env python # -*- coding: utf-8 -*- import smtplib, time, os from email.mime.text import MIMEText from email.header import Header def send_mail_html(file): sender = 'xyh@imzcy.cn' receiver = 'receiver_mail@163.com' t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) subject = '磁盘使用情况_' + t smtpserver = 'smtp.exmail.qq.com' username = 'xyh@imzcy.cn' password = 'passsword' f = open(file, 'rb') mail_body = f.read() f.close() msg = MIMEText(mail_body, _subtype='html', _charset='utf-8') msg['Subject'] = Header(subject, 'utf-8') msg['From'] = sender msg['To'] = receiver try: smtp = smtplib.SMTP() smtp.connect(smtpserver) smtp.login(username, password) smtp.sendmail(sender, receiver, msg.as_string()) except: print("邮件发送失败!") else: print("邮件发送成功!") finally: smtp.quit() file = '/root/ok/html/mail-' + time.strftime("%Y%m%d-%H", time.localtime()) + '.html' send_mail_html(file) |
1 |
Python脚本参考自:https://www.cnblogs.com/gongxr/p/7355326.html |
4、邮件样式如下图所示: