#coding=utf-8 #本文件获取kni, sapp->fs2.log日志, 输出tcp流信息 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_SAPP_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, 从status中提取expect_word的sum和speed值 def get_value_from_fs2_status(filename, expect_word): cmd_str = "cat %s | grep %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 line_res = result.split('\n') line_res = result.split('\t') for t1 in line_res: #print(t1) t2 = t1.split(':') if(t2[0] == expect_word): print("%s = %d" %(t2[0], int(t2[1]))) return 0, int(t2[1]) return 1, 0 #根据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_stream_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_sapp_fs2_stats(): metric_val = {} key_word = "Tcp_Concurrent" influxdb_field = "Tcp_Concurrent" ret, value = get_value_from_fs2_status(G_KNI_SAPP_FS2_FILE, key_word) if ret != 0: print("get %s stat error" %(key_word)) sys.exit(1) metric_val[influxdb_field] = value key_word = "Tcp_Link_Double" influxdb_field = "Tcp_Link_Double" ret, value = get_value_from_fs2_status(G_KNI_SAPP_FS2_FILE, key_word) if ret != 0: print("get %s stat error" %(key_word)) sys.exit(1) metric_val[influxdb_field] = value print(metric_val) return metric_val if __name__ == '__main__': kni_stream_init() metric_val = kni_sapp_fs2_stats() telegraf_client.metric('stream', metric_val, tags = {})