-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublic-nodes.py
executable file
·201 lines (172 loc) · 7.38 KB
/
public-nodes.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
#!/usr/bin/env python3
import sys
import os
import subprocess
import argparse
import json
import urllib.request
version = "1.0.0"
nodes = [
"public.smesh.cloud:9092",
"pub-node1.smesh.cloud:9092",
"pub-node2.smesh.cloud:9092",
"pub-node3.smesh.cloud:9092",
"pub-node4.smesh.cloud:9092",
"pub-node5.smesh.cloud:9092",
"pub-node6.smesh.cloud:9092",
"pub-node7.smesh.cloud:9092",
"pub-node8.smesh.cloud:9092"
]
def display_version():
print("public-nodes.py version", version)
def parse_options():
parser = argparse.ArgumentParser()
# add argument to embed the remote server version in the status output
parser.add_argument("--node-version", help="Embed the remote server version in the status output", action="store_true")
parser.add_argument("-v", "--version", help="Display version information and exit", action="store_true")
args = parser.parse_args()
if args.version:
display_version()
sys.exit(0)
return args
def download_grpcurl():
# Download latest grpcurl from GitHub
url = "https://github.com/fullstorydev/grpcurl/releases/latest/download/grpcurl_$(uname -s)_$(uname -m).tar.gz"
try:
response = urllib.request.urlopen(url)
data = response.read()
with open("/tmp/grpcurl.tar.gz", "wb") as file:
file.write(data)
print("Downloaded grpcurl successfully.")
except:
print("Error: Failed to download grpcurl from GitHub.")
sys.exit(1)
# Extract grpcurl
try:
subprocess.run(["tar", "-xzf", "/tmp/grpcurl.tar.gz", "-C", "/tmp/"], check=True)
print("Extracted grpcurl successfully.")
except:
print("Error: Failed to extract grpcurl.")
sys.exit(1)
# Add grpcurl to PATH
os.environ["PATH"] += ":/tmp/grpcurl"
print("Added grpcurl to PATH.")
def column_data_from_node(node):
data = {
'name': node['name'],
'port': node['port'],
'version': node['version'] if 'version' in node else None,
'peers': None,
'topLayer': None,
'syncedLayer': None,
'verifiedLayer': None
}
if 'topLayer' in node and 'syncedLayer' in node and 'verifiedLayer' in node:
data['status'] = 'SYNCED & VERIFIED' if abs(int(node['topLayer']['number']) - int(node['syncedLayer']['number'])) < 3 and abs(int(node['verifiedLayer']['number']) - int(node['syncedLayer']['number'])) < 3 else 'SYNCED' if abs(int(node['topLayer']['number']) - int(node['syncedLayer']['number'])) < 3 else 'NOT SYNCED'
data['peers'] = node['connectedPeers']
data['topLayer'] = node['topLayer']['number'] if node and node['topLayer'] else None
data['syncedLayer'] = node['syncedLayer']['number'] if node and node['syncedLayer'] else None
data['verifiedLayer'] = node['verifiedLayer']['number'] if node and node['verifiedLayer'] else None
else:
data['status'] = 'OFFLINE'
return data
def print_node_status(node):
global args, not_synced_nodes, synced_nodes, verified_nodes, offline_nodes, columns
if node and 'topLayer' in node:
if abs(int(node['topLayer']['number']) - int(node['syncedLayer']['number'])) < 3 and abs(int(node['verifiedLayer']['number']) - int(node['syncedLayer']['number'])) < 3:
synced_nodes += 1
verified_nodes += 1
elif abs(int(node['topLayer']['number']) - int(node['syncedLayer']['number'])) < 3:
synced_nodes += 1
else:
not_synced_nodes += 1
else:
offline_nodes += 1
column_data = column_data_from_node(node)
node_row = ""
for column in columns:
if column['enabled']:
if column_data[column['key']] is not None:
node_row += f"{column_data[column['key']]:{column['width']}} "
else:
node_row += f"{'':{column['width']}} "
print(node_row)
def print_all_node_status(nodes):
global columns
print("PUBLIC NODES HEALTH CHECK")
print()
print("Host public.smesh.cloud is a load balancer that distributes traffic to the other available nodes.")
print("Offline nodes will not be included in rotation.")
print()
# cycle through columns and update the width of each column to match the width of the max value in that column
for column in columns:
if column['enabled']:
for node_id in nodes.keys():
node = nodes[node_id]
column_data = column_data_from_node(node)
if column['key'] in column_data:
column_width = len(str(column_data[column['key']]))
if column_width > column['width']:
column['width'] = column_width
title_row = ""
for column in columns:
if column['enabled']:
title_row += f"{column['name']:{column['width']}} "
separator_row = ""
for column in columns:
if column['enabled']:
separator_row += f"{'':-<{column['width']}} "
print(title_row)
print(separator_row)
for node in nodes.values():
print_node_status(node)
def print_all_nodes_summary():
global not_synced_nodes, synced_nodes, verified_nodes, total_nodes
print()
print(f"Offline: {offline_nodes}/{total_nodes}, Not synced: {not_synced_nodes}/{total_nodes}, Synced: {synced_nodes}/{total_nodes}, Verified: {verified_nodes}/{total_nodes}")
def main():
global args, not_synced_nodes, synced_nodes, verified_nodes, total_nodes, offline_nodes, columns
args = parse_options()
columns = [
{ "name": "Node", "key": "name", "width": 4, "align": "left", "align_char": " ", "enabled": True },
{ "name": "Port", "key": "port", "width": 4, "align": "right", "align_char": " ", "enabled": True },
{ "name": "Version", "key": "version", "width": 7, "align": "left", "align_char": " ", "enabled": args.node_version },
{ "name": "Status", "key": "status", "width": 6, "align": "left", "align_char": " ", "enabled": True },
{ "name": "Peers", "key": "peers", "width": 5, "align": "right", "align_char": " ", "enabled": True },
{ "name": "Top", "key": "topLayer", "width": 3, "align": "right", "align_char": " ", "enabled": True },
{ "name": "Synced", "key": "syncedLayer", "width": 6, "align": "right", "align_char": " ", "enabled": True },
{ "name": "Verified", "key": "verifiedLayer", "width": 8, "align": "right", "align_char": " ", "enabled": True }
]
# Check if grpcurl is installed
try:
result = subprocess.run(["grpcurl", "-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
except:
download_grpcurl()
node_status = {}
for node in nodes:
node_name, node_port = node.split(':')
node_status[node] = { 'name': node_name, 'port': node_port }
try:
result = subprocess.run(["grpcurl", "-plaintext", "-d", "", node, "spacemesh.v1.NodeService.Status"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
node_status[node] = json.loads(result.stdout.decode('utf-8'))['status']
node_status[node]['name'] = node_name
node_status[node]['port'] = node_port
except:
continue
if args.node_version:
try:
result = subprocess.run(["grpcurl", "-plaintext", "-d", "", node, "spacemesh.v1.NodeService.Version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
except:
print(f"Error: Failed to connect to {node}.")
sys.exit(1)
version = json.loads(result.stdout.decode('utf-8'))['versionString']['value']
node_status[node]['version'] = version
not_synced_nodes = 0
synced_nodes = 0
verified_nodes = 0
offline_nodes = 0
total_nodes = len(node_status.keys())
print_all_node_status(node_status)
print_all_nodes_summary()
if __name__ == "__main__":
main()