自动化监控多个Oracle表空间
上节讲了如何利用Python自动监控Oracle表空间使用率
这节会利用一个循环一次性查询多个表空间使用率
环境设置
Linux系统为 Centos 6.8
Python环境为 Python 3.6
orale信息文件
其次建立一个新的文本文件存放多个数据库的信息
注意最后一行不能是空白行
文件名称:/home/oracle/script/oracle_list.txt
10.65.1.120 sys sys_password 1521 dctest
10.60.14.70 sys sys_password 1527 np1
最终程序
文件名称:/home/oracle/script/checktablespace_multiple.py
#!/usr/bin/python
#coding=utf8
import cx_Oracle
from sendmail_phone import *
def oraclesql(cursor):
#这里我们使用python的open方法打开文件并读取文件内容作为SQL语句执行
#可使用绝对路径或相对路径
fp=open('/home/oracle/script/tablespace.sql','r')
#fp=open('./tablespace.sql','r')
fp1=fp.read()
cursor.execute(fp1)
data=cursor.fetchall()
return data
if __name__=="__main__":
mailcontent=[]
#这里打开oracle_list文件并读取其中内容
file1=open(r"/home/oracle/script/oracle_list.txt")
oracle_list=file1.readlines()
file1.close()
#循环读取每行的内容并使用split函数获取到相关数据库信息
for i in oracle_list:
info=i.split()
ipaddress=info[0].strip()
username=info[1].strip()
password=info[2].strip()
port=info[3].strip()
tnsname=info[4].strip()
try:
db = cx_Oracle.connect(username+'/'+password+'@'+ipaddress+':'+port+'/'+tnsname ,mode=cx_Oracle.SYSDBA)
except Exception as e:
content= (tnsname+' is Unreachable,The reason is '+ str(e)).strip()
mailcontent.append(content)
else:
cursor = db.cursor()
data=oraclesql(cursor)
cursor.close()
db.close()
for i in data:
usage=int(i[4])
if usage>=90:
tablespace=i[0]
mailcontent.append('Be Careful tablespace '+tablespace+' on '+tnsname+' is '+str(usage)+'% Used!')
#这里我们判断mailcontent长度是否为0,不为0说明有超过90%的表空间,然后我们发送邮件
if len(mailcontent) != 0:
content='\n'.join(mailcontent)
send_mail(to_list,' Tablespace usage warnning',content)
运行结果
当有表空间超过阈值即可收到短信
源码位置
代码放在我的github主页,欢迎大家查看
https://github.com/bsbforever/wechat_oms
至此自动化监控Oracle表空间已经讲完。下期待定~