summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlijia <[email protected]>2019-07-26 21:34:42 +0800
committerlijia <[email protected]>2019-07-26 21:34:42 +0800
commite2d991a0ab2eb6ff9de447864e94a4bd4319e95c (patch)
treef2676564b0ef1255907461ad7d1d751675d1e65e
parent6d477839ae0a1a91f993ecbcd8a430f8b11ec6a0 (diff)
add tsg_sn_update.py, tsg_monit_device.py
-rw-r--r--deploy_etc/tsg_chassis_ip.json28
-rw-r--r--deploy_etc/tsg_chassis_port.json57
-rw-r--r--deploy_etc/tsg_sn.json3
-rw-r--r--deploy_etc/tsg_tags.json12
-rw-r--r--py_src/tsg_get_sn.py21
-rw-r--r--py_src/tsg_monit_device.py255
-rw-r--r--py_src/tsg_update_tags.py33
-rw-r--r--py_test/parse_json.py21
-rw-r--r--py_test/shell_cmd.py32
9 files changed, 462 insertions, 0 deletions
diff --git a/deploy_etc/tsg_chassis_ip.json b/deploy_etc/tsg_chassis_ip.json
new file mode 100644
index 0000000..fe8b054
--- /dev/null
+++ b/deploy_etc/tsg_chassis_ip.json
@@ -0,0 +1,28 @@
+{
+ "tsg_chassis_ip": [{
+ "type": "mxn",
+ "name": "mxn",
+ "id": 0,
+ "ip": "192.168.100.5"
+ }, {
+ "type": "mcn",
+ "name": "mcn_node1",
+ "id": 1,
+ "ip": "192.168.100.1"
+ }, {
+ "type": "mcn",
+ "name": "mcn_node2",
+ "id": 2,
+ "ip": "192.168.100.2"
+ }, {
+ "type": "mcn",
+ "name": "mcn_node3",
+ "id": 3,
+ "ip": "192.168.100.3"
+ }, {
+ "type": "mcn",
+ "name": "mcn_node4",
+ "id": 4,
+ "ip": "192.168.100.4"
+ }]
+}
diff --git a/deploy_etc/tsg_chassis_port.json b/deploy_etc/tsg_chassis_port.json
new file mode 100644
index 0000000..7deae44
--- /dev/null
+++ b/deploy_etc/tsg_chassis_port.json
@@ -0,0 +1,57 @@
+{
+ "tsg_chassis_port": [{
+ "port_id": 0,
+ "type": "physical",
+ "function": "inline",
+ "description": "to device xxx"
+ },
+ {
+ "port_id": 1,
+ "type": "physical",
+ "function": "inline",
+ "description": "to device xxx"
+ },
+ {
+ "port_id": 2,
+ "type": "physical",
+ "function": "inline",
+ "description": "to device xxx"
+ },
+ {
+ "port_id": 3,
+ "type": "physical",
+ "function": "inline",
+ "description": "to device xxx"
+ },
+ {
+ "port_id": 4,
+ "type": "physical",
+ "function": "mirror",
+ "description": "forward packet to xxx"
+ },
+ {
+ "port_id": 43,
+ "type": "backplane",
+ "function": "intercomm",
+ "description": "to mcn 1"
+ },
+ {
+ "port_id": 41,
+ "type": "backplane",
+ "function": "intercomm",
+ "description": "to mcn 2"
+ },
+ {
+ "port_id": 39,
+ "type": "backplane",
+ "function": " intercomm ",
+ "description": "to mcn 3"
+ },
+ {
+ "port_id": 37,
+ "type": "backplane",
+ "function": " intercomm ",
+ "description": "to mcn 4"
+ }
+ ]
+}
diff --git a/deploy_etc/tsg_sn.json b/deploy_etc/tsg_sn.json
new file mode 100644
index 0000000..7c2f3f8
--- /dev/null
+++ b/deploy_etc/tsg_sn.json
@@ -0,0 +1,3 @@
+{
+ "sn": "CBT2201925000001"
+}
diff --git a/deploy_etc/tsg_tags.json b/deploy_etc/tsg_tags.json
new file mode 100644
index 0000000..3afce8c
--- /dev/null
+++ b/deploy_etc/tsg_tags.json
@@ -0,0 +1,12 @@
+{
+ "tags": [{
+ "tag": "location",
+ "value": "beijing/chaoyang"
+ }, {
+ "tag ": "isp",
+ "value ": "dianxin"
+ }, {
+ "tag ": "custom_tag1",
+ "value ": "x/y/z"
+ }]
+}
diff --git a/py_src/tsg_get_sn.py b/py_src/tsg_get_sn.py
new file mode 100644
index 0000000..e0bf4f3
--- /dev/null
+++ b/py_src/tsg_get_sn.py
@@ -0,0 +1,21 @@
+import json
+
+SN_JSON_PATH = '/opt/tsg/etc/tsg_sn.json'
+
+def tsg_get_sn():
+ try:
+ with open(SN_JSON_PATH) as json_fp:
+ json_dict = json.load(json_fp)
+ return json_dict['sn']
+ except IOError:
+ return ""
+
+def main():
+ sn = tsg_get_sn()
+ if len(sn) > 0:
+ print("%s" %(sn))
+ else:
+ print("can't get sn")
+
+if __name__ == '__main__':
+ main()
diff --git a/py_src/tsg_monit_device.py b/py_src/tsg_monit_device.py
new file mode 100644
index 0000000..54398bd
--- /dev/null
+++ b/py_src/tsg_monit_device.py
@@ -0,0 +1,255 @@
+#!/usr/bin/python
+
+import argparse
+import json
+import prettytable
+import time
+import sys
+import signal
+import os
+import telegraf
+import socket
+
+TBPS = (1 * 1000 * 1000 * 1000 * 1000)
+GBPS = (1 * 1000 * 1000 * 1000)
+MBPS = (1 * 1000 * 1000)
+KBPS = (1 * 1000)
+
+#G_JSON_PATH = './mrmonit.daemon'
+G_JSON_PATH = '/var/run/mrzcpd/mrmonit.daemon'
+
+TITLE_VECTOR = ['PhyRXFrame','PhyRXBits','PhyRXMissed','PhyRXError',
+ 'PhyRXNoBUF','PhyTXFrame','PhyTXBits','PhyTXError',
+ 'UsrRXDrops', 'UsrTXDrops']
+
+TITLE_MAP = { 'PhyRXFrame' : 'ipackets',
+ 'PhyRXBits' : 'ibytes',
+ 'PhyRXMissed' : 'imissed',
+ 'PhyRXError' : 'ierrors',
+ 'PhyRXNoBUF' : 'rxnombuf',
+ 'PhyTXFrame' : 'opackets',
+ 'PhyTXBits' : 'obytes',
+ 'PhyTXError' : 'oerrors',
+ 'UsrRXDrops' : 'userrxdrop',
+ 'UsrTXDrops' : 'usertxdrop'
+ }
+udp_sock_fd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+remote_ip_port = ('192.168.11.219', 8126)
+
+def locate_vector_by_symbol(vector, symbol):
+ return [s for s in vector if s['symbol'] == symbol]
+
+def list_all_phydev(json_fp):
+ return [ s['symbol'] for s in json_fp['device']]
+
+def phydev_value_read(json_fp, str_device, str_item):
+ phydevs = locate_vector_by_symbol(json_fp['device'], str_device)
+ return phydevs[0]['stats']['accumulative'][str_item]
+
+def phydev_speed_read(json_fp, str_device, str_item):
+ phydevs = locate_vector_by_symbol(json_fp['device'], str_device)
+ return phydevs[0]['stats']['speed'][str_item]
+
+def trans_to_human_readable(value):
+ if value > TBPS:
+ return value * 1.0 / TBPS, 'T'
+ if value > GBPS:
+ return value * 1.0 / GBPS, 'G'
+ if value > MBPS:
+ return value * 1.0 / MBPS, 'M'
+ if value > KBPS:
+ return value * 1.0 / KBPS, 'K'
+
+ return value * 1.0, ' '
+
+def dump_human_table(json_fp, devsym, is_human_number = 0):
+
+ print('\nTime: %s, Physical device: %s' % (time.strftime('%c'), devsym))
+
+ table_phydev = prettytable.PrettyTable([' '] + TITLE_VECTOR,
+ vertical_char=' ',horizontal_char = '-', junction_char=' ')
+
+ ValueList = ['Accumulative']
+ SpeedList = ['Per Second']
+
+ table_phydev.align[' '] = 'r'
+
+ for item in TITLE_VECTOR:
+ table_phydev.align[item] = 'r'
+
+ for item in TITLE_VECTOR:
+ value = phydev_value_read(json_fp, devsym, TITLE_MAP[item])
+ if is_human_number:
+ h_value, h_value_unit = trans_to_human_readable(value)
+ ValueList.append('%7.2f%c' % (h_value, h_value_unit))
+ else:
+ ValueList.append(value)
+
+
+ for item in TITLE_VECTOR:
+ speed = phydev_speed_read(json_fp, devsym, TITLE_MAP[item])
+ if is_human_number:
+ h_speed, h_speed_unit = trans_to_human_readable(speed)
+ SpeedList.append('%7.2f%c' % (h_speed, h_speed_unit))
+ else:
+ SpeedList.append(speed)
+
+
+ #add by lijia for tsg oam
+ print("######")
+ print(table_phydev)
+ line_prot_buf = "interface,device=%s,flow_type=inline " %(devsym)
+
+ for num in range(0,9):
+ #Accumulative
+ #tmp_str = "%s=%s," %(TITLE_VECTOR[num],ValueList[num+1])
+ # per second
+ tmp_str = "%s=%s," %(TITLE_VECTOR[num],SpeedList[num+1])
+ line_prot_buf += tmp_str
+ num += 1
+ #tmp_str = "%s=%s" %(TITLE_VECTOR[num],ValueList[num+1])
+ tmp_str = "%s=%s" %(TITLE_VECTOR[num],SpeedList[num+1])
+ line_prot_buf += tmp_str
+ print(line_prot_buf)
+ udp_sock_fd.sendto(line_prot_buf, remote_ip_port)
+ print("######\n")
+ #add by lijia for tsg oam
+
+ table_phydev.add_row(ValueList)
+ table_phydev.add_row(SpeedList)
+ print(table_phydev)
+
+# APM sendlog format
+def dump_apm_sendlog(json_fp, telegraf_client, devsym):
+
+ __metric_dict_speed = {}
+ __metric_dict_value = {}
+ __tags = {'device' : devsym }
+
+ for item in TITLE_VECTOR:
+ value = phydev_speed_read(json_fp, devsym, TITLE_MAP[item])
+ __metric_dict_speed[item] = value
+
+ for item in TITLE_VECTOR:
+ value = phydev_value_read(json_fp, devsym, TITLE_MAP[item])
+ __metric_dict_value[item] = value
+
+ telegraf_client.metric('mr4_device_rxtx_speed', __metric_dict_speed, tags = __tags)
+ telegraf_client.metric('mr4_device_rxtx_value', __metric_dict_value, tags = __tags)
+ return
+
+def setup_argv_parser(phydev_list):
+
+ parser = argparse.ArgumentParser(description='Marsio ZeroCopy Tools -- Monitor NIC devices',
+ version = 'Marsio ZeroCopy Tools Suite 4.1')
+
+ 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('-H', '--human-readable', help = 'print value in human readable format',
+ action = 'store_true', default = 0)
+ parser.add_argument('-i', '--interface', help = 'the name of network interface',
+ action = 'append', choices=phydev_list)
+ parser.add_argument('--clear-screen', help = 'clear screen at start of loop',
+ action='store_true', default = 0)
+
+ # APM sendlog options
+ parser.add_argument('--sendlog-apm', help = 'send log to apm server',
+ action='store_true', default = 0)
+ parser.add_argument('--sendlog-apm-cfg', help = 'send log configure file',
+ type=str, default = '/opt/mrzcpd/etc/mrsendlog.conf')
+
+ return parser.parse_args()
+
+def phydev_json_load():
+ with open(G_JSON_PATH) as json_fp:
+ return json.load(json_fp)
+
+
+def sendlog_hostname(test_hostname, test_port):
+ import socket
+
+ hostname = socket.gethostname()
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ try:
+ s.connect((test_hostname, int(test_port)))
+ local_ip_addr = s.getsockname()[0]
+ except:
+ local_ip_addr = '127.0.0.1'
+ finally:
+ s.close()
+
+ return hostname, local_ip_addr
+
+def sendlog_apm_init(r_option):
+ import ConfigParser
+ import urllib
+
+ config = ConfigParser.ConfigParser()
+ config.read(r_option.sendlog_apm_cfg)
+
+ apm_server_url = config.get('sendlog_apm', 'apm_device_server')
+ url_proto, rest = urllib.splittype(apm_server_url)
+ url_host, rest = urllib.splithost(rest)
+ url_host, url_port = urllib.splitport(url_host)
+
+ hostname, local_ip_addr = sendlog_hostname(url_host, url_port)
+ sendlog_tags = {'host' : hostname, 'local_ip_addr': local_ip_addr}
+
+ if url_proto == 'udp':
+ telegraf_client = telegraf.TelegrafClient(host = url_host,
+ port = int(url_port), tags = sendlog_tags)
+ elif url_proto == 'http':
+ telegraf_client = telegraf.HttpClient(host = url_host,
+ port = int(url_port), tags = sendlog_tags)
+
+ return telegraf_client
+
+def sigint_handler(handler, frame):
+ sys.exit(0)
+
+def main():
+ signal.signal(signal.SIGINT, sigint_handler)
+
+ try:
+ json_fp = phydev_json_load()
+ phydev_list = list_all_phydev(json_fp)
+ print("##### phydev_list:%s" %(phydev_list))
+ r_option = setup_argv_parser(phydev_list)
+
+ if r_option.sendlog_apm:
+ telegraf_client = sendlog_apm_init(r_option)
+
+ phydev_user_list = phydev_list if r_option.interface is None else r_option.interface
+ print("##### phydev_user_list:%s" %(phydev_user_list))
+
+ while True:
+ if r_option.clear_screen:
+ os.system('clear')
+
+ json_fp = phydev_json_load()
+
+ if r_option.sendlog_apm:
+ for devsym in phydev_user_list:
+ dump_apm_sendlog(json_fp, telegraf_client, devsym)
+ else:
+ for devsym in phydev_user_list:
+ dump_human_table(json_fp, devsym, r_option.human_readable)
+
+ if not r_option.loop:
+ break
+
+ time.sleep(r_option.time)
+
+ 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/py_src/tsg_update_tags.py b/py_src/tsg_update_tags.py
new file mode 100644
index 0000000..c7587b3
--- /dev/null
+++ b/py_src/tsg_update_tags.py
@@ -0,0 +1,33 @@
+import subprocess
+import json
+from tsg_get_sn import *
+
+class CommandException(Exception):
+ pass
+
+def tsg_get_device_list_info_by_sn(sn):
+ command = "consul kv get device_list/%s" %(sn)
+ exitcode, output = subprocess.getstatusoutput(command)
+ #print("%d" %(exitcode))
+ if exitcode != 0:
+ raise CommandException(output)
+ return ""
+ else:
+ return output
+
+def tsg_get_tags_by_sn(sn):
+ dev_list_info = tsg_get_device_list_info_by_sn(sn)
+ if len(dev_list_info) <= 0:
+ #写个空json
+ return "{}"
+
+ json_dict = json.loads(dev_list_info)
+ tags = json_dict['tags']
+ #重新组织成合法的json格式
+ dict_to_json = json.dumps(tags)
+ json_tags = "{\"tags\":%s}" %(dict_to_json)
+ return json_tags
+
+if __name__ == '__main__':
+ sn = tsg_get_sn()
+ print(tsg_get_tags_by_sn(sn))
diff --git a/py_test/parse_json.py b/py_test/parse_json.py
new file mode 100644
index 0000000..e0bf4f3
--- /dev/null
+++ b/py_test/parse_json.py
@@ -0,0 +1,21 @@
+import json
+
+SN_JSON_PATH = '/opt/tsg/etc/tsg_sn.json'
+
+def tsg_get_sn():
+ try:
+ with open(SN_JSON_PATH) as json_fp:
+ json_dict = json.load(json_fp)
+ return json_dict['sn']
+ except IOError:
+ return ""
+
+def main():
+ sn = tsg_get_sn()
+ if len(sn) > 0:
+ print("%s" %(sn))
+ else:
+ print("can't get sn")
+
+if __name__ == '__main__':
+ main()
diff --git a/py_test/shell_cmd.py b/py_test/shell_cmd.py
new file mode 100644
index 0000000..04c1708
--- /dev/null
+++ b/py_test/shell_cmd.py
@@ -0,0 +1,32 @@
+import subprocess
+import json
+
+class CommandException(Exception):
+ pass
+
+
+def tsg_get_device_list_info_by_sn(sn):
+ command = "consul kv get device_list/%s" %(sn)
+ exitcode, output = subprocess.getstatusoutput(command)
+ #print("%d" %(exitcode))
+ if exitcode != 0:
+ raise CommandException(output)
+ return ""
+ else:
+ return output
+
+def tsg_get_tags_by_sn(sn):
+ dev_list_info = tsg_get_device_list_info_by_sn(sn)
+ if len(dev_list_info) <= 0:
+ #写个空json
+ return "{}"
+
+ json_dict = json.loads(dev_list_info)
+ tags = json_dict['tags']
+ #重新组织成合法的json格式
+ dict_to_json = json.dumps(tags)
+ json_tags = "{\"tags\":%s}" %(dict_to_json)
+ return json_tags
+
+if __name__ == '__main__':
+ print(tsg_get_tags_by_sn("CBT2201925000001"))