Syed Jahanzaib Personal Blog to Share Knowledge !

December 23, 2016

Howto get combined average Load of multi core CPU by BASH

Filed under: Linux Related — Tags: , — Syed Jahanzaib / Pinochio~:) @ 11:46 AM

cpu-load

Following is a small script to get combined multi-core CPU load (average) from remote server. I made it to create MRTG graph for CPU load of all cores combined average value for our local ESXI servers.


#!/bin/bash
# Script to query ESXI server
#set -x
HOST="$1"
SNMP_STRING="public"
TMP_HOLDER="/tmp/cpuinfo.$HOST"
OID="hrProcessorLoad."
MIB="/cfg/mibs/HOST-RESOURCES-MIB"
snmpwalk -v2c -c $SNMP_STRING -m "$MIB" $HOST |grep $OID | awk '{print $4}' > $TMP_HOLDER
CPU_CORE=`cat $TMP_HOLDER | wc -l`
CPU_LOAD_SUM=`cat $TMP_HOLDER | awk '{ sum += $1 } END { print sum }'`
CPU_LOAD_FINAL=`echo "scale=2; $CPU_LOAD_SUM/$CPU_CORE" | bc -l`
echo $CPU_LOAD_FINAL
echo $CPU_LOAD_FINAL


Result:

(following result is average value of my esxi server which have 2 physical processor with 6 cores each, so virtually there are 24 cores for ESXI guests). SNMP must be enabled on remote esxi server , I wrote on how it can be enabled one esxi, see my old postings on it.

root@linux:/temp# ./cpu_load.sh 10.0.0.1

Output:

6.70
6.70


CFG example:

ESXI Server -10.0.0.1- CPU Utilization ###
Target[esxi_cpu_1010.0.0.1]: `/temp/cpu_load.sh 10.0.0.1`
Options[esxi_cpu_1010.0.0.1]: growright, gauge, integer, noo
MaxBytes[esxi_cpu_1010.0.0.1]: 100
LegendI[esxi_cpu_1010.0.0.1]: Total CPU Cores Used %
LegendO[esxi_cpu_1010.0.0.1]:
Legend1[esxi_cpu_1010.0.0.1]: Total CPU Cores Used %
Legend2[esxi_cpu_1010.0.0.1]:
Title[esxi_cpu_1010.0.0.1]: ESXI Server -10.0.0.1- CPU Load %
PageTop[esxi_cpu_1010.0.0.1]: <h1>ESXI Server -10.0.0.1- CPU Load %</h1>
Unscaled[esxi_cpu_1010.0.0.1]: ymwd
ShortLegend[esxi_cpu_1010.0.0.1]: %
YLegend[esxi_cpu_1010.0.0.1]: % CPU Used
Colours[esxi_cpu_1010.0.0.1]: RED#ff4f27,DIRTY YELLOW#E6B420,RED#ff4f27,DIRTY YELLOW#E6B420

Syed.Jahanzaib!

3 Comments »

  1. Not work

    root@lab: /etc/mrtg/scripts # ./cpu_load.sh 192.168.33.6
    (standard_in) 1: syntax error

    Comment by I B — December 29, 2016 @ 4:42 AM


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: