Last active
August 29, 2015 14:05
-
-
Save jstanley23/4d6acbef911dd863f518 to your computer and use it in GitHub Desktop.
ZenModeler HTML Results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import subprocess | |
import socket | |
import datetime | |
import re | |
import sys | |
devices = '' | |
device_list = [] | |
devices_list = [] | |
change_regex = re.compile('processing .* for device .*\n.*Changes in configuration applied') | |
device_regex = re.compile('processing .* for device (.*)\n') | |
host_name = socket.gethostname() | |
zenoss_url = "https://REPLACEURL.COM" | |
# SCP Settings | |
rm_host = "FQDNHERE" | |
rm_short = "short name of your RM" | |
rm_dir = "/opt/zenmodel/" | |
# Set modeler command | |
# We broke out RM and Collectors. Our RM has 'zrm' | |
# in the hostname. Modify this if you are running | |
# a stand alone or named your RM differently. | |
modeler = host_name + '_zenmodeler' | |
if rm_short in host_name: | |
modeler = "zenmodeler" | |
if len(sys.argv) > 1: | |
modeler_args = ' '.join(sys.argv) | |
else: | |
modeler_args = 'run' | |
time_begin = datetime.datetime.today() | |
# Setup process call and standard streams | |
model = subprocess.Popen([modeler, modeler_args], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
out, err = model.communicate() | |
# Send full results into temp log file | |
log_file = time_begin.strftime("/tmp/zenmodel-%m-%d-%Y-%H%M%S.log") | |
log = open(log_file, "w+") | |
log.write(err) | |
log.close() | |
time_end = datetime.datetime.today() | |
total_time = "Time elapsed: %s" % ((time_end - time_begin) / 60) | |
# Find device changes | |
model_changes = change_regex.findall(err) | |
for s in model_changes: | |
d = device_regex.match(s) | |
devices_list.append(d.group(1)) | |
device_list = list(set(devices_list)) | |
if len(device_list) > 0: | |
for device in device_list: | |
d = '<a href="%(zenoss_url)s/zport/dmd/deviceSearchResults?query=\ | |
%(device)s">%(device)s</a><br>\n' % { 'zenoss_url':zenoss_url, | |
'device':device } | |
devices += d | |
else: | |
devices = 'None' | |
# Compile HTML results | |
html_body = """<style type="text/css"> | |
<!-- | |
.tab { margin-left: 40px; } | |
--> | |
</style> | |
<body size=11><font face="calibri"> | |
<h1>ZenModeler Results for:</h1> | |
<h2>%(hostname)s</h2> | |
<p>ZenModeler started at %(start_time)s | |
<br>ZenModeler ended at %(end_time)s | |
<br>Total run time was %(run_time)s | |
<br> | |
<br><b>%(device_num)s</b> devices had changes applied to them. | |
<br> | |
<br>Devices with changes applied:</p> | |
<p class="tab">%(devices)s | |
<br></p></font></body> | |
""" % { 'hostname':host_name, | |
'start_time':time_begin.strftime("%a, %b %d %Y %H:%M:%S %Z"), | |
'end_time':time_end.strftime("%a, %b %d %Y %H:%M:%S %Z"), | |
'run_time':total_time, 'device_num':len(device_list), | |
'devices':devices } | |
# Write results to file, send file to RM | |
# Change if you have a stand alone instance | |
if rm_short in host_name: | |
results_file = "%s/localhost.html" % rm_dir | |
else: | |
results_file = "/tmp/%s.html" % host_name | |
results = open(results_file, "w+") | |
results.seek(0) | |
results.write(html_body) | |
results.close() | |
# Change if you have a stand alone instance | |
if rm_short not in host_name: | |
scp_cmd = "scp" | |
remote_args = "%s:%s" % (rm_host, rm_dir) | |
scpcmd = subprocess.Popen([scp_cmd, results_file, remote_args], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
scpout, scperr = scpcmd.communicate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment