brainframe-sys-tools 0.4.post38__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- brainframe_sys_tools/__init__.py +5 -0
- brainframe_sys_tools/bf_info.py +155 -0
- brainframe_sys_tools/bf_service_monitor.py +343 -0
- brainframe_sys_tools/bf_ssh_tunnel.py +104 -0
- brainframe_sys_tools/brainframe_monitor/benchmarker.py +534 -0
- brainframe_sys_tools/brainframe_monitor/brainframe_monitor.py +105 -0
- brainframe_sys_tools/brainframe_monitor/curses_utils.py +119 -0
- brainframe_sys_tools/brainframe_monitor/fake_screen.py +30 -0
- brainframe_sys_tools/brainframe_monitor/report.py +33 -0
- brainframe_sys_tools/brainframe_monitor/requirements.txt +2 -0
- brainframe_sys_tools/check_memory.py +213 -0
- brainframe_sys_tools/cli.py +59 -0
- brainframe_sys_tools/command_utils.py +25 -0
- brainframe_sys_tools/print_utils.py +34 -0
- brainframe_sys_tools/translations/portal.en.yml +20 -0
- brainframe_sys_tools-0.4.post38.dist-info/COPYING +26 -0
- brainframe_sys_tools-0.4.post38.dist-info/METADATA +26 -0
- brainframe_sys_tools-0.4.post38.dist-info/RECORD +20 -0
- brainframe_sys_tools-0.4.post38.dist-info/WHEEL +4 -0
- brainframe_sys_tools-0.4.post38.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2021 Dilili Labs, Inc. All rights reserved. Contains Dilili Labs Proprietary Information. RESTRICTED COMPUTER SOFTWARE. LIMITED RIGHTS DATA.
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import subprocess
|
|
7
|
+
from argparse import ArgumentParser
|
|
8
|
+
|
|
9
|
+
import requests
|
|
10
|
+
|
|
11
|
+
from brainframe_sys_tools.command_utils import command, subcommand_parse_args, by_name
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
header = [
|
|
15
|
+
"Copyright (c) 2021 Dilili Labs, Inc. All rights reserved.\n\n"
|
|
16
|
+
"bf_info utility checks BrainFrame system information and all inference computing dependencies.\n"
|
|
17
|
+
"=======\n"
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
footer = ["\n\n"]
|
|
21
|
+
|
|
22
|
+
#
|
|
23
|
+
# Check if BrainFrame is still alive?
|
|
24
|
+
#
|
|
25
|
+
# curl http://localhost/api/version
|
|
26
|
+
#
|
|
27
|
+
# Check if BrainFrame has proper license installed?
|
|
28
|
+
#
|
|
29
|
+
# curl http://localhost/api/license
|
|
30
|
+
#
|
|
31
|
+
# Check all VisionCapsules configurations
|
|
32
|
+
#
|
|
33
|
+
# curl http://localhost/api/plugins
|
|
34
|
+
#
|
|
35
|
+
# Check all BrainFrame stream configurations
|
|
36
|
+
#
|
|
37
|
+
# curl http://localhost/api/streams
|
|
38
|
+
#
|
|
39
|
+
# Check if BrainFrame has connected to a stream at some point
|
|
40
|
+
#
|
|
41
|
+
# curl http://localhost/api/streams/status
|
|
42
|
+
#
|
|
43
|
+
# This will continuously pull uoutput from BrainFrame for all streams, including
|
|
44
|
+
# the zone status, timestamp. Let's don't do this in this tool
|
|
45
|
+
#
|
|
46
|
+
# curl http://localhost/api/streams/statuses
|
|
47
|
+
#
|
|
48
|
+
|
|
49
|
+
cmdlines = [
|
|
50
|
+
"date",
|
|
51
|
+
"date -u",
|
|
52
|
+
"uptime",
|
|
53
|
+
"uptime -s",
|
|
54
|
+
"cat /etc/os-release",
|
|
55
|
+
"uname -a",
|
|
56
|
+
"ldd --version",
|
|
57
|
+
"cat /proc/cpuinfo",
|
|
58
|
+
"cat /etc/docker/daemon.json",
|
|
59
|
+
"nvidia-smi",
|
|
60
|
+
"whereis nvidia",
|
|
61
|
+
"modinfo nvidia",
|
|
62
|
+
"dpkg -l | grep nvidia",
|
|
63
|
+
"apt list --upgradable",
|
|
64
|
+
"ls /dev/dri -l",
|
|
65
|
+
"df -h",
|
|
66
|
+
"which brainframe",
|
|
67
|
+
"which brainframe-client",
|
|
68
|
+
"brainframe info",
|
|
69
|
+
"cat $(brainframe info install_path)/.env",
|
|
70
|
+
"cat $(brainframe info install_path)/docker-compose.yml",
|
|
71
|
+
"cat $(brainframe info install_path)/docker-compose.override.yml",
|
|
72
|
+
"ls -la $(brainframe info install_path)",
|
|
73
|
+
"ls -la $(brainframe info data_path)/capsules",
|
|
74
|
+
"docker container ls",
|
|
75
|
+
"curl http://localhost/api/version",
|
|
76
|
+
"curl http://localhost/api/license",
|
|
77
|
+
"curl http://localhost/api/plugins | python3 -mjson.tool",
|
|
78
|
+
"curl http://localhost/api/streams | python3 -mjson.tool",
|
|
79
|
+
"curl http://localhost/api/streams/status | python3 -mjson.tool",
|
|
80
|
+
"hostname -I",
|
|
81
|
+
"cat /proc/uptime",
|
|
82
|
+
"nslookup aotu.ai",
|
|
83
|
+
"cat /proc/uptime",
|
|
84
|
+
"ping aotu.ai -c 3",
|
|
85
|
+
"cat /proc/uptime",
|
|
86
|
+
"date",
|
|
87
|
+
"date -u",
|
|
88
|
+
]
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def _parse_args():
|
|
92
|
+
parser = ArgumentParser(
|
|
93
|
+
"This tool will print out the system and machine information and save to a file"
|
|
94
|
+
)
|
|
95
|
+
parser.add_argument(
|
|
96
|
+
"-f", "--file", default="sys.info", help="The output system info file name"
|
|
97
|
+
)
|
|
98
|
+
args = subcommand_parse_args(parser)
|
|
99
|
+
|
|
100
|
+
return args
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def save_sys_info(log_filename):
|
|
104
|
+
file = open(log_filename, "w")
|
|
105
|
+
|
|
106
|
+
for line in header:
|
|
107
|
+
print(line)
|
|
108
|
+
file.writelines(line)
|
|
109
|
+
|
|
110
|
+
for line in cmdlines:
|
|
111
|
+
|
|
112
|
+
bf_info = "\n======== " + line + " ...\n"
|
|
113
|
+
|
|
114
|
+
print(bf_info)
|
|
115
|
+
file.writelines(str(bf_info))
|
|
116
|
+
|
|
117
|
+
if line.startswith("no curl"):
|
|
118
|
+
url = line.replace("no curl ", "")
|
|
119
|
+
session = requests.Session()
|
|
120
|
+
session.trust_env = False
|
|
121
|
+
|
|
122
|
+
response = session.get(url)
|
|
123
|
+
bf_info = json.dumps(response.json(), indent=2)
|
|
124
|
+
|
|
125
|
+
else:
|
|
126
|
+
sys_process = subprocess.Popen(
|
|
127
|
+
line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
|
|
128
|
+
)
|
|
129
|
+
bf_info = sys_process.stdout.read().decode("utf-8")
|
|
130
|
+
|
|
131
|
+
if len(bf_info) == 0:
|
|
132
|
+
bf_info = "None.\n"
|
|
133
|
+
|
|
134
|
+
print(bf_info)
|
|
135
|
+
# file.writelines(str(bf_info.encode('utf-8')))
|
|
136
|
+
file.writelines(str(bf_info))
|
|
137
|
+
|
|
138
|
+
for line in footer:
|
|
139
|
+
print(line)
|
|
140
|
+
file.writelines(line)
|
|
141
|
+
|
|
142
|
+
file.close()
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@command("sys_info")
|
|
146
|
+
def sys_info():
|
|
147
|
+
args = _parse_args()
|
|
148
|
+
|
|
149
|
+
save_sys_info(args.file)
|
|
150
|
+
|
|
151
|
+
print(f"Saved in {args.file}")
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
if __name__ == "__main__":
|
|
155
|
+
by_name["sys_info"]()
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2021 Dilili Labs, Inc. All rights reserved. Contains Dilili Labs Proprietary Information. RESTRICTED COMPUTER SOFTWARE. LIMITED RIGHTS DATA.
|
|
3
|
+
#
|
|
4
|
+
import os
|
|
5
|
+
import subprocess
|
|
6
|
+
from argparse import ArgumentParser, RawTextHelpFormatter
|
|
7
|
+
from datetime import datetime
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from subprocess import Popen
|
|
10
|
+
from time import sleep, time
|
|
11
|
+
from typing import Callable
|
|
12
|
+
|
|
13
|
+
from brainframe.api import BrainFrameAPI
|
|
14
|
+
|
|
15
|
+
from brainframe_sys_tools.bf_info import save_sys_info
|
|
16
|
+
from brainframe_sys_tools.command_utils import command, subcommand_parse_args, by_name
|
|
17
|
+
|
|
18
|
+
from apscheduler.schedulers.background import BackgroundScheduler
|
|
19
|
+
from apscheduler.triggers.cron import CronTrigger
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class HeartBeat:
|
|
23
|
+
def __init__(
|
|
24
|
+
self, log_path, interval, threshold, log_save=False, restart_cron=None
|
|
25
|
+
):
|
|
26
|
+
|
|
27
|
+
self.interval = int(interval)
|
|
28
|
+
self.threshold = int(threshold)
|
|
29
|
+
self.log_save = log_save
|
|
30
|
+
|
|
31
|
+
if os.path.isdir(log_path) is not True:
|
|
32
|
+
print(f"{log_path} is invalid!")
|
|
33
|
+
exit(1)
|
|
34
|
+
self.log_path = log_path
|
|
35
|
+
|
|
36
|
+
self.time_0 = datetime.now()
|
|
37
|
+
self.log_path_prefix = None
|
|
38
|
+
self.heartbeat_log_file = None
|
|
39
|
+
self.brainframe_log_filename = None
|
|
40
|
+
self.heartbeat_log_open(interval, threshold)
|
|
41
|
+
|
|
42
|
+
self.d_status = {}
|
|
43
|
+
# add BackgroundScheduler
|
|
44
|
+
self.bgscheduler = None
|
|
45
|
+
self.restart_timeout = None
|
|
46
|
+
# Schedule the job to run daily/weekly/month at midnight (00:00)
|
|
47
|
+
if restart_cron is not None:
|
|
48
|
+
# add BackgroundScheduler for timed task
|
|
49
|
+
self.bgscheduler = BackgroundScheduler()
|
|
50
|
+
# use CronTrigger parse Cron style string
|
|
51
|
+
self.cron_trigger = CronTrigger.from_crontab(restart_cron)
|
|
52
|
+
self.bgscheduler.add_job(self._cron_job, self.cron_trigger)
|
|
53
|
+
|
|
54
|
+
def heartbeat_log_open(self, interval, threshold):
|
|
55
|
+
self.time_0 = datetime.now()
|
|
56
|
+
self.log_path_prefix = str(self.log_path) + f"/{self.time_0.isoformat()}_"
|
|
57
|
+
self.brainframe_log_filename = self.log_path_prefix + "brainframe.log"
|
|
58
|
+
|
|
59
|
+
log_filename = self.log_path_prefix + "brainframe_heartbeat.log"
|
|
60
|
+
self.heartbeat_log_file = Path(log_filename).resolve().open(mode="a")
|
|
61
|
+
|
|
62
|
+
self.heartbeat_log(
|
|
63
|
+
f"BrainFrame HeartBeat starts, interval = {interval}, threshold = {threshold}"
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
def heartbeat_log_close(self):
|
|
67
|
+
self.heartbeat_log(
|
|
68
|
+
f"BrainFrame HeartBeat session closed, {datetime.now() - self.time_0}"
|
|
69
|
+
)
|
|
70
|
+
self.heartbeat_log_file.close()
|
|
71
|
+
|
|
72
|
+
def heartbeat_log(self, log_str):
|
|
73
|
+
_log_str = f"[{datetime.now()}] " + str(log_str) + "\n"
|
|
74
|
+
print(_log_str)
|
|
75
|
+
self.heartbeat_log_file.writelines(_log_str)
|
|
76
|
+
self.heartbeat_log_file.flush()
|
|
77
|
+
|
|
78
|
+
def restart_brainframe(self):
|
|
79
|
+
self.heartbeat_log(f"Stop brainframe")
|
|
80
|
+
|
|
81
|
+
if self.log_save:
|
|
82
|
+
self.save_brainframe_logs()
|
|
83
|
+
brainframe_sys_info_filename = self.log_path_prefix + "sys.info"
|
|
84
|
+
save_sys_info(brainframe_sys_info_filename)
|
|
85
|
+
|
|
86
|
+
self.stop_brainframe()
|
|
87
|
+
self.heartbeat_log(f"Save brainframe logs at {self.brainframe_log_filename}")
|
|
88
|
+
self.heartbeat_log_close()
|
|
89
|
+
|
|
90
|
+
self.heartbeat_log_open(self.interval, self.threshold)
|
|
91
|
+
self.heartbeat_log(f"Start brainframe")
|
|
92
|
+
self.start_brainframe()
|
|
93
|
+
self.heartbeat_log("brainframe has started")
|
|
94
|
+
|
|
95
|
+
def process_helper(self, cmd_str):
|
|
96
|
+
execution_start_time = datetime.now()
|
|
97
|
+
self.heartbeat_log(f"{cmd_str}: [{execution_start_time}]")
|
|
98
|
+
p = Popen(
|
|
99
|
+
cmd_str,
|
|
100
|
+
stdout=subprocess.PIPE,
|
|
101
|
+
stderr=subprocess.PIPE,
|
|
102
|
+
universal_newlines=True,
|
|
103
|
+
shell=True,
|
|
104
|
+
start_new_session=True,
|
|
105
|
+
close_fds=True,
|
|
106
|
+
)
|
|
107
|
+
# output = p.stdout.read()
|
|
108
|
+
output, err = p.communicate()
|
|
109
|
+
p.wait()
|
|
110
|
+
execution_complete_time = datetime.now()
|
|
111
|
+
self.heartbeat_log(f"{cmd_str} completed: [{execution_complete_time}]")
|
|
112
|
+
|
|
113
|
+
brainframe_log_file = (
|
|
114
|
+
Path(self.brainframe_log_filename).resolve().open(mode="a")
|
|
115
|
+
)
|
|
116
|
+
brainframe_log_file.writelines(f"[{execution_start_time}] {cmd_str}: stdout\n")
|
|
117
|
+
brainframe_log_file.writelines(output)
|
|
118
|
+
brainframe_log_file.writelines(f"[{execution_start_time}] {cmd_str}: stderr\n")
|
|
119
|
+
brainframe_log_file.writelines(err)
|
|
120
|
+
brainframe_log_file.writelines(f"[{execution_complete_time}] {cmd_str}\n")
|
|
121
|
+
brainframe_log_file.close()
|
|
122
|
+
|
|
123
|
+
self.heartbeat_log(f"Output are saved to {self.brainframe_log_filename}\n")
|
|
124
|
+
|
|
125
|
+
def save_brainframe_logs(self):
|
|
126
|
+
# os.system(f"brainframe compose logs &>> {log_filename}", )
|
|
127
|
+
cmd_str = ["brainframe compose logs"]
|
|
128
|
+
self.process_helper(cmd_str)
|
|
129
|
+
|
|
130
|
+
def stop_brainframe(self):
|
|
131
|
+
# os.system(f"brainframe compose down &>> {log_filename}")
|
|
132
|
+
cmd_str = ["brainframe compose down"]
|
|
133
|
+
self.process_helper(cmd_str)
|
|
134
|
+
|
|
135
|
+
def start_brainframe(self):
|
|
136
|
+
# os.system(f"brainframe compose up -d &>> {log_filename}")
|
|
137
|
+
cmd_str = ["brainframe compose up -d"]
|
|
138
|
+
self.process_helper(cmd_str)
|
|
139
|
+
|
|
140
|
+
def wait_for_heartbeat(self, fn: Callable, timeout: float = None):
|
|
141
|
+
"""A helpful function to wait for heartbeat."""
|
|
142
|
+
heartbeat_val, heartbeat_response = None, None
|
|
143
|
+
start_time = time()
|
|
144
|
+
while timeout is None or time() - start_time < timeout:
|
|
145
|
+
try:
|
|
146
|
+
heartbeat_response = fn()
|
|
147
|
+
heartbeat_val = time() - start_time
|
|
148
|
+
break
|
|
149
|
+
except:
|
|
150
|
+
heartbeat_val = time() - start_time
|
|
151
|
+
break
|
|
152
|
+
|
|
153
|
+
return heartbeat_val, heartbeat_response
|
|
154
|
+
|
|
155
|
+
def heartbeat_monitor(self, api):
|
|
156
|
+
# start cron scheduler
|
|
157
|
+
self.start_cron_scheduler()
|
|
158
|
+
has_restarted = False
|
|
159
|
+
while True:
|
|
160
|
+
heartbeat_val, heartbeat_response = self.wait_for_heartbeat(
|
|
161
|
+
lambda: api.version(self.threshold), self.threshold
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
if heartbeat_response:
|
|
165
|
+
has_restarted = False
|
|
166
|
+
self.heartbeat_log(
|
|
167
|
+
f"version {heartbeat_response} heartbeat OK: response time - {heartbeat_val}"
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
# zonestatus monitor.
|
|
171
|
+
self.zone_status_monitor(api)
|
|
172
|
+
# check if restart-cron task is timeout, need to deal.
|
|
173
|
+
if self.restart_timeout:
|
|
174
|
+
self.heartbeat_log(f"cron task timeout, to restart service")
|
|
175
|
+
self.restart_brainframe()
|
|
176
|
+
has_restarted = True
|
|
177
|
+
self.restart_timeout = None
|
|
178
|
+
else:
|
|
179
|
+
self.heartbeat_log(f"heartbeat failed: timeout - {heartbeat_val}")
|
|
180
|
+
if not has_restarted:
|
|
181
|
+
self.restart_brainframe()
|
|
182
|
+
has_restarted = True
|
|
183
|
+
|
|
184
|
+
if self.restart_timeout:
|
|
185
|
+
self.restart_timeout = None
|
|
186
|
+
|
|
187
|
+
sleep(self.interval)
|
|
188
|
+
|
|
189
|
+
def zone_status_monitor(self, api):
|
|
190
|
+
# check api/streams/status, analyze result is normal or not
|
|
191
|
+
status_val, status_response = self.wait_for_heartbeat(
|
|
192
|
+
lambda: api.get_latest_zone_statuses(self.threshold), self.threshold
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
if status_response:
|
|
196
|
+
# streamid,zonestatus = next(iter(status_response.items()))
|
|
197
|
+
# tstamp = zonestatus["Screen"].tstamp
|
|
198
|
+
has_changed = []
|
|
199
|
+
no_change = []
|
|
200
|
+
for streamid, zonestatus in status_response.items():
|
|
201
|
+
last_tstamp = self.d_status.get(streamid, 0.0)
|
|
202
|
+
tstamp = zonestatus["Screen"].tstamp
|
|
203
|
+
|
|
204
|
+
if last_tstamp != tstamp:
|
|
205
|
+
has_changed.append(streamid)
|
|
206
|
+
self.d_status[streamid] = tstamp
|
|
207
|
+
else:
|
|
208
|
+
no_change.append((streamid, tstamp))
|
|
209
|
+
|
|
210
|
+
self.heartbeat_log(
|
|
211
|
+
f"latest_zone_status OK: changed:{has_changed}, no change:{no_change}, response time - {status_val}"
|
|
212
|
+
)
|
|
213
|
+
else:
|
|
214
|
+
self.heartbeat_log(
|
|
215
|
+
f"latest_zone_status failed: timeout or no zonestatus - {status_val}"
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
def _cron_job(self):
|
|
219
|
+
# cron job for restart.
|
|
220
|
+
print(f"_cron_job: {self.cron_trigger}")
|
|
221
|
+
self.restart_timeout = True
|
|
222
|
+
|
|
223
|
+
def start_cron_scheduler(self):
|
|
224
|
+
# if set bgscheduler, start scheduler
|
|
225
|
+
if self.bgscheduler is not None:
|
|
226
|
+
self.heartbeat_log(f"start cron scheduler, {self.cron_trigger}")
|
|
227
|
+
self.bgscheduler.start()
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _parse_args():
|
|
231
|
+
parser = ArgumentParser(
|
|
232
|
+
description="This is the brainframe service monitor. Run the command with no arguments will launch"
|
|
233
|
+
" brainframe service and monitor the brainframe heartbeats. It will restart the service"
|
|
234
|
+
" when the heartbeats are not observed in the given timeout threshold. For example, run"
|
|
235
|
+
" the command below will monitor the heartbeats at an interval of 5 seconds. The command"
|
|
236
|
+
" will not launch brainframe automatically as '--start-now' is specified.\n\n"
|
|
237
|
+
" python brainframe_service_monitor.py --log-path ~/workspace --interval 2 --threshold 6 --start-now False\n\n"
|
|
238
|
+
"If the communication with brainframe fails, or a heartbeat deplay exceeds 6 seconds, the"
|
|
239
|
+
" service monitor will save the following log files in ~/workspace directory, then restart"
|
|
240
|
+
" the brainframe service,\n\n"
|
|
241
|
+
" <timestamp>_hearbeat.log\n"
|
|
242
|
+
" <timestamp>_brainframe.log\n"
|
|
243
|
+
" <timestamp>_sys.info\n\n"
|
|
244
|
+
"The default heartbeat interval and timeout threshold can be found in the arguments'"
|
|
245
|
+
" description.",
|
|
246
|
+
formatter_class=RawTextHelpFormatter,
|
|
247
|
+
)
|
|
248
|
+
parser.add_argument(
|
|
249
|
+
"--server-url",
|
|
250
|
+
default="http://localhost",
|
|
251
|
+
help="The URL for the BrainFrame server.",
|
|
252
|
+
)
|
|
253
|
+
parser.add_argument(
|
|
254
|
+
"--interval",
|
|
255
|
+
default=120,
|
|
256
|
+
help="The heartbeat checking interval. Default: %(default)i",
|
|
257
|
+
)
|
|
258
|
+
parser.add_argument(
|
|
259
|
+
"--threshold",
|
|
260
|
+
default=360,
|
|
261
|
+
help="The heartbeat timeout threshold. The service monitor will restart when the communication fails or timeout."
|
|
262
|
+
" Default: %(default)i",
|
|
263
|
+
)
|
|
264
|
+
parser.add_argument(
|
|
265
|
+
"--log-path",
|
|
266
|
+
type=Path,
|
|
267
|
+
default="./",
|
|
268
|
+
help="The logs will be saved in this path. Default: %(default)s",
|
|
269
|
+
)
|
|
270
|
+
parser.add_argument(
|
|
271
|
+
"--start-now",
|
|
272
|
+
action="store_true",
|
|
273
|
+
help="Start the brainframe service. Default: %(default)s",
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
parser.add_argument(
|
|
277
|
+
"--log-save",
|
|
278
|
+
action="store_true",
|
|
279
|
+
help="Brainframe log and sys.info will be saved when the service is unavailable. Default: %(default)s",
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
parser.add_argument(
|
|
283
|
+
"--restart-cron",
|
|
284
|
+
default=None,
|
|
285
|
+
type=str,
|
|
286
|
+
help="""Brainframe service will restart on midnight, using Cron-style schedule string (e.g., '* * * * *'), Default: %(default)s.
|
|
287
|
+
For example, '0 0 * * 3' is on midnight every Thursday,
|
|
288
|
+
The first 0: Minute (0 - 59)
|
|
289
|
+
The second 0: Hour (0 - 23)
|
|
290
|
+
The third *: Day of the month (1 - 31)
|
|
291
|
+
The fourth *: Month (1 - 12)
|
|
292
|
+
The fifth 3: Day of the week (0 - 6 for Monday to Sunday, accroding to apscheduler)
|
|
293
|
+
When you use an asterisk (*) in any of these fields, it represents 'every'.""",
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
args = subcommand_parse_args(parser)
|
|
297
|
+
return args
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
@command("service_monitor")
|
|
301
|
+
def service_monitor():
|
|
302
|
+
args = _parse_args()
|
|
303
|
+
|
|
304
|
+
# if restart_cron is set, check if cron string is fine.
|
|
305
|
+
if args.restart_cron:
|
|
306
|
+
try:
|
|
307
|
+
# use from_crontab() create CronTrigger instance
|
|
308
|
+
trigger = CronTrigger.from_crontab(args.restart_cron)
|
|
309
|
+
print(f"restart-cron: {trigger}")
|
|
310
|
+
except (ValueError, KeyError) as e:
|
|
311
|
+
# cron string invalid
|
|
312
|
+
print(f"Invalid argument restart-cron format: {e}")
|
|
313
|
+
exit(1)
|
|
314
|
+
|
|
315
|
+
# Connect to the BrainFrame Server
|
|
316
|
+
server_url = args.server_url
|
|
317
|
+
|
|
318
|
+
hb = HeartBeat(
|
|
319
|
+
args.log_path,
|
|
320
|
+
args.interval,
|
|
321
|
+
args.threshold,
|
|
322
|
+
log_save=args.log_save,
|
|
323
|
+
restart_cron=args.restart_cron,
|
|
324
|
+
)
|
|
325
|
+
|
|
326
|
+
if args.start_now:
|
|
327
|
+
hb.start_brainframe()
|
|
328
|
+
|
|
329
|
+
hb.heartbeat_log(
|
|
330
|
+
f"heartbeat Connecting: BrainFrameAPI({server_url}), wait_for_server_initialization() ..."
|
|
331
|
+
)
|
|
332
|
+
api = BrainFrameAPI(server_url)
|
|
333
|
+
api.wait_for_server_initialization()
|
|
334
|
+
|
|
335
|
+
hb.heartbeat_log("heartbeat Connected")
|
|
336
|
+
|
|
337
|
+
hb.heartbeat_monitor(api)
|
|
338
|
+
|
|
339
|
+
hb.heartbeat_log("heartbeat exit")
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
if __name__ == "__main__":
|
|
343
|
+
by_name["service_monitor"]()
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
#
|
|
2
|
+
# Copyright (c) 2021 Dilili Labs, Inc. All rights reserved. Contains Dilili Labs Proprietary Information. RESTRICTED COMPUTER SOFTWARE. LIMITED RIGHTS DATA.
|
|
3
|
+
#
|
|
4
|
+
|
|
5
|
+
import subprocess
|
|
6
|
+
from argparse import ArgumentParser
|
|
7
|
+
|
|
8
|
+
from brainframe_sys_tools.command_utils import command, subcommand_parse_args, by_name
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _parse_args():
|
|
12
|
+
parser = ArgumentParser("brainframe-sys-tools ssh_tunnel")
|
|
13
|
+
parser.add_argument(
|
|
14
|
+
"-s",
|
|
15
|
+
"--server-url",
|
|
16
|
+
default="dililitechnology.com",
|
|
17
|
+
help="The SSH server url or ip, Default is %(default)s.",
|
|
18
|
+
)
|
|
19
|
+
parser.add_argument(
|
|
20
|
+
"-u",
|
|
21
|
+
"--user",
|
|
22
|
+
default="ubuntu",
|
|
23
|
+
help="Username to access the SSH server, Default is %(default)s.",
|
|
24
|
+
)
|
|
25
|
+
parser.add_argument(
|
|
26
|
+
"-p",
|
|
27
|
+
"--port",
|
|
28
|
+
default="22",
|
|
29
|
+
help="The SSH server port, Default is %(default)s.",
|
|
30
|
+
)
|
|
31
|
+
parser.add_argument(
|
|
32
|
+
"-r",
|
|
33
|
+
"--remoteforward",
|
|
34
|
+
action="append",
|
|
35
|
+
required=True,
|
|
36
|
+
help="The SSH remote forward port(s) and local port(s), \
|
|
37
|
+
format is 'remoteport:localhost:localport'. Use multiple -r to specify additional rules. \
|
|
38
|
+
For example '-r 8088:localhost:80', on remote ssh server, you can use 'localhost:8088' to access local port 80",
|
|
39
|
+
)
|
|
40
|
+
parser.add_argument(
|
|
41
|
+
"-i",
|
|
42
|
+
"--identity_file",
|
|
43
|
+
default=None,
|
|
44
|
+
help="The SSH identity file, Default is %(default)s.",
|
|
45
|
+
)
|
|
46
|
+
parser.add_argument(
|
|
47
|
+
"-o",
|
|
48
|
+
"--option",
|
|
49
|
+
default=["ExitOnForwardFailure=yes"],
|
|
50
|
+
action="append",
|
|
51
|
+
help="SSH client options, Default is %(default)s. \
|
|
52
|
+
Use muliple -o to specify more options, for example '-o ServerAliveInterval=120 -o ServerAliveCountMax=3'.",
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
args = subcommand_parse_args(parser)
|
|
56
|
+
|
|
57
|
+
return args
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def run_command_list(commandlst):
|
|
61
|
+
print(" ".join(commandlst))
|
|
62
|
+
print("Press Ctrl-C, to quit")
|
|
63
|
+
|
|
64
|
+
try:
|
|
65
|
+
result = subprocess.run(
|
|
66
|
+
commandlst, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
if result.stdout:
|
|
70
|
+
print(result.stdout)
|
|
71
|
+
if result.stderr:
|
|
72
|
+
print(f"Error: {result.stderr}")
|
|
73
|
+
except KeyboardInterrupt:
|
|
74
|
+
print(" Process was interrupted by user")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@command("ssh_tunnel")
|
|
78
|
+
def ssh_tunnel():
|
|
79
|
+
args = _parse_args()
|
|
80
|
+
|
|
81
|
+
# Build the SSH command
|
|
82
|
+
ssh_command = ["ssh", "-N", "-p", args.port]
|
|
83
|
+
if args.identity_file:
|
|
84
|
+
ssh_command.extend(["-i", args.identity_file])
|
|
85
|
+
|
|
86
|
+
# Add each remote forward rule to the SSH command
|
|
87
|
+
for rf in args.remoteforward:
|
|
88
|
+
ssh_command.extend(["-R", rf])
|
|
89
|
+
|
|
90
|
+
# Add each option to command list
|
|
91
|
+
for option in args.option:
|
|
92
|
+
ssh_command.extend(["-o", option])
|
|
93
|
+
|
|
94
|
+
ssh_command.append(f"{args.user}@{args.server_url}")
|
|
95
|
+
# start ssh tunnel
|
|
96
|
+
print(
|
|
97
|
+
"Note: After establishing an SSH reverse tunnel, the remote host can access the locally mapped ports and services.\n"
|
|
98
|
+
" Please ensure the privacy and security of the local host."
|
|
99
|
+
)
|
|
100
|
+
run_command_list(ssh_command)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
if __name__ == "__main__":
|
|
104
|
+
by_name["ssh_tunnel"]()
|