运维开发网

python2.7监控flume

运维开发网 https://www.qedev.com 2020-10-27 12:32 出处:51CTO 作者:wangyang_2008
python2.7监控flume

由于工作需要,写了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网站查看

里面有更多运维相关方面的知识要点。

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号