-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathreport.py
executable file
·85 lines (76 loc) · 3.91 KB
/
report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python2
import os, sys, json
from backupcommon import scriptpath, Configuration, BackupLogger, BackupTemplate, info, error, debug, exception, create_snapshot_class
from tempfile import mkstemp
from oraexec import OracleExec
def printhelp():
print "Usage: report.py [comma separated list of databases]"
sys.exit(2)
if len(sys.argv) not in [1,2]:
printhelp()
# Directory where the executable script is located
scriptpath = scriptpath()
# Read configuration
logf = mkstemp(prefix='backupreport-', suffix='.log')
os.close(logf[0])
Configuration.init('generic')
BackupLogger.init(logf[1], 'reporting')
Configuration.substitutions.update( {'logfile': BackupLogger.logfile, 'autorestorecatalog': Configuration.get('autorestorecatalog', 'autorestore')} )
reporttemplate = BackupTemplate('reporttemplate.cfg')
def exec_sqlplus(oraexec, script, header = 'sqlplusheader'):
finalscript = "%s\n%s\n%s" % (reporttemplate.get(header), script, reporttemplate.get('sqlplusfooter'))
output = oraexec.sqlplus(finalscript, silent=True)
for line in output.splitlines():
if line.startswith('OUTLOG: '):
yield(line.strip()[8:])
def process_database(dbname):
Configuration.defaultsection = dbname
Configuration.substitutions.update({'dbname': dbname})
oraexec = OracleExec(oraclehome=Configuration.get('oraclehome', 'generic'), tnspath=os.path.join(scriptpath, Configuration.get('tnsadmin', 'generic')))
# Read job status information from the database
jobinfo = {}
for line in exec_sqlplus(oraexec, reporttemplate.get('jobstatus')):
j = json.loads(line)
if j["type"] == "job":
if j["job_name"] == "ARCHLOGBACKUP_JOB":
jobinfo["archlog"] = j
elif j["job_name"] == "IMAGECOPY_JOB":
jobinfo["imagecopy"] = j
elif j["type"] == "exec":
if j["job_name"] == "ARCHLOGBACKUP_JOB":
jobinfo["archlogexec"] = j
elif j["job_name"] == "IMAGECOPY_JOB":
jobinfo["imagecopyexec"] = j
# Read snapshot information
zfs = create_snapshot_class(dbname)
snaps = zfs.listsnapshots(True, True)
# Autorestore information
autorestoreinfo = None
try:
for line in exec_sqlplus(oraexec, reporttemplate.get('autorestorestatus'), 'sqlplusautorestoreheader'):
autorestoreinfo = json.loads(line)
except:
pass
# Print output
print "%s:" % dbname
try:
print " Backup job: %s, last: %s, duration: %s, last failure: %s" % (jobinfo['imagecopy']['state'], jobinfo['imagecopy']['last_start_date'], jobinfo['imagecopy']['last_run_duration'], jobinfo['imagecopyexec']['last_failed'])
print " Archivelog job: %s, last: %s, duration: %s, last failure: %s" % (jobinfo['archlog']['state'], jobinfo['archlog']['last_start_date'], jobinfo['archlog']['last_run_duration'], jobinfo['archlogexec']['last_failed'])
if len(snaps) > 0:
firstsnap = zfs.getsnapinfo(snaps[0])
lastsnap = zfs.getsnapinfo(snaps[-1])
print " Snapshots: %d, latest: %s, oldest: %s" % (len(snaps), firstsnap["creation"], lastsnap["creation"])
else:
print " Snapshots: none"
if autorestoreinfo is not None:
print " Last successful restore: %s, last restore failure: %s, last successful validation: %s, avg difference from target (s): %d, avg restore time (min): %d" % (autorestoreinfo["last_success"], autorestoreinfo["last_fail"], autorestoreinfo["last_validated"], autorestoreinfo["avgdiff"], autorestoreinfo["avgrestoremin"])
except:
print " Error getting information."
excludelist = ['generic','rman','zfssa','autorestore']
includelist = []
if len(sys.argv) == 2:
includelist = sys.argv[1].split(",")
# Loop through all sections
for dbname in Configuration.sections():
if dbname not in excludelist and (len(includelist) == 0 or dbname in includelist):
process_database(dbname)