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

: << =cut

=head1 NAME

nfsd - Plugin to monitor NFSv3 server activity on FreeBSD

=head1 CONFIGURATION

No configuration

=head1 AUTHORS

Plugin created by Adam Michel, based on work by Alexandre Dupouy, with the assistance of Mike Fedyk

=head1 LICENSE

GPLv2

=head1 MAGIC MARKERS

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

=cut

# This is where nfsstat lives
NFSSTAT=/usr/bin/nfsstat
if [ "$1" = "autoconf" ]; then
    if [ -x "$NFSSTAT" ]; then
        echo yes
        exit 0
    else
        echo "no (no $NFSSTAT)"
        exit 0
    fi
fi

labels=$("$NFSSTAT" -s | grep -iv "[0-9]" | grep -v ":" | sed 's/Server\ //' | tr '\n' ' ' | awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30}' | tr '[:upper:]' '[:lower:]')
values=$("$NFSSTAT" -s | grep -i "[0-9]" | tr '\n' ' ' | awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,$27,$28,$29,$30}')

read -r -a larray <<<"$labels"
read -r -a varray <<<"$values"

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

    echo 'graph_title NFSv3 Server'
    echo 'graph_args --base 1000 -l 0'
    echo "graph_vlabel requests / \${graph_period}"
    echo 'graph_total total'
    echo 'graph_category fs'
    for a in $labels; do echo "$a.label $a" ; echo "$a.type DERIVE"; echo "$a.min 0"; done
    exit 0
fi

for i in {0..29}; do
    label=${larray[$i]}
    value=${varray[$i]}
    echo "$label.value $value"
done
