forked from adamfast/DMRlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pickle_stat_reader.py
83 lines (70 loc) · 3.17 KB
/
pickle_stat_reader.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
from __future__ import print_function
from cPickle import load
from pprint import pprint
from time import ctime
from twisted.internet import reactor
from twisted.internet import task
from binascii import b2a_hex as h
# This is the only user-configuration necessary
# Tell the program where the pickle file is
# Tell the program how often to print a report
stat_file = '../dmrlink_stats.pickle'
frequency = 30
def int_id(_hex_string):
return int(h(_hex_string), 16)
def read_dict():
try:
with open(stat_file, 'rb') as file:
NETWORK = load(file)
return NETWORK
except IOError as detail:
print('I/O Error: {}'.format(detail))
except EOFError:
print('EOFError')
def print_stats():
NETWORK = read_dict()
if NETWORK != "None":
print('NETWORK STATISTICS REPORT:', ctime())
for ipsc in NETWORK:
stat = NETWORK[ipsc]['MASTER']['STATUS']
master = NETWORK[ipsc]['LOCAL']['MASTER_PEER']
print(ipsc)
if master:
print(' MASTER Information:')
print(' RADIO ID: {} (self)'.format(str(int_id(NETWORK[ipsc]['LOCAL']['RADIO_ID'])).rjust(8,'0')))
else:
print(' MASTER Information:')
print(' RADIO ID: {} CONNECTED: {}, KEEP ALIVES: SENT {} RECEIVED {} MISSED {} ({})'.format(\
str(int_id(NETWORK[ipsc]['MASTER']['RADIO_ID'])).rjust(8,'0'),\
stat['CONNECTED'],stat['KEEP_ALIVES_SENT'],\
stat['KEEP_ALIVES_RECEIVED'],\
stat['KEEP_ALIVES_MISSED'],\
NETWORK[ipsc]['MASTER']['IP']))
print(' PEER Information:')
if master:
for peer in NETWORK[ipsc]['PEERS']:
stat = NETWORK[ipsc]['PEERS'][peer]['STATUS']
print(' RADIO ID: {} CONNECTED: {}, KEEP ALIVES: RECEIVED {} ({})'.format(\
str(int_id(peer)).rjust(8,'0'),\
stat['CONNECTED'],\
stat['KEEP_ALIVES_RECEIVED'],\
NETWORK[ipsc]['PEERS'][peer]['IP']))
else:
for peer in NETWORK[ipsc]['PEERS']:
stat = NETWORK[ipsc]['PEERS'][peer]['STATUS']
if peer == NETWORK[ipsc]['LOCAL']['RADIO_ID']:
print(' RADIO ID: {} (self)'.format(str(int_id(peer)).rjust(8,'0')))
else:
print(' RADIO ID: {} CONNECTED: {}, KEEP ALIVES: SENT {} RECEIVED {} MISSED {} ({})'.format(\
str(int_id(peer)).rjust(8,'0'),\
stat['CONNECTED'],\
stat['KEEP_ALIVES_SENT'],\
stat['KEEP_ALIVES_RECEIVED'],\
stat['KEEP_ALIVES_MISSED'],\
NETWORK[ipsc]['PEERS'][peer]['IP']))
print()
print()
if __name__ == '__main__':
output_stats = task.LoopingCall(print_stats)
output_stats.start(frequency)
reactor.run()