Skip to content

Instantly share code, notes, and snippets.

@stavlor
Created February 12, 2019 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stavlor/0d6b0b330fbbf45e5fb66ba0802a533f to your computer and use it in GitHub Desktop.
Save stavlor/0d6b0b330fbbf45e5fb66ba0802a533f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import tools
import socket
import json
import datetime
import xml.etree.ElementTree as ET
import xlsxwriter
from Queue import Queue
from threading import Thread
def get_output(ip):
import recite
output = None
recite.set_verbosity(0)
if not recite.process("SET $IP " + str(ip)):
print("Error")
if not recite.process("GetSystemViews"):
pass
else:
output = recite.get_output()
result = {}
for line in output.split('\n'):
try:
key, value = line.split('=')
key = key.strip(' ')
value = value.strip(' ')
result[key] = value
except ValueError:
pass
return result
def get_rac_ip(ip):
ip1, ip2, ip3, ip4 = ip.split('.')
if int(ip4) > 40:
print "DEBUG: IP > 40: " + ip4
return ip1+ '.' +ip2+ '.' +ip3+ '.1' + ip4
def get_mgr_spare(ip):
ip1, ip2, ip3, ip4 = ip.split('.')
return ip1 + '.' + ip2 + '.' + ip3 + '.' + '11'
def split_hosts_result(result):
hosts = []
stdout = result[0]
stderr = result[1]
stdout_nsp = stdout.rstrip("\n")
lines = stdout_nsp.split("\n")
for line in lines:
try:
shotres = line.split("\t")
ip = shotres[0]
shorthost = shotres[1]
longhost = shotres[2]
hosts.append(ip)
except:
pass
return hosts
def process_additional(ip):
import tools
import recite
import socket
import json
import xml.etree.ElementTree as ET
if not tools.is_host_up(ip):
commissues.put(ip)
return
output = {}
hostname = tools.get_hostname(ip)
rac_ip = get_rac_ip(ip)
if not tools.is_host_up(rac_ip):
commissues.put(rac_ip)
return
output = get_output(rac_ip)
try:
output['Hostname'] = hostname
output['RACip'] = rac_ip
except TypeError:
pass
results.put(output)
def worker_process():
while not sitelist.empty():
process_site(sitelist.get())
def process_site(site):
import tools
import recite
import socket
import json
import xml.etree.ElementTree as ET
if not tools.is_host_up(site):
pass
mgr_ip = socket.gethostbyname(site)
process_additional(mgr_ip)
process_additional(get_mgr_spare(mgr_ip))
sitelist = Queue()
results = Queue()
commissues = Queue()
for item in tools.get_site_list():
sitelist.put(item)
maxworkers = 30
threads = []
for i in range(1,maxworkers+1):
t = Thread(target=worker_process)
t.daemon = True
t.start()
threads.append(t)
for t in threads:
t.join(timeout=50)
abnormal = ""
shutdown = ""
commissues_out = ""
while not commissues.empty():
res = commissues.get()
if not res.endswith('.11'):
commissues_out += "Could not reach " + tools.guess_hostname(res) + " - (" + res + ")\n"
while not results.empty():
status = ["Unknown", "OK", "Degraded", "Error", "DMTF Reserved", "Vendor Reserved"]
powerstates = ["Unknown", "Unknown", "On", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Shutdown", "Unknown", "Unknown", "Unknown", "Unknown", "Off"]
record = results.get()
if record is None:
#print "DEBUG: record none...\n"
continue
if 'Model' not in record:
record['Model'] = "Unknown"
if 'PowerState' not in record:
record['PowerState'] = "Unknown"
if 'ServiceTag' not in record:
record['ServiceTag'] = "Unknown"
if not 'PrimaryStatus' in record:
#print "Could not determine status for Host:", record['Hostname'], "Result message was:", record
pass
elif record['PrimaryStatus'] != '1' and record['PrimaryStatus'] != '3':
abnormal += "Host <a href=\"https://" +record['RACip']+ "/\">" + record['Hostname'] + "</a> has abnormal status (" + str(status[int(record['PrimaryStatus'])]) + ") PowerStatus: " + str(powerstates[int(record['PowerState'])]) + " Model: "+ record['Model'].rstrip('\n') + " ServiceTag:"+ record['ServiceTag'] +'\n'
elif powerstates[int(record['PowerState'])] == "Shutdown" and record['PrimaryStatus'] == '3' and not "C2" in record['Hostname']:
shutdown += "Host " + record['Hostname'] + " is currently powered down ST:" + record['ServiceTag'] + " Model:" + record['Model'].rstrip('\n') + '\n'
f = open('/usr/share/nginx/html/rac-output.html', 'w')
f.write("Last updated:" + datetime.datetime.now().isoformat() + "<br /><br />The following hosts had Errors:<br /><hr /><br />"+ abnormal.replace('\n', '<br />') + "<br />The following units are shutdown:<br /><hr /><br />"+ shutdown.replace('\n', '<br />') + "<br />Comm Issues<br /><hr /><br />" + commissues_out.replace('\n', '<br />'))
#print "The following hosts had Errors:\n", abnormal, "\nThe following units are shutdown:\n", shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment