PYTHON測試指令碼

germany006發表於2018-03-14


#!/usr/bin/python
# -*- coding: UTF-8 -*-
#CHECK ORACLE


SID="tcbjeas"
CHECK_LOG_FILE="tcbjeas.out"
FLAG=0


import os
my_file='/tmp/out.txt'
if os.path.exists(my_file):
    #刪除檔案,可使用以下兩種方法。
    os.remove(my_file)






#檢查資料庫程式是否正常執行,如果不正常執行,將輸出相關程式失敗資訊,後面的檢查資訊將停止進行。
#如果執行正常,說明資料庫是可用的,繼續後面資訊的檢查。
#check pmon process
import os
run=os.popen("ps -ef |grep ora_pmon_$SID |grep -v grep|wc -l").read()
if "0" in run:
FLAG=1
f=open("out.txt","w")
try:
f.write('WARNING!!!ora_pmon_$SID!process is not running!'+'\n')
finally:
f.close()




#check smon process
import os
run=os.popen("ps -ef |grep ora_smon_$SID |grep -v grep|wc -l").read()
if "0" in run:
FLAG=1
        f=open("out.txt","a")
        try:
                f.write('WARNING!!!ora_smon_$SID!process is not running!'+'\n')
        finally:
                f.close()


#check dbwr process
import os
dbwr=os.popen("ps -ef |grep ora_dbw0_$SID |grep -v grep|wc -l").read()
if "0" in dbwr:
FLAG=1
        f=open("out.txt","a")
        try:
                f.write('WARNING!!!ora_dbw0_$SID!process is not running!'+'\n')
        finally:
                f.close()

#check lgwr process
import os
lgwr=os.popen('ps -ef |grep ora_lgwr_$SID |grep -v grep|wc -l').read()
if "0" in lgwr:
print "lgwr"
FLAG=1
        f=open("out.txt","a")
        try:
                f.write('WARNING!!!ora_lgwr_$SID!process is not running!'+'\n')
        finally:
                f.close()


if  FLAG==1:
        f=open("out.txt","a")
        try:
                f.write('Warning!!!Oracle Instance $SID Is Down!'+'\n')
        finally:
                f.close()
else:
f=open("out.txt","a")
        try:
                f.write('OK,Oracle Process Are Running Normal!'+'\n')
        finally:
                f.close()




#檢查資料庫監聽是否正常執行,如果不正常執行,將發出警告資訊,如果沒有警告資訊說明監聽執行正常。
#check listener
import os
listen=os.popen("$ORACLE_HOME/bin/lsnrctl status|grep -i 'Instance'|grep -v grep|wc -l").read()
if "0" in listen:
        f=open("out.txt","a")
        try:
                f.write('Warning!!! Listener is not running normal!'+'\n')
        finally:
                f.close()
else:
        f=open("out.txt","a")
        try:
                f.write('OK,Listener is running normal!'+'\n')
        finally:
                f.close()



#檢查資料庫是否可以連線,如果不能連線,將發出警告資訊(not open or not connect)
#如果沒有警告資訊說明監聽執行正常。
import os
os.popen('sh sql.sh')


import os
connect=os.popen("grep -i 'OPEN' /tmp/sql.txt|grep -v grep|wc -l").read()
if "1" in connect:
        f=open("out.txt","a")
        try:
                f.write('OK,Oracle Connect Normal!'+'\n')
        finally:
                f.close()
else:
        f=open("out.txt","a")
        try:
                f.write('Warning!!Database Is Not Open'+'\n')
        finally:
                f.close()


cat sql.sh
#!/bin/bash


sqlplus user/passwd@orcl<< EOF
spool /tmp/sql.txt
set lines 500
select status from v\$instance;
spool off
exit
EOF


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

相關文章