2017-11-14 9 views
0

私は平均使用率を示したスクリプトに基づいて、各CPUの使用率を取得するスクリプトを作成しました。私が見つけたスクリプトはすごく効果的です。毎秒パーセンテージが変化します。残念ながら、私が書いたスクリプトで表示されるパーセンテージはまったく変わりません。あなたは私が間違っていたことのアイデアを持っているのですか、あるいはあなたは各CPUの利用率を知る良いアイディアを持っていますか?各CPUの使用率をどのように取得できますか?

EDITは

はここ

「ALL CPU間MAXの使用量をGETすること」である作業部は、非作業部分は、「CPUの平均使用率をGET」部分されている私スクリプトは、それが平均CPU使用率と最も使用されているCPUが表示されます。

#!/bin/bash 


PREV_TOTAL=0 
PREV_IDLE=0 

while true; do 

# GET THE MEAN USAGE OF THE CPU 
# ----------------------------- 
    # we get the mean of the cpu usage 
    CPU=(`cat /proc/stat | grep '^cpu '`) # Get the total CPU statistics. 
    unset CPU[0]       # Discard the "cpu" prefix. 
    IDLE=${CPU[4]} 
    # Calculate the total CPU time. 
    TOTAL=0 
    for VALUE in "${CPU[@]:0:4}"; do 
    let "TOTAL=$TOTAL+$VALUE" 
    done 

    # Calculate the CPU usage since we last checked. 
    let "DIFF_IDLE=$IDLE-$PREV_IDLE" 
    let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL" 
    let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10" 

    # Remember the total and idle CPU times for the next check. 
    PREV_TOTAL="$TOTAL" 
    PREV_IDLE="$IDLE" 

