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

set -e

: << =cut

=head1 NAME

resolvectl - Plugin to monitor resolvectl statistics.

=head1 APPLICABLE SYSTEMS

Any server running systemd-resolved

=head1 CONFIGURATION

[resolvectl]
user root

=head1 NOTES

Uses the command "resolvectl statistics"

=head1 AUTHOR

Scott Hollenbeck <sah6284@gmail.com>

=head1 LICENSE

GPLv2

=head1 VERSION

1.0

=head1 MAGIC MARKERS

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

=cut

. "$MUNIN_LIBDIR/plugins/plugin.sh"

if [ "$1" = "autoconf" ]; then
    resolvectl statistics >/dev/null 2>&1
    ret=$?
    if [ "$ret" -eq 0 ]; then
        echo yes
        exit 0
    else
        if [ "$ret" -eq 1 ]; then
            echo "no (could not run \"resolvectl\")"
            exit 0
        else
            echo "no (unknown resolvectl return value "$ret")"
            exit 0
        fi
    fi
fi

if [ "$1" = "config" ]; then

    echo 'graph_title resolvectl statistics'
    echo 'graph_args --base 1000 -l 0'
    echo 'graph_vlabel count'
    echo 'graph_category dns'
    echo 'current-transactions.label current transactions'
    echo 'current-transactions.type DERIVE'
    echo 'current-transactions.min 0'
    echo 'total-transactions.label total transactions'
    echo 'total-transactions.type DERIVE'
    echo 'total-transactions.min 0'
    echo 'cache-size.label cache size'
    echo 'cache-size.type DERIVE'
    echo 'cache-size.min 0'
    echo 'cache-hits.label cache hits'
    echo 'cache-hits.type DERIVE'
    echo 'cache-hits.min 0'
    echo 'cache-misses.label cache misses'
    echo 'cache-misses.type DERIVE'
    echo 'cache-misses.min 0'
    echo 'total-timeouts.label total timeouts'
    echo 'total-timeouts.type DERIVE'
    echo 'total-timeouts.min 0'
    echo 'total-timeouts-stale.label total timeouts stale'
    echo 'total-timeouts-stale.type DERIVE'
    echo 'total-timeouts-stale.min 0'
    echo 'total-failures.label total failures'
    echo 'total-failures.type DERIVE'
    echo 'total-failures.min 0'
    echo 'total-failures-stale.label total failures stale'
    echo 'total-failures-stale.type DERIVE'
    echo 'total-failures-stale.min 0'
    echo 'dnssec-secure.label dnssec secure'
    echo 'dnssec-secure.type DERIVE'
    echo 'dnssec-secure.min 0'
    echo 'dnssec-insecure.label dnssec insecure'
    echo 'dnssec-insecure.type DERIVE'
    echo 'dnssec-insecure.min 0'
    echo 'dnssec-bogus.label dnssec bogus'
    echo 'dnssec-bogus.type DERIVE'
    echo 'dnssec-bogus.min 0'
    echo 'dnssec-indeterminate.label dnssec indeterminate'
    echo 'dnssec-indeterminate.type DERIVE'
    echo 'dnssec-indeterminate.min 0'
    exit 0
fi

resolvectl statistics | awk '/Current Transactions/{print "current-transactions.value " $NF};\
/Total Transactions/{print "total-transactions.value " $NF};\
/Current Cache Size/{print "cache-size.value " $NF};\
/Cache Hits/{print "cache-hits.value " $NF};\
/Cache Misses/{print "cache-misses.value " $NF};\
/Total Timeouts:/{print "total-timeouts.value " $NF};\
/Total Timeouts \(Stale Data Served/{print "total-timeouts-stale.value " $NF};\
/Total Failure Responses:/{print "total-failures.value " $NF};\
/Total Failure Responses \(Stale Data Served/{print "total-failures-stale.value " $NF};\
/Secure/{print "dnssec-secure.value " $NF};\
/Insecure/{print "dnssec-insecure.value " $NF};\
/Bogus/{print "dnssec-bogus.value " $NF};\
/Indeterminate/{print "dnssec-indeterminate.value " $NF};'
