#!/bin/sh

#set -x

etc=/usr/local/etc/ganglia_stats.conf

ganglia_stats_sleep_interval=30
ganglia_stats_runfile=/var/run/ganlia_stats.run

if [ -f ${etc} ]; then
	. ${etc}
fi

touch ${ganglia_stats_runfile}

while [ -f ${ganglia_stats_runfile} ]; do


#init
metrics=""

# metrics
metrics="${metrics} MB_Temp"
MB_Temp_value="$(mbmon -T 3 -c 1)"
MB_Temp_type="float"
MB_Temp_unit="Celsius"

metrics="${metrics} logins"
logins_value="$(who -q | grep users | cut -d ' ' -f 4)"
logins_type="uint8"
logins_unit="Users"

metrics="${metrics} SwapIn"
SwapIn_value="$(sysctl vm.stats.vm.v_swapin | cut -d ' ' -f 2)"
SwapIn_type="uint32"
SwapIn_unit="Units"

metrics="${metrics} SwapOut"
SwapOut_value="$(sysctl vm.stats.vm.v_swapout | cut -d ' ' -f 2)"
SwapOut_type="uint32"
SwapOut_unit="Units"

metrics="${metrics} SwapPgsIn"
SwapPgsIn_value="$(sysctl vm.stats.vm.v_swappgsin | cut -d ' ' -f 2)"
SwapPgsIn_type="uint32"
SwapPgsIn_unit="Pages"

metrics="${metrics} SwapPgsOut"
SwapPgsOut_value="$(sysctl vm.stats.vm.v_swappgsout | cut -d ' ' -f 2)"
SwapPgsOut_type="uint32"
SwapPgsOut_unit="Pages"

metrics="${metrics} VMintrans"
VMintrans_value="$(sysctl vm.stats.vm.v_intrans | cut -d ' ' -f 2)"
VMintrans_type="uint32"
VMintrans_unit="Units"

metrics="${metrics} OpenFiles"
OpenFiles_value="$(sysctl kern.openfiles | cut -d ' ' -f 2)"
OpenFiles_type="uint32"
OpenFiles_unit="Units"

metrics="${metrics} hidirtybuffers"
hidirtybuffers_value="$(sysctl vfs.hidirtybuffers | cut -d ' ' -f 2)"
hidirtybuffers_type="uint32"
hidirtybuffers_unit="Units"

metrics="${metrics} lodirtybuffers"
lodirtybuffers_value="$(sysctl vfs.lodirtybuffers | cut -d ' ' -f 2)"
lodirtybuffers_type="uint32"
lodirtybuffers_unit="Units"

metrics="${metrics} numdirtybuffers"
numdirtybuffers_value="$(sysctl vfs.numdirtybuffers | cut -d ' ' -f 2)"
numdirtybuffers_type="uint32"
numdirtybuffers_unit="Units"

metrics="${metrics} hifreebuffers"
hifreebuffers_value="$(sysctl vfs.hifreebuffers | cut -d ' ' -f 2)"
hifreebuffers_type="uint32"
hifreebuffers_unit="Units"

metrics="${metrics} lofreebuffers"
lofreebuffers_value="$(sysctl vfs.lofreebuffers | cut -d ' ' -f 2)"
lofreebuffers_type="uint32"
lofreebuffers_unit="Units"

metrics="${metrics} numfreebuffers"
numfreebuffers_value="$(sysctl vfs.numfreebuffers | cut -d ' ' -f 2)"
numfreebuffers_type="uint32"
numfreebuffers_unit="Units"

metrics="${metrics} hirunningspace"
hirunningspace_value="$(sysctl vfs.hirunningspace | cut -d ' ' -f 2)"
hirunningspace_type="uint32"
hirunningspace_unit="Units"

metrics="${metrics} lorunningspace"
lorunningspace_value="$(sysctl vfs.lorunningspace | cut -d ' ' -f 2)"
lorunningspace_type="uint32"
lorunningspace_unit="Units"

metrics="${metrics} runningbufspace"
runningbufspace_value="$(sysctl vfs.runningbufspace | cut -d ' ' -f 2)"
runningbufspace_type="uint32"
runningbufspace_unit="Units"

metrics="${metrics} maxbufspace"
maxbufspace_value="$(sysctl vfs.maxbufspace | cut -d ' ' -f 2)"
maxbufspace_type="uint32"
maxbufspace_unit="Units"

metrics="${metrics} hibufspace"
hibufspace_value="$(sysctl vfs.hibufspace | cut -d ' ' -f 2)"
hibufspace_type="uint32"
hibufspace_unit="Units"

metrics="${metrics} lobufspace"
lobufspace_value="$(sysctl vfs.lobufspace | cut -d ' ' -f 2)"
lobufspace_type="uint32"
lobufspace_unit="Units"

metrics="${metrics} bufspace"
bufspace_value="$(sysctl vfs.bufspace | cut -d ' ' -f 2)"
bufspace_type="uint32"
bufspace_unit="Units"


# add the metrics
for metric in ${metrics}; do
	eval value=\${${metric}_value}
	eval type=\${${metric}_type}
	eval unit=\${${metric}_unit}
	gmetric --name ${metric} --value ${value} \
		--type ${type} --units ${unit}
done

sleep ${ganglia_stats_sleep_interval}
done

exit 0


kern.disks -> ???

vm.zone

wicontrol -i wi0 -o
wicontrol -i wi0 -C
netstat -ibdn
netstat -ss   ? -z
netstat -m
nfsstat
