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

#${cluster_dir}/apps_running.sh.template
#爬取 yarn.resourcemanager.webapp.address 页面 Applications RUNNING 数据脚本模板

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

#爬取页面信息存储到以下文件中
file_name=apps_running.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/RUNNING
#curl抓取网页信息并存储到指定位置
curl -s ${yarn_url} > ${operation_dir}/${file_name}
#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

猜你喜欢

转载自blog.csdn.net/qq_16592497/article/details/81317799