#!/bin/sh
# Shell script to collect general data.
# Copyright (C) 2001-2004 Open Challenge B.V.
# Copyright (C) 2004 OpenEyeT Professional Services.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.
# If not, write to the Free Software Foundation,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Contact information: www.OpenEyeT.nl/scc/index.html 

# This is a system module of scc, to call it separately in the
# proper environment, use: scc-collect -i -e <module_name>

# SCC-release:	1.6.26
# file-version:	$Revision: 1.30 $
 
. ${SCC_BIN}/scc_modules/scc_utils

##	START: GENERAL
echo "fix:general::layout-version:1.6.26"

echo "fix:general::hostname:${HOSTNAME}"

if [ -x /bin/dnsdomainname ]
then
	DOMAIN=$(/bin/dnsdomainname)
else
	DOMAIN=${hostname#*.}
	if [ "${DOMAIN}" = "${hostname}" -a -f /etc/resolv.conf ]
	then
		DOMAIN=$(awk '/^domain/ { print $2 }' /etc/resolv.conf 2>/dev/null | tail -1)
	fi
	if [ "${DOMAIN}" = "${hostname}" ]
	then
		DOMAIN=$(nslookup ${hostname} 2>/dev/null | sed -n "s/^Name.*${hostname}\.//p")
	fi
fi
echo "fix:general::domain:${DOMAIN}"

echo "fix:general::nodename:$(uname -n)"
scc_check_file /etc/nodename "fix:general::/etc/nodename:" "#"
echo "fix:general::model-id:$(uname -m)"

# RH7.2 installs the MesaDemo in /usr/bin. Program model is part of this demo.
if [ -x /usr/bin/model -a "${OS_NAME}" != "Linux" ]
then
	echo "fix:general::model:$(/usr/bin/model)"
	if [ "${OS_NAME}" = "HP-UX" ]
	then
		my_model="$(model | awk -F/ '{ print $NF }')"
		grep "^${my_model}[ 	]*" /usr/sam/lib/mo/sched.models /usr/lib/sched.models 2>/dev/null	|
		head -1		|
		sed -e 's/^/fix:general::cpu-type:/'

		speed="$(echo "itick_per_usec/D" | adb ${HPUX_KERNEL} /dev/kmem 2>/dev/null | awk '/[0-9]$/ { print $2 }')"
		if [ -n "${speed}" ]
		then
			echo "fix:general::cpu-speed:${speed} Mhz"
		fi
	fi
else
	if [ "${OS_NAME}" = "SunOS" ]
	then
		echo "fix:general::model:$(uname -i | sed -e 's/.*,//')"
	elif [ "${OS_NAME}" = "AIX" ]
	then
		# Contributed by Doug Probst.
		echo "fix:general::model:$(lsattr -El sys0 -a modelname | cut -d"," -f2 | cut -d" " -f1)"

		prtconf 2>/dev/null				|
		awk -F:	'/CPU Type/		{ print prefix "cpu-type:" $2 }
			/Processor Clock Speed/	{ print prefix "cpu-speed:" $2 }' prefix="fix:general::"
	elif [ -x /sbin/hwmgr ]
	then
		# TRU64
		echo "fix:general::model:$(/sbin/hwmgr get attribute -id 1 2>&1 | sed -n -e 's/^ *name = //p')"
	fi
fi

if [ -x /usr/bin/hostid ]
then
	# SunOS
	echo "fix:general::hostid:$(/usr/bin/hostid 2>/dev/null)"
else
	if [ "${OS_NAME}" = "AIX" ]
	then
		# Doug Probst: get the  serial number of the CEC, needed for all service calls.
		echo "fix:general::serial number:$(lsattr -El sys0 -asystemid | cut -d"," -f2 | cut -d" " -f1)"
	elif [ "${OS_NAME}" != "Linux" ]
	then
		echo "fix:general::serial number:$(uname -i 2>/dev/null)"
	fi
fi

if [ "${OS_NAME}" = "HP-UX" ]
then
	if [ -x /usr/contrib/bin/machinfo ]
	then
		/usr/contrib/bin/machinfo		|
		sed -n -e 's/.*machine serial number = /fix:general::System Serial Number:/p'
	else
		scc_help_info "fix:general::" <<-_X_
			The System Serial can be retrieved fast by means of machinfo.
			When this program is absent, it is obtained via stm in the hardware module.
			At the next run of scc it is  present under class "general".
		_X_
		serial="$(grep "fix:general::System Serial Number:" ${SCC_KEEP_CONFIG} | tail -1)"
		if [ "${serial}" ]
		then
			echo "${serial}" >>${SCC_KEEP_NEW}
			echo "${serial}"
		fi
	fi
elif  [ "${OS_NAME}" = "OSF1" ]
then
	# Contributed by Mattijs de Ruijter.
	echo "fix:general::System Serial Number:$(consvar -g sys_serial_num 2>/dev/null | awk '{ print $3 }')"
fi

if [ -x /usr/sbin/bootinfo ]
then
	echo "fix:general::physical key:$(/usr/sbin/bootinfo -k)"
	echo "fix:general::architecture:$(/usr/sbin/bootinfo -T)"
fi

echo "fix:general::license:$(uname -l 2>/dev/null)"

if [ -x /usr/sbin/psrinfo ]
then
	cpu_cnt=$(/usr/sbin/psrinfo | wc -l)
elif [ -x /usr/sbin/lscfg ]
then
	cpu_cnt=$(/usr/sbin/lscfg | grep "^+ proc[0-9]*" | wc -l)
elif [ -x /usr/bin/sar ]
then
	cpu_cnt=$(/usr/bin/sar -M 1 1 2>/dev/null | sed -e '1,/idle$/d' -e '/system/d' | wc -l)
elif [ -f /proc/interrupts ]
then
	cpu_cnt=$(head -1 /proc/interrupts | wc -w)
elif [ -x /usr/sbin/sizer ]
then
	cpu_cnt=$(/usr/sbin/sizer -p)
else
	cpu_cnt="?"
fi
echo "fix:general::cpu_cnt:${cpu_cnt##* }"

echo "fix:general::OS-name:${OS_NAME}"

if [ -x /usr/bin/oslevel ]
then
        # Doug Probst: added -r to display maintenance level.
	release=$(/usr/bin/oslevel -r)
else
	if [ -x /usr/sbin/sizer ]
	then
		# TRU64: sizer -v returns more detailled data.
		release="$(/usr/sbin/sizer -v | awk '{ if ( $3 ~ "^V" ) { print $3 } else { print $4 } }')"
	else
		release=$(uname -r)
	fi
fi
echo "fix:general::OS-release:${release}"

echo "fix:general::OS-version-level:$(uname -v)"

if [ "${OS_NAME}" = "HP-UX" ]
then
	hpux_trusted=0
	if [ -x /usr/lbin/modprpw ]
	then
		/usr/lbin/modprpw >/dev/null 2>&1
		if [ $? -eq 2 ]
		then
			hpux_trusted=1
		fi
	fi
	echo "fix:general::hpux-trusted:${hpux_trusted}"
fi

distro_class="fix:general::linux-distribution:"
scc_linux_distro "${distro_class}"
if [ -n "${SCC_LINUX_DISTRO}" ]
then
	echo "${distro_class}${SCC_LINUX_DISTRO}"
fi
scc_check_file /etc/lsb-release "fix:general::/etc/lsb-release:" "#"	# Linux Standard Base

scc_check_file /etc/release "fix:general::/etc/release:" "#"		# Solaris

scc_time_zone=""
if [ -f /etc/TIMEZONE ]
then
	scc_time_zone=$(sed -n -e 's/^TZ=//p' /etc/TIMEZONE)
	echo "fix:general::timezone:${scc_time_zone}"

elif [ -f /etc/sysconfig/clock ]
then
	sed	-e '/^$/d'	\
		-e 's@^@fix:general::timezone:/etc/sysconfig/clock:	@' /etc/sysconfig/clock
elif [ -h /etc/localtime ]
then
	scc_time_zone="$(ls -l /etc/localtime | sed -e 's@.*/@@')"
	echo "fix:general::timezone:/etc/localtime:${scc_time_zone}"
else
	date '+%Z'			|
	sed -e 's/^/fix:general::timezone:date:/'
fi
if [ "${scc_time_zone}" ]
then
	# Keep for system-module
	echo "fix:general::timezone:${scc_time_zone}" >>${SCC_KEEP_NEW}
fi

if [ -x /sbin/runlevel ]
then
	echo "fix:general::run level:$(/sbin/runlevel)"
elif [ -x /sbin/getrunlvl ]
then
	echo "fix:general::run level:$(/sbin/getrunlvl)"
else
	case "${OS_NAME}" in
	*BSD)	echo "fix:general::run level:$(sysctl kern.securelevel 2>/dev/null)"
		;;
	*)	echo "fix:general::run level:$(who -r 2>/dev/null | awk '{ print $3 }')"
		;;
	esac
