5fcc6d6d25dc97282.jpg_fo742.jpg

#! /bin/bash

Disks=`df -h | awk '{a[NR]=$0}END{for(i=2;i<=NR;i++)print a[i]}'`
# awk 'BEGIN{sum=0}{sum+=$1}END{print sum}' 
i=1
parent=
capacity=0
disksize=0

function size(){
    disksize=`echo $1 | tr -cd "[0-9,.]"`
    unit=`echo $1 | sed 's/[0-9,.]//g'`
    if [[ $unit == "T" ]];then
          #disksize=`echo $disksize*1024*1024 | bc`
          disksize=$(echo $disksize |awk '{ printf "%0.2f\n" ,$1*1024*1024}')
     fi
    if [[ $unit == "G" ]];then
         #disksize=`echo $disksize*1024 | bc`
         disksize=$(echo $disksize |awk '{ printf "%0.2f\n" ,$1*1024}')
     fi
    if [[ $unit == "M" ]];then
        #disksize=`echo $disksize | bc`
        disksize=$(echo $disksize |awk '{ printf "%0.2f\n" ,$1}')
    fi
}

for property in $Disks
do
    case $i in 
        1)
            str="$str,'cpname':'disk'"
            str="$str,'filesystem':'$property'"
        ;;
        2)
            size $property
            str="$str,'capacity':'$disksize'"
        ;;
        6)
            str="$str,'path':'$property'"
        ;;
    esac    
    i=$(($i+1))
    t=$(($i%7))
    if [ $t = 0 ];then
        str="$str,'os':'linux','name':'(linux)硬盘监控模板'"
        str="${str#*,}"
        parent="$parent{$str},"
        str=
        i=1
    fi
done

result="[${parent%,*}]"
result=`echo ${result//\'/\"}`
echo $result





发表评论

您的电子邮箱地址不会被公开。

Captcha Code