Need urgent help with korn shell script

Hi All,

Need urgent help with korn shell script, which monitors AIX WPARs Status. Basically WPARs run on a LPAR, So there is a Command "lswpar" which displays WPARs status. This script should be checking the WPARs Status and email support if WPAR is down or broken. General lswpar output is as below, I just need the script to check the "Name" and "State" coloumn below, If State is A Server is Active, If State is B Server is Broken, If State is T server is in Transition, If State is D Server is Defined. Script should check the State if its "A" send mail saying State Active with Server Names, If State is B, T, D scricpt should mail saying State Broken or Transition or Defined with Server Names. Please see below for script i tried so far, which is giving error as awk: Field is not correct.
The input line number is 1. The file is /tmp/wpar_state_ck.out.
The source line number is 1.
wpar_status_check_workinprogress[29]: A: bad number

lswpar command output below:
root@wparsprd01:/#lswpar
Name State Type Hostname Directory RootVG WPAR
-----------------------------------------------------------------------
usprd02 A S usprd02 /wpars/usprd02 yes 
usprd03 A S usprd03 /wpars/usprd03 yes 
usprd03 B S usprd03 /wpars/usprd03 yes 
usprd03 T S usprd03 /wpars/usprd03 yes 
usprd03 D S usprd03 /wpars/usprd03 yes 
 

Script below:

#!/usr/bin/ksh
#set -x 
state=A
echo "************************ WPAR Clients Status Check **************************" >> /tmp/wpar_state_ck.out
echo "--------------------------------------------------------------------------------" >> /tmp/wpar_state_ck.out
lswpar | grep -e Name -e State | awk '{print $1,"\t","\t","\t",$2}' >> /tmp/wpar_state_ck.out
echo "--------------------------------------------------------------------------------" >> /tmp/wpar_state_ck.out
lswpar | awk '{print $1,"\t",$2}' | grep -v Name | grep A >> /tmp/wpar_state_ck.out
# Mail the Status with State Active if A or (Defined or Transition or Broken) if D, T, B 
cat /tmp/wpar_state_ck.out | grep -e A | grep -v WPAR | grep -v State | awk '{print $2}' | wc -l | while read i
do
check=$(awk 'FNR == $i {print}' /tmp/wpar_state_ck.out) 
if [ "$check" = "$state" ];
then
echo "******************************* WPAR_State = Active *********************************" >> /tmp/wpar_state_ck.out
else
echo "****************** WPAR_State = Defined or Transition or Broken *********************" >> /tmp/wpar_state_ck.out
fi
done
#echo "################################################################################" >> /tmp/wpar_state_ck.out
#echo "A : Active State" >> /tmp/wpar_state_ck.out
#echo "D : Defined State" >> /tmp/wpar_state_ck.out
#echo "T : Transition State" >> /tmp/wpar_state_ck.out
#echo "################################################################################" >> /tmp/wpar_state_ck.out
cat /tmp/wpar_state_ck.out | mail -s "WPAR `hostname` Server monitoring Status of Client WPARs" unix_support@xyz.com
# remove the file
rm -f /tmp/wpar_state_ck.out
 

Error Message am getting:

awk: Field is not correct.
The input line number is 1. The file is /tmp/wpar_state_ck.out.
The source line number is 1.
wpar_status_check_workinprogress[29]: A: bad number

Check out the below code, I believe it will make doing what you want a little simpler:

$ cat t
Name State Type Hostname Directory RootVG WPAR
-----------------------------------------------------------------------
usprd02 A S usprd02 /wpars/usprd02 yes
usprd03 A S usprd03 /wpars/usprd03 yes
usprd03 B S usprd03 /wpars/usprd03 yes
usprd03 T S usprd03 /wpars/usprd03 yes
usprd03 D S usprd03 /wpars/usprd03 yes


while read name state type hostname directory rootvg wpar
do
  (( c+=1 ))
  if [[ $c -gt 2 ]]; then
    if [[ $state = "A" || $state = "B" || $state = "D" || $state = "T" ]]; then
      #### your code to handle these states
    fi
  else
    : # no-op
  fi
done <t
1 Like

Agree with spacebar -- can be simplified. My thoughts:

lswpar  | awk '
    /^Name/ { next; }
    /^---/  { next; }
    { state[$2] = state[$2] $1 " "; }
    END {
        printf( "==== WPAR Status Check =====\n" );
        printf( "**** WPAR state Active:\n\t%s\n\n", state["A"] );
        printf( "**** WPAR state Defined/Transition/broken:\n\t%s %s %s\n", state["D"], state["T"], state["B"] );
    }
' | mail -s "WPAR `hostname` Server monitoring Status of Client WPARs" unix_support@xyz.com

As to the reason for your error, you cannot use a shell variable ($i in this case) like you are attempting (in single quotes shell will not expand it). You could try:

check=$(awk 'FNR == I {print}'  I=$i  /tmp/wpar_state_ck.out)

But I would try something that is much more straight forward than the way you were approaching it.

1 Like

Thanks both spacebar and agama, you both are saviours, one small update in script.

Am using script suggested by agama:

Script code:
lswpar | awk '
/^Name/ { next; }
/^---/ { next; }
{ state[$2] = state[$2] $1 " "; }
END {
printf( "==== WPAR Status Check =====\n" );
printf( "**** WPAR state Active:\n\t%s\n\n", state["A"] );
printf( "**** WPAR state Defined/Transition/broken:\n\t%s %s %s\n", state["D"], state["T"], state["B"] );
}
' | mail -s "WPAR `hostname` Server monitoring Status of Client WPARs" unix_support@xyz.com

Current Mail Output from script:
==== WPAR Status Check =====
**** WPAR state A-Active:
usprd02 usprd03 usprd04 usprd05 usprd06

**** WPAR state D-Defined/T-Transition/B-broken:

I need below required output:
If All WPARs are Active, below output:

==== WPAR Status Check =====

**** WPAR state A-Active:
usprd02 usprd03 usprd04 usprd05 usprd06

All WPARs are in Active State

No Defined/Transition/Broken WPARs

If 2 WPARs are Active, and 3 are Defined/Transition/Broken each, below output:

==== WPAR Status Check =====

**** WPAR state A-Active:
usprd02 usprd03

2 WPARs are in Active State

**** WPAR state D-Defined/T-Transition/B-broken:
usprd04 usprd05 usprd06

3 WPARs are in Defined/Transition/Broken State