爬取 yarn.resourcemanager.webapp.address 页面 Cluster Scheduler 数据脚本模板

#${cluster_dir}/cluster_scheduler.sh.template
#爬取 yarn.resourcemanager.webapp.address 页面 Cluster Scheduler 数据脚本模板

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

#爬取页面信息存储到以下文件中
file_name=cluster_scheduler.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_url1=${yarn_url%%/cluster*}/ws/v1/cluster/metrics
#curl抓取网页信息并存储到指定位置
curl -s ${yarn_url1} > ${operation_dir}/${file_name}
#第一个grep过滤出需要的参数和其值,第二个grep过滤出其值,这里的单位为MB
memory_total_mb=`grep -oE "\"totalMB\":[0-9]+" ${operation_dir}/${file_name} | grep -oE [0-9]+`
#将页面网址转化为便于爬取的格式
yarn_url2=${yarn_url%%/cluster*}/cluster/scheduler
#curl抓取网页信息并存储到指定位置
curl -s ${yarn_url2} > ${operation_dir}/${file_name}
#sed编辑文件去掉所有行之前的空格、HTML语言的标签行、空行、[开头的行
sed -i 's/^[ ]*//g;/^</d;/^$/d;/^\[/d' ${operation_dir}/${file_name}
#第一个sed匹配以Queue Status结尾的行,第二个sed将该行内容替换为该行单引号中的内容即队列名
#queue_names=`sed -n '/Queue Status$/p' ${operation_dir}/${file_name} | sed -r 's/.*'\''(.+)'\''.*/\1/'`
queue_names=`sed -n '/Queue Status$/p' ${operation_dir}/${file_name} | sed -r "s/^'(.+)'.*/\1/"`
#for循环遍历提取每个队列的信息
for queue_name in ${queue_names}; do
	#sed提取选定行范围内的信息即某队列的状态信息并存储为以该队列名命名的文件
	#sed -n '/^'\'${queue_name}'\' Queue Status/,/^'/p' ${operation_dir}/${file_name} > ${operation_dir}/${queue_name}
	sed -n "/^'${queue_name}' Queue Status/,/^'/p" ${operation_dir}/${file_name} > ${operation_dir}/${queue_name}
	#grep过滤检测文件是否包含该行信息,输出内容不保留重定向到Linux黑洞
	grep "Absolute Used Capacity:" ${operation_dir}/${queue_name} > /dev/null
	#if判断上一条命令是否执行成功,分情况处理
	if [[ $? -eq 0 ]]; then
		#sed匹配对应信息的下一行内容,grep过滤出数值赋值给对应变量
		absolute_used_capacity_percent=`sed -n '/^Absolute Used Capacity:/{n;p}' ${operation_dir}/${queue_name} | grep -oE [0-9]+.[0-9]+`
		absolute_capacity_percent=`sed -n '/^Absolute Capacity:/{n;p}' ${operation_dir}/${queue_name} | grep -oE [0-9]+.[0-9]+`
	else
		#第一个sed匹配对应信息的下一行内容,第二个sed匹配memory的值赋值给对应变量
		memory_used_mb=`sed -n '/^Used Resources:/{n;p}' ${operation_dir}/${queue_name} | sed -r 's/.*memory:([0-9]+),.*/\1/'`
		memory_allocated_mb=`sed -n '/^Min Resources:/{n;p}' ${operation_dir}/${queue_name} | sed -r 's/.*memory:([0-9]+),.*/\1/'`
		#bc求出内存使用百分比并保留两位小数,awk将空缺的地方用0填充赋值给对应变量
		absolute_used_capacity_percent=`echo "scale=2;${memory_used_mb} * 100 / ${memory_total_mb}" | bc | awk '{printf "%.2f", $0}'`
		absolute_capacity_percent=`echo "scale=2;${memory_allocated_mb} * 100 / ${memory_total_mb}" | bc | awk '{printf "%.2f", $0}'`
	fi
	#echo拼接变量为一条记录,追加输到指定文件中等待 Logstash 采集
	echo "${queue_name} ${absolute_used_capacity_percent} ${absolute_capacity_percent}" >> ${logstash_input_file}
done

猜你喜欢

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