# GET THE MAX USAGE BETWEEN ALL THE CPUS 
# -------------------------------------- 

    # here we get an array with all the cpus 
    CPUS=(`cat /proc/stat | grep '^cpu[0-9]'`) 
    lengthArray=${#CPUS[@]} 
    numberOfCpus=$((lengthArray/11)) 
    i=0 
    numberOfCpus=$((lengthArray/11)) 
    TOTALS=() 
    IDLES=() 
    PERCENTAGES=() 
    PREV_TOTALS=() 
    PREV_IDLES=() 
    # we instenciate the arrays and set their values to 0 
    while [ $i -lt $numberOfCpus ]; do 
    TOTALS+=(0) 
    IDLES+=(0) 
    PERCENTAGES+=(0) 
    PREV_TOTALS+=(0) 
    PREV_IDLES+=(0) 
    i=$((i+1)) 
    done 

    i=0 
    index=1 
    limit=$((index+4)) 
    # we loop through the array to get each total for each cpu 
    while [ $i -lt $numberOfCpus ]; do 
    IDLES[$i]=${CPUS[$index+3]} 
    # since we only want the first four numbers for each cpu, we have to loop in a strange way 
    while [ $index -lt $limit ]; do 
     TOTALS[$i]=$((TOTALS[$i]+CPUS[$index])) 
     index=$((index+1)) 
    done 
    # 7 is the number of array element before the next series of number that we want 
    index=$((limit+7)) 
    # we set the limit to four+the index because we only want the four numbers after the index 
    limit=$((index+4)) 
    i=$((i+1)) 
    done 

    # now we calculate the percentage of usage for every cpu so we can get the max percentage 
    i=0 

    # we calculate the percentage for each cpu 
    while [ $i -lt $numberOfCpus ]; do 
    let "DIFF_IDLE_i=${IDLES[$i]}-${PREV_IDLES[$i]}" 
    let "DIFF_TOTAL_i=${TOTALS[$i]}-${PREV_TOTALS[$i]}" 
    let "DIFF_USAGE_i=(1000*($DIFF_TOTAL_i-$DIFF_IDLE_i)/$DIFF_TOTAL_i+5)/10" 
    PERCENTAGES[$i]=$DIFF_USAGE_i 
    PREV_TOTALS[$i]=${TOTALS[$i]} 
    PREV_IDLES[$i]=${IDLES[$i]} 
    i=$((i+1)) 
    done 

    MAX=${PERCENTAGES[0]} 
    cpu=0 
    i=0 
    # here we look for the max 
    for percent in ${PERCENTAGES[@]}; do 
    if [ $percent -gt $MAX ]; then 
     cpu=$i 
     MAX=$percent 
    fi 
    i=$((i+1)) 
    done 

    echo -en "\rCPU: $DIFF_USAGE% CPU$cpu: $MAX% \b\b" 

    # Wait before checking again. 
    sleep 1 
done 

答えて

0

私はループの中で私の配列を初期化し、そしてそれがoutsid初期化するために必要な、私の問題をついに発見しましたeループ。

#!/bin/bash 

# this function is used to get the infos about the cpu 
getCpuScores() { 
    CPU=(`cat /proc/stat | grep ^cpu$1`) 
} 

# this function is used to get the percentage of utilization of a cpu 
getPercentageOfCpu() { 
    unset CPU[0] 
    local idle=${CPU[4]} 
    local total=0 
    for val in "${CPU[@]:0:4}"; do 
     let "total=$total+$val" 
    done 

    let "diff_idle=$idle-${PREV_IDLES[$1]}" 
    let "diff_total=$total-${PREV_TOTALS[$1]}" 
    let "diff_usage=(1000*($diff_total-$diff_idle)/$diff_total+5)/10" 

    PERCENTAGES[$1]=$diff_usage 
    PREV_IDLES[$1]=$idle 
    PREV_TOTALS[$1]=$total 
} 


# first we initialize all the needed variables 
PREV_TOTAL=0 
PREV_IDLE=0 
# this is to get the number of cpu 
CPUS=(`cat /proc/stat | grep '^cpu[0-9]'`) 
lengthArray=${#CPUS[@]} 
numberOfCpus=$((lengthArray/11)) 
i=0 
PREV_TOTALS=() 
PREV_IDLES=() 
PERCENTAGES=() 
# we instenciate the arrays and set their values to 0 
while [ $i -lt $numberOfCpus ]; do 
    PREV_TOTALS+=(0) 
    PREV_IDLES+=(0) 
    PERCENTAGES+=(0) 
    i=$((i+1)) 
done 

# MAIN LOOP 

while true; do 

# GET THE MEAN USAGE OF THE CPU 
# ----------------------------- 
    # we get the mean of the cpu usage 
    CPU=(`cat /proc/stat | grep '^cpu '`) # Get the total CPU statistics. 
    unset CPU[0]       # Discard the "cpu" prefix. 
    IDLE=${CPU[4]} 
    # Calculate the total CPU time. 
    TOTAL=0 
    for VALUE in "${CPU[@]:0:4}"; do 
    let "TOTAL=$TOTAL+$VALUE" 
    done 

    # Calculate the CPU usage since we last checked. 
    let "DIFF_IDLE=$IDLE-$PREV_IDLE" 
    let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL" 
    let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10" 

    # Remember the total and idle CPU times for the next check. 
    PREV_TOTAL="$TOTAL" 
    PREV_IDLE="$IDLE" 

# GET THE MAX USAGE BETWEEN ALL THE CPUS 
# ------------------------ 
    # first we get the percentage of utilization for each cpu 
    i=0 
    while [ $i -lt $numberOfCpus ]; do 
    # we get the cpu score to be able to calculate the percentage of utilization 
    getCpuScores $i 
    # then we calculate the percentage of the cpu and put it in an array 
    getPercentageOfCpu $i 

    i=$((i+1)) 

    done 

    # then we get the max 
    MAX=${PERCENTAGES[0]} 
    cpu=0 
    i=0 

    while [ $i -lt $numberOfCpus ]; do 
    if [ ${PERCENTAGES[$i]} -gt $MAX ]; then 
     MAX=${PERCENTAGES[$i]} 
     cpu=$i 
    fi 
    i=$((i+1)) 
    done 

    # finally we display the avg cpu usage and the max cpu usage 
    echo -en "\rCPU: $DIFF_USAGE% CPU$cpu: $MAX% \b\b" 

    # Wait before checking again. 
    sleep 1 
done 

スクリプトは、平均CPU使用率と最も使用されているCPUの使用率を表示します。私は誰もが興味を持っている場合は、ここでは、最終的なスクリプトです、スクリプトを動作させるために管理しました。

関連する問題