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
|
#!/usr/bin/env python
""" Utility to scan a fedmsg setup for port availability.
Reports what percentage of fedmsg endpoints are bound and ready.
"""
import base64
import collections
import multiprocessing.pool
import socket
import sys
import time
import fedmsg.config
config = fedmsg.config.load_config()
timeout = 0.2
expected = '/wAAAAAAAAABfw=='
for_collectd = 'verbose' not in sys.argv
active = collections.defaultdict(list)
inactive = collections.defaultdict(list)
def info(content="\n"):
if not for_collectd:
sys.stdout.write(content)
sys.stdout.flush()
def scan_one(item):
name, endpoint = item
if not endpoint.startswith('tcp://'):
raise ValueError("Don't know how to deal with %r" % endpoint)
endpoint = endpoint[len('tcp://'):].split(':')
connection = None
try:
connection = socket.create_connection(endpoint, timeout)
actual = base64.b64encode(connection.recv(10))
if actual != expected:
inactive[name].append((
endpoint, "%r is not %r" % (actual, expected)))
info("F")
else:
active[name].append((endpoint, "all active"))
info(".")
except socket.error as e:
inactive[name].append((endpoint, str(e)))
info("F")
if connection:
connection.close()
def scan_all():
global active
global inactive
del active
del inactive
active = collections.defaultdict(list)
inactive = collections.defaultdict(list)
items = [(name, addr)
for name, endpoints in config['endpoints'].items()
for addr in endpoints]
# There is likely overhead in creating and destroying this thing, but we have
# memory leaks to track down.
pool = multiprocessing.pool.ThreadPool(25)
pool.map(scan_one, items)
pool.close()
info()
if 'verbose' in sys.argv:
import pprint;
pprint.pprint(dict(active))
pprint.pprint(dict(inactive))
header = "".join([
"name".center(29),
"active".rjust(8),
"inactive".rjust(9),
"percent".rjust(9),
"reason".center(32),
])
info()
info(header + "\n")
info("-" * len(header) + "\n")
active_n_total, inactive_n_total = 0, 0
for name in sorted(config['endpoints']):
active_n = len(active[name])
inactive_n = len(inactive[name])
active_n_total += active_n
inactive_n_total += inactive_n
total = active_n + inactive_n
percent = ""
if total:
percent = "%%%0.1f" % (100 * float(active_n) / total)
reasons = set([reason for _, reason in inactive[name]])
info(name.rjust(29))
info(str(active_n).rjust(8))
info(str(inactive_n).rjust(9))
info(percent.rjust(9))
info(", ".join(reasons).rjust(32) + "\n")
info("-" * len(header) + "\n")
info(" total active: %i\n" % active_n_total)
info("total inactive: %i\n" % inactive_n_total)
value = 100 * float(active_n_total) / (active_n_total + inactive_n_total)
info("percent active: %%%0.1f\n" % value)
return value
if not for_collectd:
scan_all()
else:
interval = 5
host = socket.getfqdn()
while True:
start = time.time()
value = scan_all()
stop = timestamp = time.time()
delta = stop - start
output = (
"PUTVAL "
"{host}/fedmsg/percent "
"interval={interval} "
"{timestamp}:{value}"
).format(
host=host,
interval=interval,
timestamp=int(timestamp),
value="%0.1f" % value)
print(output)
if interval - delta > 0:
time.sleep(interval - delta)
|