#!/usr/local/bin/cbsd
#v10.1.0
MYARG=""
MYOPTARG="human"
MYDESC="Show summary info/statistics page for the CBSD"
CBSDMODULE="sys"
ADDHELP="
${H3_COLOR}Description${N0_COLOR}:

Shows a general summary of the current platform and cluster

${H3_COLOR}Options${N0_COLOR}:

 ${N2_COLOR}human=${N0_COLOR}  - disable human friendly mode in bytes stats;

${H3_COLOR}Examples${N0_COLOR}:

 # cbsd summary

${H3_COLOR}See also${N0_COLOR}:

 cbsd capabilities --help

"

. ${subrdir}/nc.subr
human=1
. ${cbsdinit}

. ${system}
. ${nodes}

# incement global variable jaillist_sum, vmlist_sum
# $1 - database name
inc_jaillist_sum()
{
	local tmp_jaillist_sum=0
	local tmp_vmlist_sum=0
	local db="${1}"

	[ -z "${db}" ] && return 0

	tmp_jaillist_sum=$( cbsdsqlro ${db} "SELECT count(jname) FROM jails WHERE emulator = 'jail'" )
	tmp_vmlist_sum=$( cbsdsqlro ${db} "SELECT count(jname) FROM jails WHERE emulator != 'jail'" )

	if ! is_number ${tmp_jaillist_sum}; then
		jaillist_sum=$(( jaillist_sum + tmp_jaillist_sum ))
	fi

	if ! is_number ${tmp_vmllist_sum}; then
		vmlist_sum=$(( vmlist_sum + tmp_vmlist_sum ))
	fi
}

#inc cpu_sum, physmem_sum_bytes, freemem_sum_bytes, cpufreq_sum
#
inc_hwstats_sum()
{
	local tmp_ncpu=0
	local tmp_physmem=0
	local tmp_freemem=0
	local tmp_cpufreq=0
	local db="${1}"

	[ -z "${db}" ] && return 0

	local sqldelimer=" "

	eval $( cbsdsqlro ${db} "SELECT ncpu,physmem,freemem,cpufreq FROM local" | while read _ncpu _physmem _freemem _cpufreq; do
		echo "local tmp_ncpu=\"${_ncpu}\""
		echo "local tmp_physmem=\"${_physmem}\""
		echo "local tmp_freemem=\"${_freemem}\""
		echo "local tmp_cpufreq=\"${_cpufreq}\""
	done )

	if ! is_number ${tmp_ncpu}; then
		cpu_sum=$(( cpu_sum + tmp_ncpu ))
	fi

	if ! is_number ${tmp_physmem}; then
		physmem_sum_bytes=$(( physmem_sum_bytes + tmp_physmem ))
	fi

	if ! is_number ${tmp_freemem}; then
		freemem_sum_bytes=$(( freemem_sum_bytes + tmp_freemem ))
	fi

	if ! is_number ${tmp_cpufreq}; then
		cpufreq_sum=$(( cpufreq_sum + tmp_cpufreq ))
	fi
	[ "${tmp_cpufreq}" != "0" ] && cpu_with_freq=$(( cpu_with_freq + 1 ))
}

## MAIN
nodelist_sum=1
nodelist_offline=0
jaillist_sum=0
cpu_sum=0
physmem_sum_bytes=0
freemem_sum_bytes=0
cpusum_without_freq=0
cpufreq_sum=0

nodelist=$( cbsdsqlro nodes "SELECT nodename FROM nodelist" )

# calculate offline nodes
for i in ${nodelist}; do
	node_ip=$( cbsdsqlro nodes "SELECT ip FROM nodelist WHERE nodename='${i}'" )
	[ -z "${node_ip}" ] && continue
	if ! check_locktime ${ftmpdir}/shmux_${node_ip}.lock >/dev/null 2>&1; then
		nodelist_offline=$(( nodelist_offline + 1 ))
	fi
done

inc_jaillist_sum "local"
inc_hwstats_sum "local"

for db in ${nodelist}; do
	nodelist_sum=$(( nodelist_sum + 1 ))
	inc_jaillist_sum ${db}
	inc_hwstats_sum ${db}
done

if [ -n "${cpufreq_sum}" -a -n "${cpu_with_freq}" ]; then
	cpufreq_average=$(( cpufreq_sum / cpu_with_freq ))
fi

if [ ${human} -eq 1 ]; then
	if conv2human "${physmem_sum_bytes}"; then
		physmem_sum_sum=${convval}
	else
		physmem_sum_sum="${physmem_sum_bytes}"
	fi
else
	physmem_sum_sum="${physmem_sum_bytes}"
fi

