夜莺监控自定义上报数据(v3)
什么是自定义上报数据?
自定义上报数据就是编写完插件后,不通过agent进行采集上报,而是直接通过脚本内部定义上报数据的接口从而实现数据上报,自定义上报数据一般适用于 无法安装agent的机器,比如部分linux或者windows系统无法安装agent ,此时就可以通过自定义上报数据的方式来实现监控
Nightingale的transfer和agent两个模块都提供了上报数据的接口:
- 本地agent上报接口:
POST http://${agent.addr}/v1/push - 中心transfer上报接口:
POST http://${transfer.addr}/api/transfer/push
1、下面的例子使用本地agent上报数据,首先编写插件,内容如下:
#!/usr/bin/python3.6
# -*- coding:utf-8 -*-
'''
本插件的功能为获取两个文件夹gong和guan的时间,并与系统时间做对比,如果大于系统时间超过40分钟,则触发告警
'''
import time
import datetime
import math
import urllib3
http = urllib3.PoolManager()
import json
import requests
import subprocess #python执行shell用此模块,python2用commands模块
def check_time():
'''
@本脚本主要功能为获取两个文件夹时间与系统时间做对比
如果时间差大于40分钟,则告警
@author: gongguan
@time: 20210529
'''
result = []
value = 0
value1 = 0
ip = subprocess.getoutput('''ifconfig `route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|head -n 1''')
timestamp = int(time.time())
date_time = datetime.datetime.now() #获取当前系统时间,注意此时的类型为data_time日期类型
sys_time = str(date_time).split(' ')[1] #如果要用split切割,需要先转换为字符串才能切割
sys_hour = sys_time.split(':')[0] #获取系统的小时
sys_min = sys_time.split(':')[1] #获取系统的分钟数
#gong_modify_time = subprocess.getoutput('''stat gong | grep Modify | awk '{print $3}' |awk -F "." '{print $1}' ''')
gong_hour = subprocess.getoutput(''' stat gong | grep Modify | awk '{print $3}' |awk -F "." '{print $1}' |awk -F ':' '{print $1}' ''')
gong_min=subprocess.getoutput(''' stat gong | grep Modify | awk '{print $3}' |awk -F "." '{print $1}' |awk -F ':' '{print $2}' ''')
guan_hour = subprocess.getoutput(''' stat guan | grep Modify | awk '{print $3}' |awk -F "." '{print $1}' |awk -F ':' '{print $1}' ''')
guan_min = subprocess.getoutput(''' stat guan | grep Modify | awk '{print $3}' |awk -F "." '{print $1}' |awk -F ':' '{print $2}' ''')
#接下来将获取到的文件夹时间与系统的时间做对比并执行逻辑
if (abs(int(sys_hour) - int(gong_hour)) == 23 ) and ((60 - int(sys_min) + int(gong_min)) <= 40):
value = 1
elif (abs(int(sys_hour) - int(gong_hour)) == 0 ) and ( abs(int(sys_min) - int(gong_min)) <= 40 ):
value = 1
else:
value = 0
gong_time = {
'endpoint': ip,
'tags': '',
'timestamp': timestamp,
'metric': 'sfairone.time',
'value': value,
'step': 10,
'extra': "helloworld",
}
result.append(gong_time)
#接下来获取guan文件夹时间,与系统时间做对比并执行逻辑
if (abs(int(sys_hour) - int(guan_hour)) == 23 ) and ((60 - int(sys_min) + int(guan_min)) <= 40):
value1 = 1
elif (abs(int(sys_hour) - int(guan_hour)) == 0 ) and ( abs(int(sys_min) - int(guan_min)) <= 40 ):
value1 = 1
else:
value1 = 0
guan_time = {
'endpoint': ip, #本机IP为:10.88.22.253
'tags': '',
'timestamp': timestamp,
'metric': 'sfairtwo.time',
'value': value1,
'step': 10,
'extra': "nihao",
}
result.append(guan_time)
check_result = json.dumps(result)
#print(check_result)
#url = "http://10.88.22.183:8008/api/transfer/push" #通过transfer接口上报
url = "http://127.0.0.1:2080/v1/push" #定义上报接口,通过agent方式
r = requests.post(url=url , data=check_result)
#print(r.text)
if __name__ == '__main__':
check_time()
从上图中可以看出,定义的url上报接口地址为本机的agent地址和端口 ,然后通过Post请求方式将数据上报给服务端
注意:上报方式为post请求,通过python的requests模块发送post请求,可编写crontab方式来执行脚本实现定时执行
在本机告警指标中搜索sfairone.time,可以看到指标已经出来,如图:


