-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhost-report.py
More file actions
276 lines (248 loc) · 7.99 KB
/
host-report.py
File metadata and controls
276 lines (248 loc) · 7.99 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python
#
#
import yaml
import json
import sys
import csv
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from datetime import date
try:
import requests
except ImportError:
print("Please install the python-requests module.")
sys.exit(-1)
from requests_oauthlib import OAuth1
# URL to your Satellite 6 server
satellite_url = "https://satellite.example.com"
# URL for API
satellite_api = "%s/api/v2/" % satellite_url
# Katello API
katello_api = "%s/katello/api/" % satellite_url
# Satellite 6 OAuth credentials
oauth_secret = ""
oauth_key = ""
# Enable SSL
ssl_verify = True
mail_server = "mail.example.com"
to_address = "user@example.com"
from_address = "satellite@example.com"
# connect to Satellite API and pull information
def get_data(url):
# Performs a GET using the passed URL location
satellite = requests.get(url,
auth=(OAuth1(oauth_key, oauth_secret)),
verify=ssl_verify)
return satellite.json()
# pull information and set as json
def get_results(url):
jsn_results = get_data(url)
if jsn_results.get('error'):
print("Error: " + jsn_results['error']['message'])
else:
if jsn_results.get('results'):
return jsn_results['results']
elif 'results' not in jsn_results:
return jsn_results
else:
print("No results found")
return None
# return results value
def return_all_results(url):
results = get_results(url)
if results:
return results
def display_info_for_hosts(url):
# set host values to hosts
hosts = get_results(url)
if hosts:
# loop through all hosts
for host in hosts:
# set hostname to name
name = host['name']
try:
# set host last checkin date
last_checkin = host['subscription_facet_attributes']['last_checkin']
# set errata_counts
errata = host['content_facet_attributes']['errata_counts']
except:
# error checking
last_checkin = "error"
errata = "error"
# set number of bugfixes per host
bugfixes = errata['bugfix']
# set number of security patches per host
security_patches = errata['security']
# set total patches per host
total_patches = errata['total']
# print hostname, lastcheckin, bugfixes, securityPatches and totalPatches
print("%-40s %-30s %-10s %-10s %-10s" % (name,
last_checkin,
bugfixes,
security_patches,
total_patches))
def return_info_for_hosts(url):
# set host values to hosts
hosts = get_results(url)
# set empty list for later
outputData = []
if hosts:
# loop through all hosts
for host in hosts:
# set hostname to name
name = host['name']
try:
# set host last checkin date
last_checkin = host['subscription_facet_attributes']['last_checkin']
# set errata_counts
errata = host['content_facet_attributes']['errata_counts']
except:
# error checking
last_checkin = "error"
errata = "error"
# set number of bugfixes per host
bugfixes = errata['bugfix']
# set number of security patches per host
security_patches = errata['security']
# set total patches per host
total_patches = errata['total']
outputData.append(
{
'hostname': name,
'last_checkin': last_checkin,
'bugfixes': bugfixes,
'security_patches': security_patches,
'total_patches': total_patches
}
)
# return list of dictionaries
return outputData
def return_html_format(server_list):
# initialize variables
body = ""
html = "<html>"
header = "<h1>Satellite Host Report</h1>"
# HTML Table Styling
table_style = """\
<head>
<style>
table {
font-family: Arial, Helvetica, Open Sans, sans-serif;
width: 100%;
border-collapse: collapse;
}
th {
background-color: #1E4B5E;
color: #dddddd;
}
td, th {
text-align: left;
padding: 8px;
border: 1px solid #dddddd;
}
.nth-table tr:nth-child(even) {
color: #5cb85c;
}
</style>
</head>
"""
# HTML Table Headers
table_header = """\
<tr>
<th>Hosts</th>
<th>Last Checkin</th>
<th>Security</th>
<th>Bugfixes</th>
<th>Upgradable</th>
</tr>
"""
for server in server_list:
# pull checkin date for comparison later
checkin_date = server['checkin'].split(' ')[0]
# set to date format to compare later
checkin_date = date.fromisoformat(checkin_date)
# compare dates to set the rwo background color
# `style` requires "" around key/value pair
# this method allows us to do alternate backgrounds
if checkin_date < date.today():
row_style = ' style="background-color: #ba2d37"'
else:
row_style = ""
table_body = """\
<tr>
<td{5}>{0}</td>
<td{5}>{1}</td>
<td{5}>{2}</td>
<td{5}>{3}</td>
<td{5}>{4}</td>
</tr>
""".format(server['hostname'], server['checkin'],
server['security_patches'], server['bugfixes'], server['total_patches'], row_style)
body += table_body
# Concate all HTML output together
html += '\n' + table_style
html += '\n' + header
html += '\n' + '<table class="nth-table">'
html += '\n' + table_header
html += '\n' + body
html += '\n' + '</table>'
html += '\n' + '</html>'
return html
def main():
# pull all information from API
results = get_data(satellite_api + 'hosts')
# pull total number of hosts
total_hosts = results['total']
# pull per_page value
per_page = results['per_page']
# test static per_page
# per_page = 50
# calculate total number of pages based on hosts/per_page
pages = total_hosts // per_page
# print("Display all hosts")
page = 0
# loop through pages to display all hosts and information
# set organization to "Ivy Tech Community College"
# filter requires "" within the string
organization = '"Ivy Tech Community College"'
# declare variable to be used later to email output
server_output = []
# loop through pages
while (page < pages):
page += 1
host_info = return_info_for_hosts(satellite_api +
'hosts?page=' +
str(page) +
'&search=organization=' +
str(organization) +
'&per_page=' +
str(per_page))
for servers in host_info:
# Place server information into list for later use
server_output.append(
{
'hostname': servers['hostname'],
'checkin': servers['last_checkin'],
'bugfixes': servers['bugfixes'],
'security_patches': servers['security_patches'],
'total_patches': servers['total_patches']
}
)
# input server list to get an HTML format output
html_output = return_html_format(server_output)
# create message to send via email
msg = MIMEMultipart('alternative')
msg['Subject'] = "Satellite Host Report"
msg['From'] = from_address
msg['To'] = to_address
mail_body = MIMEText(html_output, 'html')
msg.attach(mail_body)
# Send via SMTP server
smtp = smtplib.SMTP(mail_server)
# sendmail
smtp.sendmail(from_address, to_address, msg.as_string())
smtp.quit()
if __name__ == "__main__":
main()