-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_disconnected
executable file
·211 lines (199 loc) · 7.41 KB
/
check_disconnected
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
#!/usr/bin/env python3
import argparse
import csv
import itertools
import os
import subprocess
SPACER_STR = "--------------"
def main(opts):
daemon_dict = {}
with open(opts.systemid_file) as ifile:
reader = csv.reader(ifile)
for row in reader:
daemon_dict[int(row[1])] = row[0]
# cmd = ["grep", "\"Incoming federation heartbeat\""]
cmd = ["grep", "Incoming"]
if opts.file is None:
cmd.insert(0, "ospl-daemon")
cmd.insert(0, "exec")
cmd.insert(0, "docker")
cmd.append("durability.log")
else:
cmd.append(opts.file)
# print(cmd)
proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
connections = {}
for machine in daemon_dict.values():
connections[machine] = {"connects": [], "start_disconnects": [], "disconnects": []}
# print(proc.returncode)
# print("B:", proc.stdout.decode().split(os.linesep))
system_ids = []
for line in proc.stdout.decode().split(os.linesep):
parts = line.split()
if not len(parts):
continue
# print(parts)
system_id = int(parts[10].strip("'").strip(",").strip("'"))
state = int(parts[13].strip("'").strip(",").strip("'"))
timestamp = parts[0].strip("'").strip(",").strip("'")
if opts.verbose_connections:
print(system_id, state, timestamp)
if state == 1:
system_ids.append(system_id)
if system_id in daemon_dict:
if opts.verbose_connections:
print(f"{system_id} connected")
connections[daemon_dict[system_id]]["connects"].append(timestamp)
if state == 2560:
#system_ids.append(system_id)
if system_id in daemon_dict:
if opts.verbose_connections:
print(f"{system_id} starting disconnect")
connections[daemon_dict[system_id]]["start_disconnects"].append(timestamp)
connections[daemon_dict[system_id]]["disconnects"].append(SPACER_STR)
if state == 2564:
try:
system_ids.remove(system_id)
if system_id in daemon_dict:
if opts.verbose_connections:
print(f"{system_id} disconnected")
try:
if connections[daemon_dict[system_id]]["disconnects"][-1] == SPACER_STR:
connections[daemon_dict[system_id]]["disconnects"][-1] = timestamp
else:
connections[daemon_dict[system_id]]["disconnects"].append(timestamp)
except IndexError:
print(f"{system_id} {connections[daemon_dict[system_id]]['disconnects']}")
print(f"{line}")
except ValueError:
print(f"System Id: {system_id} was not present in original list.")
print(f"Total System Ids: {len(system_ids)}")
num_verified = 0
for key in daemon_dict:
machine = daemon_dict.get(key)
if machine.startswith("#"):
continue
if key not in system_ids:
print(f"{machine} is not in SystemId list.")
else:
num_verified += 1
if opts.show_known:
print(f"{machine} is in SystemId list.")
difference = len(system_ids) - num_verified
if difference:
print(f"Number of unknown SystemIds: {difference}")
if opts.find_unknowns:
unknown_ids = set(system_ids).difference(set(list(daemon_dict.keys())))
# print(f"Ids: {unknown_ids}")
hex_codes = [f"{x:x}" for x in unknown_ids]
# print(hex_codes)
if opts.file is None:
cmd = [
"docker",
"exec",
"ospl-daemon",
"grep",
"SPDP ST0",
"ddsi2.log",
]
else:
cmd = [
"grep",
"SPDP ST0",
opts.file,
]
output = subprocess.check_output(cmd)
output = output.decode().split(os.linesep)
for line in output:
values = line.split()
# print(values)
if not len(values):
continue
code = values[4].split(":")[0]
create_code = values[6]
# print(code)
if code in hex_codes and create_code == "3c3f":
v1 = values[11]
v2 = values[14]
machine_name, ospl_version, *_ = v1.split("/")
machine_ip = v2.split(":")[0]
index = hex_codes.index(code)
host_name = machine_name.split(".")[0]
machine_system_id = list(unknown_ids)[index]
print(
f"{host_name} daemon ID is {machine_system_id} ({code}) "
f"[{machine_ip}, {ospl_version}]"
)
# print(connections)
if opts.show_disconnects:
for machine, connect_info in connections.items():
if (
len(connect_info["connects"]) >= 1
and len(connect_info["disconnects"]) > 0
):
if not opts.show_old:
if machine.startswith("#"):
continue
print(f"{machine}")
for c, d in itertools.zip_longest(
connect_info["connects"], connect_info["disconnects"]
):
print(f"{c} {d}")
# if (
# len(connect_info["connects"]) >= 1
# and len(connect_info["start_disconnects"]) > 0
# ):
# if not opts.show_old:
# if machine.startswith("#"):
# continue
# print(f"{machine}")
# for c, d in itertools.zip_longest(
# connect_info["connects"], connect_info["start_disconnects"]
# ):
# print(f"{c} {d}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-f", "--file", dest="file", help="Use a file instead of a docker container"
)
parser.add_argument(
"-s",
"--systemid-file",
default="daemon_list.csv",
help="Use an alternate file for the SystemIds",
)
parser.add_argument(
"-d",
"--show-disconnects",
action="store_true",
help="Show the disconnect events.",
)
parser.add_argument(
"-u",
"--find-unknowns",
dest="find_unknowns",
action="store_true",
help="Find info about the unknown SystemIds.",
)
parser.add_argument(
"--show-old", action="store_true", help="Show Ids with leading #."
)
parser.add_argument(
"-c",
"--combined-logs",
action="store_true",
help="Look at a combined docker log.",
)
parser.add_argument(
"-k",
"--show-known",
action="store_true",
help="Show the list of known (watched) SystemIds.",
)
parser.add_argument(
"--verbose-connections",
action="store_true",
help="Print information related to system connections/disconnections.",
)
args = parser.parse_args()
main(args)