-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract-awr-metrics-to-csv
executable file
·91 lines (77 loc) · 2.61 KB
/
extract-awr-metrics-to-csv
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
86
87
88
89
90
#!/usr/bin/env python2
# Copyright 2013 Nicolas Maupu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########
##NAME
## extract-awr-metrics-to-csv.py
##SYNOPSIS
## Extracts choosen data from Oracle AWR html file and transforms it to a more convenient CSV format.
##DESCRIPTION
## Extract data from a specific html table from AWR Oracle html file.
## To locate the right part of the html AWR file, look at <table> tag and just get the 'summary' properties
## and call this script with two parameters :
## - the filename of the AWR html file
## - the summary extracted before
## Example :
## ./extract-awr-metrics-to-csv.py filename.html "This table displays top SQL by number of parse calls"
##
## This script requires the following python modules :
## - HTMLParser
## - forked-path
##########
from HTMLParser import HTMLParser
from path import path
import sys
class AWRParser(HTMLParser):
isTable = False
rowBegin = False
tableSummary = ""
summaries = []
toWrite = ""
def __init__(self, s):
HTMLParser.__init__(self)
self.summaries = s
def handle_starttag(self, tag, attrs):
if tag == 'table':
for attr in attrs:
if attr[0] == 'summary' and attr[1] in self.summaries:
self.isTable = True
tableSummary = attr[1]
sys.stdout.write("##%s\n" % tableSummary)
def handle_endtag(self, tag):
if tag == 'table':
self.isTable = False
elif tag == 'tr' and self.isTable:
sys.stdout.write("%s\n" % self.toWrite[:-1])
self.toWrite = ""
def handle_data(self, data):
## Handle garbage '' and '\n'
if self.isTable and data != '' and data != '\n':
self.add_to_line(data)
def add_to_line(self, data):
end=";"
self.toWrite += "%s%s" % (data.strip().replace("\n", "").replace(",", "").replace(" ", "_"), end)
##########
def usage():
print("Usage: %s awr_report_file table_summary" % sys.argv[0])
def main():
try:
s = path(sys.argv[1]).bytes()
parser = AWRParser(sys.argv[2])
parser.feed(s)
except:
usage()
sys.exit(1)
if __name__ == "__main__":
main()