爬取 yarn.resourcemanager.webapp.address 页面 Applications ACCEPTED 数据脚本模板

#${cluster_dir}/apps_pending.sh.template
#爬取 yarn.resourcemanager.webapp.address 页面 Applications ACCEPTED 数据脚本模板
#版权声明:不定期更新维护地址,看这里→_→	https://blog.csdn.net/qq_16592497/article/details/81317447

#脚本模板中涉及需要被替换的变量
#URL=${URL}
#operation_dir=${operation_dir}
#logstash_input_file=${logstash_input_file}

#爬取页面信息存储到以下文件中
file_name=apps_pending.jmx

########################################################################################################################
#yarn resourcemanager HA 自动切换爬取页面
#for循环爬取 yarn resourcemanager webapp 页面,若为standby模式,则尝试爬取下一个页面
for url in ${URL[@]}; do
	#curl抓取网页信息并存储到指定位置
	curl -s ${url} > ${operation_dir}/${file_name}
	#由于yarn resourcemanager设置了HA,standby模式页面会有一行提示信息,故在此处添加判断若该页面为standby模式,则爬取下一个页面
	#之前设置的是比较规则是判断行数是否为1,后来发现若主机连接不上则爬取的文件为空,行数为 0,故更改比较规则为行数小于1
	#后来又发现有问题,正常的文件也是只有一行信息,故改为如下比较规则
	#grep过滤检测爬取的文件中是否有字符串 "This is standby RM." 输出内容不保留重定向到Linux黑洞
	grep "This is standby RM." ${operation_dir}/${file_name} > /dev/null
	#if判断上一条语句是否执行成功或者文件为空,若执行成功则说明该页面为standby模式;若文件为空则说明主机连接不上,爬取不到数据
	if [[ $? -eq 0 || ! -s ${operation_dir}/${file_name} ]]; then
		#继续下一次循环判断
		continue
	else
		#否则找到正在运行的 yarn resourcemanager webapp 页面地址,赋值给对应变量
		yarn_url=${url}
		#break结束循环
		break
	fi
done
########################################################################################################################

#将页面网址转化为便于爬取的格式
yarn_url=${yarn_url%%/cluster*}/cluster/apps/ACCEPTED

################################################################################
#curl爬取网址的异常处理,若连接超时则意味着需要替换网址或者移除监控,可从页面无数据发现
#curl爬取网页信息并存储到指定位置,-s 静默模式 --connect-timeout 设置连接超时时间(秒)
curl -s --connect-timeout 10 ${yarn_url} > ${operation_dir}/${file_name}
#if判断上一条语句是否执行成功,若失败则退出脚本
if [[ $? -ne 0 ]]; then
	#exit退出shell脚本,返回状态值 1 表示失败
	exit 1
fi
################################################################################

#sed编辑文件保留以'['开头的行,即每条 application 的信息
sed -i '/^\[/!d' ${operation_dir}/${file_name}
#awk获取以 "," 分隔的第 1 列信息,sed提取出应用名赋值给对应变量
applications=`awk -F \",\" '{ print $1 }' ${operation_dir}/${file_name} | sed -r 's/.*>(.+)<.*/\1/'`
#for循环遍历提取每个应用的信息
for application_id in ${applications}; do
	#if判断变量值是否为空,若为空则不执行循环体语句,利用break跳出循环
	if [[ -z ${applications[@]} ]]; then
		break;
	else
		#grep过滤出当前应用信息存储为以应用名命名的文件
		grep ${application_id} ${operation_dir}/${file_name} > ${operation_dir}/${application_id}
		#awk获取以 "," 分隔的第 n 列信息赋值给对应变量
		user=`awk -F \",\" '{ print $2 }' ${operation_dir}/${application_id}`
		queue=`awk -F \",\" '{ print $5 }' ${operation_dir}/${application_id}`
		#echo拼接变量为一条记录,追加输到指定文件中等待 Logstash 采集
		echo "${application_id} ${queue} ${user}" >> ${logstash_input_file}
		#清空 ${application_id} 文件中的信息,只保留文件名,目的是减少不必要的空间占用
		cat /dev/null > ${operation_dir}/${application_id}
	fi
done

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值