Chinaunix首页 | 论坛 | 博客
  • 博客访问: 761181
  • 博文数量: 274
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 862
  • 用 户 组: 普通用户
  • 注册时间: 2015-10-24 15:31
个人简介

不合格的程序猿

文章分类

全部博文(274)

文章存档

2019年(3)

2018年(1)

2017年(4)

2016年(160)

2015年(106)

我的朋友

分类: 服务器与存储

2019-06-04 16:34:00

解析系统信息文件
#!/bin/bash


## script of topcheck
#log_dir=$(dirname $0)
#LOG_FILE=$log_dir/`date +%Y-%m-%d.log`
#echo -e "=============== START ================" >> $LOG_FILE
#echo -e "====> icfs -s, `date`" >> $LOG_FILE
#/usr/bin/timeout 10 /usr/bin/icfs -s &>> $LOG_FILE
#
#echo -e "====> dstat 1 10, `date`" >> $LOG_FILE
#/usr/bin/dstat 1 10 >> $LOG_FILE
#
#echo -e "====> top -b -n 2, `date`" >> $LOG_FILE
#/usr/bin/top -b -n 2 |awk '/icfs-mon|icfs-mds|icfs-osd|icfs-fuse|ganesha|Cpu|Mem/' &>> $LOG_FILE
#
##echo -e "====> df -h, `date`" >> $LOG_FILE
##/usr/bin/timeout 5 /usr/bin/df -h >> $LOG_FILE
#
#echo -e "====> iostat -mx 1 2, `date`" >> $LOG_FILE
#/usr/bin/iostat -mx 1 2 >> $LOG_FILE
#
#echo -e "====> free -m, `date`" >> $LOG_FILE
#/usr/bin/free -m >> $LOG_FILE
#
#echo -e "==================== END ==========================\n\n" >> $LOG_FILE


# start this script
if [ $# -ne 1 ] || [ $1 = "-h" ]
then
echo -e "do it like this: $0 {file_name}"
exit 0
fi


if [ ! -z $1 ]
then 
echo -e "\n\tplease make sure that the file $1 is exist.\n"
exit -1
fi


src_file=$1
dst_file=./dst_file
dst_icfs_file=./.dst_icfs_file
dst_dstat_file=./.dst_dstat_file
dst_top_file=./.dst_top_file
dst_iostat_file=./.dst_iostat_file
dst_free_file=./.dst_free_file


str_start="START"
str_end="END"
str_icfs="icfs -s"
str_dstat="dstat "
str_top="top -b -n"
str_iostat="iostat -mx"
str_free="free -k"


echo >$dst_icfs_file
echo >$dst_dstat_file
echo >$dst_top_file
echo >$dst_iostat_file
echo >$dst_free_file


function cmd_proc_icfs_write_title()
{
arr_str=$1
attribute_num=$2
grep "attribute" $dst_icfs_file &>/dev/null
if [ $? -eq 0 ]
then
return 0
else
echo -e "icfs_attribute\t\t\t\t\t" >> $dst_icfs_file
fi


for (( idx = 0; idx < attribute_num; idx++ ))
do
let idx_base=idx*3
let idx_attribute=idx_base+2
let idx_unit=idx_base+1


if [ ${arr_str[$idx_unit]} == "kB/s" ] || [ ${arr_str[$idx_unit]} == "MB/s" ] || [ ${arr_str[$idx_unit]} == "GB/s" ]
then
echo -ne "${arr_str[$idx_attribute]}(MB/s)\t" >> $dst_icfs_file
elif [ ${arr_str[$idx_unit]} == "op/s" ]
then
echo -ne "${arr_str[$idx_attribute]}(op/s)\t" >> $dst_icfs_file
fi
done
echo -ne "\n" >> $dst_icfs_file


return 0
}


function cmd_proc_icfs_write_value()
{
value=$1
attribute=$2


if [ "$attribute" == "kB/s" ]
then
value=`echo "scale=2; $value / 1024" | bc`
fi
echo -ne "$value\t\t" >> $dst_icfs_file


return 0
}


function cmd_proc_icfs()
{
str_log=$1
if [[ $str_log != *"client io "* ]]
then
return 0
fi


#remove 'client to ' from str_log
str_log=${str_log:10}
#split str_log into array by ','
arr_str=(${str_log//,/ })
#get the number of element in arr_str
arr_len=${#arr_str[@]}
#get the number of attribute in arr_str
attribute_num=`expr $arr_len / 3`


cmd_proc_icfs_write_title "${arr_str[*]}" $attribute_num


for (( idx = 0; idx < attribute_num; idx++ ))
do
let idx_value=idx*3
let idx_att=idx_value+1


cmd_proc_icfs_write_value ${arr_str[$idx_value]} "${arr_str[$idx_att]}"
done
echo -ne "\n" >>$dst_icfs_file


return 0
}


function cmd_proc_dstat_write_title()
{
arr_str=$1
arr_len=$2


grep "attribute" $dst_dstat_file &>/dev/null
if [ $? -eq 0 ]
then
return 0
else
echo -e "dstat_attribute\t" >> $dst_dstat_file
fi


for (( idx = 0; idx < arr_len; idx++ ))
do
if [ $idx -eq 6 ] || [ $idx -eq 7 ]
then
echo -ne "${arr_str[$idx]}\t" >> $dst_dstat_file
fi
done


grep "writ" $dst_dstat_file &>/dev/null
if [ $? -eq 0 ]
then
echo -ne "\t\n" >> $dst_dstat_file
fi


return 0
}


dstat_read=0
dstat_write=0
dstat_index=0
dstat_max=0
function cmd_proc_dstat_write_value()
{
value_read=$1
value_write=$2




if [ $dstat_index -eq 0 ]
then
let dstat_index=dstat_index+1
return 0
fi


if [[ $value_read == *"k" ]] 
then
value_read=${value_read%k}
value_read=`echo "scale=2; $value_read / 1024" | bc`
elif [[ $value_read == *"B" ]]
then
value_read=${value_read%B}
value_read=`echo "scale=2; $value_read / 1024 / 1024" | bc`
else
value_read=${value_read%M}
fi
dstat_read=`echo "scale=2; $dstat_read + $value_read" | bc`


if [[ $value_write == *"k" ]] 
then
value_write=${value_write%k}
value_write=`echo "scale=2; $value_write / 1024" | bc`
elif [[ $value_write == *"B" ]]
then
value_write=${value_write%B}
value_write=`echo "scale=2; $value_write / 1024 / 1024" | bc`
else
value_write=${value_write%M}
fi
dstat_write=`echo "scale=2; $dstat_write + $value_write" | bc`


let dstat_index=dstat_index+1
if [ $dstat_index -eq $((dstat_max + 1)) ]
then
dstat_read=`echo "scale=2; $dstat_read / $dstat_max" | bc`
dstat_write=`echo "scale=2; $dstat_write / $dstat_max" | bc`
echo -e "$dstat_read\t$dstat_write\t\t" >> $dst_dstat_file
dstat_write=0
dstat_read=0
dstat_index=0
fi

return 0
}


function cmd_proc_dstat()
{
str_log=$1
if [[ $str_log == *"You did not select"* ]] || [[ $str_log == *"total-cpu-usage"* ]]
then 
return 0
fi


#split str_log into array by '|'
arr_str=(${str_log//|/ })
#get the number of element in arr_str
arr_len=${#arr_str[@]}
#get the number of attribute in arr_str
attribute_num=`expr $arr_len / 3`


if [[ $str_log == *"usr sys idl wai hiq siq"* ]]
then
cmd_proc_dstat_write_title "${arr_str[*]}" $arr_len
return 0
fi


cmd_proc_dstat_write_value ${arr_str[6]} "${arr_str[7]}"


return 0
}


function cmd_proc_top_write_title()
{
grep "attribute" $dst_top_file &>/dev/null
if [ $? -eq 0 ]
then
return 0
fi


echo -e "top_attribute\t\t\t\t\t\t\t\t\ncpu_id(%)\tosd_max(M)\tosd_min(M)\tmon(M)\t\tmds(M)\tganesha(M)\t" >>$dst_top_file


return 0
}


function cmd_proc_top_osd_info()
{
osd_value=$1

if [[ $osd_value == *"g" ]]
then
osd_value=${osd_value%g}
osd_value=`echo "scale=2; $osd_value * 1024" | bc`
elif [[ $osd_value == *"m" ]]
then
osd_value=${osd_value%m}
else
osd_value=`echo "scale=2; $osd_value / 1024" | bc`
fi


ret=`echo "$top_osd_max < $osd_value" | bc`
if [ $ret -eq 1 ]
then
top_osd_max=`echo "scale=2; $osd_value / 1" | bc`
fi


if [ `echo "$top_osd_min == 0" | bc` -eq 1 ]
then
top_osd_min=$osd_value
elif [ `echo "$top_osd_min > $osd_value" | bc` -eq 1 ]
then
top_osd_min=$osd_value
fi


return 0
}


function cmd_proc_top_mon_info()
{
mon_value=$1
if [ $mon_value == *"g" ]
then
mon_value=${mon_value%g}
mon_value=`echo "scale=2; $mon_value * 1024" | bc`
elif [ $mon_value == *"m" ]
then
mon_value=${mon_value%m}
else
mon_value=`echo "scale=2; $mon_value / 1024" | bc`
fi


top_mon=$mon_value


return 0
}


function cmd_proc_top_mds_info()
{
mds_value=$1
if [ $mds_value == *"g" ]
then
mds_value=${mds_value%g}
mds_value=`echo "scale=2; $mds_value * 1024" | bc`
elif [ $mds_value == *"m" ]
then
mds_value=${mds_value%m}
else
mds_value=`echo "scale=2; $mds_value / 1024" | bc`
fi


top_mds=$mds_value


return 0
}


function cmd_proc_top_ganesha_info()
{
ganesha_value=$1
        if [ $mds_value == *"g" ]
        then
                ganesha_value=${ganesha_value%g}
                ganesha_value=`echo "scale=2; $ganesha_value * 1024" | bc`
        elif [ $ganesha_value == *"m" ]
        then
                ganesha_value=${ganesha_value%m}
        else
                ganesha_value=`echo "scale=2; $ganesha_value / 1024" | bc`
        fi


        top_ganesha=$ganesha_value


        return 0
}


top_is_first=0
top_osd_max=0
top_osd_min=0
top_mon=0
top_mds=0
top_mds_cunt=1
top_ganesha=0
function cmd_proc_top()
{
str_log=$1
if [[ $str_log == *"Cpu"* ]]
then
let top_is_first=top_is_first+1
fi


if [ $top_is_first -eq 1 ]
then
return 0
fi

if [[ $str_log == "top_done" ]] && [[ $top_is_first -eq 2 ]]
then
echo -ne "$top_osd_max\t\t$top_osd_min\t\t$top_mon\t\t$top_mds\t$top_ganesha\t\n" >> $dst_top_file
top_is_first=0
top_osd_max=0
top_osd_min=0
return 0
elif [[ $str_log == "top_done" ]]
then
return 0
fi


cmd_proc_top_write_title
arr_str=(${str_log//,/ })
arr_len=${#arr_str[@]}


case $str_log in
*"Cpu"*)
echo -ne "${arr_str[7]}\t\t" >> $dst_top_file
;;
*"icfs-osd"*)
cmd_proc_top_osd_info "${arr_str[5]}"
;;
*"icfs-mon"*)
cmd_proc_top_mon_info "${arr_str[5]}"
;;
*"icfs-mds"*)
cmd_proc_top_mds_info "${arr_str[5]}"
;;
*"ganesha"*)
cmd_proc_top_ganesha_info "${arr_str[5]}"
;;
esac


return 0
}


function cmd_proc_iostat_write_title()
{
grep "attribute" $dst_iostat_file &>/dev/null
if [ $? -eq 0 ]
then
return 0
fi


echo -e "iostat_attribute\t\nwMB/s\t\tutil\t" >> $dst_iostat_file


return 0
}


iostat_is_first=0
iostat_average_wr=0
iostat_average_util=0
iostat_index=0
function cmd_proc_iostat()
{
str_log=$1
if [[ $str_log == *"Device:"* ]]
then
let iostat_is_first=iostat_is_first+1
fi


if [ $iostat_is_first -le 1 ] 
then
return 0
fi


if [[ $str_log != "sd"* ]] && [[ $str_log != "iostat_done" ]]
then
return 0
fi


arr_str=(${str_log// / })
if [ ${arr_str[0]} == "sda" ]
then
return 0
fi


if [[ $str_log = "iostat_done" ]] && [[ $iostat_is_first -eq 2 ]]
then
iostat_average_wr=`echo "scale=2; $iostat_average_wr / $iostat_index" | bc`
iostat_average_util=`echo "scale=2; $iostat_average_util / $iostat_index" | bc`
echo -e "$iostat_average_wr\t\t$iostat_average_util\t" >> $dst_iostat_file
iostat_average_wr=0
iostat_average_util=0
iostat_index=0
iostat_is_first=0
return 0
elif [[ $str_log = "iostat_done" ]]
then
return 0
fi


cmd_proc_iostat_write_title
wrqm_ret=`echo "${arr_str[2]} == 0.00" | bc`
if [[ $wrqm_ret -eq 1 ]]
then
return 0
fi


let iostat_index=iostat_index+1
iostat_average_wr=`echo "scale=2; $iostat_average_wr + ${arr_str[6]}" | bc`
iostat_average_util=`echo "scale=2; $iostat_average_util + ${arr_str[13]}" | bc`


return 0
}


function cmd_proc_free()
{
str_log=$1

grep "attribute" $dst_free_file &>/dev/null
if [ $? -ne 0 ]
then
echo -e "free_attribute\t\t\t\nfree(M)\tbuff/cache(M)\t" >> $dst_free_file
fi


arr_str=(${str_log// / })
if [[ ${arr_str[0]} == "Mem:" ]]
then
echo -e "${arr_str[3]}\t${arr_str[5]}\t" >> $dst_free_file
fi


return 0
}


function analyze_source_data_file()
{
while read str_line
do
case $str_line in
*"$str_icfs"*)
str_flag="icfs"
continue
;;
*"$str_dstat"*)
str_flag="dstat"
arr_str=(${str_line//,/ })
dstat_max=${arr_str[3]}
continue
;;
*"$str_top"*)
str_flag="top"
top_mds_cunt=1
continue
;;
*"$str_iostat"*)
str_flag="iostat"
continue
;;
*"$str_free"*)
str_flag="free"
continue
;;
*"$str_start"* | *"$str_end"*)
cmd_proc_top "top_done"
cmd_proc_iostat "iostat_done"
continue
;;
esac

case $str_flag in
"icfs")
cmd_proc_icfs "$str_line"
;;
"dstat")
cmd_proc_dstat "$str_line"
;;
"top")
cmd_proc_top "$str_line"
;;
"iostat")
cmd_proc_iostat "$str_line"
;;
"free")
cmd_proc_free "$str_line"
;;
esac

done < $src_file


return 0
}


function merge_submodule_data_file()
{
paste -d " " $dst_iostat_file $dst_top_file  $dst_free_file $dst_dstat_file $dst_icfs_file > $dst_file
cat $dst_file
rm -rf $dst_iostat_file $dst_top_file  $dst_free_file $dst_dstat_file $dst_icfs_file


return 0
}


analyze_source_data_file
merge_submodule_data_file

阅读(1322) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~