from time import gmtime, strftime
import random
import redis
import json
import base64
colors = ['blue', 'green', 'brown', 'red', 'purple', 'orange', 'magenta', 'darkcyan', 'gray', 'blue', 'red', 'purple',
'blue', 'green', 'brown', 'red', 'purple', 'orange', 'magenta', 'darkcyan', 'gray', 'blue', 'red', 'purple']
corner = [820, 840, 860, 880, 900, 920, 940, 960, 980, 1000, 1020, 1040, 1060, 1080, 1100,
820, 840, 860, 880, 900, 920, 940, 960, 980, 1000, 1020, 1040, 1060, 1080, 1100]
#
############ DRAW boxes
#
def headerText(x, y, name):
## draw header text
data = '' % (x, y+30)
data = data + name +' Replay' + ''
data = data + '' % (x, y+60)
data = data + 'Producer, Switch and Consumer Configurations' + ''
data = data + '' % (x, y+90)
data = data + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + ' UTC '
return data
def headerLegends(x, y):
## Draw a legend - Replay
data = data + '''
''' % (x, y+120)
data = data + '' % (x+38, y+132)
data = data + '= Replay Server '
# legend = consumer open
data = data + '''
''' % (x, y+145)
data = data + '' % (x+38, y+157)
data = data + '= Consumer (PX - Open ) '
# legend - consumer reserved
data = data + '''
''' % (x, y+170)
data = data + '' % (x+38, y+182)
data = data + '= Consumer (PX -Reserved)'
# disabled
data = data + '''
''' % (x, y+195)
data = data + '' % (x+38, y+207)
data = data + '= Disabled'
return data #'''
######################################################################
def getRedisCount():
r = redis.Redis(host='127.0.0.1', port='6379')
return r.get("totalBoxCount")
# get server box data points from redis cache
def getRedis(k):
r = redis.Redis(host='127.0.0.1', port='6379')
rkey = "server-%d" % k
if not r.hgetall(rkey):
return None
serverData = ""
try:
serverData = {
'name' : r.hget(rkey,'name'),
'type' : r.hget(rkey,'type'),
'reserved' : r.hget(rkey,'reserved'),
'disabled' : r.hget(rkey,'disabled'),
'active' : r.hget(rkey,'active'),
'flicid' : r.hget(rkey,'flicid'),
'location' : r.hget(rkey,'location'),
'ports' : json.loads(base64.b64decode(r.hget(rkey,'ports'))),
'portconfig' : json.loads(base64.b64decode(r.hget(rkey,'portconfig'))),
'switchports' : json.loads(base64.b64decode(r.hget(rkey,'switchports'))),
'pathstart' : json.loads(base64.b64decode(r.hget(rkey,'pathstart'))),
'pathend' : json.loads(base64.b64decode(r.hget(rkey,'pathend'))),
'rate' : json.loads(base64.b64decode(r.hget(rkey,'rate'))),
'pcaploop' : json.loads(base64.b64decode(r.hget(rkey,'pcaploop'))),
'linkstatus' : json.loads(base64.b64decode(r.hget(rkey,'linkstatus'))),
'playstatus' : json.loads(base64.b64decode(r.hget(rkey,'playstatus'))),
'pcaps' : json.loads(base64.b64decode(r.hget(rkey,'pcaps'))),
'stroke' : r.hget(rkey,'stroke'),
'fill' : r.hget(rkey,'fill'),
}
except:
pass
return serverData
######################################################################
def drawSwitchBox(stroke, fill, x, y, h):
data = '''
''' % (fill, stroke, h, x, y) #'''
return data
#
#######################################################################
# Producers
#
def drawProducer(input_json, x, y):
indata = json.loads(input_json)
servername = indata['name']
location = indata['location']
flicid = indata['flicid']
disabled = indata['disabled']
reserved = indata['reserved']
ports = indata['ports']
switchports = indata['switchports']
pathstart = indata['pathstart']
pathend = indata['pathend']
linkstatus = indata['linkstatus']
playstatus = indata['playstatus']
pcaps = indata['pcaps']
stroke = indata['stroke']
fill = '#ffdddd'
numports = len(ports)
portcolor = "#cccccc"
boff = [8, 8, 8, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
coff = [0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
yofs = boff[len(ports)]
# drop shadow and blur filter
data = '''
'''
### Draw paths
for i in range(0, numports):
j = unicode(i)
try:
psl = len(pathstart[j])
paths = pathstart[j]
pathe = pathend[j]
for p in range(0, psl):
c = colors[p]
ya = int(paths[p][1])+10
yb = int(pathe[p][1])+10
mx = int(paths[p][2])
data = data + drawpath(800, ya, mx, 1120, yb, c, servername+"*"+str(i)+"*"+str(p), servername, i, playstatus[i])
except:
pass
# actual box for server with rounded corners
data = data + '''
''' % ( fill, stroke, servername, numports*32+yofs, x, y) #'''
if numports < 3:
data = data + '' % (x+62, y+32, '#666666')
else:
data = data + '' % (x+12, y+30, '#666666')
data = data + servername + ''
if numports < 3:
data = data + '' % (x+63, y+46, 'black')
else:
data = data + '' % (x+13, y+44, 'black')
data = data + reserved + ''
# port boxes on server
for i in range(0, numports):
data = data + '''
''' % (portcolor, i, x+410, y+(i*32)+6) #'''
data = data + '' % (x+414, y+20+i*32)
data = data + '%s' % (ports[i])
# draw boxes on switch that these are connected to
for i in range(0, numports):
data = data + '''
''' % (portcolor, i, x+560, y+(i*32)+6, servername, i, x+560, y+(i*32)+6) #'''
# xx or na in config means port not used, gray them out
if switchports[i] != 'xx' and switchports[i] != 'na':
pfill = 'black'
else:
linkstatus[i] = False
pfill = portcolor
data = data + '' % (x+564, y+20+i*32, pfill)
data = data + '%s' % (switchports[i])
# path from server to packet broker is link status - loop@rate if ok, status and red if bad'''
sname = "'" + servername + "'"
if linkstatus[i]:
color = 'green'
else:
color = 'red'
data = data + ''' ''' % (x+410, y+(i*32)-120, i, sname, i, sname, servername, i )
xt = x+480
yt = y+(i*32)+16
tf = "#acc2e5"
# popup for the rate and loop values, hidden for now, unhides and populates with javascript and a callback
data = data + '''Port %s''' % (x+480, y+(i*32)-96, servername, i, ports[i])
data = data + ''' ''' % (x+450, y+(i*32)-66, servername, i)
data = data + ''' ''' % (x+450, y+(i*32)-46, servername, i)
data = data + '''
''' % (x+430, y+(i*32)+17, switchports[i], sname, i, sname, x+430, y+(i*32)+17, x+560, y+(i*32)+17, color)
data = data + '''''' % (xt, yt, xt+10, yt-40, xt+40, yt-40, tf, servername, i)
# pcap file names
for i in range(0,len(pcaps)):
data = data + '' % (x+360, y+26+(i*30), '#444444')
data = data + pcaps[i] + ''
# play/stop boxes
for i in range(0,len(pcaps)):
oy = i*30
sid = servername+"*ps*"+str(i)
data = data + '''
''' % ( "play", '#ddd', sid, x+370, oy+y+12, sid)
# start or stop graphic?
if playstatus[i] == 'stop':
data = data + '''
''' % (i, "play", x+374, oy+y+16, x+386, oy+y+16, x+386, oy+y+28, x+374, oy+y+28, x+374, oy+y+16, '#ee8888', sid)
else:
data = data + '''
''' % (i, "play", x+375, oy+y+14, x+387, oy+y+22, x+375, oy+y+30, '#99dd99', sid)
# Setup button
if numports < 3:
yf = 62
else:
yf = 52
data = data + '''
''' % (x+14, y+numports*32+yofs-yf, servername, location)
if numports < 3:
data = data + '' % (x+20, y+numports*32+yofs-48)
else:
data = data + '' % (x+20, y+numports*32+yofs-38)
data = data + 'Setup'
# Info button
if numports < 3:
yf = 30
else:
yf = 26
data = data + '''
''' % (x+14, y+numports*32+yofs-yf, flicid) #'''
if numports < 3:
data = data + '' % (x+25, y+numports*32+yofs-18)
else:
data = data + '' % (x+25, y+numports*32+yofs-13)
data = data + 'Info'
return data
#
#####################################################################
# Draw Consumer '''
#
def drawConsumer(input_json, x, y):
indata = json.loads(input_json)
servername = indata['name']
flicid = indata['flicid']
disabled = indata['disabled']
ports = indata['ports']
reserved = indata['reserved']
switchports = indata['switchports']
linkstatus = indata['linkstatus']
stroke = indata['stroke']
if reserved:
fill = '#c8dff8' # blue
else:
fill = '#ddffdd' # green
numports = len(ports)
portcolor = "#cccccc"
boff = [8, 8, 8, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
coff = [0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
yofs = boff[len(ports)]
# drop shadow and blur filter
data = '''
'''
# actual box for server with rounded corners
data = data + '''
''' % ( fill, stroke, servername, numports*32+yofs, x, y) #'''
data = data + '' % (x+12, y+30, '#666666')
data = data + servername + ' ' + ''
if numports < 2:
data = data + '' % 'black'
data = data + reserved + ''
else:
data = data + '' % (x+13, y+46, 'black')
data = data + reserved + ''
# port boxes on server
for i in range(0, numports):
data = data + '''
''' % (portcolor, i, x-28, y+(i*32)+10) #'''
data = data + '' % (x-24, y+24+i*32)
data = data + '%s' % (ports[i])
# draw boxes on switch that these are connected to
for i in range(0, numports):
data = data + '''
''' % (portcolor, i, x-179, y+(i*32)+10, servername, switchports[i], x-179, y+(i*32)+10) #'''
# xx or na in config means port not used, gray them out
if switchports[i] != 'xx' and switchports[i] != 'na':
pfill = 'black'
else:
linkstatus[i] = False
pfill = portcolor
data = data + '' % (x-174, y+24+i*32)
data = data + '%s' % (switchports[i])
# path from server to packet broker is link status - loop@rate if ok, status and red if bad
if linkstatus[i]:
color = 'green'
op = "0.30"
else:
color = 'red'
op = "0.10"
data = data + '' % (x-158, y+(i*32)+20, x-28, y+(i*32)+20, color)
# configure button
data = data + '''
''' % (x+346, y+numports*32+yofs-26, flicid, servername) #'''
data = data + '' % (x+357, y+numports*32+yofs-13)
data = data + 'Info'
return data
# draw path with random elbow for internal connections - note class="anims" causes line to animate on graphic- this is not particularly impressive but I've left it in'''
def drawpath(x0, y0, mx, x1, y1, color, id, name, port, anim):
strdar = "0"
strdof = "0"
if anim == "play":
strdar = "7"
strdof = "70"
else:
strdar = "0"
strdof = "0"
data = '''
''' % (id, x0, y0, mx, y0, mx, y1, x1, y1, color, strdar, strdof, id) #'''
return data