#!/bin/bash

#################################################################################
#                                                                               #
#                  ALL THIS SCRIPT IS UNDER GPL LICENSE                         #
# Version 0.1                                                                   #
# Title:     rootlogin.sh                                                       #
# Author:    Alexander Bech  (alex |at| bakarasse |dot| de)                     #
# Date:      2010-06-22                                                         #
# Purpose:   Detect root login and generate color status                        #
# Platforms: Uni*                                                               #
# Tested:    Xymon 4.2.3                                                        #
#                                                                               #
# 22/06/2010                                                                    #
# Version 0.1 - Initial write.                                                  #
#                                                                               #
#################################################################################

#################################################################################
#             Change to fit your system/wills					#
#										#
ROOTLOGIN_COLOR="yellow"        # or red					#
USERLOGIN_COLOR="blue"          # or red					#
MAXIMAL_CLIENT_MSG_LIFETIME=30	# Maximal lifetime of clientmsg. Minutes.	#
#										#
COLUMN="login"									#
CHECK_ALL_CLIENTS="yes" # Check all clients from hibbitdboard or               #
#CHECK_ALL_CLIENTS="no"   # only hosts with $COLUMN tag in bb-hosts line.        #
#										#
#################################################################################

LC_ALL=C
LANG=C
LANGUAGE=C

#DEBUG="TRUE"
DEBUG="FALSE"

function debug () {
if [ "${DEBUG}" = "TRUE" ]
then
    echo "Debug: $@"
fi
}

if [ "${CHECK_ALL_CLIENTS}" = "yes" ]
then
    HOSTLIST=$(${BBHOME}/bin/bbhostshow | ${GREP} -v "^#" )
else
    # We are server. ignore BBLOCATION for bbhostgrep:
    unset BBLOCATION
    HOSTLIST=$(${BBHOME}/bin/bbhostgrep --test-untagged ${COLUMN})
fi

#debug "${HOSTLIST}"

if [ "${HOSTLIST}" = "" ]
then
    debug "$0: No hosts with tag ${COLUMN} found in ${BBHOSTS}"
    exit 0
fi

# calculate MAXIMAL_CLIENT_MSG_LIFETIME in seconds:
MAXIMAL_CLIENT_MSG_LIFETIME_SECONDS=$((${MAXIMAL_CLIENT_MSG_LIFETIME} * 60))

# Here work:
ORIGIFS=${IFS}; IFS=$'\n'
for HOST in ${HOSTLIST}
do
    IFS=${ORIGIFS}
    read HOSTIP HOSTNAME dummy TAG dummy <<< ${HOST}
    COLOR="green"
    MSG="&${COLOR} No root login at the moment."
    
    CURR_DATE=$(${DATE})
    LOCAL_EPOCH=$(date +%s)
    
    #debug "processing ${HOSTNAME}"
    
    # Get client message section [who] from hobbitdboard:
    CLIENT_WHO_MSG=$($BB $BBDISP "clientlog ${HOSTNAME} section=who")
    if [ "${CLIENT_WHO_MSG}" = "" -a "${CHECK_ALL_CLIENTS}" = "yes" ]
    then
	# No client or no client message available.
	# debug "${HOSTNAME} = skip"
	continue
    fi

    # Get client message section [clock] from hobbitdboard:
    CLIENT_MSG_CLOCK=$($BB $BBDISP "clientlog ${HOSTNAME} section=clock")
    if [ "${CLIENT_MSG_CLOCK}" != "" ]
    then
     CLIENT_MSG_CLOCK_EPOCH=$(echo "${CLIENT_MSG_CLOCK}" | ${GREP} "^epoch")
      read dummy CLIENT_MSG_CLOCK_EPOCH dummy <<< ${CLIENT_MSG_CLOCK_EPOCH}
     CLIENT_MSG_CLOCK_EPOCH="${CLIENT_MSG_CLOCK_EPOCH%.*}"
     CLIENT_MSG_CLOCK_LOCAL=$(echo "${CLIENT_MSG_CLOCK}" | ${GREP} "^local")
      read dummy CLIENT_MSG_CLOCK_LOCAL <<< ${CLIENT_MSG_CLOCK_LOCAL}
     CLIENT_MSG_TIME=$(${DATE} -d "${CLIENT_MSG_CLOCK_LOCAL}") 
     
     CLIENT_MSG_AGE=$((${LOCAL_EPOCH} - ${CLIENT_MSG_CLOCK_EPOCH}))
     if [ ${CLIENT_MSG_AGE} -lt 0 ]
     then
        # Client time in future.
        debug "${HOSTNAME} Client time in future."
        CLIENT_MSG_AGE=0
	CLIENT_MSG_TIME=${CURR_DATE}
     fi
    else
	# No [clock] section available:
        CLIENT_MSG_AGE=0
	CLIENT_MSG_TIME=${CURR_DATE}
    fi

    if [ "${CLIENT_WHO_MSG}" = "" ]
    then
	COLOR="clear"
	MSG=$(echo -e "${CLIENT_MSG_CLOCK_LOCAL}\n&${COLOR} No login info available")
    else
	if [ "${CLIENT_WHO_MSG}" = "[who]" ]
	then
	    MSG=$(echo -e "${CLIENT_MSG_TIME}\n&${COLOR} No users logged in")
	else
	ORIGIFS=${IFS}; IFS=$'\n'
	for LINE in ${CLIENT_WHO_MSG}
	do
	    IFS=${ORIGIFS}
	    read LOGIN_NAME dummy <<< ${LINE}
	    if [ "${LOGIN_NAME}" = "root" ]
	    then
		# root logged in
		COLOR="${ROOTLOGIN_COLOR}"
		MSG="&${COLOR} root logged in!"
		debug "${HOSTNAME} root login detected"
	    else
		if [ "${LOGIN_NAME}" = "[who]" ]; then continue; fi
		if [ "${COLOR}" != "${ROOTLOGIN_COLOR}" ]; then COLOR="${USERLOGIN_COLOR}"; fi
		debug "${HOSTNAME} user login detected"
	    fi
	done
	MSG=$(echo -e "${CLIENT_MSG_TIME}\n${MSG}\n\n${CLIENT_WHO_MSG}")
	fi
    fi

    if [ ${CLIENT_MSG_AGE} -gt ${MAXIMAL_CLIENT_MSG_LIFETIME_SECONDS} ]
    then
	# message too old.
	CLIENT_MSG_TIME="${CURR_DATE}"
	COLOR="clear"
	MSG=$(echo -e "${CLIENT_MSG_TIME}\n&${COLOR} Client message older than ${MAXIMAL_CLIENT_MSG_LIFETIME} minutes\n\nLast message:\n\n${MSG}")
    fi
    
    ${BB} ${BBDISP} "status ${HOSTNAME}.${COLUMN} ${COLOR} ${MSG}"
    #debug "${BB} ${BBDISP} status ${HOSTNAME}.${COLUMN} ${COLOR} ${MSG}"
    debug "${HOSTNAME} = ${COLOR}"
done
exit 0
