-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtibrvftmon.py
138 lines (101 loc) · 2.97 KB
/
tibrvftmon.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
##
# tibrvftmon.py - example TIB/Rendezvous fault tolerant group
# monitor program
#
# rewrite TIBRV example: tibrvftmon.c
#
# LAST MODIFIED: V1.0 2016-12-26 ARIEN [email protected]
#
import sys
import signal
import getopt
from pytibrv.Tibrv import *
from pytibrv.TibrvFt import *
# Module Variables
_running = True
_oldNumActives = 0
def signal_proc(signal, frame):
global _running
_running = False
print()
print('CRTL-C PRESSED')
def usage():
print()
print('tibrvftmon.py [options]')
print('')
print('options:')
print(' [--service service] RVD Service')
print(' [--network network] RVD Network')
print(' [--daemon daemon] RVD Daemon')
print()
sys.exit(1)
def get_params(argv):
params = ['service=', 'network=', 'daemon=']
try:
opts, args = getopt.getopt(argv, '', params)
except getopt.GetoptError:
usage()
service = None
network = None
daemon = None
for opt, arg in opts:
if opt == '--service':
service = arg
elif opt == '--network':
network = arg
elif opt == '--daemon':
daemon = arg
else:
usage()
if len(args) != 0:
usage()
return service, network, daemon
def monCB(monitor: TibrvFtMonitor, groupName: str, numActiveMembers: int, closure):
global _oldNumActives
if _oldNumActives > numActiveMembers:
txt = 'one deactivated'
else:
txt = 'one activated'
print('Group [{}]: has {} active members (after {})'.format(
groupName,
numActiveMembers,
txt
))
_oldNumActives = numActiveMembers
return
# MAIN PROGRAM
def main(argv):
service, network, daemon = get_params(argv[1:])
progname = argv[0]
lostInt = 4.8
err = Tibrv.open()
if err != TIBRV_OK:
print('{}: Failed to open TIB/RV: {}'.format('', progname, TibrvStatus.text(err)))
sys.exit(1)
tx = TibrvTx()
err = tx.create(service, network, daemon)
if err != TIBRV_OK:
print('{}: Failed to initialize transport: {}'.format(progname, TibrvStatus.text(err)))
sys.exit(1)
que = TibrvQueue()
monitor = TibrvFtMonitor()
err = monitor.create(que, TibrvFtMonitorCallback(monCB), tx, 'TIBRVFT_TIME_EXAMPLE', lostInt, None)
if err != TIBRV_OK:
print('{} : Failed to start group monitor - {}', progname, TibrvStatus.text(err))
sys.exit(1)\
print('{} : Waiting for group information...'.format(progname))
# Set Signal Handler for Ctrl-C
global _running
signal.signal(signal.SIGINT, signal_proc)
while _running:
que.timedDispatch(0.5)
# CTRL-C PRESSED
# when ftMonitor destroyed,
# callback would be triggered within numActiveMembers = 0
# it is fault alert, and should be ignored
monitor.destroy()
tx.destroy()
Tibrv.close()
return
if __name__ == "__main__":
main(sys.argv)