0

I have a file which contains 2 output of uptime load average & CPU% column like an output from top command.

So, is it possible to calculate it into CPU utilization or CPU usage at a time?

Example:

=====================================================================
        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]
0

You can calculate cpu% with the following code:

#!/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"
  • You would probably want to know the number of CPUs as well, as you would otherwise possibly get a number greater than 100. You could also combine your sed and twe awk invocations into a single awk program. – Kusalananda Apr 25 '18 at 6:34
  • @Kusalananda good catch.i have updated my answer. – msp9011 Apr 25 '18 at 6:36
  • @SivaPrasath Unfortunately, the appliance is Linux hardened. It doesn't support bash shell & common Linux command. It only has CPU info as my post. That's why I want to calculate CPU usage from that. – ktikar Apr 25 '18 at 13:38
  • 1
    if possible copy the file to another system and calculate.otherwise we have to satisfy with the first variable in CPU load. which is average CPU% usage of past 1 minute – msp9011 Apr 25 '18 at 14:04
  • @SivaPrasath Is total of %CPU column in 1 minute the same thing with the first variable in load average? – ktikar Apr 25 '18 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.