python2.7 monitor flume

Due to work needs, I wrote the python2.7 monitoring flume function, the following is dry goods, I hope to help you
#!/usr/bin/env python

-- coding: utf-8 --

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

#Analog curl http://ip:port/metrics, the definition here is 41415 at startup, the default is 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')


#Cycle check function def check(ip,type,sink_file_path):
with open("%s"%sink_file_path,"r") as f1:
for line in f1.readlines(): #Data
value taken half an hour ago
line = line .strip("\n") #After
half an hour, take the latest value and compare it with the previous data
sink_ban = comm(ip,"41415",type)
if line == sink_ban:
DingToken = "xxxxxx"
content = "server ip : %s"%ip +'\n' + "flume application: flume" +'\n' + "flume environment: online environment"
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) #Write the
result after 10 minutes to the file
with open("%s"%sink_file_path, " w") as f:
f.write(sink4 +'\n')

ban_time = 0
#10 minutes later time
ban = (datetime.datetime.now()+datetime.timedelta(minutes=10)).strftime("%H:%M")
ban_time = ban

while True: #current
time
now = datetime.datetime.now().strftime("%H:%M")
if now == ban_time: #current
time plus 10 minutes time
ban1 = (datetime.datetime.now() +datetime.timedelta(minutes=10)).strftime("%H:%M")
ban_time = ban1 #Read
the file content 10 minutes ago and compare the file 10 minutes later
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)

For more automated operation and maintenance, please go to www.wangshuying.cn website to see
more important points of knowledge about operation and maintenance.

Guess you like

Origin blog.51cto.com/461884/2544044