add stream

This commit is contained in:
lijia
2019-08-26 18:40:04 +08:00
parent d7f3f505e2
commit 6f0fd1189a

View File

@@ -0,0 +1,148 @@
#coding=utf-8
#本文件获取kni, sapp->fs2.log日志, 抽取可拦截、已拦截计数, 存入influxDB->stream
#syslog, fs2.stat有其自己的输出间隔, 如果是每秒输出1次的话, 本模块只能读到最后一个值, 是不对的!!
#本模块读取这些变量时, 应该是读两次, 分别记录两次的sum, 然后除以时间间隔,
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, 提取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_LINK_NEW"
influxdb_field = "Tcp_Link_New"
ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_SAPP_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_LINK_DEL"
influxdb_field = "Tcp_Link_Del"
ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_SAPP_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_CONCURRENT"
influxdb_field = "Tcp_Concurrent"
ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_SAPP_FS2_FILE, key_word)
if ret != 0:
print("get %s stat error" %(key_word))
sys.exit(1)
#TCP_CONCURRENT 只有当前瞬时值有意义, 使用sum
metric_val[influxdb_field] = sum
key_word = "TCP_LINK_DOUBLE"
influxdb_field = "Tcp_Link_Double"
ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_SAPP_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_LINK_C2S"
influxdb_field = "Tcp_Link_C2S"
ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_SAPP_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_LINK_S2C"
influxdb_field = "Tcp_Link_S2C"
ret, sum, speed = get_sum_speed_from_fs2_matrix(G_KNI_SAPP_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_stream_init()
metric_val = kni_sapp_fs2_stats()
telegraf_client.metric('stream', metric_val, tags = {})