#!/bin/bash
# -*- bash -*-

: << =cut

=head1 NAME

openvpn_as_traffic -OpenVPN traffic

=head1 CONFIGURATION

Require jsonpipe software:
https://github.com/dvxhouse/jsonpipe

[openvpn_as*]
  user root

=head1 AUTHOR

Ricardo Fraile <rfrail3@yahoo.es>

=head1 LICENSE

GPLv2

=head1 MAGICK MARKERS

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

=cut

. $MUNIN_LIBDIR/plugins/plugin.sh


# Script folder
D_BIN="/usr/local/openvpn_as/scripts"
SUM1=0
SUM2=0
NOW=`date +%s`

if [ "$1" = "autoconf" ]; then
	echo yes 
	exit 0
fi

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

	echo "graph_title OpenVPN traffic"
	echo 'graph_args --base 1000'
	echo 'graph_vlabel Bytes in (-) / out (+)'
	echo 'graph_category OpenVPN'
	echo "graph_info OpenVPN traffic"


	echo "down.label Bytes"
	echo "down.type DERIVE"
	echo "down.graph no"
	echo "down.cdef down,8,*"
       	echo "down.min 0"

	echo "up.label Bytes"
      	echo "up.type DERIVE"
        echo "up.negative down"
	echo "up.cdef up,8,*"
        echo "up.info Bytes"
	echo "up.min 0"
	exit 0
fi

# List users values, bytes sent
LIST1=`$D_BIN/sacli VPNStatus | tr '/' - |  jsonpipe  | grep "/openvpn_[0-9]/client_list/[0-9]/4" | tr -d '"'  | awk '{print $2} '`

# Sum list
for i in $LIST1; do
        SUM1=`expr $SUM1 + $i`
done

# List users values, bytes received
LIST2=`$D_BIN/sacli VPNStatus | tr '/' - |  jsonpipe  | grep "/openvpn_[0-9]/client_list/[0-9]/3" | tr -d '"'  | awk '{print $2} '`

# Sum list
for i in $LIST2; do
        SUM2=`expr $SUM2 + $i`
done


echo "down.value $SUM1"
echo "up.value $SUM2"
