#!/bin/ksh

#############################################################################
##
## iMS monitoring and alarm script
##
## This script is part an example framework of what can be done.
## There are MANY ways that you can take this from here.
##
## File: tcprate
##       A script to count the number of TCP/IP connections on ports which
## 	 some iMS services use.
##
#############################################################################

_CURR_WDIR=$(dirname $0)

OS_TYPE="`uname -s`"

if [ -z $HEALTHMON_TMPDIR ]
then
    . $_CURR_WDIR/../../etc/alarms.cfg
fi


case $OS_TYPE in
SunOS )
	netstat -P tcp -f inet -n | tail +5 > $HEALTHMON_TMPDIR/check_sys.tcprate.netstat.$$
        ;;
* )
	netstat -t inet -n | tail +2 > $HEALTHMON_TMPDIR/check_sys.tcprate.netstat.$$
        ;;
esac

## Add the tcp/ip connection information

	$AWK ' BEGIN {
                ##  grep -i nsmsg.*port *.conf
                ##  todo: these port numbers should be extracted from configs.
		pCol[1]="25"
		pCol[2]="143"
		pCol[3]="110"
		pCol[4]="993"
		pCol[5]="995"
		pCol[6]="389"
		pCol[7]="80"
		pCol[8]="" 
	}
	{
                if ( OSTYPE ~/SunOS/ ) {
                        num=split ($1, prtIn, ".");
                        rnum=split ($2, prtExt, ".");
                        for (i=1; pCol[i]; i++)
                        if ( (prtIn[num] == pCol[i]) && ($7 == "ESTABLISHED")) {
                                ipAddr = prtExt[1];
                                for (j=2; j<rnum ; j++ )
                                        ipAddr = sprintf("%s.%s", ipAddr, prtExt[j]);
                                ipAddr = sprintf("%s %s", ipAddr, pCol[i]);
                                printf("%s\n", ipAddr)
                        }

                } else {

                        num=split ($4, prtIn, ":");
                        rnum=split ($5, prtExt, ":");
                        for (i=1; pCol[i]; i++)
                                if ( (prtIn[num] == pCol[i]) && ($6 == "ESTABLISHED")) {
                                        ipAddr = prtExt[1];
                                        ipAddr = sprintf("%s %s", ipAddr, pCol[i]);
                                        printf("%s\n", ipAddr)
                                }
                }

	}' OSTYPE=$OS_TYPE $HEALTHMON_TMPDIR/check_sys.tcprate.netstat.$$ | sort | uniq -c |  \
	$AWK ' BEGIN {
		pCol[1]="25"  ; pName[1]="smtp" ;
		pCol[2]="143" ; pName[2]="imap" ;
		pCol[3]="110" ; pName[3]="pop3" ;
		pCol[4]="993" ; pName[4]="imaps";
		pCol[5]="995" ; pName[5]="pop3s";
		pCol[6]="389" ; pName[6]="ldap";
		pCol[7]="80"  ; pName[7]="http";
		pCol[8]=""    ; pName[8]=""; 
	}
	{	
		for (i=1; pCol[i]; i++)
			if ( $3 == pCol[i] ) {
				SrcCount[pCol[i]]++;
				TotalCount[pName[i]] = TotalCount[pName[i]] + $1
			}
	}
 	END {
                printf "ESTABLISHED connections:\n\n" ;
                printf "PORT  " ;

		for (i=1; pCol[i]; i++) {
                	printf "\t%s", pName[i] ;
                }
                printf "\nTotal " ;
		for (i=1; pCol[i]; i++) {
                	printf "\t%d", TotalCount[pName[i]] ;
                }
                printf "\nIndiv " ;
		for (i=1; pCol[i]; i++) {
                	printf "\t%d", SrcCount[pCol[i]] ;
                }
	} ' 

	printf "\n" 

rm -f $HEALTHMON_TMPDIR/check_sys.tcprate.netstat.$$


## end of script
