#!/bin/ksh

###########################################################################
#
# Script : cluster_status
# Author : www.aixhealthcheck.com
# Version: 1.6
# Date   : March 24, 2015
# 
# What it does:
# -------------
# This script will monitor HACMP clusters and produce HTML output.
# Big advantage of using this script: it can monitor more than 8 clusters
# (clstat will only monitor up to 8 clusters) and it will provide a 
# single view on all clusters at once.
#
# Usage:
# ------
# See readme.
#
# Comments:
# ---------
# Please send any comments to info@aixhealtcheck.com or check out 
# website www.aixhealthcheck.com for additional information.
#
# See the readme file for additional information. 
#
###########################################################################
#
# Updates in version 1.4:
#
# * Option to se snmp community in the clhosts file.
# * Creates subdir snmp if it doesn't exist.
# * Removed typo.
# * Tested it with HA 5.3 and 5.4 (up to 5.4.0.1).
# * Corrected order of nodes in html page even if its different from snmp output
# * Shows right amount of nodes of cluster. If snmp info shows 2 nodes and
#   you've added 3 nodes in the cluster info (clhosts) it will show 2.
# * Tested it with adding an exta node and removing a node to see if it updates
# * Introduced extra command to retrieve HA software version if
#   hamcp mib doesn't provide one.
#
# Issues:
#
# * there's a problem with the way cl.html shows the cluster
#   when it is completely down...
#
###########################################################################
#
# Updates in version 1.5:
#
# * Included location of resource group
# * Tested with HA 5.5
#
###########################################################################
#
# Updates in version 1.6:
#
# * Included location of resource group
# * Tested with HA 6.1
#

#
# Variable declarations
#

# My BASE directory. This directory will be used to create pages and store
# temporary files.

export BASE=/tmp/cl

# The URL used for this page.
# Where will this page be stored on your website?

export MYURL="http://localhost/cl.html"

# MYPAGELOCATION determines where to store your web page
# Make sure this directory exists.

export MYPAGELOCATION=$BASE/clinfo.html

# REFRESH determines how often you want your webpage refreshed.
# Amount in seconds.
export REFRESH=20

#
# End variable declarations
#



#
# Is cluster_status script already active?
# If so, send yourself a DGSP
# 

unset result
result=`ps -ef | grep cluster_status | grep -v grep | grep -v $$ | wc -l | awk '{print $1}'`
if [ $result -gt 0 ]
then
   exit
fi

#
# Collect our cluster status information via snmp
# This is done via a separate script, called snmp.sh
#

cd $BASE
mkdir snmp 2>/dev/null
$BASE/snmp.sh >/dev/null 2>/dev/null


#
# Function clusterstat
# will produce the status of a single cluster in a HTML table
#

function clusterstat
{
   export info=$1
   let teller=0
   unset clusternummer
   clusternummer=`echo $info | cut -f2 -d:`
   unset dispclusternummer
   dispclusternummer=`cat $BASE/snmp/$clusternummer.out | grep "Cluster ID"| awk '{print $3}'`
   if [ -z "$dispclusternummer" ] ; then
      dispclusternummer="unknown"
   fi
   unset clusternaam
   clusternaam=`echo $info | cut -f3 -d:`
   if [ -z "$clusternaam" ] ; then
clusternaam="unknown"
   fi
   unset version
   version=`cat $BASE/snmp/$clusternummer.out | grep Version| awk '{print $2}'`
   if [ "$version" = "" ] ; then
         version="unknown"
   fi

   echo "<font face=verdana size=1><b>$clusternaam [$dispclusternummer] [$version]</b></font><br>"
   echo "<table border=1 cellspacing=2 cellpadding=1 width=259>"
   aantalnodes=`grep -i "Number of nodes:" $BASE/snmp/$clusternummer.out | awk '{print $4}'` 
   if [ ! -z "$aantalnodes" ] ; then
      echo "<tr>"
      if [ $aantalnodes = 3 ] 
      then
tds=5
      fi
      if [ $aantalnodes = 2 ] 
      then
tds=3
      fi
   
      clusterstatus=`cat $BASE/snmp/$clusternummer.out | grep "Cluster State:" | awk '{print $3}'`
      case $clusterstatus in
      "UP") echo "<TD COLSPAN=$tds BGCOLOR="#00ff00">"
;;
      "DOWN") echo "<TD COLSPAN=$tds BGCOLOR="#ff0000">"
;;
      "UNKNOWN") echo "<TD COLSPAN=$tds BGCOLOR="#ffff00">"
;;
      "") echo "<TD COLSPAN=$tds BGCOLOR="#cccccc">"
        clusterstatus="No SNMP info"
;;
      *) echo "<TD COLSPAN=$tds BGCOLOR="#cccccc">"
        clusterstatus="Unknown"
