VMware Cloud Community
esxi1979
Expert
Expert
Jump to solution

Invoke-VMScript - Bash script gives err

I have below script to check if all NFS mounts are ok post a vm reboot.

====

cat /etc/fstab |grep -v "#" |grep nfs |awk '{print $2}'  |sort > /tmp/fstab_nfs_mount

cat /proc/mounts |grep -v "#" |grep -v rpc|grep nfs |awk '{print $2}'  |grep -v nfsd |sort > /tmp/proc_nfs_mount

ffnm='/tmp/fstab_nfs_mount'

pfnm='/tmp/proc_nfs_mount'

if diff $ffnm $pfnm > /dev/null ; then

echo "All NFS mounts are mounted as defined. :)"

/usr/bin/logger "All NFS mounts are mounted as defined. :)"

else

echo "Seems all NFS mounts are not mounted properlyy :("

/usr/bin/logger "Seems all NFS mounts are not mounted properly :("

fi

====

It works well on the RHEL box when run directly

Now i am passing this with Invoke-VMScript

===

Foreach ($line in $csv)

{

$script = @"

cat /etc/fstab |grep -v "#" |grep nfs |awk '{print $2}'  |sort > /tmp/fstab_nfs_mount

cat /proc/mounts |grep -v "#" |grep -v rpc|grep nfs |awk '{print $2}'  |grep -v nfsd |sort > /tmp/proc_nfs_mount

ffnm='/tmp/fstab_nfs_mount'

pfnm='/tmp/proc_nfs_mount'

if diff $ffnm $pfnm > /dev/null ; then

echo "All NFS mounts are mounted as defined. :)"

/usr/bin/logger "All NFS mounts are mounted as defined. :)"

else

echo "Seems all NFS mounts are not mounted properlyy :("

/usr/bin/logger "Seems all NFS mounts are not mounted properly :("

fi

"@

$report = Invoke-VMScript -VM  $($line.vm)  -GuestUser $user  -GuestPassword $pass -ScriptText $script

$report.ScriptOutput

}

===

I got err

diff: missing operand after `diff'

diff: Try `diff --help' for more information.

Seems all NFS mounts are not mounted properlyy Smiley Sad

=

What is that which is missing ?

Thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try changing the inline string delimiters from @"..."@ to @'...'@.

I suspect it is a quoting issue.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

Try changing the inline string delimiters from @"..."@ to @'...'@.

I suspect it is a quoting issue.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
sajal1
Hot Shot
Hot Shot
Jump to solution

I do not see [ ] brackets after if statement. Apart from what LucD said can you check this as well?

0 Kudos
esxi1979
Expert
Expert
Jump to solution

Ah that helped !

0 Kudos
esxi1979
Expert
Expert
Jump to solution

LucD‌ & All

Any suggestions on below pls,

Foreach ($line in $csv)