if [ ${human} -eq 1 ]; then
	if conv2human "${freemem_sum_bytes}"; then
		freemem_sum_sum=${convval}
	else
		freemem_sum_sum="${freemem_sum_bytes}"
	fi
else
	freemem_sum_sum="${freemem_sum_bytes}"
fi

# show info
${ECHO} "${BOLD}  ${H5_COLOR}Summary statistics for the CBSD farm  ${N0_COLOR}"
${ECHO} "${BOLD}  ${H1_COLOR}====================================  ${N0_COLOR}"
echo

spaces=""
max_param_len=24

param_name="CBSD version"
delimer_len=$(( max_param_len - $( strlen "${param_name}" ) ))
printf "  ${N1_COLOR}${param_name}: %-${delimer_len}s ${N2_COLOR}%-3s${N0_COLOR}\n" "${spaces}" ${myversion}

param_name="current platform"
delimer_len=$(( max_param_len - $( strlen "${param_name}" ) ))
printf "  ${N1_COLOR}${param_name}: %-${delimer_len}s ${N2_COLOR}%-16s${N0_COLOR}\n" "${spaces}" ${platform}

param_name="total nodes"
delimer_len=$(( max_param_len - $( strlen "${param_name}" ) ))
if [ ${nodelist_offline} -ne 0 ]; then
	printf "  ${N1_COLOR}${param_name}: %-${delimer_len}s ${N2_COLOR}%-3s${N0_COLOR}(${W2_COLOR}%-2s${N1_COLOR} Offline${N0_COLOR})\n" "${spaces}" ${nodelist_sum} ${nodelist_offline}
else
	printf "  ${N1_COLOR}${param_name}: %-${delimer_len}s ${N2_COLOR}%-3s${N0_COLOR}\n" "${spaces}" ${nodelist_sum}
fi

param_name="total jail environments"
delimer_len=$(( max_param_len - $( strlen "${param_name}" ) ))
printf "  ${N1_COLOR}${param_name}: %-${delimer_len}s ${N2_COLOR}%-16s${N0_COLOR}\n" "${spaces}" ${jaillist_sum}

param_name="total virtual machines"
delimer_len=$(( max_param_len - $( strlen "${param_name}" ) ))
printf "  ${N1_COLOR}${param_name}: %-${delimer_len}s ${N2_COLOR}%-16s${N0_COLOR}\n" "${spaces}" ${vmlist_sum}

param_name="total CPU core's"
delimer_len=$(( max_param_len - $( strlen "${param_name}" ) ))
printf "  ${N1_COLOR}${param_name}: %-${delimer_len}s ${N2_COLOR}%-16s${N0_COLOR}\n" "${spaces}" ${cpu_sum}

if [ -n "${cpufreq_average}" -a "${cpufreq_average}" != "0 " ]; then
	param_name="average frequency, Mhz"
	delimer_len=$(( max_param_len - $( strlen "${param_name}" ) ))
	printf "  ${N1_COLOR}${param_name}: %-${delimer_len}s ${N2_COLOR}%-16s${N0_COLOR}\n" "${spaces}" ${cpufreq_average}
fi

param_name="total RAM"
delimer_len=$(( max_param_len - $( strlen "${param_name}" ) ))
printf "  ${N1_COLOR}${param_name}: %-${delimer_len}s ${N2_COLOR}%-16s${N0_COLOR}\n" "${spaces}" ${physmem_sum_sum}

param_name="total free RAM"
delimer_len=$(( max_param_len - $( strlen "${param_name}" ) ))
printf "  ${N1_COLOR}${param_name}: %-${delimer_len}s ${N2_COLOR}%-16s${N0_COLOR}\n" "${spaces}" ${freemem_sum_sum}

param_name="total storage, Gb"
delimer_len=$(( max_param_len - $( strlen "${param_name}" ) ))
printf "  ${N1_COLOR}${param_name}: %-${delimer_len}s ${N2_COLOR}%-16s${N0_COLOR}\n" "${spaces}" "unknown"

echo
# show info
${ECHO} "${BOLD}  ${H5_COLOR}General node info  ${N0_COLOR}"
${ECHO} "${BOLD}  ${H1_COLOR}====================================  ${N0_COLOR}"
echo

eval $( capabilities )

for i in bhyve jail qemu xen virtualbox; do
	eval bool_status="\$emulator_${i}_available"
	eval info="\$emulator_${i}_info"
	case "${bool_status}" in
		1)
			status="available"
			status_color="${N2_COLOR}"
			;;
		*)
			status="unavailable"
			status_color="${N4_COLOR}"
			;;
	esac
	printf "  ${status_color}%-11s: %-11s${N0_COLOR} ( %-53s )${N0_COLOR}\n" "${i}" ${status} ${info}
done
echo
exit 0