;;
      esac

      echo "<font face=verdana size=-2>Clusterstate: $clusterstatus"
      clustersubstatus=`grep "Cluster SubState:" $BASE/snmp/$clusternummer.out | awk '{print $3}'`
      if [ "$clustersubstatus" != "STABLE" ]
      then
         if [ "$clusterstatus" = "No SNMP info" ]
         then
            echo "<TR><TD COLSPAN=$tds BGCOLOR=#cccccc>"
         else
            echo "<TR><TD COLSPAN=$tds BGCOLOR=#ffff00>"
         fi
      else
         echo "<TR><TD COLSPAN=$tds BGCOLOR=#00ff00>"
      fi
      echo "<font size=1 face=verdana>"
      echo Cluster Substate: $clustersubstatus
      echo "</td></tr>"

      echo "<TR>"

      node[1]=""
      node[2]=""
      node[3]=""

      if [ $aantalnodes = 2 ]
      then 
	 breed=124
      else
         breed=79
      fi

      let teller=0

      grep Node $BASE/snmp/$clusternummer.out | awk '{print $2,$3,$4}' | while read nummer nodenaam status ; do

	 if [ ! -z "$nodenaam" ] ; then
         case $status in
         "UP") echo "<TD BGCOLOR="#00ff00" WIDTH=$breed valign=top>"
                printf "<font face=verdana size=-2>$nodenaam "
                echo $status
		# check if theres a resource group active on this node
		nrnummer=`echo $nummer | sed "s/://g"`
		unset swurrel
		swurrel=`grep ^ResGrp $BASE/snmp/$clusternummer.out | grep " $nrnummer" | awk '{print $2}'`
		if [ ! -z "${swurrel}" ] ; then
			echo "<br />$swurrel"
		fi
		
                echo "</TD>"
;;
         "DOWN") echo "<TD BGCOLOR="#ff0000" WIDTH=$breed>" 
      printf "<font face=verdana size=-2>$nodenaam "
      echo $status
      echo "</TD>"
;;
      *) echo "<TD BGCOLOR="#cccccc" WIDTH=$breed>" 
      printf "<font face=verdana size=-2>$iederenode "
                echo ${node[$teller]}
                echo "</TD>"
;;
         esac
	fi

      done
      echo "</tr>"
   else
      echo "<TR><TD BGCOLOR="#cccccc" WIDTH=259><font face=verdana size=-2>No SNMP info<td></tr>" 
      echo "<TR><TD BGCOLOR="#cccccc" WIDTH=259><font face=verdana size=-2>No SNMP info<td></tr>" 
      echo "<TR><TD BGCOLOR="#cccccc" WIDTH=259><font face=verdana size=-2>No SNMP info<td></tr>" 
   fi
   echo "</table>"
}


#
# MAIN
#

# The new page will be built to file $BASE/cl_new

{

# Creating the header

echo "<HTML><BODY topmargin=0>"
echo "<META http-equiv=\"Refresh\" content=\"20\"; URL=\"$MYURL\">"
echo "<font face=\"verdana\" size=-2>"
echo "<TITLE>Status of HACMP clusters</TITLE>"
echo "<table width=790 cellspacing=1 cellpadding=1 border=0 bgcolor=cccccc>"
echo "<TR height=10><TD><font face=verdana size=2><B>Status of HACMP clusters</font></B><TD>&nbsp;&nbsp;<TD>"
echo "<font size=1 face=verdana>Refresh every $REFRESH seconds. Last update: "
date 
echo "</table>" 

# Creating the cluster tables
# It will create a row of 3 tables, to place 3 clusters next to each other.

echo "<table border=0 cellspacing=0 cellpadding=0 width=790>"

let rowcounter=1
echo "<TR>"
cat $BASE/clhosts | grep "^cluster:" | awk -F":" '{print $3":"$2":"$5}' | sort | while read rest ; do 
   echo "<TD>"
   clusterstat "$rest" 2>/dev/null
   let rowcounter="$rowcounter+1"
   if [ $rowcounter -gt 3 ] ; then
      echo "</TR><TR>"
      let rowcounter=1
   fi
done

echo "</tr></table>" 

# Footer

echo "<table width=790 cellpadding=1 cellspacing=1 border=0><TR height=1><TD></td></tr></table>" 
echo "<table width=790 cellpadding=1 cellspacing=1 border=0 bgcolor=cccccc><TR><Td>"
echo "<font face=verdana size=1>On-line HACMP cluster monitoring page created by"
echo " <a href=\"mailto:info@aixhealthcheck.com\">AIX Health Check</a>."
echo "<BR>Tip: Press F11 for a full screen overview, if you can't see all clusters.</table>"
echo "<table width=790 cellpadding=1 cellspacing=1 border=0><TR height=1><TD></td></tr></table>"
echo "</HTML>" 

} > $BASE/cl_new

# Move the file to the correct location

mv $BASE/cl_new $MYPAGELOCATION
chmod 775 $MYPAGELOCATION
exit

