這篇文章主要介紹“python怎么批量統計Oracle數據庫的空間使用量”,在日常操作中,相信很多人在python怎么批量統計Oracle數據庫的空間使用量問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”python怎么批量統計Oracle數據庫的空間使用量”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
數據庫的空間使用情況是好多單位需要關注的,當Oracle數據庫服務器比較多的時候,手動統計就顯得費時費力了,下面編寫了python腳本批量統計Oracle的表空間總使用量
#! /usr/bin/python
# -*- coding: UTF-8 -*-
import cx_Oracle as oracle
import time
def nowdate():
#獲取當前時間
nowdate=time.strftime("%Y%m%d",time.localtime())
return nowdate
def get_connect(userinfo):
#獲取Oracle數據庫的登陸信息
try:
conn=oracle.connect(userinfo)
cursor=conn.cursor()
except Exception as error:
print(error)
else:
return cursor
def get_sql(filename):
#獲取統計Oracle數據庫的sql語句
filename=filename
try:
with open(filename) as file:
sql=file.read()
except FileNotFoundError:
error="Sorry,the file " + filename + " does not exist."
print(error)
else:
return sql
def get_data(sql):
#獲取Oracle數據庫的表空間使用情況
cursor.execute(sql)
data = cursor.fetchall()
return data
def get_instance_name():
#獲取Oracle數據庫實例名字
cursor.execute('select instance_name from v$instance')
data = cursor.fetchall()
cursor.close()
# conn.close()
return data
def put_data(instance_name,instance_data,nowtime):
#將得到的數據insert到特定的實例的表中,這里選擇的是202的實例
host = "10.29.29.1"
port = "1521"
sid = "test209"
dsn = oracle.makedsn(host, port, sid)
conn =oracle.connect("liuwenhe", "liuwenhe", dsn)
cursor = conn.cursor()
insert_sql="insert into liuwenhe.tongji values ('"+instance_name+"','"+str(instance_data)+"','"+nowtime+"')"
cursor.execute(insert_sql)
cursor.close()
conn.commit()
conn.close()
if __name__=='__main__':
try:
userinfofile='userinfo.txt'
with open(userinfofile) as file:
userinfos=file.readlines()
for userinfo in userinfos:
cursor=get_connect(userinfo)
instance_name1=get_instance_name()
instance_name=instance_name1[0][0]
sql=get_sql('select')
cursor=get_connect(userinfo)
instance_data1=get_data(sql)
instance_data=instance_data1[0][0]
nowtime=nowdate()
put_data(instance_name,instance_data,nowtime)
except Exception as e:
print (e)
其中統計Oracle表空間的的sql為(不包含undo表空間和臨時表空間):
select
sum(round(used_gb))used_M
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(+) and a.tablespace_name!='UNDOTBS1' and a.tablespace_name!='UNDOTBS' );
到此,關于“python怎么批量統計Oracle數據庫的空間使用量”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。