#!/usr/bin/env python
# -*- coding:utf-8 -*-
from dingtalkchatbot.chatbot import DingtalkChatbot
import json
import requests
import datetime
headers = {'Content-Type': 'application/json;charset=utf-8'}
# dd_url = "https://oapi.dingtalk.com/robot/send?access_token=83a159d7c62306ec1dcf7a4e4007c0e04d71b1bece0d8818c834f9e058ad261c"
# dd_url = 'https://oapi.dingtalk.com/robot/send?access_token=f8208c8e20a9c490845b64234ccd78b016ca4dfb7287c938424530a7a8c4ed88'
dd_url = "https://oapi.dingtalk.com/robot/send?access_token=18c4de04ae7b5ff356c17603168c5b7f6fb689175ad45ff7f8718110c2aeb666"
data=[]
def alert_title(msg):
return '' + msg + '' + '\n\n '
def set_text(title, msg):
return "" + title + ":" + " " \
+ "" + msg + " \n\n "
def send_dd(content):
now_time = str(datetime.datetime.now())
title = alert_title('yarn运行时长超1小时告警')
send_text = title + \
set_text('告警等级', '一般告警') \
+ set_text('告警时间', now_time) \
+ set_text('告警信息', content)
at_mobiles = []
dingMsg = DingtalkChatbot(dd_url)
result = dingMsg.send_markdown(title='yarn任务告警', text=send_text, at_mobiles=at_mobiles)
print(result)
def check_yarn():
bf_time = datetime.timedelta(days=0,seconds=7200,microseconds=0)
df_time = datetime.timedelta(days=0, seconds=3600, microseconds=0)
start_time = (datetime.datetime.utcnow() - bf_time).isoformat()
limit_time = (datetime.datetime.utcnow() - df_time).isoformat()
url = 'http://ip:7180/api/v33/clusters/TANK-V2/services/yarn/yarnApplications?from='+start_time+'&limit=1000&offset=0&to=now'
r = requests.get(url, auth=('jiankong', 'jiankong'))
r.encoding = ('utf8')
page = r.text
page = json.loads(page)
item = page['applications']
for bulk in item:
if bulk['state'] == 'RUNNING':
if bulk['startTime'] < limit_time and 'krill' not in bulk['name']:
result = '任务名:'+bulk['name']+' 任务ID:'+bulk['applicationId']+' 启动时间:'+bulk['start_time']+' 当前状态:'+bulk['state']
data.append(result)
if len(data) > 0:
str='\n\n'
content = str.join(data)
send_dd(content)
else:
pass
if __name__ == '__main__':
check_yarn()
















