#!/bin/ksh
#######################################################################
#
#Copyright @ 2007 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A.
#All rights reserved.U.S. Government Rights - Commercial software.  Government users are subject to the
#Sun Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its supplements.
#Use is subject to license terms.  Sun,  Sun Microsystems,  the Sun logo and  Sun Ray are trademarks or
#registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.UNIX is a registered trademark
#in the U.S. and other countries, exclusively licensed through X/Open Company, Ltd.
#
#SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
#THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
#TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
#PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
#ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
#DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
#
## Source: canary_probe_utwho
## Application Name: Ron Shipper
## Application SIC Code:
## CVS ID: @(#) $Id: canary_probe_utwho,v 2.3 2007/06/07 17:59:36 waynec Exp $ Sun IT
#
## Author: Sean Meighan
## Date Created: March 2004
#
## General Description(Purpose):  Simple System Monitoring Application is a simple yet powerful monitoring
## tool that lets you quickly identify error conditions on systems running
## Solaris (SPARC and x86) and Linux.   The Monitor is useful for locating
## and monitoring sources of unusual program load, such as runaway processes,
## memory leaks, problems with network interface traffic, etc.
#
## Usage: The script will print a list of the actove Sun Ray users
#
## Required Parameters:
## Optional Parameters:
## Return Codes:
#
## Possible Points of Failure:
#
#
#######################################################################

trap "rm -f /tmp/map1.$$ /tmp/map2.$$ /tmp/map3.$$ /tmp/map4.$$" 0 1 2 15
PATH=/bin:/usr/bin

connected=false
all=false
firmware=false
load=false
forceload=false
fwvers=

if [ ! -d /tmp/SUNWut ]
then
	print -u2 "canary_probe_utwho must be run on a Sun Ray server"
	exit 1
fi

function Usage
{
	print -u2 "Usage: $0 [-caflL]"
	print -u2 "	-c	list connected sessions"
	print -u2 "	-a	include idle (not logged in) sessions"
	print -u2 "	-f	list firmware version"
	print -u2 "	-l	do firmware upgrade of idle sessions"
	print -u2 "	-L	force firmware upgrade of all sessions"
	exit 1
}

while getopts :cfalL ARGNAME
do
	case $ARGNAME in
	   c)	connected=true;;	
	   a)	all=true;;
	   l)	load=true;;
	   L)	load=true; forceload=true;;
	   f)	firmware=true;;
	   *)	print -u2 "Invalid argument" "$*"
		Usage;;
	esac
done

if [ $load = "true" ]
then
	all=true
	case "$(id)" in
		'uid=0('*) ;;
		*) print -u2 "Must be root to use -l option"
		   exit;;
	esac
	fwvers=$(/opt/SUNWut/sbin/utfwadm -P | $AWK '{ print $3; exit}')
	print "Upgrading units to version $fwvers:"
fi

$AWK '/Dtlogin\..*\.environment:/ {
	sub("Dtlogin...", "", $1)
	sub("\.environment:", "", $1)
	sub("SUN_SUNRAY_TOKEN=", "", $2)
	sub("JavaBadge-nonPers", "JavaBadgeNP", $2)
	print $1, $2
	}' /etc/dt/config/Xconfig | sort >/tmp/map1.$$

$AWK '
	BEGIN {
		name = ""
		disp = ""
	}

	FILENAME != fn {
		if (name != "" && disp != "") {
			print disp, name
			name = ""
			disp = ""
		}
		fn = FILENAME
	}

	/uid=/ {
		user = substr($0, index($0, "=") + 1)
		cmd = "getent passwd " user
		cmd | getline name
		close(cmd)
		sub(":.*", "", name)
	}

	/XID=/ {
		disp = substr($0, index($0, "=") + 1)
		name = "????"
	}

	END {
		if (name != "" && disp != "")
			print disp, name
	}' /tmp/SUNWut/session_proc/* | sort >/tmp/map2.$$

join -j 1 /tmp/map1.$$ /tmp/map2.$$ | sort +1 > /tmp/map4.$$

if [ $connected = "false" -a $firmware = "false" -a $load = "false" ]
then
	sort -n /tmp/map4.$$ | \
		$AWK -v all=$all '
			{
				if (all == "false" && $3 == "????")
					next
				printf "%3d %-36s %-8s\n", $1, $2, $3
			}'
	exit
fi
	
exec 8<> /dev/tcp/0.0.0.0/7010
print -u8 status
$AWK '
	BEGIN {
		mhi = 0
	}

	/terminalId=/ {
		tid = substr($0, index($0, "P"))
	}

	/tokenName=/ {
		token = substr($0, index($0, "=") + 1)
		sub("JavaBadge-nonPers", "JavaBadgeNP", token)
	}

	/terminalIPA=/ {
		ipa = substr($0, index($0, "=") + 1)
	}

	/MultiheadTerminalIndex=/ {
		mhi = substr($0, index($0, "=") + 1)
	}

	/firmware=/ {
		fw = substr($0, index($0, "=") + 1)
	}

	/^end$/ {
		print token, ipa, tid, mhi, fw
		mhi = 0
	}
	' </dev/fd/8 | sort >/tmp/map3.$$

join -j1 2 -j2 1 -o 1.1 1.2 1.3 2.2 2.3 2.4 2.5 /tmp/map4.$$ /tmp/map3.$$ | \
	$AWK -v firmware=$firmware -v all=$all -v fwvers="$fwvers" \
		-v load=$load -v forceload=$forceload -v host=`hostname` '{
		if (all == "false" && $3 == "????")
			next
		if (firmware == "true")
			printf "%3d.%d %-8s %-15s %s\n", $1, $6, \
				$3, $4, $7
		else if (load == "true") {
			if ($7 == fwvers)
				next
			if (forceload == "false" && $3 != "????")
				next
			printf "%3d.%d %-8s %-15s %s\n", $1, $6, \
				$3, $4, $7
			cmd = "/opt/SUNWut/lib/utload -S " host " -w -b -d " \
				$1 " -m " $6
			system(cmd)
		} else
			printf "%3d.%d %-32s %-8s %-15s %-s\n", $1, $6, \
				$2, $3, $4, $5 }' | \
	sort -n
