Page tree

Welcome to FreeSoftwareServers Confluence Wiki

Skip to end of metadata
Go to start of metadata

This is meant to be run from XYMon as a Server-Side Script.

Many Many Many thanks to the following :

    Creators of pyVmomi :

https://github.com/vmware/pyvmomi

“pyVmomi is the Python SDK for the VMware vSphere API that allows you to manage ESX, ESXi, and vCenter.”

    Creator of python-vmstats :

https://github.com/lgeeklee/python-vmstats

“Python script using pyVmomi to get VM statistics”

    And Unix.Linux for helping me get the script working!

http://unix.stackexchange.com/questions/310232/scripting-help-if-grep-used-x-gb-y-then-bash/310255#310255

This took me HOURS just to be able to find some sort of way to query the free RAM on my ESXi host, but in the end, I got a great output with the script above, and it even shows the CPU usage!

sudo su
apt-get install -y python-pip wget git
pip install --upgrade pyvmomi
cd /opt
git clone https://github.com/lgeeklee/python-vmstats.git
chmod 777 /opt/python-vmstats/py-vminfo.py

Test your "cmd" to grep :

python /opt/python-vmstats/py-vminfo.py -s SERVER -u USERNAME -p PASSWORD -m "VMNAME"

Example hosts.cfg :

1.2.3.4 [IP-HOSTNAME] # [IP-HOSTNAME]_ram [IP-HOSTNAME]_cpu [IP-HOSTNAME]_disk

ToDo:

ID_RSA on XYMon Server :

 

cat << 'EOL' >/usr/lib/xymon/client/ext/[SERVER]_id_rsa
-----BEGIN RSA PRIVATE KEY-----
KEYSTRING
-----END RSA PRIVATE KEY-----
EOL
sudo chown xymon:nogroup /usr/lib/xymon/client/ext/[SERVER]_id_rsa
sudo chmod 400 /usr/lib/xymon/client/ext/[SERVER]_id_rsa

Note: For Multiple Datastores, see bottom

cat << 'EOL' >/usr/lib/xymon/client/ext/[SERVER]_ramdiskcpuntp.sh
#!/bin/bash
#This script was created by FreeSoftwareServers
#Check out my site https://www.freesoftwareservers.com/wiki/xymon-aka-hobbit-aka-big-brother-3965320.html for more cool XYMon Stuff!
#In hosts.cfg you need to add both server_ram & server_cpu & server_disks & server_ntp
#Substitute "server" for what you put in server variable below aka IP/Hostname

#ESXi Variables
server=[IP-HOSTNAME]
username=[ESXI_USERNAME]
password=[ESXI_PWD]
vmname="Ubunutu Server 14 LTS x64 - XYMon"
datastore=VMFS-5

#id_rsa for XYMon must be owned by xymon and chmod 400
id_rsa="/usr/lib/xymon/client/ext/[SERVER]_id_rsa"

#Threshold Variables
ramthreshold=95
cputhreshold=90
diskthreshold=90

#Get CPU & RAM Status
python /opt/python-vmstats/py-vminfo.py -s $server -u $username -p $password -m "$vmname" >> "/tmp/${server}_health"

#Get RamPercent
memory=$(grep "Memory Usage" "/tmp/${server}_health" | 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))

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

#Get CPUPercent
cpu=$(grep "CPU Usage" "/tmp/${server}_health" | 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))

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

#Get Disk Status
ssh -i $id_rsa $username@$server df -h >> "/tmp/${server}_df"
diskpercent="$(cat "/tmp/${server}_df" | grep $datastore | grep % | awk '{ print $5 }' | sed 's/%//g')"

#Get DiskHigh
if (( diskpercent > diskthreshold )); then
        diskhigh=true
fi

#Get NTP Status
ssh -i $id_rsa $username@$server "ntpq -p" >> "/tmp/${server}_ntp"
ntprunning="$(cat "/tmp/${server}_ntp"| grep "*")"
##XYMON PORTION##

##XYMon NTP Section##
HOSTNTP="${server}_ntp"
COLUMN=ntp
cmd="$(cat "/tmp/${server}_ntp")"
$XYMONHOME/bin/xymongrep $HOSTNTP | while read L
   do
      set $L# To get one line of output from xymongrep

      HOSTIP="$1"
      MACHINEDOTS="$2"
      MACHINE=`echo $2 | $SED -e's/\./,/g'`

      COLOR=green
      MSG="$HOSTNTP status for host $MACHINEDOTS"

if [ -z "$ntprunning" ]

then

COLOR=red
MSG="${MSG}
There is a Problem with NTP

ESXi Host Information :

"${cmd}"
"
else

MSG="${MSG}
NTP Is Synchronizing

ESXi Host Information :

"${cmd}"
"
fi

$XYMON $XYMSRV "status $MACHINE.$COLUMN $COLOR `date`

${MSG}
"
done

