Dispaly IP address in binary during boot

This commit is contained in:
Kris-Sekula 2017-03-09 22:26:38 +00:00
parent 0ce13fc9c8
commit fab04d1b15
1 changed files with 158 additions and 107 deletions

View File

@ -1,122 +1,159 @@
#!/usr/bin/env python #!/usr/bin/env python
#
# version 1.0
#
# #
# For debugging we use the bottom left corner pixel and: # For debugging we use the bottom left corner pixel and:
# #
# show red dot failed to get data # show red dot if failed to get data
# show blue dot if failed to refresh the cookie. # show blue dot if failed to refresh the cookie.
# show orage when we start refreshing data starts. # show orange when we start refreshing data.
# show yellow when initial login fails. # show yellow when login fails.
#
#
# ver 1.1 added dispaly of ip address on boot in binary format.
#
#
import time import time
import json import json
import requests import requests
import time import time
import unicornhat as unicorn import unicornhat as unicorn
import urllib3
import socket
import commands
print("Run colours based on data") urllib3.disable_warnings()
unicorn.set_layout(unicorn.PHAT) unicorn.set_layout(unicorn.PHAT)
unicorn.rotation(180) unicorn.rotation(180)
unicorn.brightness(0.4) unicorn.brightness(0.4)
height,width=unicorn.get_shape() height,width=unicorn.get_shape()
# show one blue pixel to indcatie the script started
unicorn.set_pixel(3,1,0,0,255) # login details for your APIC
unicorn.set_pixel(4,1,0,0,255)
unicorn.set_pixel(3,2,0,0,255) apic = '10.50.138.215'
unicorn.set_pixel(4,2,0,0,255) username = 'aci-health'
unicorn.show() password = 'aci-health'
# show blue pixels in each corner
def show_icon(timedel):
unicorn.set_pixel(0,0,0,0,255)
unicorn.set_pixel(0,3,0,0,255)
unicorn.set_pixel(7,0,0,0,255)
unicorn.set_pixel(7,3,0,0,255)
unicorn.show()
time.sleep(timedel)
# show ipv4 address in binary on the 4x8 LEDs
def show_ip(timedel):
for address in filter(None, commands.getoutput("hostname -I").split(" ")):
show_icon(1)
try:
print "processing: ",address
for octetidx,octet in enumerate(address.split(".")):
bin_octet="{0:08b}".format(int(octet))
for idx,c in enumerate(bin_octet[::-1]):
if c == "0":
unicorn.set_pixel(idx,octetidx,0,255,0)
elif c == "1":
unicorn.set_pixel(idx,octetidx,255,0,0)
print "display: ", address
unicorn.show()
time.sleep(timedel)
except:
print "skip ipv6"
pass
unicorn.clear()
unicorn.show()
return()
# some colour defs # some colour defs
red=(255,0,0) red=(255,0,0)
orange=(255,128,0) orange=(255,128,0)
green=(0,255,0) green=(0,255,0)
# login details for your APIC
apic = '1.1.1.1'
username = 'admin'
password = 'cisco123'
# #
# get data from APIC function # get data from APIC function
# #
def getdata(): def getdata():
url='https://%s/api/node/mo/topology/health.json' % apic url='https://%s/api/node/mo/topology/health.json' % apic
try: try:
print "Getting data from APIC...",apic print "Getting data from APIC...",apic
response = requests.get(url,cookies=cook,timeout=2,verify=False) response = requests.get(url,cookies=cook,timeout=2,verify=False)
json_data = response.json() json_data = response.json()
health = json_data["imdata"][0]["fabricHealthTotal"]["attributes"]["cur"] health = json_data["imdata"][0]["fabricHealthTotal"]["attributes"]["cur"]
print "currnet health score is",health print "currnet health score is",health
show_unicorn(int(health)) show_unicorn(int(health))
print "\n---------------\n" print "\n---------------\n"
except: except:
unicorn.clear() unicorn.clear()
unicorn.set_pixel(0,0,255,0,0) unicorn.set_pixel(0,0,255,0,0)
unicorn.show() unicorn.show()
print "Missing health data, or timeout" print "Missing health data, or timeout"
pass pass
return() return()
# #
# refresh session cookie # refresh session cookie
# #
def refreshSession(oldcook): def refreshSession(oldcook):
url = 'https://%s/api/aaaRefresh.json' % apic url = 'https://%s/api/aaaRefresh.json' % apic
print "\n---------------\n" print "\n---------------\n"
r = None r = None
while r is None: while r is None:
try: try:
r = requests.get(url, cookies=oldcook,timeout=10,verify=False) r = requests.get(url, cookies=oldcook,timeout=10,verify=False)
newcook = r.cookies newcook = r.cookies
print "got a new cookie" print "got a new cookie"
print "\n---------------\n" print "\n---------------\n"
except: except:
unicorn.clear() unicorn.clear()
unicorn.set_pixel(0,0,0,0,255) unicorn.set_pixel(0,0,0,0,255)
unicorn.show() unicorn.show()
print "timeout refreshing cookie" print "timeout refreshing cookie"
print "\n---------------\n" print "\n---------------\n"
newcook = loginAPIC() newcook = loginAPIC()
pass pass
return(newcook) return(newcook)
# #
# display the health score # display the health score
# #
def show_unicorn(health): def show_unicorn(health):
if health == 100: if health == 100:
colour=green colour=green
level=8 level=8
elif 90 <= health <= 99: elif 90 <= health <= 99:
colour=green colour=green
level=7 level=7
elif 60 <= health <= 89: elif 60 <= health <= 89:
colour=orange colour=orange
level=6 level=6
elif 50 <= health <= 59: elif 50 <= health <= 59:
colour=orange colour=orange
level=5 level=5
elif 30 <= health <= 49: elif 30 <= health <= 49:
colour=orange colour=orange
level=4 level=4
elif 20 <= health <= 29: elif 20 <= health <= 29:
colour=red colour=red
level=2 level=2
else: else:
colour=red colour=red
level=1 level=1
print "level",level print "level",level
print "colour",colour print "colour",colour
for y in range(level): for y in range(level):
for x in range(width): for x in range(width):
@ -125,44 +162,58 @@ def show_unicorn(health):
b=colour[2] b=colour[2]
unicorn.set_pixel(y,x,r,g,b) unicorn.set_pixel(y,x,r,g,b)
unicorn.show() unicorn.show()
time.sleep(0.05) time.sleep(0.05)
return() return()
def loginAPIC(): def loginAPIC():
headers={'content-type':'application/json'} headers={'content-type':'application/json'}
auth = {'aaaUser': {'attributes': {'name':username,'pwd':password}}} auth = {'aaaUser': {'attributes': {'name':username,'pwd':password}}}
url = 'https://%s/api/aaaLogin.json' % apic url = 'https://%s/api/aaaLogin.json' % apic
r = None r = None
while r is None: while r is None:
try: try:
r = requests.post( url, data=json.dumps(auth), timeout=5, verify=False ) r = requests.post( url, data=json.dumps(auth), timeout=5, verify=False )
print "status ",r.status_code print "logging in, status ",r.status_code
cook = r.cookies cook = r.cookies
print "cook is:",cook except:
except: unicorn.clear()
unicorn.clear() unicorn.set_pixel(0,0,255,255,0)
unicorn.set_pixel(0,0,255,255,0) unicorn.show()
unicorn.show() print "timeout logging in, but we will keep trying"
print "timeout logging in, but we will keep trying" show_ip(5)
pass pass
return(cook) return(cook)
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
# Main program loop starts here # Main program loop starts here
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
# Perform initial login, later we will need to refresh the cookie frequently print "Stared monitorig script..."
show_icon(1)
print "waiting for network"
while len(filter(None, commands.getoutput("hostname -I").split(" "))) < 1:
print "Waiting for at least 1 ip"
print "got an ip address"
# show ip addresses for 10s (1s delay between if more IPs found)
show_ip(10)
# Perform initial login, later we will need to refresh the cookie frequently
cook = loginAPIC() cook = loginAPIC()
while True: while True:
for num in range(0,8): for num in range(0,8):
unicorn.clear() unicorn.clear()
unicorn.set_pixel(0,0,255,128,0) unicorn.set_pixel(0,0,255,128,0)
unicorn.show() unicorn.show()
getdata() getdata()
time.sleep(5) time.sleep(5)
print "Refreshing cookie" print "Refreshing cookie"
cook = refreshSession(cook) cook = refreshSession(cook)