{

$script = @'

EMAIL='xxx'

function sysstat {

echo -e "

#####################################################################

    Health Check Report (CPU,Process,Disk Usage, Memory)

#####################################################################

Hostname         : `hostname`

Kernel Version   : `uname -r`

Uptime           : `uptime | sed 's/.*up \([^,]*\), .*/\1/'`

Last Reboot Time : `who -b | awk '{print $3,$4}'`

*********************************************************************

CPU Load - > Threshold < 1 Normal > 1 Caution , > 2 Unhealthy

*********************************************************************

"

MPSTAT=`which mpstat`

MPSTAT=$?

if [ $MPSTAT != 0 ]

then

        echo "Please install mpstat!"

        echo "On Debian based systems:"

        echo "sudo apt-get install sysstat"

        echo "On RHEL based systems:"

        echo "yum install sysstat"

else

echo -e ""

LSCPU=`which lscpu`

LSCPU=$?

if [ $LSCPU != 0 ]

then

        RESULT=$RESULT" lscpu required to producre acqurate reults"

else

cpus=`lscpu | grep -e "^CPU(s):" | cut -f2 -d: | awk '{print $1}'`

i=0

while [ $i -lt $cpus ]

do

        echo "CPU$i : `mpstat -P ALL | awk -v var=$i '{ if ($3 == var ) print $4 }' `"

        let i=$i+1

done

fi

echo -e "

Load Average   : `uptime | awk -F'load average:' '{ print $2 }' | cut -f1 -d,`

Heath Status : `uptime | awk -F'load average:' '{ print $2 }' | cut -f1 -d, | awk '{if ($1 > 2) print "Unhealthy"; else if ($1 > 1) print "Caution"; else print "Normal"}'`

"

fi

echo -e "

*********************************************************************

                             Process

*********************************************************************

=> Top memory using processs/application

PID %MEM RSS COMMAND

`ps aux | awk '{print $2, $4, $6, $11}' | sort -k3rn | head -n 10`

=> Top CPU using process/application

`top b -n1 | head -17 | tail -11`

*********************************************************************

Disk Usage - > Threshold < 90 Normal > 90% Caution > 95 Unhealthy

*********************************************************************

"

df -Pkh | grep -v 'Filesystem' > /tmp/df.status

while read DISK

do

        LINE=`echo $DISK | awk '{print $1,"\t",$6,"\t",$5," used","\t",$4," free space"}'`

        echo -e $LINE

        echo

done < /tmp/df.status

echo -e "

Heath Status"

echo

while read DISK

do

        USAGE=`echo $DISK | awk '{print $5}' | cut -f1 -d%`

        if [ $USAGE -ge 95 ]

        then

                STATUS='Unhealty'

        elif [ $USAGE -ge 90 ]

        then

                STATUS='Caution'

        else

                STATUS='Normal'

        fi

        LINE=`echo $DISK | awk '{print $1,"\t",$6}'`

        echo -ne $LINE "\t\t" $STATUS

        echo

done < /tmp/df.status

rm /tmp/df.status

TOTALMEM=`free -m | head -2 | tail -1| awk '{print $2}'`

TOTALBC=`echo "scale=2;if($TOTALMEM<1024 && $TOTALMEM > 0) print 0;$TOTALMEM/1024"| bc -l`

USEDMEM=`free -m | head -2 | tail -1| awk '{print $3}'`

USEDBC=`echo "scale=2;if($USEDMEM<1024 && $USEDMEM > 0) print 0;$USEDMEM/1024"|bc -l`

FREEMEM=`free -m | head -2 | tail -1| awk '{print $4}'`

FREEBC=`echo "scale=2;if($FREEMEM<1024 && $FREEMEM > 0) print 0;$FREEMEM/1024"|bc -l`

TOTALSWAP=`free -m | tail -1| awk '{print $2}'`

TOTALSBC=`echo "scale=2;if($TOTALSWAP<1024 && $TOTALSWAP > 0) print 0;$TOTALSWAP/1024"| bc -l`

USEDSWAP=`free -m | tail -1| awk '{print $3}'`

USEDSBC=`echo "scale=2;if($USEDSWAP<1024 && $USEDSWAP > 0) print 0;$USEDSWAP/1024"|bc -l`

FREESWAP=`free -m |  tail -1| awk '{print $4}'`

FREESBC=`echo "scale=2;if($FREESWAP<1024 && $FREESWAP > 0) print 0;$FREESWAP/1024"|bc -l`

echo -e "

*********************************************************************

                     Memory

*********************************************************************

=> Physical Memory

Total\tUsed\tFree\t%Free

${TOTALBC}GB\t${USEDBC}GB \t${FREEBC}GB\t$(($FREEMEM * 100 / $TOTALMEM  ))%

=> Swap Memory

Total\tUsed\tFree\t%Free

${TOTALSBC}GB\t${USEDSBC}GB\t${FREESBC}GB\t$(($FREESWAP * 100 / $TOTALSWAP  ))%

"

}

FILENAME="health-`hostname`-`date +%y%m%d`-`date +%H%M`.txt"

sysstat > $FILENAME

echo -e "Reported file $FILENAME generated in current directory." $RESULT

if [ "$EMAIL" != '' ]

then

        STATUS=`which mail`

        if [ "$?" != 0 ]

        then

                echo "The program 'mail' is currently not installed."

        else

                cat $FILENAME | mail -s "$FILENAME" $EMAIL

        fi

fi

'@

$report = Invoke-VMScript -VM  $($line.vm)  -GuestUser $user  -GuestPassword $pass -ScriptText $script

$report.ScriptOutput

}

===

Above script works as it is when run on the linux box ... yes its Centos

In above i got email but the email has many fileds missing & below err as well

#####################################################################

    Health Check Report (CPU,Process,Disk Usage, Memory)

Hostname         : xxx

Kernel Version   : 2.6.32-573.1.1.el6.x86_64

Uptime :  2:23

Last Reboot Time :

....

Error :-

bash: line 33: [: 0: unary operator expected

bash: line 80: [: -ge: unary operator expected

bash: line 83: [: -ge: unary operator expected

bash: line 80: [: -ge: unary operator expected

bash: line 83: [: -ge: unary operator expected

bash: line 80: [: -ge: unary operator expected

bash: line 83: [: -ge: unary operator expected

bash: line 80: [: -ge: unary operator expected

bash: line 83: [: -ge: unary operator expected

bash: line 80: [: -ge: unary operator expected

bash: line 83: [: -ge: unary operator expected

bash: line 106: * 100 /   : syntax error: operand expected (error token is "* 100 /   ")

Reported file health-xxxx-160102-0104.txt generated in current directory.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like some of the operands in the conditional expressions are an empty string.

Those operands are the variables filled in by the expressions between the back ticks.

The Invoke-VMScript cmdlet makes a copy of the script to the guest OS (/tmp if I'm not mistaken)

One way to check what is actually in the transferred bash script is to build in a pause, and then have a look at the contents of the file


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos