rewrite, introduce steady state

This commit is contained in:
Thomas Jensen 2019-07-19 23:55:17 +02:00
parent b78f287064
commit 4ff1d4a574

View File

@ -9,32 +9,47 @@ GPIO.setwarnings(False) # don't show warnings
al = Alertmanager("http://prometheus.lan.uctrl.net:9093") al = Alertmanager("http://prometheus.lan.uctrl.net:9093")
colors = { colors = {
'red': 1, 'red': {
'orange': 2, 'io': 1,
'green': 3, 'steady': True,
'blue': 4,
'clear': 5
}
types = {
'alert': {
'color': 'red',
'prev': 0 'prev': 0
}, },
'warn': { 'orange': {
'color': 'orange', 'io': 2,
'steady': True,
'prev': 0
},
'green': {
'io': 3,
'steady': False,
'prev': 0
},
'blue': {
'io': 4,
'steady': True,
'prev': 0
},
'clear': {
'io': 5,
'steady': False,
'prev': 0 'prev': 0
} }
} }
severities = {
'alert': 'red',
'warn': 'orange',
'info': 'white'
}
boot_seq = ['clear', 'blue', 'green', 'orange', 'red'] boot_seq = ['clear', 'blue', 'green', 'orange', 'red']
for color, io in colors.items(): for c, v in colors.items():
GPIO.setup(io, GPIO.OUT) GPIO.setup(v['io'], GPIO.OUT)
def setColor(color, state): def setColor(color, state):
io = colors[color] io = colors[color]['io']
if GPIO.input(io) is not int(state): if GPIO.input(io) is not int(state):
print('Setting color', color.upper(), str(state)) print('Setting color', color.upper(), str(state))
@ -55,38 +70,45 @@ while True:
start = time.time() start = time.time()
for alert in al.alerts(): for alert in al.alerts():
if alert["labels"]["alertname"] == "DeadMansSwitch": if 'color' in alert["labels"]:
c = alert["labels"]["color"]
elif 'severity' in alert["labels"]:
c = severities[alert["labels"]["severity"]]
else:
continue continue
for t in types: d[c].append(alert["status"]["state"])
if alert["labels"]["severity"] == t:
d[t].append(alert["status"]["state"])
for t in types: for c in colors:
print(t, json.dumps(Counter(d[t]), indent=4, sort_keys=True)) print(c, json.dumps(Counter(d[c]), indent=4, sort_keys=True))
for x in range(0, 10): for x in range(0, 10):
for t, v in types.items(): for c, v in colors.items():
if Counter(d[t])['active'] > 0: if (Counter(d[c])['active'] > 0 and v['steady']) \
setColor(v['color'], True) or (Counter(d[c])['active'] > v['prev'] and not v['steady']):
setColor(c, True)
time.sleep(.5) time.sleep(.5)
for t, v in types.items(): # Turn color off if active alerts have increased, causing color to flash
if v['prev'] < Counter(d[t])['active']: for c, v in colors.items():
setColor(v['color'], False) if Counter(d[c])['active'] > v['prev']:
setColor(c, False)
time.sleep(.5) time.sleep(.5)
setColor('green', False) for c, v in colors.items():
if c == 'green':
continue
for t, v in types.items(): if Counter(d[c])['active'] == 0:
if Counter(d[t])['active'] == 0: setColor(c, False)
setColor(v['color'], False)
if v['prev'] > Counter(d[t])['active']: # Use green to indicate decrease in active alerts
if v['prev'] > Counter(d[c])['active'] and v['steady']:
setColor('green', True) setColor('green', True)
v['prev'] = Counter(d[t])['active'] v['prev'] = Counter(d[c])['active']
print(time.time() - start) print(time.time() - start)
print('---') print('---')