fi

# Edit scc-localize to change the default setting of this variable.
prefix="var"
if [ "${SCC_ROOT_PW}" != "var" ]
then
	prefix="fix"
fi
if [ -f /tcb/files/auth/system/default ]
then
	# HP-UX trusted system
	grep ":u_pwd" /tcb/files/auth/r/root
elif [ -f /etc/shadow ]
then
	# default shadow password file
	grep "^root:" /etc/shadow
elif [ -f /etc/master.passwd ]
then
	# FreeBSD, NetBSD and OpenBSD
	grep "^root:" /etc/master.passwd
else
	# old-fashioned password file
	grep "^root:" /etc/passwd 2>/dev/null
fi							|
scc_crypt						|
sed -e "s/^/${prefix}:general::superuser password:/"

if [ "${OS_NAME}" = "HP-UX" ]
then
	# /usr/sam/lbin/getmem is not supported and does not work correctly for more than 512MB.

	pg_size=$(getconf PAGE_SIZE 2>/dev/null)
	pg_nmbr=$(echo "memory_installed_in_machine/D" | adb ${HPUX_KERNEL} /dev/kmem 2>/dev/null | awk '{ if ( NF == 2 ) print $2 }' )

	# pg_nmbr does not include the entries in the Page Deallocation Table.
	# The shell does not "round" the following calculation, it truncates.
	# On systems with some pages deallocated, this results in odd memory sizes.
	# To avoid this, increase the number of "alive" pages with the size of the PDT
	# (50 on the systems I have seen).
	# Avoid overflow by dividing $pg_size separately.
	memory=$(( ( ${pg_nmbr:-0} + 50 ) * ( ${pg_size:-4096} / 1024 ) / 1024  ))
	echo "fix:general::memory real:${memory} MB"

	memory=$(dmesg 2>/dev/null | grep '^ *Physical:')
	if [ "${memory}" ]
	then
		# Make this data variable, to avoid that "changes" get logged when
		# this data disappears from the fixed sized kernel message buffer.
		echo "var:general::memory real (dmesg):$(echo "${memory}" | awk '{ print $2 }')"
		echo "var:general::memory lockable (dmesg):$(echo "${memory}" | awk '{ print $5 }')"
		echo "var:general::memory available (dmesg):$(echo "${memory}" | awk '{ print $8 }')"
	fi
