#!/usr/bin/sh
#
# System Information Collector Script
#
# Written by Tako van Gaalen <tako@flexnet.org>
#
# Version 4.0
#
#
#

# Setting variable options
DATE=`date +'%d-%m-%Y %H:%M'`

# Servers that need to be reach va other server
# (Prefix of hostname)
SERV_PREF_GW="xx|xx|xx"

# Server which is to be used to reach other servers
GATEWAY="gateway1"

# Default show all information?
SHOWALL=1

# Default ping check?
PINGSERVER=1


################################################################################
# DO NOT CHANGE ANYTHING BELOW!!!                                              #
################################################################################


VERSION="4.0"

# Log debug output to standard error
debug() {
        if [ "$SYSINFODEBUG" -eq 1 ]; then
                echo "debug: [$DATE] $1" >&2
        fi
}


# Show all the cli options to our users.
usage() {
    debug "usage: Printing usage and exiting"
    cat<<EOF
Usage: sysinfo [-dhvpaYSADWC] [-s <node|file>] [-o file]
-d                 = debug mode
-h                 = print this help information and exit
-v                 = print sysinfo version 
-p                 = disable ping check
-s <node|file>     = specify server name or file with list
-o <file>          = output file, comma seperated
-a                 = show all output (Default)
-Y                 = show system info
-S                 = show storage info
-A                 = show authentication info
-D                 = show databases info
-W                 = show webservers info
-C                 = show cluster info
EOF
}

if [ "$1" = "" ]
        then
        debug "No option given, printing help and exitting."
        usage
        exit 1
fi

# Set the getopt strings
GETOPTS="ads:ho:vpASDCWY"

# Check the command line options
while getopts $GETOPTS opt; do
    case "$opt" in
	d)
	SYSINFODEBUG="1"
	debug "Turning on debug mode"
	;;
	s)
	INPUTSERVER="$OPTARG"
	debug "Setting server input "
	;;
	o)
	DATABASE=1
	OUTPUTFILE="$OPTARG"
	debug "Setting output file"
	;;
	p)
	debug "Setting SERVERPING=0"
	PINGSERVER=0
	;;
	v)
	debug "Printing version and exitting"
	echo "Sysinfo $VERSION, by tako@flexnet.org"
	exit 1
	;;
	h)
	debug "Printing help and exitting"
	usage
	exit 1
	;;
        a)
        debug "Setting SHOWALL=1"
        SHOWALL=1
        ;;
	A)
	debug "Setting SHOWAUTH=1"
	SHOWAUTH=1
	SHOWALL=0
	;;	
        D)
        debug "Setting SHOWDB=1"
        SHOWDB=1
	SHOWALL=0
        ;;     
        W)
        debug "Setting SHOWWEB=1"
        SHOWWEB=1
	SHOWALL=0
        ;;
        C)
        debug "Setting SHOWCLUSTER=1"
        SHOWCLUSTER=1
	SHOWALL=0
        ;;
        S)
        debug "Setting SHOWSTOR=1"
        SHOWSTOR=1
	SHOWALL=0
        ;;
        Y)
        debug "Setting SHOWSYS=1"
        SHOWSYS=1
	SHOWALL=0
        ;;
    esac
done
	

# Is -s server or list of servers
debug "Checking for server name or file"
if [ -f $INPUTSERVER ]
    then
	SERVER=`cat $INPUTSERVER`
    else
	SERVER=$INPUTSERVER
fi


# Database shit

createdb () {
        if [ "$DATABASE" -eq "1" ]
            then
        debug "Creating DB file with header"
        printf "HOSTNAME;HARDWARE;HOSTID;ARCHITECTURE;OS;VERSION;KERNEL;HP_LVM;SUN_LVM;VERITAS_LVM;SAN;NIS;LDAP;SYBASE;ORACLE;WEBSPHERE;WEBSERVER;CLUSTER\n" > $OUTPUTFILE
        fi

}

# Sysinfo function