2、下面的例子通过中心transfer的方式来实现数据上报,python脚本代码如下:
#!/usr/bin/python3.6
# -*- coding:utf-8 -*-
import time
import datetime
import math
import urllib3
http = urllib3.PoolManager()
import json
import requests
import subprocess #python执行shell用此模块,python2用commands模块
def check_time():
'''
@本脚本主要功能为获取两个文件夹时间与系统时间做对比
如果时间差大于40分钟,则告警
@author: gongguan
@time: 20210529
'''
result = []
value = 0
value1 = 0
ip = subprocess.getoutput('''ifconfig `route|grep '^default'|awk '{print $NF}'`|grep inet|awk '{print $2}'|head -n 1''')
timestamp = int(time.time())
date_time = datetime.datetime.now() #获取当前系统时间,注意此时的类型为data_time日期类型
sys_time = str(date_time).split(' ')[1] #如果要用split切割,需要先转换为字符串才能切割
sys_hour = sys_time.split(':')[0] #获取系统的小时
sys_min = sys_time.split(':')[1] #获取系统的分钟数
#gong_modify_time = subprocess.getoutput('''stat gong | grep Modify | awk '{print $3}' |awk -F "." '{print $1}' ''')
gong_hour = subprocess.getoutput(''' stat gong | grep Modify | awk '{print $3}' |awk -F "." '{print $1}' |awk -F ':' '{print $1}' ''')
gong_min=subprocess.getoutput(''' stat gong | grep Modify | awk '{print $3}' |awk -F "." '{print $1}' |awk -F ':' '{print $2}' ''')
guan_hour = subprocess.getoutput(''' stat guan | grep Modify | awk '{print $3}' |awk -F "." '{print $1}' |awk -F ':' '{print $1}' ''')
guan_min = subprocess.getoutput(''' stat guan | grep Modify | awk '{print $3}' |awk -F "." '{print $1}' |awk -F ':' '{print $2}' ''')
#接下来将获取到的文件夹时间与系统的时间做对比并执行逻辑
if (abs(int(sys_hour) - int(gong_hour)) == 23 ) and ((60 - int(sys_min) + int(gong_min)) <= 40):
value = 1
elif (abs(int(sys_hour) - int(gong_hour)) == 0 ) and ( abs(int(sys_min) - int(gong_min)) <= 40 ):
value = 1
else:
value = 0
gong_time = {
'endpoint': ip,
'tags': '',
'timestamp': timestamp,
'metric': 'test.time',
'value': value,
'step': 10,
'extra': "helloworld",
}
result.append(gong_time)
#接下来获取prog文件夹时间,与系统时间做对比并执行逻辑
if (abs(int(sys_hour) - int(guan_hour)) == 23 ) and ((60 - int(sys_min) + int(guan_min)) <= 40):
value1 = 1
elif (abs(int(sys_hour) - int(guan_hour)) == 0 ) and ( abs(int(sys_min) - int(guan_min)) <= 40 ):
value1 = 1
else:
value1 = 0
guan_time = {
'endpoint': ip,
'tags': '',
'timestamp': timestamp,
'metric': 'test1.time',
'value': value1,
'step': 10, #必写字段
'extra': "nihao", #必写字段,可自定义内容,会写入告警事件中
}
result.append(guan_time)
check_result = json.dumps(result)
#print(check_result)
#transfer模块在服务端机器22.183上
url = "http://10.88.22.183:8008/api/transfer/push" #通过transfer接口上报
#url = "http://127.0.0.1:2080/v1/push" #定义上报接口,通过agent方式
r = requests.post(url=url , data=check_result)
#print(r.text)
if __name__ == '__main__':
check_time()
从上面可以看出,修改了指标为test.time和test1.time,下面的url地址修改为transfer中心方式上报数据,通过定时任务执行后可以看到指标如下:

注意:windows上报数据的方式可以通过bat脚本执行python脚本,然后定时任务计划里执行bat脚本即可


