! # / Usr / bin / Python
# - * - Coding: UTF-. 8 - * -
Import cx_Oracle AS Oracle
Import Time
DEF nowdate ():
# Get the current time
nowdate = time.strftime ( "% Y% m% d", time.localtime ())
return nowdate
DEF get_connect (UserInfo):
# Oracle database login information to obtain
the try:
conn = oracle.connect (UserInfo)
the Cursor = conn.cursor ()
the except Exception error AS:
Print (error)
the else:
return the Cursor
DEF get_sql (filename):
# obtain statistical Oracle database sql statement
filename = filename
the try:
with Open (filename) AS File:
sql = File.read ()
the except FileNotFoundError:
= error "Sorry, at The File" + filename + "does not exist."
Print (error)
the else:
return SQL
DEF get_Data (SQL):
# get Oracle database table space usage
cursor.execute (SQL)
the Data = the Cursor. or fetchall ()
return Data
DEF get_instance_name ():
# Oracle database instance name Get
the cursor.execute ( 'SELECT from V $ instance instance_name')
Data = cursor.fetchall ()
cursor.close ()
# conn.Close ()
return Data
DEF put_data (instance_name, instance_data, nowtime) :
table # insert resultant data to a specific instance, where 202 is an example of the selected
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 which statistics table space is (do not contain undo table space and temporary table space):
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' );