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!

Single Script to Create All Storage CFG file in MRTG

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

auto

Reference Post:

It’s a headache if you want to create configuration file in bulk for all the storage available in remote servers, that includes disks/memory. To make life a little easier, I made following script which simply query the storage indexes and create CFG file in /cfg folder.

#!/bin/bash
# Script to query remote server
#set -x
# Colors Config . . . [[ JZ . . . ]]
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
MIB_NAME="HOST-RESOURCES-MIB"

#If MIB is not present, EXIT with error
MIB_CHECK=`locate $MIB_NAME > /tmp/miblocate.txt`
MIB_CHECK_RESULT=`cat /tmp/miblocate.txt`
if [ -z "$MIB_CHECK_RESULT" ]; then
echo -e "$COL_RED MRTG ERROR MNF1: HOST-RESOURCES-MIB not found, cannot continue without it. Download them first ... $COL_RESET"
exit 1
fi

HOST="$1"
# Temporary Folder where all CFG will be placed . . . [[ JZ . . . ]]
CFGDIR="cfg"
# Checking if $CFGDIR folder is previously present or not . . .
{
if [ ! -d "/$CFGDIR" ]; then
echo
echo -e "$COL_RED /$CFGDIR folder not found, Creating it so all cfg will be placed here . . . $COL_RESET"
mkdir /temp
else
echo
echo -e "$COL_GREEN /$CFGDIR folder is already present , so no need to create it, Proceeding further . . . $COL_RESET"
echo
fi
}

CFG_FILE_NAME="/$CFGDIR/storage.$1.cfg"
SNMP_STRING="PUBLIC"
snmpwalk -Os -c $SNMP_STRING -v 2c -m "/cfg/mibs/HOST-RESOURCES-MIB" $HOST hrStorageDescr |
sed -nre 's,hrStorageDescr.([0-9]*) = STRING: (.*),\1 \2,p' |
grep -Ev ' /(proc|sys)($|/)' |
while read NM DESC; do

STORAGE_SIZE=`snmpwalk -Onqv -Os -c $SNMP_STRING -v 2c -m "/cfg/mibs/HOST-RESOURCES-MIB" $HOST hrStorageSize.${NM}`
ALLOCATION_UNIT=`snmpwalk -Onqv -Os -c $SNMP_STRING -v 2c -m "/cfg/mibs/HOST-RESOURCES-MIB" $HOST hrStorageAllocationUnits.${NM} | awk '{print $1}'`

# FORMULAS
MAXSIZE_BITS=`echo $(($STORAGE_SIZE*$ALLOCATION_UNIT*8))`
MAXSIZE_MB=`echo $(($STORAGE_SIZE*$ALLOCATION_UNIT/1024/1024))`
MAXSIZE_GB=`echo $(($STORAGE_SIZE*$ALLOCATION_UNIT/1024/1024/1024))`
MAXSIZE_TB=`echo $(($STORAGE_SIZE*$ALLOCATION_UNIT/1024/1024/1024/1024))`

#TITLE LINES
MAXSIZE_MB_T=`echo "scale=2; $STORAGE_SIZE*$ALLOCATION_UNIT / 1024 / 1024" | bc -l`
MAXSIZE_GB_T=`echo "scale=2; $STORAGE_SIZE*$ALLOCATION_UNIT/1024/1024/1024" | bc -l`
MAXSIZE_TB_T=`echo "scale=2; $MAXSIZE_GB/1000" | bc -l`

if [ $MAXSIZE_BITS -eq 0 ]; then
MAXSIZE_BITS="1"
fi
# Under 1GB TITLE
if [ $MAXSIZE_MB -lt 1024 ]; then
#echo "$hrStorageSize.${NM} = MAX Size in MB = $MAXSIZE_MB_T"
MAXSIZE_TITLE="MAX Size in MB = $MAXSIZE_MB_T"
fi
# ABOVE 1GB TITLE
if [ $MAXSIZE_MB -gt 1024 ]; then
#echo "$hrStorageSize.${NM} = MAX Size in GB = $MAXSIZE_GB_T"
MAXSIZE_TITLE="MAX Size in GB = $MAXSIZE_GB_T"
fi
# ABOVE 1 TB TITLE
if [ $MAXSIZE_MB -gt 1048576 ]; then
#echo "$hrStorageSize.${NM} = MAX Size in TB = $MAXSIZE_TB_T"
MAXSIZE_TITLE="MAX Size in TB = $MAXSIZE_TB_T"
fi

