#!/bin/sh
#############################################################################
# Copyright (C)2004-2005, Sun Microsystems Inc. All rights reserved.
# Use is subject to license terms.
#
# Name: solaris_inventory
# Description: Inventory collection script for the Agent.

# pragma ident	"$Id: solaris_inventory.sh,v 1.6.2.3 2005/06/10 17:50:52 jr150870 Exp $"
##############################################################################

debug()
{
    echo "$*"
    log daemon.info "$*"
}

error()
{
    log daemon.err "$*"
}

warning()
{
    log daemon.warning "$*"
}

##
# Log a message to syslogd
# $1 - priority. Something like daemon.warning
# rest - the message
log()
{
    level=$1
    shift
    msg="$*"
    logger -t "IM-CC_ENG-inv solaris_inventory.sh" -p $level "$msg"
}

#### Path to this file and its brethren
platform="/usr/lib/cc-cfw/platform"
invagent="$platform/invagent/"
invAgentBin="$invagent/bin"

PATH="/bin:/usr/bin:/usr/sbin"

PERL=/usr/bin/perl

#### Directory to collect the data#####
DATADIR="/var/tmp"

#### Version of the Inventory Agent, not this file.
INVENTORY_AGENT_VERSION="1.0"
OS_NAME=`/usr/bin/uname -s`

#### Files for static data,pkginfo,patchinfo and hwinfo ####
## NB: file names must start with inv_ so ALLTMPS will match!
STATIC_DATA="$DATADIR/inv_static"
PKG_INFO_DATA="$DATADIR/inv_pkginfo"
PATCH_INFO_DATA="$DATADIR/inv_patchinfo"
HW_INFO_DATA="$DATADIR/inv_hwinfo"
PATCHPRO_ANALYSIS_DATA_NAME="patchpro_analysis"
PATCHPRO_ANALYSIS_DATA="$DATADIR/inv_$PATCHPRO_ANALYSIS_DATA_NAME"
PATCHPRO_LIST_DATA_NAME="patchpro_installed_patch_list"
PATCHPRO_LIST_DATA="$DATADIR/inv_$PATCHPRO_LIST_DATA_NAME"

#### cksum file ####
CKSUM_FILE="$DATADIR/inv_cksum"

##### All Files ######
ALLTMPS="$DATADIR/inv_*"

# This is the default patch collection/baseline.
# We go ahead and specify the collection on the command line because 
# the user can change the default by changing a patchpro configuration file. 
# We need to use what SWUP and the rest think are the right collection to be
# using rather than what the user likes for local reports.
PATCHPRO_COLLECTION=`/usr/lib/cc-ccr/bin/ccr -g cns.swup.patchbaseline`
rc=$?
if [ $rc -ne 0 ];then
    error "Can not get cns.swup.patchbaseline from CCR. No recovery available. Stopping"
    exit 1
fi

STATIC_DATA_VERSION=1
PKG_INFO_DATA_VERSION=1
HW_INFO_DATA_VERSION=1
PATCH_INFO_DATA_VERSION=1
PATCHPRO_ANALYSIS_DATA_VERSION=1
PATCHPRO_LIST_DATA_VERSION=1

