需求:kill 掉yarn上超时的任务,实现不同队列不同超时时间的kill机制,并带有任务名的白名单功能

此为python脚本,可配置crontab使用

# _*_ coding=utf-8 _*_
# !/usr/bin/python
import re
import commands
import time run_app_arr = []
timeout_app_arr = []
ONE_HOURE = 1
THREE_HOURE = 3
TEST_QUEUE_NAME = ['hue', 'etl-test']
ONLINE_QUEUE_NAME = ['default']
KILL_WHITE_LIST = ['org.apache.spark.sql.hive.thriftserver.HiveThriftServer2']
DINGDING_URL = 'xxx'
ding_cmd = """ curl %s -H 'Content-Type: application/json' -d '{"msgtype": "text", "text": {"content": "== YARN OVERTIME JOB KILL 告警 ==\n\n 当前时间: %s \n kill_app_id: %s \n kill_app_name: %s \n kill_app_queue: %s "}}' """
f = None
try:
f = open('/home/hadoop/autokillhadoopjob/check_timeout_job.log', 'a')
commond = '. /etc/profile && yarn application -list | grep "http://" |grep "RUNNING" |cut -f1,2,5'
# 获得正在运行job的id,name,queue 加到 run_app_arr
status, output = commands.getstatusoutput(commond)
f.write('#' * 50 + '\n')
f.write('=> start_time: %s \n' % (time.strftime('%Y-%m-%d %H:%M:%S')))
if status == 0 :
for line in output.split('\n'):
if line.startswith('application_'):
app_line = re.split('\t', line)
running_app_id = app_line[0].strip()
running_app_name = app_line[1].strip()
app_queue = app_line[2].strip()
# 根据所在队列 筛选出app加到数组中
if app_queue in TEST_QUEUE_NAME or app_queue in ONLINE_QUEUE_NAME:
run_app_arr.append((running_app_id, running_app_name, app_queue))
else:
f.write('yarn -list 执行失败. status: %s.'%(status)) # 遍历所有队列的running job,如有超时加到timeout_app_arr
for run_app in run_app_arr:
running_app_id = run_app[0]
running_app_name = run_app[1]
running_app_queue = run_app[2]
commond = ". /etc/profile && yarn application -status " + running_app_id + "| grep 'Start-Time' | awk -F ':' '{print $2}'"
status, output = commands.getstatusoutput(commond)
if status == 0:
for line in output.split('\n'):
start_timestamp = line.strip()
if start_timestamp.isdigit():
# 计算任务耗时
elapsed_time = time.time() - int(start_timestamp) / 1000
cost_time = round(elapsed_time / 60 / 60, 2)
f.write('=> cost_time: %sh \n' % (cost_time))
# print cost_hour
# 筛选出超时的job 加到数据组中/过滤掉白名单任务
if running_app_name not in KILL_WHITE_LIST:
if (running_app_queue in TEST_QUEUE_NAME and cost_time > ONE_HOURE) \
or (running_app_queue in ONLINE_QUEUE_NAME and cost_time > THREE_HOURE):
# if cost_hour > 0:# 测试
f.write('=> timeout app => %s # %s # %s\n' % (running_app_id, running_app_name, running_app_queue))
timeout_app_arr.append((running_app_id, running_app_name, running_app_queue))
else:
f.write('yarn -status 执行失败. status: %s.'%(status)) if len(timeout_app_arr) == 0:
f.write('=> no timeout job.\n') # kill掉超时的job 并dingding报警
for kill_app in timeout_app_arr:
kill_app_id = kill_app[0]
kill_app_name = kill_app[1]
kill_app_queue = kill_app[2]
commond = '. /etc/profile && yarn application -kill ' + kill_app_id
status, output = commands.getstatusoutput(commond)
if status == 0:
f.write('=> kill app sucessfully: %s # %s # %s.\n' % (kill_app_id, kill_app_name, kill_app_queue))
current_time = time.strftime('%Y-%m-%d %H:%M:%S')
cmd = ding_cmd % (DINGDING_URL, current_time, kill_app_id, kill_app_name, kill_app_queue)
commands.getstatusoutput(cmd)
else:
f.write('=> kill app failed: %s # %s # %s.\n' % (kill_app_id, kill_app_name, kill_app_queue)) f.write('=> stop_time: %s \n' % (time.strftime('%Y-%m-%d %H:%M:%S'))) except Exception as e:
f.write('=> Exception: %s \n' % (e.message))
finally:
if f:
f.close()

yarn application -kill application_id yarn kill 超时任务脚本的更多相关文章

  1. hadoop job -kill 和 yarn application -kill 区别

    hadoop job -kill 调用的是CLI.java里面的job.killJob(); 这里会分几种情况,如果是能查询到状态是RUNNING的话,是直接向AppMaster发送kill请求的.Y ...

  2. hadoop job -kill 与 yarn application -kii(作业卡了或作业重复提交或MapReduce任务运行到running job卡住)

    问题详情  解决办法 [hadoop@master ~]$ hadoop job -kill job_1493782088693_0001 DEPRECATED: Use of this script ...

  3. yarn application命令介绍

    yarn application 1.-list     列出所有 application 信息    示例:yarn  application -list 2.-appStates <Stat ...

  4. kill 进程卡住,超时kill方法

    还是有漏洞 ,万一 working.py未超时, kill_job.sh 会不会杀死别人的进程啊start.sh#!/bin/bash python working.py &python wo ...

  5. spark-shell启动报错:Yarn application has already ended! It might have been killed or unable to launch application master

    spark-shell不支持yarn cluster,以yarn client方式启动 spark-shell --master=yarn --deploy-mode=client 启动日志,错误信息 ...

  6. yarn application ID 增长达到10000后

    Job, Task, and Task Attempt IDs In Hadoop 2, MapReduce job IDs are generated from YARN application I ...

  7. Yarn application has already exited with state FINISHED

    如果在运行spark-sql时遇到如下这样的错误,可能是因为yarn-site.xml中的配置项yarn.nodemanager.vmem-pmem-ratio值偏小,它的默认值为2.1,可以尝试改大 ...

  8. spark利用yarn提交任务报:YARN application has exited unexpectedly with state UNDEFINED

    spark用yarn提交任务会报ERROR cluster.YarnClientSchedulerBackend: YARN application has exited unexpectedly w ...

  9. 【深入浅出 Yarn 架构与实现】3-1 Yarn Application 流程与编写方法

    本篇学习 Yarn Application 编写方法,将带你更清楚的了解一个任务是如何提交到 Yarn ,在运行中的交互和任务停止的过程.通过了解整个任务的运行流程,帮你更好的理解 Yarn 运作方式 ...

随机推荐

  1. WebService C#开发/调用

    简单描述C#开发WebService操作步骤以及调用方式 WebService开发 第一步:创建Web空项目 第二步:为创建的Web空项目添加Web服务 第三步:实现WebService方法(仅供参考 ...

  2. php 二维数组按照指定字段进行排序

    $allItem = [ ["id"=>10,"updated_at"=>"2018-11-01"], ["id&qu ...

  3. 细看Thread的 start() 和 run()方法

    1.start(): 我们先来看看API中对于该方法的介绍: 使该线程开始执行:Java 虚拟机调用该线程的 run 方法. 结果是两个线程并发地运行:当前线程(从调用返回给 start 方法)和另一 ...

  4. java中经常使用的Swing组件总结

    1.按钮(Jbutton) Swing中的按钮是Jbutton,它是javax.swing.AbstracButton类的子类,swing中的按钮可以显示图像,并且可以将按钮设置为窗口的默认图标,而且 ...

  5. sublime 最近用的有点卡

    index_files:false,

  6. 【ThreadLocal】使用ThreadLocal实现线程安全

    非线程安全 public class UnSafeThreadLocalDemo { private int count = 0; public static void main(String[] a ...

  7. requests 请求几个接口 出现’您的账户在其它设备使用过,为保障安全,需重新登入才能在本设备使用‘

    因为接口和接口直接有个字段是关联的 在登陆请求后,后台响应了个token,下面的请求 ,请求头要带上这个token 才认为是登陆后 的操作

  8. JavaScript for...in 循环

    JavaScript for...in 语句循环遍历对象的属性. 语法 for (对象中的变量) { 要执行的代码 } 注释:for...in 循环中的代码块将针对每个属性执行一次. 实例 循环遍历对 ...

  9. 【ZZ】终于有人把云计算、大数据和人工智能讲明白了!

    终于有人把云计算.大数据和人工智能讲明白了! https://mp.weixin.qq.com/s/MqBP0xziJO-lPm23Bjjh9w 很不错的文章把几个概念讲明白了...图片拷不过来... ...

  10. Scrapy学习篇(六)之Selector选择器

    当我们取得了网页的response之后,最关键的就是如何从繁杂的网页中把我们需要的数据提取出来,python从网页中提取数据的包很多,常用的有下面的几个: BeautifulSoup它基于HTML代码 ...