IDENT="stor_$(echo "${DESC}" | tr '[A-Z]/ ' '[a-z]_' | sed 's/\:/-/g')"
echo "Target[${IDENT}]: hrStorageUsed.${NM}&hrStorageSize.${NM}:$SNMP_STRING@$HOST * hrStorageAllocationUnits.${NM}&hrStorageAllocationUnits.${NM}:$SNMP_STRING@$HOST"
echo "Title[${IDENT}]: $HOST - Storage: ${DESC} : / $MAXSIZE_TITLE"
echo "PageTop[${IDENT}]: <h1>$HOST - Storage: ${DESC} Report / $MAXSIZE_TITLE</h1>"
echo "Kilo[${IDENT}]: 1024"
echo "MaxBytes[${IDENT}]: $MAXSIZE_BITS"
echo "ShortLegend[${IDENT}]: iB"
echo "YLegend[${IDENT}]: Bytes"
echo "Legend1[${IDENT}]: Used"
echo "Legend2[${IDENT}]: Total"
echo "LegendI[${IDENT}]: Used:"
echo "LegendO[${IDENT}]: Total:"
echo "Options[${IDENT}]: gauge,growright,nobanner,nopercent"
done > $CFG_FILE_NAME

if [ -f $CFG_FILE_NAME ];
then
echo -e "$COL_GREEN MRTG CFG file name $CFG_FILE_NAME for $HOST is created ... $COL_RESET"
else
echo -e "$COL_RED ERROR: Unable to create CFG FILEs, check script errors ... $COL_RESET"
fi

echo -e "$COL_GREEN
SCRIPT ENDS HERE ... $COL_RESET"


Run Method:

./querystorage.sh 10.0.0.1

1


CFG Files Example:


Target[stor_c-_label-os__serial_number_b6ff670d]: hrStorageUsed.1&hrStorageSize.1:PUBLIC@10.0.0.1 * hrStorageAllocationUnits.1&hrStorageAllocationUnits.1:PUBLIC@10.0.0.1
Title[stor_c-_label-os__serial_number_b6ff670d]: 10.0.0.1 - Storage: C: Label:OS Serial Number b6ff670d : / MAX Size in GB = 278.14
PageTop[stor_c-_label-os__serial_number_b6ff670d]: <h1>10.0.0.1 - Storage: C: Label:OS Serial Number b6ff670d Report / MAX Size in GB = 278.14</h1>
Kilo[stor_c-_label-os__serial_number_b6ff670d]: 1024
MaxBytes[stor_c-_label-os__serial_number_b6ff670d]: 2389226520576
ShortLegend[stor_c-_label-os__serial_number_b6ff670d]: iB
YLegend[stor_c-_label-os__serial_number_b6ff670d]: Bytes
Legend1[stor_c-_label-os__serial_number_b6ff670d]: Used
Legend2[stor_c-_label-os__serial_number_b6ff670d]: Total
LegendI[stor_c-_label-os__serial_number_b6ff670d]: Used:
LegendO[stor_c-_label-os__serial_number_b6ff670d]: Total:
Options[stor_c-_label-os__serial_number_b6ff670d]: gauge,growright,nobanner,nopercent

Target[stor_d-_label-Data__serial_number_f40779eb]: hrStorageUsed.2&hrStorageSize.2:PUBLIC@10.0.0.1 * hrStorageAllocationUnits.2&hrStorageAllocationUnits.2:PUBLIC@10.0.0.1
Title[stor_d-_label-Data__serial_number_f40779eb]: 10.0.0.1 - Storage: D: Label:Data Serial Number f40779eb : / MAX Size in TB = 1.11
PageTop[stor_d-_label-Data__serial_number_f40779eb]: <h1>10.0.0.1 - Storage: D: Label:Data Serial Number f40779eb Report / MAX Size in TB = 1.11</h1>
Kilo[stor_d-_label-Data__serial_number_f40779eb]: 1024
MaxBytes[stor_d-_label-Data__serial_number_f40779eb]: 9566888624128
ShortLegend[stor_d-_label-Data__serial_number_f40779eb]: iB
YLegend[stor_d-_label-Data__serial_number_f40779eb]: Bytes
Legend1[stor_d-_label-Data__serial_number_f40779eb]: Used
Legend2[stor_d-_label-Data__serial_number_f40779eb]: Total
LegendI[stor_d-_label-Data__serial_number_f40779eb]: Used:
LegendO[stor_d-_label-Data__serial_number_f40779eb]: Total:
Options[stor_d-_label-Data__serial_number_f40779eb]: gauge,growright,nobanner,nopercent

Target[stor_physical_memory]: hrStorageUsed.5&hrStorageSize.5:PUBLIC@10.0.0.1 * hrStorageAllocationUnits.5&hrStorageAllocationUnits.5:PUBLIC@10.0.0.1
Title[stor_physical_memory]: 10.0.0.1 - Storage: Physical Memory : / MAX Size in GB = 31.81
PageTop[stor_physical_memory]: <h1>10.0.0.1 - Storage: Physical Memory Report / MAX Size in GB = 31.81</h1>
Kilo[stor_physical_memory]: 1024
MaxBytes[stor_physical_memory]: 273308712960
ShortLegend[stor_physical_memory]: iB
YLegend[stor_physical_memory]: Bytes
Legend1[stor_physical_memory]: Used
Legend2[stor_physical_memory]: Total
LegendI[stor_physical_memory]: Used:
LegendO[stor_physical_memory]: Total:
Options[stor_physical_memory]: gauge,growright,nobanner,nopercent


Graph Examples:

2

Jz!

Blog at WordPress.com.

%d bloggers like this: