运维开发网 https://www.qedev.com
2020-03-11 09:49
出处:网络
#!/bin/bash
export JAVA_HOME=/app/jdk/jdk1.8.0_92
export HADOOP_CONF_DIR=/home/hdfs/balancer/hadoop-conf
相关专题:#!/bin/bash
export JAVA_HOME=/app/jdk/jdk1.8.0_92
export HADOOP_CONF_DIR=/home/hdfs/balancer/hadoop-conf
rm -f hostname.txt
rm -f dfused.txt
rm -f hostdfs.txt
rm -f hostdfs_alert.txt
###获取大于97%的主机名和dfs used%
function checkdfs(){
echo "start checking hdfs used rate"
hdfs dfsadmin -report | grep -i "50010" | awk -F ":" ‘{print $2}‘| awk ‘{print $1}‘ > hostname.txt
hdfs dfsadmin -report | grep "DFS Used%" | awk -F ": " ‘{print $2}‘ | awk -F "%" ‘{print $1}‘ > dfused.txt
livesum=$(hdfs dfsadmin -report | grep "Live datanodes" | awk ‘{print $3}‘ | awk -F "):" ‘{print $1}‘ | awk -F "(" ‘{print $2}‘)
echo $livesum
sed -i ‘1d‘ dfused.txt
let livesum+=1
sed -i $livesum‘,$d‘ dfused.txt
sed -i $livesum‘,$d‘ hostname.txt
linesum=$(cat hostname.txt | wc -l)
echo $linesum
harr=($(awk ‘{print $1}‘ hostname.txt))
darr=($(awk ‘{print $1}‘ dfused.txt))
if [ $linesum -gt 0 ]
then
for (( i = 0 ; i < ${#harr[@]} ; i++ ))
do
for (( j = 0 ; j < ${#darr[@]} ; j++ ))
do
if [ $i -eq $j ]
then
echo ${harr[$i]} ":" ${darr[$j]} >> hostdfs.txt
fi
done
done
else
echo "Not Live Datanodes"
fi
ddarr=($(cat hostdfs.txt | awk ‘$3>97{print $3}‘ |sort -ru))
for (( m = 0; m< ${#ddarr[@]} ; m++ ))
do
if [[ $(echo $(cat hostdfs.txt) | grep "${ddarr[$m]}") != "" ]]
then
echo $(cat hostdfs.txt | grep "${ddarr[$m]}" | awk ‘NR==1{print $1}‘) ":" ${ddarr[$m]} >> hostdfs_alert.txt
fi
done
}
checkdfs
url="http://xxx/monitor/report/sendReport"
linenum=$(cat hostdfs_alert.txt | wc -l)
let linenum+=1
echo $linenum
#上报数据
function postdfs(){
for (( k = 1; k< $linenum ; k++ ))
do
key=$(echo $(sed -n "$k p" hostdfs_alert.txt | awk -F ":" ‘{print "bdp.ops."$1".##cluster=bdp##env=prod"}‘) | awk ‘{gsub(/[[:blank:]]*/,"",$0);print $0;}‘ )
echo $key >>key.txt
value=$(sed -n "$k p" hostdfs_alert.txt | awk -F ": " ‘{print $2}‘)
data="{\"key\":\"$key\",\"value\":\"$value\"}"
echo $data
cmd="curl -s -X POST -H ‘Content-type‘:‘application/json‘ -d ‘${data}‘ ${url}"
sh -c "$cmd"
done
}
postdfs
data_expression="x>97"
data_time=5
data_count=1
data_alertMode="1,2,3"
data_members="01384016"
add_url="http://xxx/monitor/rule/addReport"
keynum=$(cat key.txt | wc -l)
let keynum+=1
echo $keynum
function dfsalert(){
for((n=1;n
do
data_key=$(sed -n "$n p" key.txt | awk ‘{print $1}‘)
str1=$(sed -n "$n p" key.txt | awk -F "." ‘{print $3"."$4"."$5"."$6}‘)
str2="以下主机IP HDFS使用已经超过97%:"
data_description=${str2}${str1}
add_data="{\"id\":\"$data_key\",\"expression\":\"$data_expression\",\"time\":$data_time,\"count\":$data_count,\"alertMode\":\"$data_alertMode\",\"members\":\"$data_members\",\"description\":\"$data_description\"}"
echo $add_data
add_cmd="curl -s -X POST -H ‘Content-type‘:‘application/json‘ -d ‘${add_data}‘ ${add_url}"
sh -c "$add_cmd"
done
}
dfsalert
分享到
0
0
0