top 명령의 출력과 같은 가동 시간 로드 평균 및 CPU% 열의 출력 2개가 포함된 파일이 있습니다.
그렇다면 CPU 사용률 또는 일회성 CPU 사용량으로 계산할 수 있습니까?
예:
=====================================================================
APPLIANCE CPU USAGE
=====================================================================
0.90 1.05 0.84 1/151 13615
=====================================================================
APPLIANCE PROCESSES
=====================================================================
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 2184 700 ? Ss 22:48 0:00 init [3]
root 2 0.0 0.0 0 0 ? S< 22:48 0:00 [migration/0]
root 3 0.0 0.0 0 0 ? SN 22:48 0:00 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S< 22:48 0:00 [watchdog/0]
root 5 0.0 0.0 0 0 ? S< 22:48 0:00 [migration/1]
root 6 0.0 0.0 0 0 ? SN 22:48 0:00 [ksoftirqd/1]
root 7 0.0 0.0 0 0 ? S< 22:48 0:00 [watchdog/1]
root 8 0.0 0.0 0 0 ? S< 22:48 0:00 [events/0]
root 9 0.0 0.0 0 0 ? S< 22:48 0:00 [events/1]
root 10 0.0 0.0 0 0 ? S< 22:48 0:00 [khelper]
root 11 0.0 0.0 0 0 ? S< 22:48 0:00 [kthread]
root 15 0.0 0.0 0 0 ? S< 22:48 0:00 [kblockd/0]
root 16 0.0 0.0 0 0 ? S< 22:48 0:00 [kblockd/1]
root 17 0.0 0.0 0 0 ? S< 22:48 0:00 [kacpid]
root 163 0.0 0.0 0 0 ? S< 22:48 0:00 [cqueue/0]
root 164 0.0 0.0 0 0 ? S< 22:48 0:00 [cqueue/1]
root 167 0.0 0.0 0 0 ? S< 22:48 0:00 [khubd]
root 169 0.0 0.0 0 0 ? S< 22:48 0:00 [kseriod]
root 242 0.0 0.0 0 0 ? S 22:48 0:00 [khungtaskd]
root 243 0.0 0.0 0 0 ? S 22:48 0:00 [pdflush]
root 244 0.0 0.0 0 0 ? S 22:48 0:00 [pdflush]
root 245 0.0 0.0 0 0 ? S< 22:48 0:00 [kswapd0]
root 246 0.0 0.0 0 0 ? S< 22:48 0:00 [aio/0]
root 247 0.0 0.0 0 0 ? S< 22:48 0:00 [aio/1]
root 411 0.0 0.0 0 0 ? S< 22:49 0:00 [kpsmoused]
root 440 0.0 0.0 0 0 ? S< 22:49 0:00 [ata/0]
root 441 0.0 0.0 0 0 ? S< 22:49 0:00 [ata/1]
root 442 0.0 0.0 0 0 ? S< 22:49 0:00 [ata_aux]
root 446 0.0 0.0 0 0 ? S< 22:49 0:00 [scsi_eh_0]
root 447 0.0 0.0 0 0 ? S< 22:49 0:00 [scsi_eh_1]
root 448 0.0 0.0 0 0 ? S< 22:49 0:00 [scsi_eh_2]
root 449 0.0 0.0 0 0 ? S< 22:49 0:00 [scsi_eh_3]
root 450 0.0 0.0 0 0 ? S< 22:49 0:00 [scsi_eh_4]
root 451 0.0 0.0 0 0 ? S< 22:49 0:00 [scsi_eh_5]
root 452 1.0 0.0 0 0 ? S< 22:49 0:00 [kjournald]
root 469 2.0 0.0 0 0 ? S< 22:49 0:00 [kauditd]
root 497 3.0 0.0 2368 672 ? S<s 22:49 0:00 /sbin/udevd -d
root 1395 4.0 0.0 0 0 ? S< 22:49 0:00 [kjournald]
root 1397 5.0 0.0 0 0 ? S< 22:49 0:00 [kjournald]
root 1399 6.0 0.0 0 0 ? S< 22:49 0:00 [kjournald]
답변1
다음 코드를 사용하여 CPU%를 계산할 수 있습니다.
#!/bin/bash
FILE=<file path>
CPU_USAGE=`sed -n 'H; /^USER/h; ${g;p;}' $FILE | awk '{print $3}' | awk '{sum+=$1} END {print sum ;}'`
CPU_CORES=`nproc --all`
ACTUAL_CPU_USAGE=`echo "$CPU_USAGE $CPU_CORES" | awk '{print $1/$2"%"}'`
echo "CPU : $ACTUAL_CPU_USAGE"