最近,自学了Python基础,突发奇想,把以前通过shell自定义通过nagios实现Oracle表空间以及ASM以及备份的脚本改进下,首先感叹的是Python脚本看上去确实挺好的,效率还不错。
这是初步版本,姑且定义为v0.1吧,目前具备的功能很简单——得到超过阀值的表空间信息。
#############################################
# python3.5 #
# Any questions contact me ! #
# 2424623044@qq.com #
#############################################
#/usr/bin/python3
#coding=utf-8
import cx_Oracle
jdbc=["system/redhat@10.1.14.67/ora.my.com",] #后面增加完整的数据库JDBC即可
for d in jdbc:
con = cx_Oracle.Connection(d)
cur=con.cursor()
c = cur.execute("""select tablespace_name,
max_gb,
used_gb,
round(100 * used_gb / max_gb) "pct_used(%)"
from (select a.tablespace_name tablespace_name,
round((a.bytes_alloc - nvl(b.bytes_free, 0)) / power(2, 30),
2) used_gb,
round(a.maxbytes / power(2, 30), 2) max_gb
from (select f.tablespace_name,
sum(f.bytes) bytes_alloc,
sum(decode(f.autoextensible,
'YES',
f.maxbytes, 'NO',
f.bytes)) maxbytes
from dba_data_files f
group by tablespace_name) a,
(select f.tablespace_name, sum(f.bytes) bytes_free
from dba_free_space f
group by tablespace_name) b
where a.tablespace_name = b.tablespace_name(+)
union all
select h.tablespace_name tablespace_name,
round(sum(nvl(p.bytes_used, 0)) / power(2, 30), 2) used_gb,
round(sum(decode(f.autoextensible,
'YES',
f.maxbytes,
'NO',
f.bytes)) / power(2, 30),
2) max_gb
from v$temp_space_header h, v$temp_extent_pool p, dba_temp_files f
where p.file_id(+) = h.file_id
and p.tablespace_name(+) = h.tablespace_name
and f.file_id = h.file_id
and f.tablespace_name = h.tablespace_name
group by h.tablespace_name)""")
for x in c:
if x[3]>60: #阀值为60,这里是为了测试
print ("The IP:%s TBS %s usages is %s"%(d.split('@')[1].split('/')[0],x[1],x[3]))
cur.close()
con.close()
#结果:Warning:IP:10.1.14.67 TBS 3 usages is overtarget 99
遗留问题:
1.未封装,数据库JDBC信息透明
2.未多线程化,脚本相对比较低级
3.未加入邮件告警功能
4.未设置异常处理
后续继续优化。