Oracle 自動化運維-Python監控Oracle告警日誌

chenoracle發表於2020-03-17


Oracle 自動化運維-Python監控Oracle告警日誌


一:paramiko模組的安裝

二:使用paramiko監控Oracle alert日誌

參考:

一:paramiko模組的安裝

paramiko模組的安裝

環境設定

Linux系統為 Centos 6.7

Python環境為 Python 3.6

連線Unix類伺服器模組: paramiko

這裡我們採用pip3命令安裝,會自動下載缺失的模組

[oracle@jumplinux01 bin]$ ./pip3 install paramiko

Collecting paramiko

  Downloading (206kB)

    100% |████████████████████████████████| 215kB 658kB/s 

Collecting bcrypt>=3.1.3 (from paramiko)

  Downloading (56kB)

    100% |████████████████████████████████| 61kB 821kB/s 

Collecting cryptography>=2.5 (from paramiko)

  Downloading (2.3MB)

    100% |████████████████████████████████| 2.3MB 339kB/s 

Collecting pynacl>=1.0.1 (from paramiko)

  Downloading (759kB)

    100% |████████████████████████████████| 768kB 278kB/s 

Collecting six>=1.4.1 (from bcrypt>=3.1.3->paramiko)

  Downloading

Collecting cffi>=1.1 (from bcrypt>=3.1.3->paramiko)

  Downloading (399kB)

    100% |████████████████████████████████| 399kB 194kB/s 

Collecting pycparser (from cffi>=1.1->bcrypt>=3.1.3->paramiko)

  Downloading (112kB)

    100% |████████████████████████████████| 112kB 151kB/s 

Installing collected packages: six, pycparser, cffi, bcrypt, cryptography, pynacl, paramiko

Successfully installed bcrypt-3.1.7 cffi-1.14.0 cryptography-2.8 paramiko-2.7.1 pycparser-2.20 pynacl-1.3.0 six-1.14.0

問題:在linux環境下,安裝完python3.6.2後,如果發現並沒有pip3,需要手動安裝

[root@jumplinux01 ~]# wget

[root@jumplinux01 ~]# ll -rth pip-18.0.tar.gz 

-rw-r--r-- 1 root root 1.2M Jul 22  2018 pip-18.0.tar.gz

[root@jumplinux01 ~]# cp pip-18.0.tar.gz /home/oracle/softwore/

[root@jumplinux01 ~]# chown oracle.oracle /home/oracle/softwore -R

[root@jumplinux01 ~]# su - oracle

[oracle@jumplinux01 ~]$ cd softwore/

[oracle@jumplinux01 softwore]$ tar -xvf pip-18.0.tar.gz 

[oracle@jumplinux01 softwore]$ cd pip-18.0

[oracle@jumplinux01 pip-18.0]$ python setup.py install

......

Installed /usr/local/python36/lib/python3.6/site-packages/pip-18.0-py3.6.egg

Processing dependencies for pip==18.0

Finished processing dependencies for pip==18.0

驗證安裝

import paramiko 無報錯說明安裝匯入成功

[oracle@jumplinux01 bin]$ python

Python 3.6.1 (default, Mar 16 2020, 21:33:03) 

[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import paramiko

>>> 

二:使用paramiko監控Oracle alert日誌

環境設定

Linux系統為 Centos 6.7

Python環境為 Python 3.6

連線Unix類伺服器模組: paramiko

使用的命令

我們使用grep命令來判斷alert日誌中是否有ORA Error及Checkpoint報警

監控alert日誌程式

如下程式使用paramiko連線linux/Unix伺服器後檢視alert日誌檢視報警關鍵字,當發現錯誤時輸入結果

程式名稱為:checkoraclelog.py

[oracle@jumplinux01 script]$ vim checkoraclelog.py

#!/usr/bin/python

#coding=utf8

import paramiko


def oraclelog(ssh,path):

        alert_log=[]

        command='grep  -E \'ORA-|Checkpoint|Error\' '+path

        stdin,stdout,stderr=ssh.exec_command(command)

        err=stderr.readlines()

        if len(err) != 0:

            print (err)

            return False

        else:

            stdout_content=stdout.readlines()

        if len(stdout_content)!=0:

            result='\n'.join(stdout_content)

            result= 'Oralce log on '+hostname+ ' have errors\n'+'The log path is '+path+'\n'+result

            alert_log.append(result)

            return alert_log

        else:

            return 'noerror'


if __name__ == '__main__':

    hostname='192.168.2.222'

    username='root'

    password='******'

    try:

        #使用SSHClient方法定義ssh變數

        ssh = paramiko.SSHClient()

        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        #連線目標伺服器

        ssh.connect(hostname=hostname,port=22,username=username,password=password)

        path='/u01/app/oracle19/diag/rdbms/cjcdb01/cjcdb01/trace/alert_cjcdb01.log'

        alert_log=oraclelog(ssh,path)

        ssh.close()

        if alert_log:

            if alert_log !='noerror':

                for i in alert_log:

                    print (i)

            else:

                print ('There is no ORA- error on '+hostname)

    except Exception as e:

        print (hostname+' '+str(e))

執行效果:

[oracle@jumplinux01 script]$ python checkoraclelog.py 

Oralce log on 192.168.2.222 have errors

The log path is /u01/app/oracle19/diag/rdbms/cjcdb01/cjcdb01/trace/alert_cjcdb01.log

Error attempting to elevate VKTM's priority: no further priority changes will be attempted for this process

ORA-65221 signalled during: alter pluggable database application APP$CDB$CATALOG begin install '18.0.0.0.0'...

ORA-65214 signalled during: alter pluggable database application APP$CDB$CATALOG end install '18.0.0.0.0'...

ORA-65213 signalled during: alter pluggable database application APP$CDB$SYSTEM begin install '1.0'...

ORA-65214 signalled during: alter pluggable database application APP$CDB$SYSTEM end   install '1.0'...

ORA-65209 signalled during: create lockdown profile PRIVATE_DBAAS...

ORA-65209 signalled during: create lockdown profile SAAS...

ORA-65209 signalled during: create lockdown profile PUBLIC_DBAAS...

Checkpoint not complete

歡迎關注我的微信公眾號"IT小Chen",共同學習,共同成長!!!

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29785807/viewspace-2680950/,如需轉載,請註明出處,否則將追究法律責任。

相關文章