#!/bin/sh

# Set default values
APPDIR=${APPDIR:-..}
VERBOSE=${VERBOSE:-0}

. ${APPDIR}/common.subr

sect "Storage information"

if check_privs /var/run/dmesg.boot; then
	controllers=""
	subsect "Available hard drives:"
	for i in `sysctl -n kern.disks`; do
		grep "^$i:" /var/run/dmesg.boot | grep -iv "attempt\|failure"
		controllers="$controllers `grep "^$i" /var/run/dmesg.boot | grep -owE "(on|at).*" | awk '{print $2}' | grep -v ata`"
	done

	if [ -n "$controllers" ]; then
		echo
		subsect "Raid controllers:"

		# We only want to display each controller once
		controllers=`echo $controllers | tr " " "\n" | uniq`
		for c in $controllers
		do
			subsubsect "$c:"
			getpciconf $c "vendor device"
		done
	fi
fi

subsect "\nCurrently mounted filesystems:"
if ! is_verbose 1; then
	mount | awk '{print $1,$2,$3}'
else
	mount
fi

echo

if check_privs /etc/fstab; then
	# Compare mounted filesystems with those in /etc/fstab
	# Warn user when there are missing entries (ignore devfs
	# entries as these are almost always false positives due
	# to jail setups.
	# 
	# Note: Systems running ezjail use /etc/fstab.* to separate
	#	jail-specific file systems from system-wide ones
	#	Check those too.
	#
	# XXX: devfs mounts are mostly done by scripts, ignore them.
	mount | grep -vw "^devfs" | while read line
	do
		fs=`echo $line | awk '{print $1}'`
		mp=`echo $line | awk '{print $3}'`

		if ! `grep $fs /etc/fstab* | grep -q $mp`
		then
			warn "Not found in fstab(5): $fs on $mp"
		fi
	done
	echo
fi

# Need to load system configuration to check this
getsysconf
if checkyesno nfs_server_enable; then
	subsect "NFS-exported file systems:"
	showmount -e
fi

if dump -W | grep -q Level; then
	subsect "\nDump status:"
	dump -W
fi

subsect "\nI/O statistics:"
iostat
info "Run iostat(8) or gstat(8) to see live statistics."

subsect "\nDisk usage:"
df -h

if which -s gmirror; then
if gmirror status >/dev/null 2>&1; then
	subsect "\nGmirror information"
	if is_verbose 1; then
		gmirror list | sed -En '/Geom name|Name:|State|Components|Balance/p'
	else
		gmirror status
	fi
	info "Check the gmirror(8) manual page for more information."
fi
fi

if which -s geli; then
if geli status >/dev/null 2>&1; then
	subsect "\nGeli information"
	if is_verbose 1; then
		geli list | sed -En '/Geom name|Encryption|KeyLength|Crypto/p'
	else
		geli status
	fi
fi
fi

if which -s gjournal; then
if gjournal status >/dev/null 2>&1; then
	subsect "\nGeom journal status"
	gjournal status
	info "Check the gjournal(8) manual page for more information."
fi
fi

# XXX: This probably needs a bit of extending
if which -s graid3; then
if graid3 status >/dev/null 2>&1; then
	subsect "\nGeom RAID3 status"
	graid3 status
	info "Check the graid3(8) manual page for more information."
fi
fi

# XXX: This probably needs a bit of extending
if which -s gstripe; then
if gstripe status >/dev/null 2>&1; then
	subsect "\nGeom Stripe status"
	gstripe status
	info "Check the gstripe(8) manual page for more information."
fi
fi

# XXX: This probably needs a bit of extending
if which -s gconcat; then
if gconcat status >/dev/null 2>&1; then
	subsect "\nGeom Concat status"
	gconcat status
	info "Check the gconcat(8) manual page for more information."
fi
fi

exit 0
