python批量統計Oracle資料庫的空間使用量

賀子_DBA時代發表於2019-01-01

資料庫的空間使用情況是好多單位需要關注的,當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'  );


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29654823/viewspace-2287091/,如需轉載,請註明出處,否則將追究法律責任。

相關文章