#!/bin/bash GREP="/bin/grep" SED="/bin/sed" TAIL="/usr/bin/tail" SSH="/usr/bin/ssh" BASENAME="/usr/bin/basename" # script name must be like esx_cpu_machine.domain or esx_mem_machine.domain temp="$(basename $0 | $SED 's/^esx_//g')" which_info="${temp%%_*}" HOST="${temp##*_}" case "$which_info" in "cpu" | "mem" | "hmem" | "gmem" ) ;; * ) echo "Script name incorrect, should be esx_xxx_hostname where xxx in {cpu,mem,hmem,gmem}" exit 1;; esac # --------------- function get_infos () { summary="$($SSH "$HOST" \ vim-cmd hostsvc/hostsummary \; \ vim-cmd vmsvc/getallvms)" } function get_vmlist () { total_linenumber="$(echo "$summary" | wc -l)" vmlist_linenumber="$(echo "$summary" | $GREP -n "^Vmid")" vmlist_linenumber="${vmlist_linenumber/:*/}" vmlist="$(echo "$summary" | $TAIL -n $(($total_linenumber - $vmlist_linenumber)) | tr -s " ")" } function overallCpuUsage () { cpu="$(echo "$1" | $GREP "overallCpuUsage")" cpu="${cpu/*overallCpuUsage = /}" cpu="${cpu/,*/}" } function overallMemoryUsage () { mem="$(echo "$1" | $GREP "overallMemoryUsage")" mem="${mem/*overallMemoryUsage = /}" mem="${mem/,*/}" } # --------------- function get_vmsvcsummaries () { n=0 cmd="" # echo "$vmlist" | \ while read vmline ; do id[$n]="${vmline%% *}" temp="${vmline#${id[$n]} }" name[$n]="${temp%% *}" cmd="$cmd""vim-cmd vmsvc/get.summary ${id[$n]} \\\; " (( n++ )) done <" ]; then vmcpu[$n]="0" fi (( n++ )) done <" ]; then vmmem[$n]="0" fi (( n++ )) done <" ]; then vmmem[$n]="0" fi (( n++ )) done <