#!/bin/sh
# System statistics for FreeBSD
# Author: Gergely Czuczy <phoemix@harmless.hu>
#
# Define the environment variable "logarithmic" in plugins.conf to
# make graphs with logarithmic scale. Default is linear scale.
#
#%# family=auto
#%# capabilities=autoconf

sysctl='/usr/bin/vmstat'

if [ "$logarithmic" ]; then
    graph_args="--logarithmic --units=si"
else
    graph_args="--lower-limit 0"
fi

case $1 in
    config)
	cat <<EOF
graph_title System Statistics
graph_vlabel per second
graph_scale no
graph_category system
graph_args $graph_args
graph_info FreeBSD systat plugin
softint.label Software interrupts
softint.type DERIVE
softint.min 0
hardint.label Hardware interrupts
hardint.type DERIVE
hardint.min 0
syscall.label System calls
syscall.type DERIVE
syscall.min 0
cs.label Context switches
cs.type DERIVE
cs.min 0
forks.label Fork rate
forks.type DERIVE
forks.min 0
EOF
	exit 0
	;;
    autoconf)
	if [ ! -x ${sysctl} ]; then
	    echo "no (${sysctl} is not executable)"
	    exit 0
	fi
	ostype=`uname -s`
	if [ ${ostype} = "OpenBSD" ]; then
	    echo "yes"
	    exit 0
	fi
	echo "no (Your OS is not supported by this plugin)"
	exit 0
	;;
    suggest)
	exit 0
	;;
esac

vmstat -s | awk '
BEGIN {forks=0;}
/^[ 0-9]*software interrupts/{print "softint.value",$1}
/^[ 0-9]*interrupts/{print "hardint.value",$1}
/^[ 0-9]*syscalls/{print "syscall.value",$1}
/^[ 0-9]*cpu context switches/{print "cs.value",$1}
/^[ 0-9]*forks/ {forks+=$1}
END {print "forks.value",forks;}
'
