#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
ip=os.popen("/sbin/ifconfig | grep 'inet addr:'| grep -v '10.' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'").read().strip('\n') #截取ip为字符串,去掉回车
hostname=os.popen("hostname").read().strip('\n')#截取主机名为字符串,去掉回车
sub=ip+'--'+hostname
df=os.popen("df -Th").read() #磁盘空间使用情况
lweek = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
fname = '/oracle/diag/rdbms/pmsdg/pmsdg/trace/alert_pmsdg.log'#alert文件
f = open(fname, "rb")
foname = 'foo1.txt'#累计报错文件
fo = open(foname, "rb")
f_in_foo = fo.readlines() #将文件内容转化成一个列表
row_count= len(f_in_foo)#累计报错文件是否为空
if row_count==0:
row_count_last = 0
else:
a=f_in_foo[row_count-1]
import string
import re
m = re.search(r'[^:]+$', str(a)) #正则截取最后一个冒号后面的值m.group(0)
row_count_last=string.atoi(m.group(0))
row_count=row_count_last+1
fo.close()
f_in_split = f.read().splitlines() #将文件内容转化成一个列表
fo2name = 'foo2.txt'
fo2 = open(fo2name, "wb") #本次新加报错记录文件
fo = open(foname, "ab")
z=0#邮件发送标记
for i in f_in_split[row_count:]:
if i.startswith('alter') or i.startswith('Errors') or i.startswith('Shutting down') or i.startswith('ORA-'):
z=1
while True:
a=f_in_split[row_count_last]
if a[0:3] in lweek :
fo.write(a+" line_num:"+str(row_count_last+1)+"\n") #记录所在行数。(list计数从0开始,文本行数从1开始,所以加一)
fo.write(i+" line_num:"+str(row_count+1)+"\n")
fo2.write(df+"\n")
fo2.write(a+" line_num:"+str(row_count_last+1)+"\n")
fo2.write(i+" line_num:"+str(row_count+1)+"\n")
break
row_count_last-=1
row_count = row_count + 1
row_count_last = row_count-1
fo.close()
fo2.close()
if z:
###发送邮件
#导入smtplib和MIMEText
import smtplib
from email.mime.text import MIMEText
fo = open(fo2name, "rb")
def send_mail(sub,content):
#############
#要发给谁
mailto_list=["lijianwei@mail.haoyisheng.com","liuyonglian@mail.haoyisheng.com"]
#####################
#设置服务器,用户名、口令以及邮箱的后缀
mail_host="smtp.mail.haoyisheng.com"
mail_user="lijianwei@mail.haoyisheng.com"
mail_pass="ljw1987"
mail_postfix="mail.haoyisheng.com"
# mail_host="smtp.163.com"
# mail_user="18210588493@163.com"
# mail_pass="ljw1987"
# mail_postfix="163.com"
######################
'''
to_list:发给谁
sub:主题
content:内容
send_mail("lijianwei@mail.haoyisheng.com","sub","content")
'''
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain',_charset='gbk')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(mailto_list)
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(me, mailto_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
if send_mail(sub,fo.read()): #fo.read()将文件用字符串读取,f.readlines()=f.read().splitlines()列表形式读取
print u'发送成功'
else:
print u'发送失败'
fo.close()