From d7f3f505e2f48feea5efd7bccd2b5d209cf35541 Mon Sep 17 00:00:00 2001 From: lijia Date: Mon, 26 Aug 2019 18:39:31 +0800 Subject: update stream and protocol. --- deploy_etc/rsyslog.conf | 96 +++++++++ py_tools/tsg_monit_app.py | 401 ++++++++++++++++++++++++++++++++++++++ py_tools/tsg_monit_protocol_v3.py | 172 ++++++++++++++++ py_tools/tsg_monit_stream.py | 401 -------------------------------------- service_script/certstore.service | 15 ++ timer/monitor.service | 6 + timer/monitor.timer | 12 ++ timer/monitor.timer.sh | 5 + timer/readme.txt | 3 + 9 files changed, 710 insertions(+), 401 deletions(-) create mode 100644 deploy_etc/rsyslog.conf create mode 100644 py_tools/tsg_monit_app.py create mode 100644 py_tools/tsg_monit_protocol_v3.py delete mode 100644 py_tools/tsg_monit_stream.py create mode 100644 service_script/certstore.service create mode 100644 timer/monitor.service create mode 100644 timer/monitor.timer create mode 100644 timer/monitor.timer.sh create mode 100644 timer/readme.txt diff --git a/deploy_etc/rsyslog.conf b/deploy_etc/rsyslog.conf new file mode 100644 index 0000000..68072af --- /dev/null +++ b/deploy_etc/rsyslog.conf @@ -0,0 +1,96 @@ +# rsyslog configuration file + +# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html +# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html + +#### MODULES #### + +# The imjournal module bellow is now used as a message source instead of imuxsock. +$ModLoad imuxsock # provides support for local system logging (e.g. via logger command) +$ModLoad imjournal # provides access to the systemd journal +#$ModLoad imklog # reads kernel messages (the same are read from journald) +#$ModLoad immark # provides --MARK-- message capability + +# Provides UDP syslog reception +#$ModLoad imudp +#$UDPServerRun 514 + +# Provides TCP syslog reception +#$ModLoad imtcp +#$InputTCPServerRun 514 + +#### GLOBAL DIRECTIVES #### + +# Where to place auxiliary files +$WorkDirectory /var/lib/rsyslog + +# Use default timestamp format +#$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat +$template tsgformat,"%$NOW% %TIMESTAMP:8:15% %HOSTNAME% <%PRI%> [%syslogseverity-text%] %msg%\n" #定义模板 $template +$ActionFileDefaultTemplate tsgformat + +# File syncing capability is disabled by default. This feature is usually not required, +# not useful and an extreme performance hit +#$ActionFileEnableSync on + +# Include all config files in /etc/rsyslog.d/ +$IncludeConfig /etc/rsyslog.d/*.conf + +# Turn off message reception via local log socket; +# local messages are retrieved through imjournal now. +$OmitLocalLogging on + +# File to store the position in the journal +$IMJournalStateFile imjournal.state + + +#### RULES #### + +# Log all kernel messages to the console. +# Logging much else clutters up the screen. +#kern.* /dev/console + +# Log anything (except mail) of level info or higher. +# Don't log private authentication messages! +*.info;mail.none;authpriv.none;cron.none /var/log/messages + +# The authpriv file has restricted access. +authpriv.* /var/log/secure + +# Log all the mail messages in one place. +mail.* -/var/log/maillog + + +# Log cron stuff +cron.* /var/log/cron + +# Everybody gets emergency messages +*.emerg :omusrmsg:* + +# Save news errors of level crit and higher in a special file. +uucp,news.crit /var/log/spooler + +# Save boot messages also to boot.log +local7.* /var/log/boot.log + + +# ### begin forwarding rule ### +# The statement between the begin ... end define a SINGLE forwarding +# rule. They belong together, do NOT split them. If you create multiple +# forwarding rules, duplicate the whole block! +# Remote Logging (we use TCP for reliable delivery) +# +# An on-disk queue is created for this action. If the remote host is +# down, messages are spooled to disk and sent when it is up again. +#$ActionQueueFileName fwdRule1 # unique name prefix for spool files +#$ActionQueueMaxDiskSpace 1g # 1gb space limit (use as much as possible) +#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown +#$ActionQueueType LinkedList # run asynchronously +$ActionResumeRetryCount -1 # infinite retries if host is down +# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional +#*.* @@remote-host:514 +#for UDP: +*.* @192.168.10.234:514 +#for TCP: +#*.* #@192.168.10.180:514 +# ### end of the forwarding rule ### diff --git a/py_tools/tsg_monit_app.py b/py_tools/tsg_monit_app.py new file mode 100644 index 0000000..ae75590 --- /dev/null +++ b/py_tools/tsg_monit_app.py @@ -0,0 +1,401 @@ +#coding=utf-8 +import argparse +import json +import prettytable +import time +import sys +import signal +import os +import telegraf +import socket +from get_traffic_by_psutil import * +from get_traffic_by_marsio import * + +from sys import path +path.append(r'../py_common') #将存放module的路径添加进来 +path.append(r'./py_common') #将存放module的路径添加进来 +from common_telegraf import * +from common_logger import * +from common_args import * +from common_logger import * +from common_get_tags import * +from common_whoami import * + +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' +G_APP_JSON_PATH = '/var/run/mrzcpd/mrmonit.app.%s' + +#TITLE_VECTOR_RX = ['RxOnline', 'RxDeliver', 'RxMissed', 'RxBits'] +#TITLE_VECTOR_TX = ['TxOnline', 'TxDeliver', 'TxMissed', 'TxBits'] +#TITLE_VECTOR_FTX = ['FTXOnline', 'FTXDeliver', 'FTXMissed', 'FTXBits'] + +TITLE_VECTOR_RX = ['RxPkts', 'RxBits', 'RxDrops'] +TITLE_VECTOR_TX = ['TxPkts', 'TxBits', 'TxDrops'] +TITLE_VECTOR_FTX = ['FTxPkts', 'FTxBits', 'FTxDrops'] + +TITLE_APP_STAT = ['PKTRx', 'PKTTx', 'MbufAlloc', 'MbufFree', 'MbufInUse'] + +TITLE_APP_MAP = { + 'PKTRx' : 'packet_recv_count', + 'PKTTx' : 'packet_send_count', + 'MbufAlloc' : 'mbuf_alloc_count', + 'MbufFree' : 'mbuf_free_count', + 'MbufInUse' : 'mbuf_in_use_count' + } + +TITLE_MAP = { 'RxOnline' : 'rx_on_line', + 'RxPkts' : 'rx_deliver', + 'RxDrops' : 'rx_missed', + 'RxBits' : 'rx_total_len', + 'TxOnline' : 'tx_on_line', + 'TxPkts' : 'tx_deliver', + 'TxDrops' : 'tx_missed', + 'TxBits' : 'tx_total_len', + 'FTXOnline' : 'ftx_on_line', + 'FTxPkts' : 'ftx_deliver', + 'FTxDrops' : 'ftx_missed', + 'FTxBits' : 'ftx_total_len' + } +# ##################################add by lijia for tsg oam +TITLE_VECTOR = ['RxPkts', 'RxBits', 'RxDrops', 'TxPkts', 'TxBits', 'TxDrops'] +udp_sock_fd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +telegraf_ip = '127.0.0.1' +telegraf_port = 8126 +# ##################################add by lijia for tsg oam + +def locate_vector_by_symbol(vector, symbol): + return [s for s in vector if s['symbol'] == symbol] + +def list_all_vdev(json_fp): + return [ s['symbol'] for s in json_fp['raw']] + +def vdev_value_read(json_fp, str_device, str_item): + phydevs = locate_vector_by_symbol(json_fp['raw'], str_device) + return phydevs[0]['stats']['accumulative'][str_item] + +def vdev_speed_read(json_fp, str_device, str_item): + phydevs = locate_vector_by_symbol(json_fp['raw'], str_device) + return phydevs[0]['stats']['speed'][str_item] + +def vdev_streams_read(json_fp, str_device): + phydevs = locate_vector_by_symbol(json_fp['raw'], str_device) + return phydevs[0]['rxstreams'], phydevs[0]['txstreams'] + +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 vec_trans_to_human_readable(vec): + r_vector = [] + for value in vec: + h_value, h_value_unit = trans_to_human_readable(value) + r_vector.append('%7.2f%c' % (h_value, h_value_unit)) + + return r_vector + +def dump_one_device(json_fp, devsym, title_vector_rx, title_vector_tx, speed): + + __rd_function = vdev_value_read if speed == 0 else vdev_speed_read + + ValueListSum = [0] * len(title_vector_rx + title_vector_tx) + nr_rxstream, nr_txstream = vdev_streams_read(json_fp, devsym) + + for stream_id in range(max(nr_rxstream, nr_txstream)): + ValueList = [] + + for item in title_vector_rx: + value = __rd_function(json_fp, devsym, TITLE_MAP[item])[stream_id] \ + if stream_id < nr_rxstream else 0 + ValueList.append(value) + + for item in title_vector_tx: + value = __rd_function(json_fp, devsym, TITLE_MAP[item])[stream_id] \ + if stream_id < nr_txstream else 0 + ValueList.append(value) + + for i,v in enumerate(ValueList): + ValueListSum[i] += v + + return ValueListSum + +def dump_summary_table(json_fp, appsym, devsym, title_vector_rx, title_vector_tx, + is_human_number = 0, speed = 1): + + print('\nTime: %s, App: %s' % (time.strftime('%c'), appsym)) + table_phydev = prettytable.PrettyTable([' '] + title_vector_rx + title_vector_tx, + vertical_char=' ',horizontal_char = '-', junction_char=' ') + + for item in[' '] + title_vector_rx + title_vector_tx: + table_phydev.align[item] = 'r' + + ValueListTotal = [0] * len(title_vector_rx + title_vector_tx) + + for dev in devsym: + ValueListSum = dump_one_device(json_fp, dev, title_vector_rx, title_vector_tx, speed) + + for i,v in enumerate(ValueListSum): + ValueListTotal[i] += v + + if is_human_number: + table_phydev.add_row([dev] + vec_trans_to_human_readable(ValueListSum)) + else: + table_phydev.add_row([dev] + ValueListSum) + + if is_human_number: + table_phydev.add_row(['Total'] + vec_trans_to_human_readable(ValueListTotal)) + else: + table_phydev.add_row(['Total'] + ValueListTotal) + + print(table_phydev) + + + +def dump_human_table(json_fp, appsym, devsym, title_vector_rx, title_vector_tx, + is_human_number = 0, speed = 1): + print("##### into dump_human_table") + print('\nTime: %s, App: %s, Device: %s ' % (time.strftime('%c'), appsym, devsym)) + + table_phydev = prettytable.PrettyTable([' '] + title_vector_rx + title_vector_tx, + vertical_char=' ',horizontal_char = '-', junction_char=' ') + + __rd_function = vdev_value_read if speed == 0 else vdev_speed_read + + for item in[' '] + title_vector_rx + title_vector_tx: + table_phydev.align[item] = 'r' + + ValueListSum = [0] * len(title_vector_rx + title_vector_tx) + nr_rxstream, nr_txstream = vdev_streams_read(json_fp, devsym) + + for stream_id in range(max(nr_rxstream, nr_txstream)): + ValueList = [] + + for item in title_vector_rx: + value = __rd_function(json_fp, devsym, TITLE_MAP[item])[stream_id] \ + if stream_id < nr_rxstream else 0 + ValueList.append(value) + + for item in title_vector_tx: + value = __rd_function(json_fp, devsym, TITLE_MAP[item])[stream_id] \ + if stream_id < nr_txstream else 0 + ValueList.append(value) + + str_leader = '' + str_leader += 'RX[%d]' % stream_id if stream_id < nr_rxstream else '' + str_leader += 'TX[%d]' % stream_id if stream_id < nr_txstream else '' + + if is_human_number: + table_phydev.add_row([str_leader] + vec_trans_to_human_readable(ValueList)) + else: + table_phydev.add_row([str_leader] + ValueList) + + for i,v in enumerate(ValueList): + ValueListSum[i] += v + + if is_human_number: + table_phydev.add_row(['Total'] + vec_trans_to_human_readable(ValueListSum)) + else: + table_phydev.add_row(['Total'] + ValueListSum) + + print(table_phydev) + + +def dump_status_table(json_fp, appsym): + print("##### into dump_status_table") + json_fp_appstat = json_fp['appstat'] + nr_stream = len(json_fp['appstat']['packet_recv_count']) + + print('\nTime: %s, App: %s' % (time.strftime('%c'), appsym)) + table_phydev = prettytable.PrettyTable(['TID'] + TITLE_APP_STAT, + vertical_char=' ',horizontal_char = '-', junction_char=' ') + + for item in['TID'] + TITLE_APP_STAT: + table_phydev.align[item] = 'r' + + ValueListSum = [0] * len(TITLE_APP_STAT) + + for tid in range(nr_stream): + ValueList = [] + for item in TITLE_APP_STAT: + value = json_fp_appstat[TITLE_APP_MAP[item]][tid] + ValueList.append(value) + + table_phydev.add_row([tid] + ValueList) + for i,v in enumerate(ValueList): + ValueListSum[i] += v + + table_phydev.add_row(['Total'] + ValueListSum) + print(table_phydev) + +def dump_apm_sendlog(json_fp, telegraf_client, appsym, user_interface, title_vector_rx, title_vector_tx): + + for dev in user_interface: + ValueListSumSpeed = dump_one_device(json_fp, dev, title_vector_rx, title_vector_tx, 1) + #ValueListSumValue = dump_one_device(json_fp, dev, title_vector_rx, title_vector_tx, 0) + + sendlog_dict_speed = {} + #sendlog_dict_value = {} + sendlog_tag = {'appname': appsym, 'device': dev, 'sled' : node_name} + + for id, value in enumerate(title_vector_rx + title_vector_tx): + sendlog_dict_speed[value] = int(ValueListSumSpeed[id]) + #sendlog_dict_value[value] = ValueListSumValue[id] + #print(sendlog_dict_speed) + telegraf_client.metric('app', sendlog_dict_speed, tags = sendlog_tag) + #telegraf_client.metric('app', sendlog_dict_value, tags = sendlog_tag) + + return + + +def global_json_load(): + with open(G_JSON_PATH) as json_fp: + return json.load(json_fp) + +def app_json_load(appsym): + with open(G_APP_JSON_PATH % appsym) as json_fp: + return json.load(json_fp) + +def app_symbol_load(): + j_global = global_json_load() + return [s["symbol"] for s in j_global["app"] if s["registed"] == 1] + +def sigint_handler(handler, frame): + sys.exit(0) + +def check_vdev_options(json_fp, r_option): + + if r_option.interface == None: + return + + vdev_list = list_all_vdev(json_fp) + for devsym in r_option.interface: + if devsym not in vdev_list: + print("monit_stream: error: argument -i/--interface: invalid interface.") + sys.exit(1) + +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 app_setup_argv_parser(applist): + + parser = setup_common_args() + + #add monit_stream custom args + parser.add_argument('app', metavar='APP', help = 'the name of slave application', nargs = '*', default=applist) + parser.add_argument('-i', '--interface', help = 'the name of network interface', + action = 'append') + parser.add_argument('-m', '--metrics', help = 'group of metrics', choices=['rx','tx','ftx'], + default = ['rx','tx']) + parser.add_argument('--status', help = 'print application running status', + action='store_true', default = 0) + + return parser.parse_args() + +def get_and_send_app_traffic(): + signal.signal(signal.SIGINT, sigint_handler) + + # Check Parameters + try: + applist = app_symbol_load() + #print("##### %s" %(applist)) + if len(applist) == 0: + print("monit_stream: error: no running application.") + sys.exit(1) + + r_option = app_setup_argv_parser(applist) + + #for appsym in r_option.app: + # __json_fp = app_json_load(appsym) + + except IOError as err: + print("%s, program %s is not running." % (str(err), appsym)) + sys.exit(1) + + title_vector_rx = [] + title_vector_tx = [] + if 'rx' in r_option.metrics: + title_vector_rx.extend(TITLE_VECTOR_RX) + if 'tx' in r_option.metrics: + title_vector_tx.extend(TITLE_VECTOR_TX) + + try: + while True: + for appsym in r_option.app: + if appsym.find('sapp') == -1: + #only interesting sapp traffic + continue + json_fp = app_json_load(appsym) + check_vdev_options(json_fp, r_option) + user_interface = r_option.interface if r_option.interface != None else list_all_vdev(json_fp) + + if r_option.status: + dump_status_table(json_fp, appsym) + continue + + dump_apm_sendlog(json_fp, telegraf_client, appsym, user_interface, + title_vector_rx, title_vector_tx) + + if r_option.debug: + dump_summary_table(json_fp, appsym, user_interface, title_vector_rx, title_vector_tx, + 0, 1) + + #if not r_option.loop: + break + time.sleep(r_option.time) + + except KeyboardInterrupt: + pass + except ValueError as err: + print(("%s, perhaps program is not running.") % str(err)) + except IOError as err: + print(("%s, perhaps program is not running.") % str(err)) + + return 0 + + +if __name__ == '__main__': + global node_name + global telegraf_client + global logger + + arg_options = setup_common_args().parse_args() + + logger = logger_init(arg_options.log_level) + + telegraf_server_ip = arg_options.telegraf_ip + telegraf_server_port = int(arg_options.telegraf_port) + + telegraf_tags = tsg_get_tags() + if len(telegraf_tags) <= 0: + logger.info("not found telegraf tags.") + + telegraf_client = telegraf_init(telegraf_server_ip, telegraf_server_port, telegraf_tags) + + type, id, node_name = tsg_whoami() + + get_and_send_app_traffic() \ No newline at end of file diff --git a/py_tools/tsg_monit_protocol_v3.py b/py_tools/tsg_monit_protocol_v3.py new file mode 100644 index 0000000..75bfd5c --- /dev/null +++ b/py_tools/tsg_monit_protocol_v3.py @@ -0,0 +1,172 @@ +#coding=utf-8 +#本文件获取kni fs2日志, 抽取可拦截、已拦截计数, 存入influxDB +import sys +import time +import re +import subprocess +from sys import path +path.append(r'../py_common') #将存放module的路径添加进来 +path.append(r'./py_common') #将存放module的路径添加进来 +from common_get_tags import * +from common_whoami import * +from common_system_cmd import * +from common_telegraf import * +from common_args import * +from common_logger import * +from common_get_tags import * + +G_KNI_FS2_FILE = "/home/tsg/kni/fs2_sysinfo.log" + +def find_expect_word_index(line_array, expect_word): + index = 0 + for column in line_array: + if column == expect_word: + return index + else: + index += 1 + + return -1 + +#根据fs2.log, 提取expect_word的sum和speed值 +#return value: +#ret, sum, speed +def get_sum_speed_from_fs2_matrix(filename, expect_word): + cmd_str = "cat %s | grep -A 2 %s" %(filename, expect_word) + ret, result = system_cmd_run(cmd_str) + if ret != 0: + print("no result for cmd: %s" %(cmd_str)) + return 1, 0, 0 + + res = result.split('\n') + #print(res) + + if len(res) != 3: + print("result lines is not 3!" %(res)) + return 1, 0, 0 + + line = res[0].split() + index = find_expect_word_index(line, expect_word) + #后面的行有sum, speed, 多一列 + index += 1 + + line = res[1].split() + sum = int(line[index]) + + line = res[2].split() + speed = int(line[index]) + + return 0, sum, speed + +def kni_protocol_init(): + global telegraf_client + + comm_arg_parser = setup_common_args() + arg_options = comm_arg_parser.parse_args() + + telegraf_server_ip = arg_options.telegraf_ip + telegraf_server_port = int(arg_options.telegraf_port) + telegraf_tags = tsg_get_tags() + + telegraf_client = telegraf_init(telegraf_server_ip, telegraf_server_port, telegraf_tags) + + return 0 + +def kni_protocol_fs2_stats(): + metric_val = {} + + key_word = "Ethernet_bps" + influxdb_field = "Eth_Bit" + + ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_FS2_FILE, key_word) + if ret != 0: + print("get %s stat error" %(key_word)) + sys.exit(1) + metric_val[influxdb_field] = speed + + key_word = "Ethernet_pps" + influxdb_field = "Eth_Pkt" + ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_FS2_FILE, key_word) + if ret != 0: + print("get %s stat error" %(key_word)) + sys.exit(1) + metric_val[influxdb_field] = speed + + key_word = "IPv4_bps" + influxdb_field = "Ipv4_Bit" + ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_FS2_FILE, key_word) + if ret != 0: + print("get %s stat error" %(key_word)) + sys.exit(1) + metric_val[influxdb_field] = speed + + key_word = "IPv4_pps" + influxdb_field = "Ipv4_Pkt" + ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_FS2_FILE, key_word) + if ret != 0: + print("get %s stat error" %(key_word)) + sys.exit(1) + metric_val[influxdb_field] = speed + + key_word = "IPv6_bps" + influxdb_field = "Ipv6_Bit" + ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_FS2_FILE, key_word) + if ret != 0: + print("get %s stat error" %(key_word)) + sys.exit(1) + metric_val[influxdb_field] = speed + + key_word = "IPv6_pps" + influxdb_field = "Ipv6_Pkt" + ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_FS2_FILE, key_word) + if ret != 0: + print("get %s stat error" %(key_word)) + sys.exit(1) + metric_val[influxdb_field] = speed + + key_word = "TCP_bps" + influxdb_field = "Tcp_Bit" + ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_FS2_FILE, key_word) + if ret != 0: + print("get %s stat error" %(key_word)) + sys.exit(1) + metric_val[influxdb_field] = speed + + key_word = "TCP_pps" + influxdb_field = "Tcp_Pkt" + ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_FS2_FILE, key_word) + if ret != 0: + print("get %s stat error" %(key_word)) + sys.exit(1) + metric_val[influxdb_field] = speed + + key_word = "UDP_pps" + influxdb_field = "Udp_Bit" + ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_FS2_FILE, key_word) + if ret != 0: + print("get %s stat error" %(key_word)) + sys.exit(1) + metric_val[influxdb_field] = speed + + key_word = "UDP_bps" + influxdb_field = "Udp_Pkt" + ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_FS2_FILE, key_word) + if ret != 0: + print("get %s stat error" %(key_word)) + sys.exit(1) + metric_val[influxdb_field] = speed + + #print(metric_val) + return metric_val + + +if __name__ == '__main__': + kni_protocol_init() + metric_val = kni_protocol_fs2_stats() + + telegraf_client.metric('protocol', metric_val, tags = {}) + + + + + + \ No newline at end of file diff --git a/py_tools/tsg_monit_stream.py b/py_tools/tsg_monit_stream.py deleted file mode 100644 index ae75590..0000000 --- a/py_tools/tsg_monit_stream.py +++ /dev/null @@ -1,401 +0,0 @@ -#coding=utf-8 -import argparse -import json -import prettytable -import time -import sys -import signal -import os -import telegraf -import socket -from get_traffic_by_psutil import * -from get_traffic_by_marsio import * - -from sys import path -path.append(r'../py_common') #将存放module的路径添加进来 -path.append(r'./py_common') #将存放module的路径添加进来 -from common_telegraf import * -from common_logger import * -from common_args import * -from common_logger import * -from common_get_tags import * -from common_whoami import * - -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' -G_APP_JSON_PATH = '/var/run/mrzcpd/mrmonit.app.%s' - -#TITLE_VECTOR_RX = ['RxOnline', 'RxDeliver', 'RxMissed', 'RxBits'] -#TITLE_VECTOR_TX = ['TxOnline', 'TxDeliver', 'TxMissed', 'TxBits'] -#TITLE_VECTOR_FTX = ['FTXOnline', 'FTXDeliver', 'FTXMissed', 'FTXBits'] - -TITLE_VECTOR_RX = ['RxPkts', 'RxBits', 'RxDrops'] -TITLE_VECTOR_TX = ['TxPkts', 'TxBits', 'TxDrops'] -TITLE_VECTOR_FTX = ['FTxPkts', 'FTxBits', 'FTxDrops'] - -TITLE_APP_STAT = ['PKTRx', 'PKTTx', 'MbufAlloc', 'MbufFree', 'MbufInUse'] - -TITLE_APP_MAP = { - 'PKTRx' : 'packet_recv_count', - 'PKTTx' : 'packet_send_count', - 'MbufAlloc' : 'mbuf_alloc_count', - 'MbufFree' : 'mbuf_free_count', - 'MbufInUse' : 'mbuf_in_use_count' - } - -TITLE_MAP = { 'RxOnline' : 'rx_on_line', - 'RxPkts' : 'rx_deliver', - 'RxDrops' : 'rx_missed', - 'RxBits' : 'rx_total_len', - 'TxOnline' : 'tx_on_line', - 'TxPkts' : 'tx_deliver', - 'TxDrops' : 'tx_missed', - 'TxBits' : 'tx_total_len', - 'FTXOnline' : 'ftx_on_line', - 'FTxPkts' : 'ftx_deliver', - 'FTxDrops' : 'ftx_missed', - 'FTxBits' : 'ftx_total_len' - } -# ##################################add by lijia for tsg oam -TITLE_VECTOR = ['RxPkts', 'RxBits', 'RxDrops', 'TxPkts', 'TxBits', 'TxDrops'] -udp_sock_fd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) -telegraf_ip = '127.0.0.1' -telegraf_port = 8126 -# ##################################add by lijia for tsg oam - -def locate_vector_by_symbol(vector, symbol): - return [s for s in vector if s['symbol'] == symbol] - -def list_all_vdev(json_fp): - return [ s['symbol'] for s in json_fp['raw']] - -def vdev_value_read(json_fp, str_device, str_item): - phydevs = locate_vector_by_symbol(json_fp['raw'], str_device) - return phydevs[0]['stats']['accumulative'][str_item] - -def vdev_speed_read(json_fp, str_device, str_item): - phydevs = locate_vector_by_symbol(json_fp['raw'], str_device) - return phydevs[0]['stats']['speed'][str_item] - -def vdev_streams_read(json_fp, str_device): - phydevs = locate_vector_by_symbol(json_fp['raw'], str_device) - return phydevs[0]['rxstreams'], phydevs[0]['txstreams'] - -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 vec_trans_to_human_readable(vec): - r_vector = [] - for value in vec: - h_value, h_value_unit = trans_to_human_readable(value) - r_vector.append('%7.2f%c' % (h_value, h_value_unit)) - - return r_vector - -def dump_one_device(json_fp, devsym, title_vector_rx, title_vector_tx, speed): - - __rd_function = vdev_value_read if speed == 0 else vdev_speed_read - - ValueListSum = [0] * len(title_vector_rx + title_vector_tx) - nr_rxstream, nr_txstream = vdev_streams_read(json_fp, devsym) - - for stream_id in range(max(nr_rxstream, nr_txstream)): - ValueList = [] - - for item in title_vector_rx: - value = __rd_function(json_fp, devsym, TITLE_MAP[item])[stream_id] \ - if stream_id < nr_rxstream else 0 - ValueList.append(value) - - for item in title_vector_tx: - value = __rd_function(json_fp, devsym, TITLE_MAP[item])[stream_id] \ - if stream_id < nr_txstream else 0 - ValueList.append(value) - - for i,v in enumerate(ValueList): - ValueListSum[i] += v - - return ValueListSum - -def dump_summary_table(json_fp, appsym, devsym, title_vector_rx, title_vector_tx, - is_human_number = 0, speed = 1): - - print('\nTime: %s, App: %s' % (time.strftime('%c'), appsym)) - table_phydev = prettytable.PrettyTable([' '] + title_vector_rx + title_vector_tx, - vertical_char=' ',horizontal_char = '-', junction_char=' ') - - for item in[' '] + title_vector_rx + title_vector_tx: - table_phydev.align[item] = 'r' - - ValueListTotal = [0] * len(title_vector_rx + title_vector_tx) - - for dev in devsym: - ValueListSum = dump_one_device(json_fp, dev, title_vector_rx, title_vector_tx, speed) - - for i,v in enumerate(ValueListSum): - ValueListTotal[i] += v - - if is_human_number: - table_phydev.add_row([dev] + vec_trans_to_human_readable(ValueListSum)) - else: - table_phydev.add_row([dev] + ValueListSum) - - if is_human_number: - table_phydev.add_row(['Total'] + vec_trans_to_human_readable(ValueListTotal)) - else: - table_phydev.add_row(['Total'] + ValueListTotal) - - print(table_phydev) - - - -def dump_human_table(json_fp, appsym, devsym, title_vector_rx, title_vector_tx, - is_human_number = 0, speed = 1): - print("##### into dump_human_table") - print('\nTime: %s, App: %s, Device: %s ' % (time.strftime('%c'), appsym, devsym)) - - table_phydev = prettytable.PrettyTable([' '] + title_vector_rx + title_vector_tx, - vertical_char=' ',horizontal_char = '-', junction_char=' ') - - __rd_function = vdev_value_read if speed == 0 else vdev_speed_read - - for item in[' '] + title_vector_rx + title_vector_tx: - table_phydev.align[item] = 'r' - - ValueListSum = [0] * len(title_vector_rx + title_vector_tx) - nr_rxstream, nr_txstream = vdev_streams_read(json_fp, devsym) - - for stream_id in range(max(nr_rxstream, nr_txstream)): - ValueList = [] - - for item in title_vector_rx: - value = __rd_function(json_fp, devsym, TITLE_MAP[item])[stream_id] \ - if stream_id < nr_rxstream else 0 - ValueList.append(value) - - for item in title_vector_tx: - value = __rd_function(json_fp, devsym, TITLE_MAP[item])[stream_id] \ - if stream_id < nr_txstream else 0 - ValueList.append(value) - - str_leader = '' - str_leader += 'RX[%d]' % stream_id if stream_id < nr_rxstream else '' - str_leader += 'TX[%d]' % stream_id if stream_id < nr_txstream else '' - - if is_human_number: - table_phydev.add_row([str_leader] + vec_trans_to_human_readable(ValueList)) - else: - table_phydev.add_row([str_leader] + ValueList) - - for i,v in enumerate(ValueList): - ValueListSum[i] += v - - if is_human_number: - table_phydev.add_row(['Total'] + vec_trans_to_human_readable(ValueListSum)) - else: - table_phydev.add_row(['Total'] + ValueListSum) - - print(table_phydev) - - -def dump_status_table(json_fp, appsym): - print("##### into dump_status_table") - json_fp_appstat = json_fp['appstat'] - nr_stream = len(json_fp['appstat']['packet_recv_count']) - - print('\nTime: %s, App: %s' % (time.strftime('%c'), appsym)) - table_phydev = prettytable.PrettyTable(['TID'] + TITLE_APP_STAT, - vertical_char=' ',horizontal_char = '-', junction_char=' ') - - for item in['TID'] + TITLE_APP_STAT: - table_phydev.align[item] = 'r' - - ValueListSum = [0] * len(TITLE_APP_STAT) - - for tid in range(nr_stream): - ValueList = [] - for item in TITLE_APP_STAT: - value = json_fp_appstat[TITLE_APP_MAP[item]][tid] - ValueList.append(value) - - table_phydev.add_row([tid] + ValueList) - for i,v in enumerate(ValueList): - ValueListSum[i] += v - - table_phydev.add_row(['Total'] + ValueListSum) - print(table_phydev) - -def dump_apm_sendlog(json_fp, telegraf_client, appsym, user_interface, title_vector_rx, title_vector_tx): - - for dev in user_interface: - ValueListSumSpeed = dump_one_device(json_fp, dev, title_vector_rx, title_vector_tx, 1) - #ValueListSumValue = dump_one_device(json_fp, dev, title_vector_rx, title_vector_tx, 0) - - sendlog_dict_speed = {} - #sendlog_dict_value = {} - sendlog_tag = {'appname': appsym, 'device': dev, 'sled' : node_name} - - for id, value in enumerate(title_vector_rx + title_vector_tx): - sendlog_dict_speed[value] = int(ValueListSumSpeed[id]) - #sendlog_dict_value[value] = ValueListSumValue[id] - #print(sendlog_dict_speed) - telegraf_client.metric('app', sendlog_dict_speed, tags = sendlog_tag) - #telegraf_client.metric('app', sendlog_dict_value, tags = sendlog_tag) - - return - - -def global_json_load(): - with open(G_JSON_PATH) as json_fp: - return json.load(json_fp) - -def app_json_load(appsym): - with open(G_APP_JSON_PATH % appsym) as json_fp: - return json.load(json_fp) - -def app_symbol_load(): - j_global = global_json_load() - return [s["symbol"] for s in j_global["app"] if s["registed"] == 1] - -def sigint_handler(handler, frame): - sys.exit(0) - -def check_vdev_options(json_fp, r_option): - - if r_option.interface == None: - return - - vdev_list = list_all_vdev(json_fp) - for devsym in r_option.interface: - if devsym not in vdev_list: - print("monit_stream: error: argument -i/--interface: invalid interface.") - sys.exit(1) - -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 app_setup_argv_parser(applist): - - parser = setup_common_args() - - #add monit_stream custom args - parser.add_argument('app', metavar='APP', help = 'the name of slave application', nargs = '*', default=applist) - parser.add_argument('-i', '--interface', help = 'the name of network interface', - action = 'append') - parser.add_argument('-m', '--metrics', help = 'group of metrics', choices=['rx','tx','ftx'], - default = ['rx','tx']) - parser.add_argument('--status', help = 'print application running status', - action='store_true', default = 0) - - return parser.parse_args() - -def get_and_send_app_traffic(): - signal.signal(signal.SIGINT, sigint_handler) - - # Check Parameters - try: - applist = app_symbol_load() - #print("##### %s" %(applist)) - if len(applist) == 0: - print("monit_stream: error: no running application.") - sys.exit(1) - - r_option = app_setup_argv_parser(applist) - - #for appsym in r_option.app: - # __json_fp = app_json_load(appsym) - - except IOError as err: - print("%s, program %s is not running." % (str(err), appsym)) - sys.exit(1) - - title_vector_rx = [] - title_vector_tx = [] - if 'rx' in r_option.metrics: - title_vector_rx.extend(TITLE_VECTOR_RX) - if 'tx' in r_option.metrics: - title_vector_tx.extend(TITLE_VECTOR_TX) - - try: - while True: - for appsym in r_option.app: - if appsym.find('sapp') == -1: - #only interesting sapp traffic - continue - json_fp = app_json_load(appsym) - check_vdev_options(json_fp, r_option) - user_interface = r_option.interface if r_option.interface != None else list_all_vdev(json_fp) - - if r_option.status: - dump_status_table(json_fp, appsym) - continue - - dump_apm_sendlog(json_fp, telegraf_client, appsym, user_interface, - title_vector_rx, title_vector_tx) - - if r_option.debug: - dump_summary_table(json_fp, appsym, user_interface, title_vector_rx, title_vector_tx, - 0, 1) - - #if not r_option.loop: - break - time.sleep(r_option.time) - - except KeyboardInterrupt: - pass - except ValueError as err: - print(("%s, perhaps program is not running.") % str(err)) - except IOError as err: - print(("%s, perhaps program is not running.") % str(err)) - - return 0 - - -if __name__ == '__main__': - global node_name - global telegraf_client - global logger - - arg_options = setup_common_args().parse_args() - - logger = logger_init(arg_options.log_level) - - telegraf_server_ip = arg_options.telegraf_ip - telegraf_server_port = int(arg_options.telegraf_port) - - telegraf_tags = tsg_get_tags() - if len(telegraf_tags) <= 0: - logger.info("not found telegraf tags.") - - telegraf_client = telegraf_init(telegraf_server_ip, telegraf_server_port, telegraf_tags) - - type, id, node_name = tsg_whoami() - - get_and_send_app_traffic() \ No newline at end of file diff --git a/service_script/certstore.service b/service_script/certstore.service new file mode 100644 index 0000000..f0493c1 --- /dev/null +++ b/service_script/certstore.service @@ -0,0 +1,15 @@ +[Unit] +Description=certstore module +Requires=mrtunnat.service +After=mrtunnat.service + +[Service] +WorkingDirectory=/home/ceiec/certstore +ExecStart=/home/ceiec/certstore/certstore --normal +#ExecStop=/home/tsg/kni/kni_service_scipt stop +#Type=forking +Restart=always +RestartSec=5s + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/timer/monitor.service b/timer/monitor.service new file mode 100644 index 0000000..30ba829 --- /dev/null +++ b/timer/monitor.service @@ -0,0 +1,6 @@ +[Unit] +Description=monitor service + +[Service] +Type=simple +ExecStart=/opt/tsg/monitor/monitor.timer.sh diff --git a/timer/monitor.timer b/timer/monitor.timer new file mode 100644 index 0000000..fc1b8c0 --- /dev/null +++ b/timer/monitor.timer @@ -0,0 +1,12 @@ +[Unit] +Description=run monitor every 10s + +[Timer] +# Time to wait after enable this unit +OnActiveSec=1 +# Time between running each consecutive time +OnUnitActiveSec=10 +Unit=monitor.service + +[Install] +WantedBy=multi-user.target diff --git a/timer/monitor.timer.sh b/timer/monitor.timer.sh new file mode 100644 index 0000000..9df9ac2 --- /dev/null +++ b/timer/monitor.timer.sh @@ -0,0 +1,5 @@ +#!/bin/sh +/opt/tsg/bin/tsg_update_tags +/opt/tsg/bin/tsg_monit_device --telegraf-ip 127.0.0.1 --telegraf-port 8126 +/opt/tsg/bin/tsg_monit_stream --telegraf-ip 127.0.0.1 --telegraf-port 8126 +echo "monitor" >> /tmp/moniotr.log diff --git a/timer/readme.txt b/timer/readme.txt new file mode 100644 index 0000000..4fe8655 --- /dev/null +++ b/timer/readme.txt @@ -0,0 +1,3 @@ +timerֳ֣ +һִ˳ģ +һҪsleepһᣬֱȡֵģifconfigspeed, syslogspeed \ No newline at end of file -- cgit v1.2.3