####
## Write metadata in current form to the front of a file
## $1 = blob version
## $2 = blob command status
## $3 = file name containing the data to which we prepend the metadata.
## Globals:
## OS_NAME
## INVENTORY_AGENT_VERSION
PrependMetadata()
{
    if [ $# -ne 3 ];then
        debug "Wrong number of parameters to echoMetadata()"
        exit 99
    fi
    blob_ver=$1
    blob_status=$2
    file=$3
    # Save the data
    cp $file "${file}_"
    {
        echo transport_jms_data_inventory_agent_version=$INVENTORY_AGENT_VERSION
        echo transport_jms_data_inventory_agent_blob_version=$1
        echo transport_jms_data_inventory_agent_blob_command_status=$2
        echo transport_jms_data_inventory_agent_blob_create_time=`date -u +"%Y%m%d%H%M%S"`
        echo transport_jms_data_inventory_agent_os_name=$OS_NAME
        # blank line terminates list of metadata
        echo
        #copy the data back
        cat "${file}_"
        rm -f "${file}_"
    } > $file
}

#####Start with removing the old data #####
rm -rf $ALLTMPS

#####  First collect static data  #####

# Find the IP of this host
if [ -x $PERL ]; then
    echo "use Socket;" > /tmp/findip.perl
    echo "(\$name, \$a, \$t, \$l, @a)=gethostbyname(\""`hostname`"\");" >> /tmp/findip.perl
    echo "print join(\", \",map (inet_ntoa(\$_),@a)), \"\\\n\";" >> /tmp/findip.perl
    ip=`$PERL /tmp/findip.perl 2> /dev/null`
    ret=$?
    if [ $ret -gt 0 ]; then
	ip=
    fi
fi
if [ "$ip" = "" ]; then
    grep `hostname` /etc/hosts > /tmp/findip.txt
    ret=$?
    if [ $ret -eq 0 ]; then
	ip=`head -1 /tmp/findip.txt | cut -f1`
    fi
fi
# Tidy up.
rm -f /tmp/findip.txt /tmp/findip.perl

## host OS name ##
/usr/bin/echo os-name:  `/usr/bin/uname -s` > $STATIC_DATA
## host id ##
/usr/bin/echo host-id:  `/usr/bin/hostid`   >>  $STATIC_DATA
## host name ##
/usr/bin/echo host-name:  `/usr/bin/uname -n`   >>  $STATIC_DATA
## host IP ##
/usr/bin/echo host-address: $ip >> $STATIC_DATA
## domain name ##
/usr/bin/echo host-domain:  `/usr/bin/domainname` >> $STATIC_DATA
## reboot ##
##/usr/bin/echo reboot:  `who -b | awk '{print $4,$5,$6}'`   >>  $STATIC_DATA
/usr/bin/echo "reboot: `/usr/lib/cc-cfw/platform/invagent/bin/getboottime`"   >>  $STATIC_DATA
## host nodename ##
/usr/bin/echo node-name:  `/usr/bin/uname -n` >> $STATIC_DATA
## host OS release level ##
/usr/bin/echo os-release:  `/usr/bin/uname -r` >> $STATIC_DATA
## host OS version ##
/usr/bin/echo os-version:  `/usr/bin/uname -v` >> $STATIC_DATA
## host hardware ##
/usr/bin/echo hardware-name:  `/usr/bin/uname -i` >> $STATIC_DATA
## host machine hardware class ##
/usr/bin/echo hardware-implementation:  `/usr/bin/uname -m` >> $STATIC_DATA
## host ISA or processor type ##
/usr/bin/echo hardware-architecture:  `/usr/bin/uname -p` >> $STATIC_DATA
# locale info
/usr/bin/echo locale: `locale | head -1 | cut -f2 -d=` >> $STATIC_DATA
# serial number hostid on sparc, somewhere else for x86
/usr/bin/echo serial-number: `hostid` >> $STATIC_DATA

# Now the metadata (so we can include the command status)
PrependMetadata $STATIC_DATA_VERSION 0 $STATIC_DATA

#### Collect the pkginfo into a seperate data file, so that a seperate chksum can be calculated ####
## Start off with the host OS name, since this is used for parsing by the tether services. ##
/usr/bin/echo os-name:  `/usr/bin/uname -s` >  $PKG_INFO_DATA
$PERL $invAgentBin/gatherpkginfo.pl >> $PKG_INFO_DATA
rc=$?

# Now the metadata (so we can include the command status)
PrependMetadata $PKG_INFO_DATA_VERSION $rc $PKG_INFO_DATA


#### Collect the patch info into a seperate data file, so that a seperate chksum can be calculated ####
/usr/bin/echo os-name:  `/usr/bin/uname -s` >  $PATCH_INFO_DATA
/usr/bin/showrev -p >> $PATCH_INFO_DATA
rc=$?

# Now the metadata (so we can include the command status)
PrependMetadata $PATCH_INFO_DATA_VERSION $rc $PATCH_INFO_DATA


#### Collect the hardware info into a seperate data file, so that a seperate chksum can be calculated ####
/usr/bin/echo os-name:  `/usr/bin/uname -s` >  $HW_INFO_DATA
$invAgentBin/prtpicl -v -c cpu >> $HW_INFO_DATA
rc=$?

# Now the metadata (so we can include the command status)
PrependMetadata $HW_INFO_DATA_VERSION $rc $HW_INFO_DATA


#### Collect PatchPro Analysis Data ####
# Note: putting error msgs in $PATCHPRO_ANALYSIS_DATA is now the official 
# way to return status. 
# Note: If we find an old version of smpatch it won't know about the -@ flag.
#       For now, we just send the error message up to the server.
if [ -x /usr/sbin/smpatch ];then
    /usr/sbin/smpatch analyze -@ -C patchpro.patchset=$PATCHPRO_COLLECTION > $PATCHPRO_ANALYSIS_DATA 2>&1
    rc=$?
    # Now the metadata (so we can include the command status)
    PrependMetadata $PATCHPRO_ANALYSIS_DATA_VERSION $rc $PATCHPRO_ANALYSIS_DATA
else
    # Just the metadata so we can let the server know about the error
    error "/usr/sbin/smpatch not found on this system." > $PATCHPRO_ANALYSIS_DATA
    PrependMetadata $PATCHPRO_ANALYSIS_DATA_VERSION 999 $PATCHPRO_ANALYSIS_DATA
fi

#### Collect PatchPro patch list (includes metadata required to resolve uninstall dependencies).
# Note: putting error msgs in the output file is the official way to return status.
# Note: If we find an old version of smpatch it won't know about the list command or the -@ flag.
#       For now, we just send the error message up to the server. TBD
if [ -x /usr/sbin/smpatch ];then
    # list subcommand does not support the -C option to override the patchset/collection
    /usr/sbin/smpatch list -@ > $PATCHPRO_LIST_DATA 2>&1
    rc=$?
    # Now the metadata (so we can include the command status)
    PrependMetadata $PATCHPRO_LIST_DATA_VERSION $rc $PATCHPRO_LIST_DATA
else
    error "/usr/sbin/smpatch not found on this system." > $PATCHPRO_LIST_DATA
    PrependMetadata $PATCHPRO_LIST_DATA_VERSION 999 $PATCHPRO_LIST_DATA
fi

#### Form the cksum string in a seperate file ####
# Note: the order of the fields you find here is defined by the InventoryAgent.java code
# If you change this you had best change the other!
/usr/bin/echo `/usr/bin/cksum  $STATIC_DATA | awk '{ print $1 }'\
              `:`/usr/bin/cksum  $PKG_INFO_DATA | awk '{ print $1 }'\
              `:`/usr/bin/cksum  $PATCH_INFO_DATA | awk '{ print $1 }'\
              `:`/usr/bin/cksum  $HW_INFO_DATA | awk '{ print $1 }'\
              `:`/usr/bin/cksum  $PATCHPRO_ANALYSIS_DATA | awk '{ print $1 }' \
              `:`/usr/bin/cksum  $PATCHPRO_LIST_DATA | awk '{ print $1 }'` \
              > $CKSUM_FILE.tmp

#### remove the trailing newline which echo emits ####
/usr/bin/tr -d '\n' < $CKSUM_FILE.tmp > $CKSUM_FILE
/usr/bin/rm $CKSUM_FILE.tmp
