#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# __author__ = caozhi
# create_time 2019-05-07,update_time 2019-05-07
# version = 1.0
# nginx程序 80和8080端口 check 监测,然后将数据提交到open-falcon
# url : http://127.0.0.1:80/check
import os
import sys
import json
import requests
import time
import re
data = []
nginx_data = '/home/caozhi/emmby/video_nginx_80port_check/nginx_ip'
now_time = int(time.time())
def record(value, Tags):
record = {}
record['metric'] = 'video_nginx_80port_check'
record['endpoint'] = os.uname()[1]
record['timestamp'] = now_time
record['step'] = 120
record['value'] = value
record['counterType'] = 'GAUGE'
record['Tags'] = Tags
data.append(record)
return(data)
def post_falcon(data):
if data:
print('这是data的json数据')
print(data)
falcon_request = requests.post("http://127.0.0.1:1988/v1/push", data=json.dumps(data))
#falcon_request = requests.post("http://127.0.0.1:1988/v1/push", json=data)
print('json参数请求返回状态码为:' + str(falcon_request.status_code))
print('json参数请求返回为:' + str(falcon_request.text))
def read_file(nginx_data):
with open(nginx_data, 'r+') as f:
#nginx_data = f.readlines()
nginx_data = f.read()
return nginx_data
if __name__ == '__main__':
print('***************************************')
print('本次执行脚本时间:{}'.format(time.strftime("%Y%m%d %H:%M", time.localtime())))
nginx_data = read_file(nginx_data=nginx_data).strip()
for port in 80,8080:
for nginx_ip in nginx_data.split('\n'):
print('***************************************')
print(nginx_ip)
url = 'http://' + nginx_ip + ':' + str(port) + '/check'
try:
response = requests.get(url, timeout=3)
print('返回的状态码为: ' + str(response.status_code))
print('get请求地址为: ' + response.url)
except:
print('{} is down'.format(nginx_ip))
value = 999
else:
if response.status_code != 200:
print('{} is error'.format(nginx_ip))
value = response.status_code
else:
print('{} is correct'.format(nginx_ip))
print(response.text.strip())
if re.search('[0-9]{10}',response.text.strip()):
print('This {} 是数字'.format(response.text.strip()))
value = response.status_code
else:
print('This {} 不是数字'.format(response.text.strip()))
value = 444
response.status_code = 520
finally:
Tags = str(port) + '=' + nginx_ip
record(value=value, Tags=Tags)
print('Tags: {}, value: {}'.format(Tags,value))
print('***************************************')
post_falcon(data=data)
nginx程序 80和8080端口 url check 监测
最新推荐文章于 2024-04-23 11:34:10 发布