Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python batch count the space usage of Oracle database

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "python how to batch count the space usage of Oracle database". In daily operation, I believe that many people have doubts about how python batch statistics the space usage of Oracle database. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the question of "how python batch statistics the space usage of Oracle database". Next, please follow the editor to study!

The space usage of the database is something that many units need to pay attention to. When there are many Oracle database servers, manual statistics will be time-consuming and laborious. Here is a python script to batch count the total table space usage of Oracle.

#! / 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):

# obtain login information of Oracle database

Try:

Conn=oracle.connect (userinfo)

Cursor=conn.cursor ()

Except Exception as error:

Print (error)

Else:

Return cursor

Def get_sql (filename):

# get the sql statement of statistical Oracle database

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):

# get the tablespace usage of Oracle database

Cursor.execute (sql)

Data = cursor.fetchall ()

Return data

Def get_instance_name ():

# get the name of Oracle database instance

Cursor.execute ('select instance_name from vogue instance')

Data = cursor.fetchall ()

Cursor.close ()

# conn.close ()

Return data

Def put_data (instance_name,instance_data,nowtime):

# insert the obtained data to the table of a specific instance. Here, the instance of 202is 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)

The sql of statistical Oracle table space is (excluding 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.tablespaceful nameplate for UNDOTBS1' and a.tablespaceful nameplate for UNDOTBS')

At this point, the study on "how to batch count the space usage of Oracle database by python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report