Skip to content

Commit e5cdeeb

Browse files
author
Randy Johnson
committed
First draft
1 parent 0a17781 commit e5cdeeb

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed

filestorage

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
#!/bin/env python
2+
3+
##################################################################################################
4+
# Name: filestorage #
5+
# Author: Randy Johnson #
6+
# Description: Prints controlfile, redolog, datafile, tempfile storage utilization for a #
7+
# database. #
8+
# #
9+
# Usage: filestorage [options] #
10+
# #
11+
# Options: #
12+
# -h, --help show this help message and exit #
13+
# -s print SQL query. #
14+
# -v print version info. #
15+
# #
16+
# History: #
17+
# #
18+
# Date Ver. Who Change Description #
19+
# ---------- ---- ---------------- ------------------------------------------------------------- #
20+
# 08/27/2012 1.00 Randy Johnson Initial release. #
21+
# 07/17/2015 2.00 Randy Johnson Updated for Python 2.4-3.4 compatibility. #
22+
# 08/09/2015 3.00 Randy Johnson Added prompts for username, password, tnsname. #
23+
# 10/25/2015 3.01 Randy Johnson Added error checking. #
24+
# 12/19/2017 4.00 Randy Johnson Substantial updates to include file level detail in addition #
25+
# to changing from using Python to format the report to using #
26+
# native SQL*Plus. #
27+
##################################################################################################
28+
29+
30+
# --------------------------------------
31+
# ---- Import Python Modules -----------
32+
# --------------------------------------
33+
from optparse import OptionParser
34+
from os import environ
35+
from os.path import basename
36+
from sys import argv
37+
from sys import exit
38+
from sys import version_info
39+
from signal import SIGPIPE
40+
from signal import SIG_DFL
41+
from signal import signal
42+
from Oracle import ErrorCheck
43+
from Oracle import RunSqlplus
44+
from Oracle import SetOracleEnv
45+
from Oracle import ParseConnectString
46+
from Oracle import FormatNumber
47+
48+
# --------------------------------------
49+
# ---- Main Program --------------------
50+
# --------------------------------------
51+
if (__name__ == '__main__'):
52+
Cmd = basename(argv[0]).split('.')[0]
53+
CmdDesc = 'File Storage Report'
54+
Version = '4.00'
55+
VersionDate = 'Tue Dec 19 19:06:16 CST 2017'
56+
DevState = 'Production'
57+
Banner = CmdDesc + ': Release ' + Version + ' ' + DevState + '. Last updated: ' + VersionDate
58+
Sql = ''
59+
SqlHeader = '/***** ' + CmdDesc.upper() + ' *****/'
60+
ErrChk = False
61+
ArgParser = OptionParser()
62+
InStr = ''
63+
ConnStr = ''
64+
Colsep = "'~'"
65+
66+
# For handling termination in stdout pipe; ex: when you run: oerrdump | head
67+
signal(SIGPIPE, SIG_DFL)
68+
69+
ArgParser.add_option('-d', dest='Detail', action='store_true', default=False, help="Show Detail")
70+
ArgParser.add_option('--s', dest='Show', action='store_true', default=False, help="print SQL query.")
71+
ArgParser.add_option('--v', dest='ShowVer', action='store_true', default=False, help="print version info.")
72+
73+
# Parse command line arguments
74+
Options, args = ArgParser.parse_args()
75+
76+
Detail = Options.Detail
77+
Show = Options.Show
78+
ShowVer = Options.ShowVer
79+
80+
if (ShowVer == True):
81+
print('\n%s' % Banner)
82+
exit()
83+
84+
Sql = "set lines 2000\n"
85+
Sql += "set timing off\n"
86+
Sql += "set time off\n"
87+
Sql += "set feedback off\n"
88+
Sql += "set echo off\n"
89+
Sql += "column total format 999,999,999,999,999.99 heading 'Size (MB)'\n"
90+
Sql += "column type format a20 heading 'Type'\n"
91+
Sql += "column datafiles format a80 heading 'Data Files'\n"
92+
Sql += "column tablespace_name format a30 heading 'Tablespace'\n"
93+
Sql += "column file_name format a70 heading 'File'\n"
94+
Sql += "column thread# format 9999999999999999999 heading 'Thread'\n"
95+
Sql += "column group# format 99999999 heading 'Group'\n"
96+
Sql += "column member format a70 heading 'Member'\n"
97+
Sql += "column name format a101 heading 'File'\n\n"
98+
99+
if (Detail) :
100+
Sql += "\n set pages 50000\n"
101+
102+
Sql += "prompt\n"
103+
Sql += "prompt ===================\n"
104+
Sql += "prompt Controlfiles\n"
105+
Sql += "prompt ===================\n"
106+
Sql += " select name\n"
107+
Sql += " , block_size*file_size_blks/1024/1024 total\n"
108+
Sql += " from v$controlfile\n"
109+
Sql += " order by name;\n\n"
110+
111+
Sql += "prompt\n"
112+
Sql += "prompt ===================\n"
113+
Sql += "prompt Datafiles\n"
114+
Sql += "prompt ===================\n"
115+
Sql += " select tablespace_name\n"
116+
Sql += " , file_name\n"
117+
Sql += " , bytes/1024/1024 total\n"
118+
Sql += " from dba_data_files\n"
119+
Sql += "order by tablespace_name, file_name;\n\n"
120+
121+
Sql += "prompt\n"
122+
Sql += "prompt ===================\n"
123+
Sql += "prompt Tempfiles\n"
124+
Sql += "prompt ===================\n"
125+
Sql += " select tablespace_name\n"
126+
Sql += " , file_name\n"
127+
Sql += " , bytes/1024/1024 total\n"
128+
Sql += " from dba_temp_files\n"
129+
Sql += "order by tablespace_name, file_name;\n\n"
130+
131+
Sql += "prompt\n"
132+
Sql += "prompt ===================\n"
133+
Sql += "prompt Redologs\n"
134+
Sql += "prompt ===================\n"
135+
Sql += " select l.thread#\n"
136+
Sql += " , l.group#\n"
137+
Sql += " , lf.member file_name\n"
138+
Sql += " , l.bytes/1024/1024 total\n"
139+
Sql += " from v$logfile lf\n"
140+
Sql += " , v$log l\n"
141+
Sql += " where lf.group# = l.group#\n"
142+
Sql += " order by l.thread#\n"
143+
Sql += " , l.group#;\n\n"
144+
145+
Sql += "prompt\n"
146+
Sql += "prompt ===================\n"
147+
Sql += "prompt Summary\n\n"
148+
Sql += "prompt ===================\n"
149+
150+
Sql += "select 'datafiles' type, sum(bytes)/1024/1024 total from dba_data_files\n"
151+
Sql += " union\n"
152+
Sql += "select 'tempfiles' type, sum(bytes)/1024/1024 total from dba_temp_files\n"
153+
Sql += " union\n"
154+
Sql += "select 'redologs' type, sum(bytes)/1024/1024 total from v$log\n"
155+
Sql += " union\n"
156+
Sql += "select 'controlfiles' type, sum(block_size*file_size_blks)/1024/1024 total from v$controlfile;\n\n"
157+
158+
Sql += "prompt\n"
159+
Sql += "\nset pages 0\n"
160+
Sql += "select 'Total' type, (dfiles.bytes + tfiles.bytes + rfiles.bytes + cfiles.bytes)/1024/1024 total\n"
161+
Sql += " from (select 'a' col1, sum(bytes) bytes from dba_data_files) dfiles,\n"
162+
Sql += " (SELECT 'a' col1, sum(bytes) bytes from dba_temp_files) tfiles,\n"
163+
Sql += " (SELECT 'a' col1, sum(bytes) bytes from v$log) rfiles,\n"
164+
Sql += " (SELECT 'a' col1, sum(block_size*file_size_blks) bytes from v$controlfile) cfiles\n"
165+
Sql += " WHERE dfiles.col1 = tfiles.col1\n"
166+
Sql += " AND dfiles.col1 = rfiles.col1\n"
167+
Sql += " AND dfiles.col1 = cfiles.col1;\n\n"
168+
169+
Sql = Sql.strip()
170+
171+
if(Show):
172+
print('-----------cut-----------cut-----------cut-----------cut-----------cut-----------')
173+
print(Sql)
174+
print('-----------cut-----------cut-----------cut-----------cut-----------cut-----------')
175+
exit()
176+
177+
# Check/setup the Oracle environment
178+
if (not('ORACLE_SID' in list(environ.keys()))):
179+
print('ORACLE_SID is required.')
180+
exit(1)
181+
else:
182+
# Set the ORACLE_HOME just in case it isn't set already.
183+
if (not('ORACLE_HOME' in list(environ.keys()))):
184+
(OracleSid, OracleHome) = SetOracleEnv(environ['ORACLE_SID'])
185+
186+
# Parse the connect string if any, prompt for username, password if needed.
187+
if (len(args) > 0 and Show == False):
188+
InStr = args[0]
189+
ConnStr = ParseConnectString(InStr)
190+
191+
# Execute the report
192+
if (ConnStr != ''):
193+
(Stdout) = RunSqlplus(Sql, ErrChk, ConnStr)
194+
else:
195+
(Stdout) = RunSqlplus(Sql, ErrChk)
196+
197+
Stdout = Stdout.strip()
198+
ComponentList = ['sqlplus','rdbms', 'oracore']
199+
(rc, ErrorList) = ErrorCheck(Stdout, ComponentList)
200+
201+
if (rc != 0):
202+
#PrintError(Sql, Stdout, ErrorList)
203+
print(Stdout)
204+
exit(rc)
205+
else:
206+
print("\n%s" % Stdout)
207+
exit(0)
208+
# --------------------------------------
209+
# ---- End Main Program ----------------
210+
# --------------------------------------

0 commit comments

Comments
 (0)