DanieleO
DanieleO

Reputation: 472

CPU Usage Server and Monitoring info

I need to check, using a shell script, possibly without installing any particular package (OS:Linux Suse 12), the total CPU % usage in order to monitor the level without pass the critical threshold.

It is a Huge server with 2x E5-2667 v4 8/core.

enter image description here

Looking over the questions I found something and I tried it:

1-top -bn1 | grep "Cpu(s)" | \sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \awk '{print 100 - $1"%"}'

2-CPU_LOAD=$(sar -P ALL 1 2 |grep 'Average.*all' |awk -F" " '{print 100.0 -$NF}') 

I also tried to do 100-idle from iostat

  1. Is that really correct on a multi cpu/multi core system?
  2. Is correct calculate the cpu total usage by using the cpu load from the uptime?

Using the code i got an avg of single core, While i need a result of a total CPU used in % Regards, Thanks

Upvotes: 0

Views: 559

Answers (3)

gbajson
gbajson

Reputation: 1730

If you would like to get any detailed stats you might also use perf. In this example you may see the number of all CPU cycles during 1 second:

-bash-4.1# perf stat -a sleep 1

Performance counter stats for 'system wide':

   4002.822144      task-clock (msec)         #    3.999 CPUs utilized            (100.00%)
         22809      context-switches          #    0.006 M/sec                    (100.00%)
          1332      cpu-migrations            #    0.333 K/sec                    (100.00%)
         23794      page-faults               #    0.006 M/sec                  
    5409531172      cycles                    #    1.351 GHz                      (100.00%)
<not supported>      stalled-cycles-frontend  
<not supported>      stalled-cycles-backend   
    3874289082      instructions              #    0.72  insns per cycle          (100.00%)
     715152901      branches                  #  178.662 M/sec                    (100.00%)
      20583742      branch-misses             #    2.88% of all branches        
   1.001065623 seconds time elapsed

Upvotes: 1

gbajson
gbajson

Reputation: 1730

You may also check uptime.

Uptime gives a one line display of the following information. The current time, how long the system has been running, how many users are currently logged on, and the system load averages for the past 1, 5, and 15 minutes.

Upvotes: 0

Raman Sailopal
Raman Sailopal

Reputation: 12867

You are probably better implementing the solution completely in awk:

top -bn1 | awk -F, '/id/ { for (i=1;i<=NF;i++) { if ( $i ~ /[[:digit:]]{2}.[[:digit:]][[:blank:]]+id/ ) { split($i,arry," ");print arry[1]" - idle" }'

Take the output from top and then check for any output containing id. If the condition is met, take each comma delimited piece of data on the line and pattern match against 2 numbers, a decimal and then one or more numbers, a blank and then id. If this is the case, split the variable based on a blank space into an array and print the first element.

Upvotes: 1

Related Questions