监控Oracle长会话

2018-01-05 Python 宅必备

关于监控系统我们前面介绍了很多,学会了如何使用Django新建网站以及获取数据监控数据至MySQL或redis

然后将获得的数据库处理后再前端显示

往期可以到我的个人网页查看

http://www.zhaibibei.cn/oms/

http://www.zhaibibei.cn/domanager/

这期讲如何使用Django批量监控Oracle 长会话

开发环境

操作系统:CentOS 7.4

Python版本 :3.6

Django版本: 1.10.5

操作系统用户:oms

1. 程序原理

原理为通过Django来批量连接Oracle数据库,然后查看v$session视图的last_call_et字段,如果发现有运行超过指定时间的则报警出来

支持Oracle 9i/10g/11g

12c我没环境应该也可以

2 . 程序主体

#coding=utf-8
from django.core.management.base import BaseCommand
from monitor.models import oraclelist
from monitor.command.getoracleinfo import *
from monitor.command.sendmail_phone import *
from monitor.command.sendmail_wechat import *
class Command(BaseCommand):
    def handle(self, *args, **options):
        mailcontent=[]
        ip=oraclelist.objects.all().order_by('tnsname')
        for i in ip:
            if i.monitor_type==1:
                ipaddress=i.ipaddress
                username=i.username
                password=i.password
                port=i.port
                tnsname=i.tnsname
                try:
                    db = cx_Oracle.connect(username+'/'+password+'@'+ipaddress+':'+port+'/'+tnsname ,mode=cx_Oracle.SYSDBA)
                except Exception as  e:
                    content= (i.tnsname+' is Unreachable,The reason is '+str(e)).strip()
                    mailcontent.append(content)
                    #print (mailcontent)
                else:
                    cursor = db.cursor()
                    job=checkjob(cursor)
                    session=checkactivesession(cursor)
                    cursor.close()
                    db.close()
                    if job=='error':   
                        jobresult=  'The job have errors on  '+i.tnsname
                        mailcontent.append(jobresult)
                    if session=='error':   
                        sessionresult=  'The Session On  '+i.tnsname +' have long running sessions '
                        mailcontent.append(sessionresult)
        if len(mailcontent) != 0:
            mailcontent='\n'.join(mailcontent)
            send_mail_phone(to_list,'Oracle Job&Session  Status Monitor',mailcontent)
            token=GetToken()
            Send_Message(token,2,'Oracle Job&Session Status Monitor',mailcontent)

程序路径为:

mysite/monitor/management/commands/checkoracle_job_session.py

这个程序和前面说的检查Job的程序放在一起

该程序解释如下:

  1. 首先获取oraclelist表中所有的数据库信息

  2. 然后当monitor_type等于1时连接数据库

  3. 然后通过getoracleinfo.py中的checkactivesession函数获取会话的情况

  4. 当返回值为error时候获取tns名称并写入mailcontent列表中

  5. 最后判断mailcontent是否有数据,有的话则报警

这里有邮件和微信报警,微信报警请看我昨天的推送

3. 获取Job执行信息的函数

我们通过如下函数获取Job的执行情况,该程序可单独于Django运行

路径为:

mysite/monitor/command/checkoraclejob.py

该函数执行一个文件里的sql语句

检查v$session 中状态为ACTIVE或KILLED的非后台进程的会话,当LAST_CALL_ET字段大于7200(2小时)时候则报警出来

之所以单独排除CJQ 和QMN,因为这2个后台进程在9i中的TYPE为USERS而不是BACKGROUND

def checkactivesession(cursor):

    fp=open(os.environ['HOME_DIR']+'/mysite/monitor/command/sql/getsession.sql','r')

    fp1=fp.read()

    s=cursor.execute(fp1)

    fp.close()

    row=s.fetchone()

    if row is None:

        return 'normal'

    else:

        return 'error'

getsession.sql

select a.sid,a.serial#,a.username,a.machine,a.program,a.sql_hash_value,a.type,a.LAST_CALL_ET

  from v$session a

 where a.status IN ('ACTIVE', 'KILLED')

 and a.type <> 'BACKGROUND'

  AND a.LAST_CALL_ET>7200

  and a.PROGRAM not like '%CJQ%'

  and a.PROGRAM not like '%QMN%'

4. 最终结果

1.jpg

可以看如果有超过2小时活动的会话则会发邮件

5. 设置自动运行

这里我们设置每天凌晨进行一次,并重定向所有日志至一个文件

这样我们可以通过检查该日志文件判断脚本是否正常运行

45  *  *   *   *   /usr/bin/python2.7  /ezio/website/manage.py checkoracle_job_session >>/home/oracle/crontab.log  2>&1

源代码位置

欢迎访问我的github主页查看源码

https://github.com/bsbforever/oms_django