sysinfo () {

	# Define function variables
OS=`uname -s`
HOSTNAME=`uname -n`
UPTIME=`
                case $OS in
                AIX)
                uptime | cut -d " " -f6-9
                ;;
                *)
                uptime | cut -d " " -f 4-7
                ;;
                esac
                `
PLATFORM=`
                case $OS in
                AIX)
                uname -p
                ;;
                *)
                uname -i
                ;;
                esac`
HARDWARE=`
        case $OS in
                SunOS)
                /usr/platform/$PLATFORM/sbin/prtdiag -v | grep Microsystems | cut -d " " -f 8-
                ;;
                HP-UX)
                model | cut -d "/" -f3
                ;;
                Linux)
                echo "Unknown"
                ;;
        esac
`
HOSTID=`
        case $OS in
                HP-UX)
                echo "N/A"
                ;;
                SunOS)
                /usr/bin/hostid
                ;;
                Linux)
                /usr/bin/hostid
                ;;
                AIX)
                /usr/sbin/hostid
                ;;
        esac
                `
ARCHITECTURE=`if \[ \`uname -s\` != "SunOS" \]; then echo N/A; else uname -p; fi`
VERSION=`
                case $OS in
                SunOS)
                uname -r
                ;;
                HP-UX)
                uname -r
                ;;
                Linux)
                cat /proc/version | cut -d " " -f 9-12
                ;;
                AIX)
                oslevel
                ;;
                esac
                `
                KERNEL=`
                case $OS in

                Linux)
                uname -r
                ;;
                *)
                uname -v
                ;;
                esac
`
                LINUX_LVM=`
        case $OS in
                Linux)
                echo "YES"
                ;;
                *)
                echo "N/A"
                ;;
        esac
                `
SUN_LVM=`
        case $OS in
                SunOS)
                if \[ \`df -lk | grep md | grep -v grep | wc -l\` = "0" \]; then echo "YES"; else echo "NO"; fi;
                ;;
                *)
                echo "N/A"
                ;;

        esac
                `
VERITAS_LVM=`
        case $OS in
                HP-UX)
                if \[ \`bdf -l | grep vx | grep -v grep | wc -l\` != "0" \]; then echo "YES";else echo "NO"; fi;
                ;;
                SunOS)
                if \[ \`df -lk | grep vx | grep -v grep | wc -l\` != "0" \]; then echo "YES";else echo "NO"; fi;
                ;;
                *)
                echo "N/A"
                ;;
        esac
                `
HP_LVM=`
        case $OS in
                HP-UX)
                if \[ \`bdf -l | grep vx | grep -v grep | wc -l\` = "0" \]; then echo "YES";else echo "NO"; fi;
                ;;
                *)
                echo "N/A"
                ;;
        esac
                `
SAN=`
        case $OS in
                SunOS)
                if \[ \`iostat -E | grep EMC | wc -l\` != "0" \]; then echo YES; else echo NO; fi
                ;;
                HP-UX)
                if \[ \`sudo ioscan -fnkC disk | grep EMC | wc -l\` != "0" \]; then echo YES; else echo NO; fi
                ;;
                AIX)
                if \[ \`lspv | grep ^hdiskpower | wc -l\` != "0" \]; then echo YES; else echo NO; fi
                ;;
                Linux)
                echo "N/A"
                ;;
        esac
`
LDAP=`if \[ \`ps -ef | grep [l]dap | wc -l\` != "0" \]; then echo YES; else echo NO; fi`
NIS=`if \[ \`ps -ef | grep [y]pbind | wc -l\` != "0" \]; then echo YES; else echo NO; fi`
SYBASE=`if \[ \`ps -ef | grep [s]ybase | wc -l\` != "0" \]; then echo YES; else echo NO; fi`
ORACLE=`if \[ \`ps -ef | grep pmon | grep -v grep | wc -l\` = "0" \]; then echo "NO"; else echo "YES";fi`
WEBSPHERE=`if \[ \`ps -ef | grep -i was | grep -v grep | wc -l\` = "0" \]; then echo NO; else echo YES; fi`
WEBSERVER=`if \[ \`ps -ef | grep -i apache | grep -v grep | wc -l\` = "0" \]; then echo "None"; else echo "Apache"; fi`
CLUSTER=`if \[ -f /usr/cluster/bin/scstat \]; then if \[ -z \`/usr/cluster/bin/scstat \` \];then echo "NO"; else echo "YES";fi; else echo "NO"; fi `


	
        SSH=`case \`echo $SERVER | cut -c1-2\` in
                as|bs|ws)
                echo ssh $GATEWAY ssh
                ;;
                *)
                echo ssh
                ;;
        esac`

	debug "Checking how to use ssh - SSH = $SSH"

		debug "Starting SSH command."
                $SSH $s "

		# System information
                printf \"\n\"
                printf \"[.........System]\n\"
                printf \"Hostname\t: $HOSTNAME\n\"
                printf \"OS\t\t: $OS\n\"

           
		if [ \"$SHOWSYS\" = \"1\" ] || [ \"$SHOWALL\" = \"1\" ]
		    then
                printf \"Uptime\t\t: $UPTIME\n\"
                printf \"Hardware\t: $HARDWARE\n\"
                printf \"Host ID\t\t: $HOSTID\n\"
                printf \"Architecture\t: $ARCHITECTURE\n\"
                printf \"Version\t\t: $VERSION\n\"
                printf \"Kernel\t\t: $KERNEL\n\"
		fi

		if [ \"$SHOWSTOR\" = \"1\" ] || [ \"$SHOWALL\" = \"1\" ]
		    then
                printf \"[........Storage]\n\"
                printf \"HP LVM\t\t: $HP_LVM\n\"
                printf \"Sun LVM\t\t: $SUN_LVM\n\"
                printf \"Linux LVM\t: $LINUX_LVM\n\"
                printf \"Veritas LVM\t: $VERITAS_LVM\n\"
                printf \"SAN Storage\t: $SAN\n\"
		fi


		if [ \"$SHOWAUTH\" = \"1\" ] || [ \"$SHOWALL\" = \"1\" ]
		    then
                printf \"[.......Security]\n\"
                printf \"NIS\t\t: $NIS\n\"
                printf \"LDAP\t\t: $LDAP\n\"
		fi
		
		if [ \"$SHOWDB\" = \"1\" ] || [ \"$SHOWALL\" = \"1\" ]
		    then
                printf \"[......Databases]\n\"
                printf \"Sybase\t\t: $SYBASE\n\"
                printf \"Oracle\t\t: $ORACLE\n\"
		fi

		if [ \"$SHOWWEB\" = \"1\" ] || [ \"$SHOWALL\" = \"1\" ]
		    then
                printf \"[......Webserver]\n\"
                printf \"Websphere\t: $WEBSPHERE\n\"
                printf \"Webserver\t: $WEBSERVER\n\"
		fi

		if [ \"$SHOWCLUSTER\" = \"1\" ] || [ \"$SHOWALL\" = \"1\" ]
		    then
                printf \"[........Cluster]\n\"
                printf \"Clustered\t: $CLUSTER\n\"
		fi

                printf \"\n\"

                "
		if [ "$DATABASE" -eq "1" ]
		    then
		debug "Adding output to database file" 
	printf "$HOSTNAME;$HARDWARE;$HOSTID;$ARCHITECTURE;$OS;$OSVERSION;$KERNEL;$HP_LVM;$SUN_LVM;$VERITAS_LVM;$SAN;$SYBASE;$NIS;$LDAP;$ORACLE;$WEBSPHERE;$WEBSERVER;$CLUSTER\n" >> $OUTPUTFILE
		fi
}
	
# Actual sysinfo loop

if [ "PINGSERVER" -eq "1" ]
    then
	debug "Starting ping check"
	createdb
	for s in $SERVER
	do
	printf "Trying server $s...\n"

	if [ `ping $s 2 | grep [a]live | wc -l` != "0" ]
	    then
		printf "Server is up, collecting information.\n"
 		
		sysinfo
	    else
	    	printf "Server is down, skipping.\n"
	    wait

	fi
	done
    else
	debug "Ping check skipped."
	createdb
	for s in $SERVER
	do
	sysinfo
	done
fi

