-
Notifications
You must be signed in to change notification settings - Fork 7
/
reportgen.py
executable file
·223 lines (200 loc) · 9.62 KB
/
reportgen.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python
# reportgen.py
# simple script to generate cis scan report based on the ouptut from custom plugins
# output all controls, grouped together with PASS, FAIL, VERIFY
# default output format is html
# Abdul Karim <[email protected]>
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import absolute_import
try:
import json
except ImportError:
import simplejson as json
import sys
import os
import csv
from optparse import OptionParser
raw_log_path='reports/raw'
report_path = 'reports/html'
def getRuns(reports_path):
"""
Get number of run from log locations
"""
dates = [f for f in os.listdir(reports_path) if os.path.isdir(os.path.join(raw_log_path, f))]
return sorted(dates, reverse=True)
def getHosts (hostfile):
"""
return all hosts in inventory file
"""
#dates = [f for f in os.listdir(report_path) if os.path.isdir(os.path.join(raw_log_path, f))]
with open(hostfile, 'r') as f:
host_list = f.readlines()
hosts=[]
for line in host_list:
h = line[1]
if not line.startswith ('#'):
hosts.append(line.strip())
return hosts
def getHostStatus(datestamp,hostname):
"""
get overall status of a host
"""
try:
with open(raw_log_path+'/'+datestamp+'/summary_report_'+datestamp+'.csv', 'rb') as f:
reader = csv.reader(f)
thelist = list(reader)
except:
thelist={}
hoststatus=''
for line in thelist:
if line != []:
status=line[2].strip()
h = line[1].strip()
if h.startswith(hostname) and status.startswith('FAIL'):
return 'FAIL'
break
hoststatus=status
return hoststatus
def generate_report(report_path,inventoryfile):
"""
Process reports output in html
"""
htmlfile = open(report_path+'/cis_report.html','w')
htmlfile.write("<html>")
htmlfile.write( "<head>")
htmlfile.write( ' <meta name="viewport" content="width=device-width, initial-scale=1">')
htmlfile.write( ' <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">')
htmlfile.write( ' <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>')
htmlfile.write( ' <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>')
htmlfile.write( '</head>')
htmlfile.write( '<body>')
htmlfile.write( '<center><h3> cis scan report</h3></center>' )
run_list=getRuns(raw_log_path)
host_list=getHosts(inventoryfile)
count=1
for host in host_list:
hostname=host.strip()
reportcount=1
reportstatus=''
htmlfile.write( '<div class="container">')
htmlfile.write( '<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#host'+str(count)+'">'+hostname+''+reportstatus+'</button>')
htmlfile.write( '<div id="host'+str(count)+'" class="collapse">')
for rundate in run_list:
runstatus = getHostStatus(rundate,hostname)
btn_class = 'btn-info'
try:
if runstatus.startswith ('FAIL'):
btn_class='btn-danger'
elif runstatus.startswith('VERIFY'):
btn_class='btn-warn'
elif runstatus.startswith('PASS'):
btn_class='btn-success'
except KeyError as err:
btn_class='btn-disabled'
htmlfile.write( '<div class="container">')
htmlfile.write( '<button type="button" class="btn '+btn_class+'" data-toggle="collapse" data-target="#report_'+str(count)+''+str(reportcount)+'">'+rundate+'</button>')
htmlfile.write( '<div id="report_'+str(count)+''+str(reportcount)+'" class="collapse">')
htmlfile.write( '<div class="container">')
htmlfile.write( '<button type="button" class="btn btn-danger" data-toggle="collapse" data-target="#reportfailed_'+str(count)+''+str(reportcount)+'">FAILED</button>')
htmlfile.write( '<div id="reportfailed_'+str(count)+''+str(reportcount)+'" class="collapse">')
htmlfile.write( '<div class="table-responsive">')
htmlfile.write( "<table class='table'>")
htmlfile.write( "<tr><td>Date of run: "+rundate+"</td>")
htmlfile.write( "<tr bgcolor=grey><th> control</th> <th> status </th> <th>command</th> <th>output</th></tr>")
try:
with open(raw_log_path+'/'+rundate+'/'+hostname+'_'+rundate+'.json','r') as f:
data = f.readlines()
for line in data:
j = json.loads(line)
for control, result in j.iteritems():
colour='lightgreen'
if result['status'] == 'FAIL':
colour='red'
htmlfile.write( '<tr bgcolor="+colour+"><td>'+control+'</td> <td>'+result["status"]+'</td>')
htmlfile.write( "<td>"+result['cmd']+"</td>")
htmlfile.write( "<td>"+str(result['output'])+"</td></tr>")
except IOError as err:
htmlfile.write( "<tr><td>no run for this date</td> <td> %s </td> </tr>" % (err))
htmlfile.write( "</table>")
htmlfile.write( '</div>')
htmlfile.write( '</div>')
htmlfile.write( '</div>')
htmlfile.write( '<div class="container">')
htmlfile.write( '<button type="button" class="btn btn-success" data-toggle="collapse" data-target="#reportpass_'+str(count)+''+str(reportcount)+'">PASS</button>')
htmlfile.write( '<div id="reportpass_'+str(count)+''+str(reportcount)+'" class="collapse">')
htmlfile.write( '<div class="table-responsive">')
htmlfile.write( "<table class='table'>")
htmlfile.write( "<tr><td>Date of run: "+rundate+"</td>")
htmlfile.write( "<tr bgcolor=grey><th> control</th> <th> status </th> <th>command</th> <th>output</th></tr>")
try:
with open(raw_log_path+'/'+rundate+'/'+hostname+'_'+rundate+'.json','r') as f:
data = f.readlines()
for line in data:
j = json.loads(line)
for control, result in j.iteritems():
colour='lightgreen'
if result['status'] == 'PASS':
colour='green'
htmlfile.write( "<tr bgcolor="+colour+"><td>"+control+"</td> <td>"+result['status']+"</td>")
htmlfile.write( "<td>"+result['cmd']+"</td>")
htmlfile.write( "<td>"+result['output']+"</td></tr>")
except IOError as err:
htmlfile.write( "<tr><td>no run for this date</td> <td> %s</td> </tr>" %(err))
htmlfile.write( "</table>")
htmlfile.write( '</div>')
htmlfile.write( '</div>')
htmlfile.write( '</div>')
htmlfile.write( '<div class="container">')
htmlfile.write( '<button type="button" class="btn btn-warning" data-toggle="collapse" data-target="#reportverify_'+str(count)+''+str(reportcount)+'">VERIFY</button>')
htmlfile.write( '<div id="reportverify_'+str(count)+''+str(reportcount)+'" class="collapse">')
htmlfile.write( '<div class="table-responsive">')
htmlfile.write( "<table class='table'>")
htmlfile.write( "<tr><td>Date of run: "+rundate+"</td>")
htmlfile.write( "<tr bgcolor=grey><th> control</th> <th> status </th> <th>command</th> <th>output</th></tr>")
try:
with open(raw_log_path+'/'+rundate+'/'+hostname+'_'+rundate+'.json','r') as f:
data = f.readlines()
for line in data:
j = json.loads(line)
for control, result in j.iteritems():
colour='lightgreen'
if result['status'] == 'VERIFY' or result['status'] == 'UNKNOWN':
colour='orange'
htmlfile.write( "<tr bgcolor="+colour+"><td>"+control+"</td><td>"+result['status']+"</td>")
htmlfile.write( "<td>"+result['cmd']+"</td>")
htmlfile.write( "<td>"+result['output']+"</td></tr>")
except IOError as err:
htmlfile.write( "<tr><td>no run for this date</td> <td>%s</td> </tr>" % (err))
htmlfile.write( "</table>")
htmlfile.write( '</div>')
htmlfile.write( '</div>')
htmlfile.write( '</div>')
htmlfile.write( '</div>')
htmlfile.write( '</div>')
reportcount = reportcount + 1
htmlfile.write( '</div>')
htmlfile.write( '</div>')
count = count + 1
htmlfile.write( "</body>")
htmlfile.write( "</html>")
print ('Output generated in '+report_path)
def parse_cmdline():
"""
Process commandline arguments etc
"""
parser = OptionParser()
parser.add_option("-i", "--inventory", dest="inventoryfile",
help="inventory list of hostnames (required)")
(options, args) = parser.parse_args()
if not options.inventoryfile:
parser.print_help()
sys.exit(1)
return options,args
def main():
options,args = parse_cmdline()
if not os.path.exists(report_path):
os.mkdir(report_path)
generate_report(report_path,options.inventoryfile)
if __name__ == "__main__":
main()