summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorwangmenglan <[email protected]>2023-12-04 15:41:02 +0800
committerwangmenglan <[email protected]>2023-12-08 16:51:17 +0800
commit052fc5bb8d6765ac20c2f8ce5dda97750d929dcd (patch)
tree61746aa5a7a2073a955288ea8472e4fb5dddcb13 /tools
parent0c5df1fe2e8451cb85ee25bf01fb111d15a4ed0f (diff)
support obp dynamic configuration loadingv4.6.63-20231208
Diffstat (limited to 'tools')
-rw-r--r--tools/CMakeLists.txt1
-rw-r--r--tools/monit_obp/CMakeLists.txt2
-rw-r--r--tools/monit_obp/monit_obp.py252
-rw-r--r--tools/systemd/CMakeLists.txt4
-rw-r--r--tools/systemd/mrapm_obp.service.in14
5 files changed, 273 insertions, 0 deletions
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 9754a4f..7140a0c 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -1,6 +1,7 @@
add_subdirectory(tcpdump)
add_subdirectory(monit_device)
add_subdirectory(monit_stream)
+add_subdirectory(monit_obp)
add_subdirectory(pagstat)
add_subdirectory(dlogreader)
add_subdirectory(devbind)
diff --git a/tools/monit_obp/CMakeLists.txt b/tools/monit_obp/CMakeLists.txt
new file mode 100644
index 0000000..f420dbb
--- /dev/null
+++ b/tools/monit_obp/CMakeLists.txt
@@ -0,0 +1,2 @@
+configure_file(monit_obp.py monit_obp)
+install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/monit_obp DESTINATION ${MR_INSTALL_BINDIR_RELATIVE_PATH} COMPONENT Program)
diff --git a/tools/monit_obp/monit_obp.py b/tools/monit_obp/monit_obp.py
new file mode 100644
index 0000000..be77da9
--- /dev/null
+++ b/tools/monit_obp/monit_obp.py
@@ -0,0 +1,252 @@
+#!/usr/bin/env python2
+
+import prettytable
+import argparse
+import json
+import time
+import sys
+import signal
+import os
+import socket
+
+from functools import partial
+from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
+
+TBPS = (1 * 1000 * 1000 * 1000 * 1000)
+GBPS = (1 * 1000 * 1000 * 1000)
+MBPS = (1 * 1000 * 1000)
+KBPS = (1 * 1000)
+
+G_JSON_PATH = '/var/run/mrzcpd/mrmonit.daemon'
+
+TITLE_VECTOR = [
+ 'WorkLine',
+ 'R1',
+ 'R2',
+ 'R3',
+ 'R4',
+ 'last_active_time'
+]
+
+TITLE_ERROR_VECTOR = [
+ 'Reason'
+]
+
+TITLE_ERROR_MAP = {
+ 'Reason': 'error_reason'
+}
+
+OBP_DEVICE_TYPE_VECTOR = ['SINO-OLP-6500']
+
+TITLE_MAP = {
+ 'WorkLine': 'workline',
+ 'R1': 'r1',
+ 'R2': 'r2',
+ 'R3': 'r3',
+ 'R4': 'r4',
+ 'last_active_time': 'last_active_time'
+}
+
+TITLE_MAP_PROMETHEUS = {
+ 'WorkLine': 'obp_workline',
+ 'R1': 'obp_r1_optical_power',
+ 'R2': 'obp_r2_optical_power',
+ 'R3': 'obp_r3_optical_power',
+ 'R4': 'obp_r4_optical_power',
+ 'last_active_time': 'obp_last_active_time'
+}
+
+def locate_vector_by_key(vector, key, symbol):
+ return [s for s in vector if s[key] == symbol]
+
+def list_all_obp_dev(json_fp):
+ return [s['symbol'] for s in json_fp['OBP']['SINO-OLP-6500']]
+
+def list_all_channel(json_fp, devsym):
+ obp_devs = locate_vector_by_key(json_fp['OBP']['SINO-OLP-6500'], "symbol", devsym)
+ return [s['channel_id'] for s in obp_devs[0]['channel']]
+
+def obp_dev_get_channel(json_fp, str_device, channel_id):
+ obp_devs = locate_vector_by_key(json_fp['OBP']['SINO-OLP-6500'], "symbol", str_device)
+ return locate_vector_by_key(obp_devs[0]['channel'], 'channel_id', channel_id)
+
+def timestamp_to_locale_time(timestamp):
+ local_time = time.localtime(float(timestamp))
+ return time.strftime('%c', local_time)
+
+def dump_human_table(json_fp, devsym, channel_list):
+ title_flag = 0
+ table_obp_dev = prettytable.PrettyTable([' '] + TITLE_VECTOR,
+ vertical_char=' ', horizontal_char='-', junction_char=' ')
+
+ table_obp_dev.align[' '] = 'r'
+
+ for item in TITLE_VECTOR:
+ table_obp_dev.align[item] = 'r'
+
+ for channel_id in channel_list:
+ build_table_flag = 1
+ channel = obp_dev_get_channel(json_fp, devsym, channel_id)
+ channel_table_list = []
+ channel_table_list.append('channel_id:%d' % channel_id)
+
+ for item in TITLE_VECTOR:
+ try:
+ if item == 'last_active_time':
+ if channel[0][TITLE_MAP[item]] == "0":
+ channel_table_list.append("--")
+ else:
+ channel_table_list.append(timestamp_to_locale_time(channel[0][TITLE_MAP[item]]))
+ else:
+ channel_table_list.append(channel[0][TITLE_MAP[item]])
+ except Exception as e:
+ build_table_flag = 0
+ break
+
+ if build_table_flag == 0:
+ continue
+
+ table_obp_dev.add_row(channel_table_list)
+ if title_flag == 0:
+ print('\nTime: %s, OBP device: %s' % (time.strftime('%c'), devsym))
+ title_flag = 1
+ if title_flag > 0:
+ print(table_obp_dev)
+
+def dump_error_reason(json_fp, devsym, channel_list):
+ title_flag = 0
+
+ for channel_id in channel_list:
+ channel = obp_dev_get_channel(json_fp, devsym, channel_id)
+ if 'error_reason' not in channel[0]:
+ continue
+
+ if title_flag == 0:
+ print('\nTime: %s, OBP device: %s Output error message.' % (time.strftime('%c'), devsym))
+ title_flag = 1
+ print('channel_id:%d Reason:%s' % (channel_id, str(channel[0]['error_reason'])))
+
+# APM sendlog format
+
+def dump_prometheus_output(json_fp, devsym, channel_id):
+ resp = ''
+ channel = obp_dev_get_channel(json_fp, devsym, channel_id)
+ for item in TITLE_VECTOR:
+ try:
+ if item == 'WorkLine':
+ if channel[0][TITLE_MAP[item]] == 'bypass':
+ resp += '%s{dev=\"%s\", channel=\"%u\"} %u\n' % (TITLE_MAP_PROMETHEUS[item], devsym, channel_id, 0)
+ elif channel[0][TITLE_MAP[item]] == 'inline':
+ resp += '%s{dev=\"%s\", channel=\"%u\"} %u\n' % (TITLE_MAP_PROMETHEUS[item], devsym, channel_id, 1)
+ elif item == 'R1' or item == 'R2' or item == 'R3' or item == 'R4' :
+ resp += '%s{dev=\"%s\", channel=\"%u\"} %0.2f\n' % (TITLE_MAP_PROMETHEUS[item], devsym, channel_id, float(channel[0][TITLE_MAP[item]]))
+ else:
+ resp += '%s{dev=\"%s\", channel=\"%u\"} %u\n' % (TITLE_MAP_PROMETHEUS[item], devsym, channel_id, float(channel[0][TITLE_MAP[item]]))
+ except Exception as e:
+ resp += ''
+ return resp
+
+def setup_argv_parser(obp_dev_list):
+ parser = argparse.ArgumentParser(
+ description='Marsio ZeroCopy Tools -- Monitor OBP devices')
+
+ parser.add_argument('-t', '--time', help='interval, seconds to wait between updates',
+ type=int, default=1)
+ parser.add_argument('-l', '--loop', help='print loop, exit when recv a signal',
+ action='store_true', default=0)
+ parser.add_argument('-i', '--device', help='the name of OBP device name',
+ action='append', choices=obp_dev_list)
+ parser.add_argument('--clear-screen', help='clear screen at start of loop',
+ action='store_true', default=0)
+
+ # APM sendlog options
+ parser.add_argument('--prometheus-client', help='Run as prometheus client',
+ action='store_true', default=0)
+
+ parser.add_argument('--prometheus-client-listen-port', help='Default Port of prometheus client',
+ type=int, default=8903)
+
+ return parser.parse_args()
+
+def obp_dev_json_load():
+ with open(G_JSON_PATH) as json_fp:
+ return json.load(json_fp)
+
+def sigint_handler(handler, frame):
+ sys.exit(0)
+
+
+class PrometheusClient(BaseHTTPRequestHandler):
+ def __init__(self, request, client_address, server):
+ self.json_fp = obp_dev_json_load()
+ self.obp_dev_list = list_all_obp_dev(self.json_fp)
+ BaseHTTPRequestHandler.__init__(self, request, client_address, server)
+
+ def do_GET(self):
+ if self.path == '/metrics':
+ resp = ''
+
+ for devsym in self.obp_dev_list:
+ channel_list = list_all_channel(self.json_fp, devsym)
+ for channel_id in channel_list:
+ resp += dump_prometheus_output(self.json_fp, devsym, channel_id)
+ self.send_response(200)
+ self.send_header('Content-type', 'text/plain; version=0.0.4')
+ self.end_headers()
+ self.wfile.write(resp)
+ else:
+ self.send_error(404)
+ self.end_headers()
+
+def prometheus_client_init(json_fp, obp_dev_list, prometheus_client_port):
+ HTTPServer(("", prometheus_client_port), PrometheusClient).serve_forever()
+
+
+def main():
+ signal.signal(signal.SIGINT, sigint_handler)
+
+ try:
+ json_fp = obp_dev_json_load()
+ obp_dev_list = list_all_obp_dev(json_fp)
+ r_option = setup_argv_parser(obp_dev_list)
+
+ if r_option.prometheus_client:
+ prometheus_client_init(json_fp, obp_dev_list,
+ r_option.prometheus_client_listen_port)
+
+ sleep_interval = r_option.time
+ obp_dev_user_list = obp_dev_list if r_option.device is None else r_option.device
+ if len(obp_dev_user_list) == 0:
+ print("monit_obp: error: No OBP device exists.")
+ sys.exit(1)
+
+ while True:
+ if r_option.clear_screen:
+ os.system('clear')
+
+ json_fp = obp_dev_json_load()
+
+ for devsym in obp_dev_user_list:
+ channel_list = list_all_channel(json_fp, devsym)
+ dump_human_table(json_fp, devsym, channel_list)
+
+ for devsym in obp_dev_user_list:
+ channel_list = list_all_channel(json_fp, devsym)
+ dump_error_reason(json_fp, devsym, channel_list)
+
+ if not r_option.loop:
+ break
+
+ time.sleep(float(sleep_interval))
+
+ except KeyboardInterrupt:
+ pass
+ except ValueError as err:
+ print(("%s, perhaps mrzcpd program is not running.") % str(err))
+ except IOError as err:
+ print(("%s, perhaps mrzcpd program is not running.") % str(err))
+
+ return 0
+
+if __name__ == '__main__':
+ main()
diff --git a/tools/systemd/CMakeLists.txt b/tools/systemd/CMakeLists.txt
index 9ecee55..641dafb 100644
--- a/tools/systemd/CMakeLists.txt
+++ b/tools/systemd/CMakeLists.txt
@@ -4,6 +4,7 @@ configure_file(mrzcpd.service.in mrzcpd.service)
configure_file(mrtunnat.service.in mrtunnat.service)
configure_file(mrapm_device.service.in mrapm_device.service)
configure_file(mrapm_stream.service.in mrapm_stream.service)
+configure_file(mrapm_obp.service.in mrapm_obp.service)
install(FILES ${CMAKE_BINARY_DIR}/tools/systemd/mrzcpd_hwdb_setup.service
DESTINATION ${MR_INSTALL_SYSTEM} COMPONENT Program)
@@ -23,3 +24,6 @@ install(FILES ${CMAKE_BINARY_DIR}/tools/systemd/mrapm_device.service
install(FILES ${CMAKE_BINARY_DIR}/tools/systemd/mrapm_stream.service
DESTINATION ${MR_INSTALL_SYSTEM} COMPONENT Program)
+install(FILES ${CMAKE_BINARY_DIR}/tools/systemd/mrapm_obp.service
+ DESTINATION ${MR_INSTALL_SYSTEM} COMPONENT Program)
+
diff --git a/tools/systemd/mrapm_obp.service.in b/tools/systemd/mrapm_obp.service.in
new file mode 100644
index 0000000..34bf70c
--- /dev/null
+++ b/tools/systemd/mrapm_obp.service.in
@@ -0,0 +1,14 @@
+[Unit]
+Description=Marsio APM logwritter(monit_obp)
+After=mrzcpd.service
+Requires=mrzcpd.service
+
+[Service]
+Type=simple
+ExecStart=@MR_INSTALL_PREFIX@/bin/monit_obp --prometheus-client
+RestartSec=10s
+Restart=always
+PrivateTmp=True
+
+[Install]
+WantedBy=multi-user.target