##添加计划任务,python脚本必须加上
. /home/oracle/.bash_profile;*/10 * * * * . /home/oracle/.bash_profile; /oracle/ogg11r2/monitor/monitorggs.py
#!/usr/bin/env python # -*- coding: utf-8 -*-
import subprocess
import datetime
current_time = str(datetime.datetime.now())
logname ='/oracle/ogg11r2/monitor/ggs.log'
fo1 = open(logname,"wb")
child = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
gginfo=child.communicate("info all")
fo1.write("".join(gginfo[0])+"\n"+current_time)
fo1.close()
f = open(logname,"rb")
f_in_split = f.read().splitlines()
for i in f_in_split:
if i.startswith('MANAGER ABENDED'):
cmd="start mgr"
g = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
g.communicate(cmd)
elif i.startswith('EXTRACT ABENDED EE_USER'):
cmd="start EE_USER"
g = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
g.communicate(cmd)
elif i.startswith('EXTRACT ABENDED E_SIPRCT'):
cmd="start E_SIPRCT"
g = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
g.communicate(cmd)
elif i.startswith('EXTRACT ABENDED PP_USER'):
cmd="start PP_USER"
g = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
g.communicate(cmd)
elif i.startswith('EXTRACT ABENDED P_SIPRCT'):
cmd="start P_SIPRCT"
g = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
g.communicate(cmd)
f.close()
python3.5.2
#!/usr/bin/env python # Version = 3.5.2 # -*- coding: utf-8 -*- # __auth__ = 'lijianwei' import subprocess import datetime current_time = str(datetime.datetime.now()) logname = '/oracle/ogg11r2/monitor/ggs.log' def popen(cmd_outer,cmd_inner): child = subprocess.Popen([cmd_outer], stdin=subprocess.PIPE,stdout=subprocess.PIPE) gginfo,ggerr=child.communicate(bytes(cmd_inner,encoding='utf8')) return str(gginfo, encoding='utf8') result = popen("/oracle/ogg11r2/ggsci","info all") gglist = result.split('\n') send_flag = 0 for i in gglist: if 'ABENDED' in i: fo1 = open(logname, "w") fo1.write('{}\n{}\n'.format(current_time, i)) temp_list = i.split(' ') last_list = [] for i in temp_list: if i != '': last_list.append(i) if len(last_list) == 2: popen("/oracle/ogg11r2/ggsci", "start mgr") fo1.write('{}\n{}\n'.format(current_time, 'Run cmd: start mgr')) elif len(last_list) > 2: cmd1 = 'view report ' + last_list[2] cmd2 = 'start ' + last_list[2] view_log = popen("/oracle/ogg11r2/ggsci", cmd1) fo1.write('{}\n{}\n'.format(current_time, view_log)) popen("/oracle/ogg11r2/ggsci", cmd2) fo1.write('{}\n{}\n'.format(current_time, 'Run cmd: {}'.format(cmd2))) fo1.close() send_flag = 1 print(send_flag) if send_flag == 1: import smtplib import email.mime.multipart import email.mime.text def sendmail(sub, con): msg = email.mime.multipart.MIMEMultipart() msg['from'] = "wumingxiaoyao@life.org.com" msg['to'] = "hello@python.good.com" msg['subject'] = sub content = con txt = email.mime.text.MIMEText(content) msg.attach(txt) smtp = smtplib.SMTP() smtp.connect('smtp.mail.haoyisheng.com', '25') smtp.login('lijianwei@mail.haoyisheng.com', 'ljw1987') smtp.sendmail('lijianwei@mail.haoyisheng.com', 'lijianwei@mail.haoyisheng.com',str(msg)) smtp.sendmail('lijianwei@mail.haoyisheng.com', 'liuwei@mail.haoyisheng.com',str(msg)) smtp.sendmail('lijianwei@mail.haoyisheng.com', 'liuyonglian@mail.haoyisheng.com',str(msg)) smtp.sendmail('lijianwei@mail.haoyisheng.com', 'lijinyao@mail.haoyisheng.com',str(msg)) smtp.quit() with open(logname, 'rb') as logifle: sendmail('GGS-基础平台数据库', str(logifle.read(), encoding='utf8'))