1. ##添加计划任务,python脚本必须加上. /home/oracle/.bash_profile;
  2. */10 * * * * . /home/oracle/.bash_profile; /oracle/ogg11r2/monitor/monitorggs.py
  3.  
  4. #!/usr/bin/env python # -*- coding: utf-8 -*- 
  5. import subprocess
  6. import datetime
  7. current_time = str(datetime.datetime.now())
  8. logname ='/oracle/ogg11r2/monitor/ggs.log'
  9. fo1 = open(logname,"wb")
  10. child = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
  11. gginfo=child.communicate("info all")
  12. fo1.write("".join(gginfo[0])+"\n"+current_time)
  13. fo1.close()
  14. = open(logname,"rb")
  15. f_in_split = f.read().splitlines()
  16. for i in f_in_split:
  17. if i.startswith('MANAGER ABENDED'):
  18. cmd="start mgr"
  19. = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
  20. g.communicate(cmd)
  21. elif i.startswith('EXTRACT ABENDED EE_USER'):
  22. cmd="start EE_USER"
  23. = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
  24. g.communicate(cmd)
  25. elif i.startswith('EXTRACT ABENDED E_SIPRCT'):
  26. cmd="start E_SIPRCT"
  27. = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
  28. g.communicate(cmd)
  29. elif i.startswith('EXTRACT ABENDED PP_USER'):
  30. cmd="start PP_USER"
  31. = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
  32. g.communicate(cmd)
  33. elif i.startswith('EXTRACT ABENDED P_SIPRCT'):
  34. cmd="start P_SIPRCT"
  35. = subprocess.Popen(["/oracle/ogg11r2/ggsci"], stdin=subprocess.PIPE,stdout=subprocess.PIPE)
  36. g.communicate(cmd)
  37. 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'))