#!/usr/bin/sh
# -*- sh -*-

: << =cut

=head1 NAME

co2mon - Plugin to monitor MT8057 (RAD-0301) CO2 ppm and temperature values.

=head1 NOTES


=head1 CONFIGURATION

The following environment variables are used by this plugin:

 co2_warning  - Warning CO2 ppm (Default: 800)
 co2_critical - Critical CO2 ppm (Default: 1200)
 temp_warning  - Warning temp (Default: 30)
 temp_critical - Critical temp (Default: 35)

=head1 MAGIC MARKERS:

 #%# family=auto
 #%# capabilities=autoconf

=cut

. $MUNIN_LIBDIR/plugins/plugin.sh
is_multigraph

DATADIR='/var/lib/co2mon'
CO2_FILE="$DATADIR/CntR"
TEMP_FILE="$DATADIR/Tamb"
HEARTBEAT_FILE="$DATADIR/heartbeat"

print_values() {
    read co2 < "$CO2_FILE"
    read temp < "$TEMP_FILE"
    read heartbeat_sec < "$HEARTBEAT_FILE"

    echo 'multigraph co2mon'
    echo "co2.value ${heartbeat_sec:+$heartbeat_sec:}${co2:-U}"
    echo 'multigraph co2mon_temp'
    echo "temp.value ${heartbeat_sec:+$heartbeat_sec:}${temp:-U}"
}

if [ "$1" = "autoconf" ]; then
    if [ -s "$CO2_FILE" -o -s "$TEMP_FILE" ] ; then
	echo yes
    else
	echo no
    fi
    exit 0
fi

if [ "$1" = "config" ]; then
    echo 'multigraph co2mon'
    echo "graph_title CO2 ppm"
#    echo 'graph_args --upper-limit 3000 -l 0'
    echo 'graph_vlabel ppm'
#    echo 'graph_scale no'
    echo 'graph_category sensors'
    echo "co2.label CO2 ppm"
    co2_warning=${co2_warning:-800}
    co2_critical=${co2_critical:-1200}
    print_thresholds 'co2'

    echo 'multigraph co2mon_temp'
    echo "graph_title Temperature"
#    echo 'graph_args --upper-limit 100 -l -10'
    echo 'graph_vlabel Degreess Celsius'
#    echo 'graph_scale no'
    echo 'graph_category sensors'
    echo "temp.label temp"
    temp_warning=${temp_warning:-30}
    temp_critical=${temp_critical:-35}
    print_thresholds 'temp'

    exit 0
fi

print_values
