0

I have scoured the internet for a way to query an ESXi hosts used RAM, and I found one which even included the Used Mhz to boot, in a nice output. My real end goal is to get an XYMon script to monitor the output. I can make the XYMon script, but I have no idea how to get the IF THEN statement to work.

Here is the output of my query :

[Host] Name                    : esxi.domain.com
[Host] CPU Detail              : Processor Sockets: 1, Cores per Socket 4
[Host] CPU Type                : Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
[Host] CPU Usage               : Used: 836 Mhz, Total: 16028 Mhz
[Host] Memory Usage            : Used: 59 GB, Total: 64 GB

I need an IF THAN statement that basically says IF (Used # of RAM) is greater than (Y), THEN.

If it's possible to make the script portable, I would like there to be an IF (Used # of RAM) > (%Total of RAM) THEN. This way I can publish the script and people can use it without modifying the Y parameter.

2

percentage can be found this way:

CMD > /tmp/esxihealth
percent=$(awk '/Memory Usage/ { printf "%d\n",100*$6/$9+.5 ;}' /tmp/esxihealth)

no need for so many pipes.

in awk $6 will pick the 6-th field (by default field are separated by one or more space or tab).

|improve this answer|||||
  • By my count it's $5 and $8. And the self-answer now indicates they want a rounded value, so 100*$5/$8+.5 – dave_thompson_085 Sep 16 '16 at 9:55
  • found 6 and 9, anyway this is not that hard to change. – Archemar Sep 16 '16 at 9:58
1

I'm not the greatest bash scripter, so someone might have a more elegant solution, but the solution below works. Your Y variable is the threshold variable in the script, and the name of the file that contains the data you have given is called memstats:

#!/bin/bash

memory=$(grep "Memory Usage" memstats | grep -o '[0-9]*' | tr '\n' ' ')
used=$(echo $memory | cut -d' ' -f1)
total=$(echo $memory | cut -d' ' -f2)


threshhold=50

if (($used > $threshhold)); then
        echo "do this (used is greater than threshold)"
else
        echo "do this else (used is less than threshold)"
fi
|improve this answer|||||
0
#/bin/bash

memory=$(grep "Memory Usage" /tmp/esxihealth | grep -o '[0-9]*' | tr '\n' ' ')
ramused=$(echo $memory | cut -d' ' -f1)
ramtotal=$(echo $memory | cut -d' ' -f2)
rampercent=$((200*$ramused/$ramtotal % 2 + 100*$ramused/$ramtotal))
ramthreshold=95

if (( rampercent > ramthreshold )); then
        ramhigh=true
fi

cpu=$(grep "CPU Usage" /tmp/esxihealth | grep -o '[0-9]*' | tr '\n' ' ')
cpuused=$(echo $cpu | cut -d' ' -f1)
cputotal=$(echo $cpu | cut -d' ' -f2)
cpupercent=$((200*$cpuused/$cputotal % 2 + 100*$cpuused/$cputotal))
cputhreshold=90

if (( cpupercent > cputhreshold )); then
       cpuhigh=true
fi

if [ ! -z "$ramhigh" ] || [ ! -z "$cpuhigh" ]; then
    ...
else
    ...
fi
|improve this answer|||||

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.