##XYMon RAM Section##
HOSTRAM="${server}_memory"
COLUMN=memory
cmd="$(cat "/tmp/${server}_health" | grep Host)"

$XYMONHOME/bin/xymongrep $HOSTRAM | while read L
   do
      set $L	# To get one line of output from xymongrep

      HOSTIP="$1"
      MACHINEDOTS="$2"
      MACHINE=`echo $2 | $SED -e's/\./,/g'`

      COLOR=green
      MSG="$HOSTRAM status for host $MACHINEDOTS"

if [ ! -z "$ramhigh" ]

then

COLOR=red
MSG="${MSG}
Ram Utilization is Above Safe Threshold

ESXi Host Information :

"${cmd}"
"
else

MSG="${MSG}
Ram Utilization is Below Threshold

ESXi Host Information :

"${cmd}"
"
fi

$XYMON $XYMSRV "status $MACHINE.$COLUMN $COLOR `date`

${MSG}
"
done

##XYMon CPU Section##
HOSTCPU="${server}_cpu"
COLUMN=cpu

$XYMONHOME/bin/xymongrep $HOSTCPU | while read L
   do
      set $L	# To get one line of output from xymongrep

      HOSTIP="$1"
      MACHINEDOTS="$2"
      MACHINE=`echo $2 | $SED -e's/\./,/g'`

      COLOR=green
      MSG="$HOSTCPU status for host $MACHINEDOTS"

if [ ! -z "$cpuhigh" ]

then

COLOR=red
MSG="${MSG}
CPU Utilization is Above Safe Threshold

ESXi Host Information :

"${cmd}"
"
else

MSG="${MSG}
CPU Utilization is Below Threshold

ESXi Host Information :

"${cmd}"
"
fi

$XYMON $XYMSRV "status $MACHINE.$COLUMN $COLOR `date`

${MSG}
"
done

##XYMon Disk Section##
HOSTDISK="${server}_disk"
COLUMN=disk
cmd="$(cat "/tmp/${server}_df")"

##XYMon Configs -- Leave Alone##
$XYMONHOME/bin/xymongrep $HOSTDISK | while read L
   do
      set $L    # To get one line of output from xymongrep

      HOSTIP="$1"
      MACHINEDOTS="$2"
      MACHINE=`echo $2 | $SED -e's/\./,/g'`

      COLOR=green
      MSG="$HOSTDISK status for host $MACHINEDOTS"

if [ ! -z "$diskhigh" ]

then

COLOR=red
MSG="${MSG}
Disk Utilization is Above Safe Threshold

Disk Status :

"${cmd}"
"
else

MSG="${MSG}
Disk Utilization is Below Threshold

Disk Status :

"${cmd}"
"
fi

$XYMON $XYMSRV "status $MACHINE.$COLUMN $COLOR `date`

${MSG}
"
done

rm "/tmp/${server}_ntp"
rm "/tmp/${server}_df"
rm "/tmp/${server}_health"

exit 0
EOL
sudo chown xymon:xymon /usr/lib/xymon/client/ext/[SERVER]_ramdiskcpuntp.sh
sudo chmod 500 /usr/lib/xymon/client/ext/[SERVER]_ramdiskcpuntp.sh
/usr/lib/xymon/client/bin/xymoncmd /usr/lib/xymon/client/ext/[SERVER]_ramdiskcpuntp.sh
sudo sh -c 'echo "*/15 * * * * root /usr/lib/xymon/client/bin/xymoncmd /usr/lib/xymon/client/ext/[SERVER]_ramdiskcpuntp.sh" >> /etc/crontab'

Note: My Client Side Scripts don't seem to be automatically invoked by XYmon, I suspect its because [esxi] isn't the column name. But since this does multiple columns, thats impossible! So for now I just used the above.

I'm keeping this down here for futher testing...

 

cat << 'EOL' >/etc/xymon/clientlaunch.d/[SERVER]_ramdiskcpuntp.cfg
[esxi]
	ENVFILE $XYMONCLIENTHOME/etc/xymonclient.cfg
	CMD $XYMONCLIENTHOME/ext/[SERVER]_ramdiskcpuntp.sh
	LOGFILE $XYMONCLIENTHOME/logs/[SERVER]_ramdiskcpuntp.log
	INTERVAL 10m

EOL

MULTI-DATASTORE NOTES:

datastore1=SSD
datastore2=HDD
 
diskpercent1="$(cat "/tmp/${server}_df" | grep $datastore1 | grep % | awk '{ print $5 }' | sed 's/%//g')"
diskpercent2="$(cat "/tmp/${server}_df" | grep $datastore2 | grep % | awk '{ print $5 }' | sed 's/%//g')"

#Get DiskHigh datastore1
if (( diskpercent1 > diskthreshold )); then
        diskhigh=true
fi

#Get DiskHigh datastore2
if (( diskpercent2 > diskthreshold )); then
        diskhigh=true
fi

  • No labels