#!/bin/sh
#
# Plugin to measure uptime. Especially the average and max values on the
# bigger graphs (yearly) can be interesting.
#
# Magic markers - optional - used by installation scripts and
# munin-config:
#
#%# family=auto
#%# capabilities=autoconf

if [ "$1" = "autoconf" ]; then
    if [ -x /sbin/sysctl ]; then
        if /sbin/sysctl kern.boottime >/dev/null 2>&1; then
            if [ -x /bin/date ]; then
                echo yes
                exit 0
            else
                echo no '(no /bin/date)'
                exit 0
            fi
        else
            echo no '(sysctl kern.boottime failed)' 2>&1
            exit 0
        fi
    else
        echo no '(no /sbin/sysctl)'
        exit 0
    fi
fi


if [ "$1" = "config" ]; then
        echo 'graph_title Uptime'
        echo 'graph_args --base 1000 -l 0 '
        echo 'graph_scale no'
        echo 'graph_category system'
        echo 'graph_vlabel uptime in days'
        echo 'uptime.label uptime'
        echo 'uptime.draw AREA'
        exit 0
fi


boottime=`/sbin/sysctl -n kern.boottime`
now=`/bin/date +%s`

awk -v "boottime=$boottime" -v "now=$now" </dev/null '
END {
    printf "uptime.value %.2f\n", (now - boottime) / 86400;
}'
