前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python批量统计Oracle数据库的空间使用量

Python批量统计Oracle数据库的空间使用量

作者头像
星哥玩云
发布2022-08-18 17:27:08
4470
发布2022-08-18 17:27:08
举报
文章被收录于专栏:开源部署

数据库的空间使用情况是好多单位需要关注的,当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'  );

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档