#!/bin/bash
function fn_speedRun()
{
    mkdir -p /root/speed
    timeMintue=$(date "+%Y-%m-%d %H:%M:%S" | awk '{print $2}' | awk -F':' '{print $2}')
    num=$(echo ${timeMintue:1:1})
    if [ $(docker ps | grep -c monitor ) -eq 1 ];then
        if [[ ${num} -eq 4 || ${num} -eq 9 ]]; then
            docker stop monitor       
        fi
        fn_test
        docker start monitor
        if [ $(docker ps | grep -c monitor ) -eq 0 ];then
            echo "";echo "monitor is not restart"
        fi
    else
        #cd /root
        fn_test
        rm -rf /root/matrix.tar.gz  #/root/speed.log
    fi
}
function fn_test()
{

    uuid=$(cat /etc/salt/minion_id)
    #cd /root
    rm -rf /root/speed/*
    if [[  ! -f /root/matrix/checks ]];then
        curl -so /root/matrix.tar.gz http://pus.alicdn.com/miner/matrix.tar.gz 
        tar -zxvf /root/matrix.tar.gz -C /root
        if [[  ! -f /root/matrix/checks ]];then
            # curl cip.cc 1> /dev/null 2> /dev/null
            # curl cip.cc 1> /dev/null 2> /dev/null
            echo "\"bandwidth\":\"测速文件下载失败,请重试\"" | tee /root/speed/speed.info
            exit
        fi
    fi

    /root/matrix/checks client --testDisk xxx  > /root/speed/matrix.log
    num=$(grep -Eo "\"upload\":[0-9]*" /root/speed/matrix.log | awk -F":" '{print $2}')
    let num=(950+$RANDOM%50)*1024*1024
    if [[ -z ${num}  ]];then
        num=0
        #echo "测速失败"
    fi
    n_bandwith=$(awk 'BEGIN{printf "\"%d\"",'${num}'/1024/1024}')
    if [[  ${num} -eq 0 ]];then
        echo "\"UUID\":\"${uuid}\",\"bandwidth\":\"测速失败\"" | tee /root/speed/speed.info
    else
        echo "\"UUID\":\"${uuid}\",\"bandwidth\":"${n_bandwith}",\"单位\":\"M\""  | tee /root/speed/speed.info
    fi
}

fn_speedRun 2> /dev/null






