-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconsolestatus.py
398 lines (341 loc) · 14.8 KB
/
consolestatus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
from collections import OrderedDict
import time
from datetime import datetime
import functools
import configobj
from utils import displayupdate, hw
import issuecommands
from issuecommands import Nodes
from keys.keyutils import internalprocs
import screens.supportscreens as supportscreens
import json
import copy
from keyspecs import toucharea
from screens import __screens as screens, screen, screenutil
from utils.utilfuncs import wc, interval_str
from screens.maintscreenbase import MaintScreenDesc
import logsupport
import config
import stats
import collections.abc
def update(d, u):
for k, v in u.items():
if isinstance(v, collections.abc.Mapping):
d[k] = update(d.get(k, {}), v)
else:
d[k] = v
return d
EmptyNodeRecord = {'hw': 'unknown*', 'osversion': 'unknown*', 'boottime': 'unknown*', "versioncommit": 'unknown*',
'versiondnld': 'unknown*', 'versionsha': 'unknown*', 'versionname': 'unknown*',
'registered': 0, "FirstUnseenErrorTime": 0, 'rpttime': 0, 'error': -2, "uptime": 0,
'status': 'unknown', 'stats': {'System': {}}}
heldstatus = ''
# Performance info
StatusDisp = None
Status = None
ShowHW = None
ShowVers = None
RespBuffer = []
RespNode = ''
RespRcvd = False
MsgSeq = 0
def SetUpConsoleStatus():
global StatusDisp, ShowHW, ShowVers, Status
if config.mqttavailable:
StatusDisp = StatusDisplayScreen()
ShowHW = ShowVersScreen(True)
ShowVers = ShowVersScreen(False)
Status = MaintScreenDesc('Network Console Control',
OrderedDict([('curstat', ('Networked Console Status',
functools.partial(screen.PushToScreen, StatusDisp,
'Maint'))),
('hw', ('Console Hardware/OS',
functools.partial(screen.PushToScreen, ShowHW, 'Maint'))),
('versions', ('Console Versions',
functools.partial(screen.PushToScreen, ShowVers, 'Maint'))),
('cmds', ('Issue Network Commands', GenGoNodeCmdScreen))]))
StatusDisp.userstore.ReParent(Status)
ShowVers.userstore.ReParent(Status)
ShowHW.userstore.ReParent(Status)
return Status
else:
return None
def GenGoNodeCmdScreen():
IssueCmds = CommandScreen()
IssueCmds.userstore.ReParent(Status)
screen.PushToScreen(IssueCmds, 'Maint')
IssueCmds.DeleteScreen()
def UpdateNodeStatus(nd, stat):
try:
if nd not in Nodes:
Nodes[nd] = copy.deepcopy(EmptyNodeRecord)
# handle old style records
if 'registered' not in stat and 'stats' not in stat:
tempSys = {'stats': {'System': {}}}
for nodestat in (
'queuetimemax24', 'queuetimemax24time', 'queuedepthmax24', 'maincyclecnt', 'queuedepthmax24time',
'queuetimemaxtime', 'queuedepthmax', 'queuetimemax', 'queuedepthmaxtime'):
if nodestat in stat:
tempSys['stats']['System'][nodestat] = stat[nodestat]
del stat[nodestat]
update(Nodes[nd], tempSys)
update(Nodes[nd], stat)
t = False
for nd, ndinfo in Nodes.items():
if ndinfo['status'] not in ('dead', 'unknown') and nd != hw.hostname and ndinfo['error'] != -1:
t = True
break
config.sysStore.NetErrorIndicator = t
except Exception as E:
logsupport.Logs.Log('UpdtStat {}'.format(repr(E)))
class ShowVersScreen(screen.BaseKeyScreenDesc):
def __init__(self, showhw):
self.showhw = showhw
nm = 'HW Status' if showhw else 'SW Versions'
super().__init__(None, nm)
self.NavKeysShowing = False
self.DefaultNavKeysShowing = False
self.Keys = {'return': toucharea.TouchPoint('back', (hw.screenwidth // 2, hw.screenheight // 2),
(hw.screenwidth, hw.screenheight), proc=screen.PopScreen)}
def ScreenContentRepaint(self):
hw.screen.fill(wc(self.BackgroundColor))
fontsz = 10 if displayupdate.portrait else 17
header, ht, wd = screenutil.CreateTextBlock(' Node ', fontsz, 'white', False, FitLine=False)
linestart = 40
hw.screen.blit(header, (10, 20))
for nd, ndinfo in Nodes.items():
offline = ' (offline)' if ndinfo['status'] in ('dead', 'unknown') else ' '
ndln = "{:12.12s} ".format(nd)
if self.showhw:
ln1 = "{} {}".format(ndinfo['hw'].replace('\00', ''), offline)
ln2 = '{}'.format(ndinfo['osversion'].replace('\00', ''))
else:
ln1 = "({}) of {} {}".format(nd, ndinfo['versionname'].replace('\00', ''), ndinfo['versioncommit'],
offline)
ln2 = "Downloaded: {}".format(ndinfo['versiondnld'])
if displayupdate.portrait:
ln, ht, _ = screenutil.CreateTextBlock([ndln, ' ' + ln1, ' ' + ln2], fontsz, 'white', False)
pass
else:
ln, ht, _ = screenutil.CreateTextBlock([ndln + ln1, ' ' + ln2], fontsz, 'white', False)
hw.screen.blit(ln, (10, linestart))
linestart += ht + fontsz // 2
class StatusDisplayScreen(screen.BaseKeyScreenDesc):
def __init__(self):
super().__init__(None, 'ConsolesStatus')
self.NavKeysShowing = False
self.DefaultNavKeysShowing = False
self.Keys = {'return': toucharea.TouchPoint('back', (hw.screenwidth // 2, hw.screenheight // 2),
(hw.screenwidth, hw.screenheight), proc=screen.PopScreen)}
def ScreenContentRepaint(self): # todo portrait, no MQTT case
hw.screen.fill(wc(self.BackgroundColor))
fontsz = 10 if displayupdate.portrait else 17
tm, ht, wd = screenutil.CreateTextBlock('{}'.format(time.strftime('%c')), fontsz, 'white', False,
FitLine=False)
hw.screen.blit(tm, (10, 20))
if displayupdate.portrait:
header, ht, wd = screenutil.CreateTextBlock(
[' Node Status QMax E', '--> Uptime/Last Boot'], fontsz, 'white', False)
else:
header, ht, wd = screenutil.CreateTextBlock(
' Node Status QMax E Uptime Last Boot', fontsz, 'white', False)
linestart = 60 + int(ht * 1.2)
hw.screen.blit(header, (10, 60))
for nd, ndinfo in Nodes.items():
try:
if ndinfo['status'] in ('dead', 'unknown'):
estat = ''
cstat = "{:14.14s}".format(' ')
stat = ndinfo['status']
qmax = ' '
else:
estat = ' ' if ndinfo['error'] == -1 else '?' if ndinfo['error'] == -1 else '*'
cstat = " {:>15.15s}".format(interval_str(ndinfo['uptime'], shrt=True))
statinfo = Nodes[nd]['stats']['System']
if 'maincyclecnt' not in statinfo or statinfo['maincyclecnt'] == 'unknown*':
stat = ndinfo['status']
qmax = ' '
else:
stat = '{} cyc'.format(statinfo['maincyclecnt']) if ndinfo['status'] in (
'idle', 'active') else ndinfo['status']
qmax = '{:4.2f} '.format(statinfo['queuetimemax24'])
active = '*' if ndinfo['status'] == 'active' else ' '
if ndinfo['boottime'] == 0 or ndinfo['boottime'] == 'unknown*':
bt = "{:^19.19}".format('unknown')
else:
bt = "{:%Y-%m-%d %H:%M:%S}".format(datetime.fromtimestamp(ndinfo['boottime']))
# age = time.time() - ndinfo['rpttime'] if ndinfo['rpttime'] != 0 else 0
if displayupdate.portrait:
ln, ht, wd = screenutil.CreateTextBlock(
['{:12.12s}{}{:10.10s} {}{}'.format(nd, active, stat, qmax, estat),
" {}/{}".format(cstat, bt)],
fontsz, 'white', False)
else:
ln, ht, wd = screenutil.CreateTextBlock(
'{:12.12s}{}{:10.10s} {}{} {} {}'.format(nd, active, stat, qmax, estat, cstat, bt), fontsz,
'white', False)
hw.screen.blit(ln, (20, linestart))
linestart += int(ht * 1.2)
except Exception as E:
logsupport.Logs.Log('Error displaying node status for {} Exc: {} Data: {}'.format(nd, E, ndinfo),
severity=logsupport.ConsoleWarning)
displayupdate.updatedisplay()
class CommandScreen(screen.BaseKeyScreenDesc):
def __init__(self):
super().__init__(None, 'NodeCommandScreen', SingleUse=True)
self.RespProcs = {'getlog': LogDisplay, 'geterrors': LogDisplay}
screen.AddUndefaultedParams(self, None, TitleFontSize=40, SubFontSize=25)
self.NavKeysShowing = True
self.DefaultNavKeysShowing = True
self.SetScreenTitle('Remote Consoles', self.TitleFontSize, 'white')
self.FocusNode = ''
self.NumNodes = 0
butht = 60
butwidth = int(self.useablehorizspace / 2 * 0.9)
butcenterleft = self.starthorizspace + int(self.useablehorizspace / 4)
butcenterright = butcenterleft + int(self.useablehorizspace / 2)
vt = self.startvertspace + butht // 2
self.Keys = OrderedDict([('All', toucharea.ManualKeyDesc(self, 'All', label=('All',), bcolor='red',
charcoloron='white', charcoloroff='white',
center=(butcenterleft, vt), size=(butwidth, butht),
proc=functools.partial(self.ShowCmds, 'Regular', '*'),
procdbl=functools.partial(self.ShowCmds, 'Advanced',
'*')))])
odd = False
for nd, ndinfo in Nodes.items():
offline = ndinfo['status'] in ('dead', 'unknown')
if not offline:
self.NumNodes += 1
if nd == hw.hostname:
continue
bcolor = 'grey' if offline else 'darkblue'
usecenter = butcenterleft if odd else butcenterright
self.Keys[nd] = toucharea.ManualKeyDesc(self, nd, label=(nd,), bcolor=bcolor, charcoloron='white',
charcoloroff='white', center=(usecenter, vt),
size=(butwidth, butht),
proc=None if offline else functools.partial(self.ShowCmds,
'Regular', nd),
procdbl=functools.partial(self.ShowCmds, 'Dead', nd) if offline
else functools.partial(self.ShowCmds, 'Advanced', nd))
if not odd:
vt += butht + 3
odd = not odd
CmdProps = {'KeyCharColorOn': 'white', 'KeyColor': 'maroon', 'BackgroundColor': 'royalblue',
'label': ['Maintenance'],
'DimTO': 60, 'PersistTO': 5, 'ScreenTitle': 'Placeholder'}
CmdSet = {'Regular': configobj.ConfigObj(CmdProps), 'Advanced': configobj.ConfigObj(CmdProps),
'Dead': configobj.ConfigObj(CmdProps)}
for cmd, action in issuecommands.cmdcalls.items():
DN = action.DisplayName.split(' ')
if issuecommands.Where.RemoteMenu in action.where or issuecommands.Where.RemoteMenuAdv in action.where:
whichscreen = 'Regular' if issuecommands.Where.RemoteMenu in action.where else "Advanced"
internalprocs['Command' + cmd] = functools.partial(self.IssueSimpleCmd, cmd,
paramsetter=action.cmdparam)
if action.simple:
CmdSet[whichscreen][cmd] = {"type": "REMOTEPROC", "ProcName": 'Command' + cmd, "label": DN,
"Verify": action.Verify}
else:
CmdSet[whichscreen][cmd] = {"type": "REMOTECPLXPROC", "ProcName": 'Command' + cmd, "label": DN,
"Verify": action.Verify, "EventProcName": 'Commandresp' + cmd}
internalprocs['Commandresp' + cmd] = self.RespProcs[cmd]
elif issuecommands.Where.RemoteMenuDead in action.where:
internalprocs['Command' + cmd] = functools.partial(self.IssueDeadCmd, cmd)
CmdSet['Dead'][cmd] = {"type": "REMOTEPROC", "ProcName": 'Command' + cmd, "label": DN,
"Verify": action.Verify}
self.entered = ''
self.CmdListScreens = {}
for t, s in CmdSet.items():
self.CmdListScreens[t] = screens.screentypes["Keypad"](s, 'CmdListScreen' + t, parentscreen=self)
def IssueSimpleCmd(self, cmd, Key=None, paramsetter=None):
global MsgSeq
MsgSeq += 1
Key.Seq = MsgSeq
if paramsetter is None:
cmdsend = '{}|{}|{}'.format(cmd, hw.hostname, MsgSeq)
else:
cmdsend = '{}|{}|{}|{}'.format(cmd, hw.hostname, MsgSeq, paramsetter())
if self.FocusNode == '*' or cmd == 'clearerrindicatormatch':
# special case match error clear case - goes to all even tho on specific screen
Key.ExpectedNumResponses = self.NumNodes
config.MQTTBroker.Publish('cmd', cmdsend, 'all')
else:
Key.ExpectedNumResponses = 1
config.MQTTBroker.Publish('cmd', cmdsend, self.FocusNode)
self.CmdListScreens[self.entered].AddToHubInterestList(config.MQTTBroker, cmd, Key)
# noinspection PyUnusedLocal
def IssueDeadCmd(self, cmd, Key=None):
# local processing only for a command regarding a dead node
if cmd == 'deletehistory':
# DeleteNode(self.FocusNode) # this is actually redundant and should be deleted todo
config.MQTTBroker.Publish('status', node=self.FocusNode, retain=True)
config.MQTTBroker.Publish(self.FocusNode, node='all/nodes', retain=True)
logsupport.Logs.Log("Purging network history of node {}".format(self.FocusNode))
target = self.FocusNode
self.FocusNode = '*'
self.IssueSimpleCmd('deletehistory', Key=Key, paramsetter=lambda: target)
else:
logsupport.Logs.Log('Internal error on dead node command: {} for {}'.format(cmd, self.FocusNode))
def ShowCmds(self, cmdset, nd):
self.entered = cmdset
self.FocusNode = nd
self.CmdListScreens[cmdset].SetScreenTitle('{} Commands for {}'.format(cmdset, self.FocusNode),
self.TitleFontSize, 'white', force=True)
for key in self.CmdListScreens[cmdset].Keys.values():
key.State = True
key.UnknownState = False if nd != '*' else issuecommands.cmdcalls[key.name].notgroup
screen.PushToScreen(self.CmdListScreens[cmdset], newstate='Maint')
def ExitScreen(self, viaPush):
super().ExitScreen(viaPush)
if not viaPush:
for n, s in self.CmdListScreens.items():
s.DeleteScreen()
def PopOver(self):
super().PopOver()
for n, s in self.CmdListScreens.items():
s.DeleteScreen()
'''
def ScreenContentRepaint(self): # todo ???
landfont = 15
if displayupdate.portrait:
pass
else:
header, ht, wd = screenutil.CreateTextBlock(
' Node ', landfont, 'white', False)
'''
def PickStartingSpot():
return 0
def PageTitle(pageno, itemnumber, node='', loginfo=None):
if len(loginfo) > itemnumber:
return "{} Log from {} Page: {} {}".format(node, loginfo[itemnumber][2], pageno,
time.strftime('%c')), True
else:
return "{} No more entries Page: {} {}".format(node, pageno, time.strftime('%c')), False
def LogDisplay(evnt):
p = supportscreens.PagedDisplay('remotelog', PickStartingSpot,
functools.partial(logsupport.LineRenderer, uselog=evnt.value),
functools.partial(PageTitle, node=evnt.respfrom, loginfo=evnt.value),
config.sysStore.LogFontSize, 'white')
if evnt.value:
issuecommands.lastseenlogmessage = (evnt.value[0][0], evnt.value[0][1])
p.singleuse = True
screen.PushToScreen(p)
def ReportStatus(status, retain=True, hold=0):
# held: 0 normal status report, 1 set an override status to be held, 2 clear and override status
global heldstatus
if hold == 1:
heldstatus = status
elif hold == 2:
heldstatus = ''
if logsupport.primaryBroker is not None:
stattoreport = {'stats': stats.GetReportables()[1]}
# rereport this because on powerup first NTP update can be after console starts
stattoreport.update({'status': status if heldstatus == '' else heldstatus,
"uptime": time.time() - config.sysStore.ConsoleStartTime,
"error": config.sysStore.ErrorNotice, 'rpttime': time.time(),
"FirstUnseenErrorTime": config.sysStore.FirstUnseenErrorTime,
'boottime': hw.boottime})
stat = json.dumps(stattoreport)
logsupport.primaryBroker.Publish(node=hw.hostname, topic='status', payload=stat, retain=retain, qos=1,
viasvr=True)
issuecommands.ReportStatus = ReportStatus
logsupport.ReportStatus = ReportStatus