#!/bin/sh
# Shell script to collect general data.
# Copyright (C) 2001-2004 Open Challenge B.V.
# Copyright (C) 2004-2005 OpenEyeT Professional Services.
# Copyright (C) 2005-2009 QNH.
#
# 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: http://scc.qnh-infrastructure.nl/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.9.28
# file-version:	$Revision: 1.59 $
 
set -u

. ${SCC_BIN}/scc_modules/scc_utils

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

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

if [ -x /bin/dnsdomainname ]
then
	DOMAIN=$(/bin/dnsdomainname 2>/dev/null)
elif [ -x /bin/domainname -a "${OS_NAME}" = "Darwin" ]
then
	DOMAIN=$(/bin/domainname)		# Indicated by Antoine Davous.
fi
if [ -z "${DOMAIN:-}" ]
then
	DOMAIN=${hostname#*.}
	if [ "${DOMAIN}" = "${hostname}" -a -f /etc/resolv.conf ]
	then
		DOMAIN=$(awk '/^domain/ { print $2 }' /etc/resolv.conf 2>/dev/null | tail -n 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)"
elif [ "${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)"
elif [ -x /sbin/hwmgr ]
then
	# TRU64
	echo "fix:general::model:$(/sbin/hwmgr get attribute -id 1 2>&1 | sed -n -e 's/^ *name = //p')"
elif [ -x /usr/sbin/dmidecode ]
then
	model="$(/usr/sbin/dmidecode 2>/dev/null			|
		grep -vi				\
			-e 'Not Specified'		\
			-e 'Not Applicable'		\
			-e 'Not Available'		\
			-e 'To Be Filled By O.E.M'	\
			-e 'None'			\
			-e 'unknown'				|
	awk	'/^[ 	]*System Information/	{ started = 1; next }
		/^[ 	]*Manufacturer/		{ if ( started ) { sub( ".*Manufacturer.[ 	]*", "" ); man = $0; prd=""; ver=""; } next }
		/^[ 	]*Product Name/		{ if ( started ) { sub( ".*Product Name.[ 	]*", "" ); prd = $0 } next }
		/^[ 	]*Version/		{ if ( started ) { sub( ".*Version.[ 	]*", "" ); ver = $0 } next }
		/^Handle/			{ started = 0; next }
		END	{
				data = sprintf( "%s %s %s",  man, prd, ver );
				gsub( "[ 	][ 	]*", " ", data );
				print data;
			}')"
	if [ "${model}" != "  " ]		# Two spaces separating 3 empty attributes
	then
		echo "fix:general::model:${model}"
	fi
elif [ -f /var/run/dmesg.boot ]
then
	# For BSD systems, example line:
	#bios0: VMware, Inc. VMware Virtual Platform
	awk '/^bios0:/ { $1=""; print "fix:general::model:" $0; exit; }' /var/run/dmesg.boot
fi

if [ -x /usr/bin/hostid ]
then
	# SunOS
	echo "fix:general::hostid:$(/usr/bin/hostid 2>/dev/null)"
elif [ "${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

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 -n 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

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 [ -f /proc/cpuinfo ]
then
	cpu_cnt="$(grep -c "^processor" /proc/cpuinfo)"
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 -n 1 /proc/interrupts | wc -w)
elif [ -x /usr/sbin/sizer ]
then
	cpu_cnt=$(/usr/sbin/sizer -p)
else
	cpu_cnt="?"
	if [ -x /sbin/sysctl ]		# BSD systems
	then
		sysctl_cpu_cnt="$(/sbin/sysctl -n hw.ncpu 2>/dev/null)"
		if [ "${sysctl_cpu_cnt}" ]
		then
			cpu_cnt="${sysctl_cpu_cnt}"
		fi
	fi
fi
echo "fix:general::cpu_cnt:${cpu_cnt##* }"

if [ "${OS_NAME}" = "HP-UX" ]
then
	my_model="$(/usr/bin/model | awk -F/ '{ print $NF }')"
	grep "^${my_model}[ 	]*" /usr/sam/lib/mo/sched.models /usr/lib/sched.models 2>/dev/null	|
	head -n 1											|
	sed -e 's/^/fix:general::cpu-type:/'

	speed="$(echo "itick_per_usec/${HPUX_ADB_NMBR_FORMAT}" | 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
elif [ "${OS_NAME}" = "AIX" ]
then
	prtconf 2>/dev/null				|
	awk -F:	'/CPU Type/		{ print prefix "cpu-type:" $2, "(" l ")" }
		/Processor Type/	{ l = $2; }
		/Processor Clock Speed/	{ print prefix "cpu-speed:" $2 }' prefix="fix:general::"
elif [ -f /proc/cpuinfo ]
then
	awk '/^cpu.*H[zZ]/	{
					print "var:general::cpu-speed:" $4, $2;	# MHz or GHz
					exit;
				}' /proc/cpuinfo
elif [ -x /sbin/sysctl ]
then
	sysctl_cpu_type="$(/sbin/sysctl -n hw.model 2>/dev/null)"
	if [ "${sysctl_cpu_type}" ]
	then
		echo "fix:general::cpu-type::${sysctl_cpu_type}"
	fi
elif [ -x /usr/sbin/psrinfo ]
then
	# CPU speed can be variable on OpenSolaris.
	/usr/sbin/psrinfo -v					|
	sed -n	-e 's/,//'		\
		-e 's/.*operates at /var:general::cpu-speed:/p'	|
	head -n 1
fi

echo "fix:general::OS-name:${OS_NAME}"
echo "fix:general::license:$(uname -l 2>/dev/null)"

if [ -x /usr/bin/oslevel ]
then
        # Doug Probst: added -r to display maintenance level.
	release=$(/usr/bin/oslevel -r 2>/dev/null)
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:"
if [ -n "${SCC_LINUX_DISTRO}" ]			# Determined in scc-collect!
then
	echo "${distro_class}:linux distro name:${SCC_LINUX_DISTRO}"
fi
scc_linux_distro "${distro_class}:linux distro"

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.
# There is not much use for a variable checksum of the root-passwd.
# Do not add the checksum of the password to the snapshot when this variable is set to "var".
if [ "${SCC_ROOT_PW:-}" != "var" ]
then
	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/^/fix:general::superuser password:/"
fi

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/${HPUX_ADB_NMBR_FORMAT}" | 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 -n 1)"
	if [ "${memory}" ]
	then
		echo "fix:general::memory real:${memory}"
		echo "fix:general::memory available:$(dmesg | grep '^avail mem =' | tail -n 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
	busybox_check free
	if [ $? -eq 0 ]		# a real program
	then
		echo "fix:general::memory real:$(free -b | awk '/^Mem/ { print $2 / ( 1024 * 1024 ) }') MB"
	else			# the busybox substitue
		echo "fix:general::memory real:$(free | awk '/Mem:/ { print $2 / 1024 }') MB"
	fi
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
