python2.7监控flume

自闭症网瘾萝莉.ら 提交于 2020-10-27 09:53:11

由于工作需要,写了python2.7监控flume的功能,下面是干货,希望能帮到大家
#!/usr/bin/env python

-- coding: utf-8 --

import datetime,os,sys,time,socket,urllib,json,urllib2,sys
import requests

#模拟curl http://ip:port/metrics,这里启动时定义是41415,默认是41414
def comm(ip,port,type):
url="http://%s:%s/metrics" %(ip,port)
req=urllib2.Request(url)
res=urllib2.urlopen(req).read()
j=json.loads(res)
sink = j["%s"%type]["EventDrainSuccessCount"]
return sink






sink4_42 = comm("10.1.1.42","41415","SINK.sink4")
sink4_43 = comm("10.1.1.43","41415","SINK.sink4")
sink4_44 = comm("10.1.1.44","41415","SINK.sink4")
sink4_45 = comm("10.1.1.45","41415","SINK.sink4")


#把结果写入文件
sink_list = ["sink42","sink43","sink44","sink45"]
for sink in sink_list:
#无文件时自动创建相应文件
if not os.path.isfile(sink+"."+"txt"):
fd = open("/tmp"+"/"+sink+"."+"txt",mode="w")
fd.close()
if sink == "sink42":
with open("/tmp/sink42.txt", "w") as f:
f.write(sink4_42 + '\n')
if sink == "sink43":
with open("/tmp/sink43.txt", "w") as f:
f.write(sink4_43 + '\n')
elif sink == "sink44":
with open("/tmp/sink44.txt", "w") as f:
f.write(sink4_44 + '\n')
elif sink == "sink45":
with open("/tmp/sink45.txt", "w") as f:
f.write(sink4_45 + '\n')

















#循环检查函数
def check(ip,type,sink_file_path):
with open("%s"%sink_file_path,"r") as f1:
for line in f1.readlines():
#半小时前数据取值
line = line.strip("\n")
#半小时后取最新的值与之前的数据做对比
sink_ban = comm(ip,"41415",type)
if line == sink_ban:
DingToken = "xxxxxx"
content = "服务器ip: %s"%ip + '\n' + "flume应用:flume" + '\n' + "flume环境: 线上环境"
def send_dingtalk(content):
token = DingToken
headers = {'Content-Type': 'application/json'}
url = "https://oapi.dingtalk.com/robot/send?access_token=" + token
msg = {"msgtype": "text", "text": {"content": content}, "at": {}}
req = requests.post(url, headers=headers, data=json.dumps(msg))
if req.status_code != 200:
print(content)
send_dingtalk(content)
else:
sink4 = comm(ip,"41415",type)
#把10分钟后结果写入文件
with open("%s"%sink_file_path, "w") as f:
f.write(sink4 + '\n')























ban_time = 0
#10分钟以后时间
ban = (datetime.datetime.now()+datetime.timedelta(minutes=10)).strftime("%H:%M")
ban_time = ban


while True:
#当前时间
now = datetime.datetime.now().strftime("%H:%M")
if now == ban_time:
#当前时间加上10分钟时间
ban1 = (datetime.datetime.now()+datetime.timedelta(minutes=10)).strftime("%H:%M")
ban_time = ban1
#读取10分钟前的文件内容与10分钟后的文件做结比
check("10.1.1.42","SINK.sink4","/tmp/sink42.txt")
time.sleep(1)
check("10.1.1.43","SINK.sink4","/tmp/sink43.txt")
time.sleep(1)
check("10.1.1.44","SINK.sink4","/tmp/sink44.txt")
time.sleep(1)
check("10.1.1.45","SINK.sink4","/tmp/sink45.txt")













 else:
    time.sleep(1)

更多自动化运维,请移步到www.wangshuying.cn网站查看
里面有更多运维相关方面的知识要点。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!