elif [ "${OS_NAME}" = "SunOS" ]
then
	memory="$(dmesg | grep '^mem =' | tail -1)"
	if [ "${memory}" ]
	then
		echo "fix:general::memory real:${memory}"
		echo "fix:general::memory available:$(dmesg | grep '^avail mem =' | tail -1)"
	else
		echo "fix:general::memory real:$(prtconf 2>/dev/null | sed -n -e 's/^Memory size: //p')"
	fi
elif [ "$OS_NAME" = "AIX" ]
then
        # bootinfo should have worked but does not, this does (Doug Probst).
	echo "fix:general::memory real:$(lsattr -El mem0 -a goodsize | awk '{ print $2 }') MB"
elif [ -x /usr/bin/free ]
then
	echo "fix:general::memory real:$(free -b | awk '/^Mem/ { print $2 / ( 1024 * 1024 ) }') MB"
elif [ -f /proc/meminfo ]
then
	echo "fix:general::memory:$(awk '/^MemTotal:/	{ $1=""; print }' /proc/meminfo 2>/dev/null)"
elif [ -x /sbin/hwmgr ]
then
	echo "fix:general::memory:$(/sbin/hwmgr get attribute -id 1 2>&1 | sed -n -e 's/^memory//p')"
fi

if [ -x /usr/bin/pagesize ]
then
	echo "fix:general::memory pagesize:$(/usr/bin/pagesize)"
fi

echo "var:general::uptime:$(uptime 2>/dev/null | sed -e 's/.* up //' -e 's/,.*//' -e 's/^ *//')"
if [ -f /etc/rc.log ]
then
	echo "var:general::rclog:$(ls -l /etc/rc.log)"
fi
if [ -f /etc/shutdownlog ]
then
	echo "var:general::shutdownlog:$(ls -l /etc/shutdownlog)"
fi

exit 0
