diff --git a/plugins/openvpn/openvpn_multi b/plugins/openvpn/openvpn_multi index ce2b60c3..3bc05918 100755 --- a/plugins/openvpn/openvpn_multi +++ b/plugins/openvpn/openvpn_multi @@ -1,89 +1,178 @@ -#!/bin/sh -# -# Plugin Configuration -# [openvpn_multi] -# user root -# env.statusfile /var/log/openvpn.status -# env.userlist user1 user2 ... -# -# -# No error handling/mapping for ${userlist} => ${statusfile} -# -# Magic markers (optional - used by munin-config and some installation -# scripts): -# -#%# family=auto -#%# capabilities=autoconf suggest +#!/usr/bin/perl -w -if [ "$1" = "autoconf" ]; then - if [ -f ${statusfile} ]; then - echo yes - exit 0 - else - echo "no (${statusfile} not found)" - exit 1 - fi -fi +=head1 NAME -if [ "$1" = "suggest" ]; then - echo "Configure a status file..." - exit 0 -fi +openvpn_multi - Plugin for monitoring OpenVPN users traffic -FieldAttrib () { - echo "${USER}_in.label ${USER} Received" - echo "${USER}_in.type DERIVE" - echo "${USER}_in.min 0" - echo "${USER}_in.graph no" - echo "${USER}_in.cdef ${USER}_in,1,*" - echo "${USER}_out.label ${USER} Bps" - echo "${USER}_out.type DERIVE" - echo "${USER}_out.min 0" - echo "${USER}_out.negative ${USER}_in" - echo "${USER}_out.cdef ${USER}_out,1,*" +=head1 USAGE + +Standard plugin + +=head1 AUTHOR + +Copyright 2013 Pierre Schweitzer + +=head1 LICENSE + +GNU GPL v2 or any later version + +=head1 MAGIC MARKERS + + #%# family=auto + #%# capabilities=autoconf + +=cut + +use strict; +use Munin::Common::Defaults; +use Munin::Plugin; + +my $statusfile = ($ENV{'statusfile'} || '/var/log/openvpn.status'); + +sub config { + open FILE, $statusfile or die $!; + + print "multigraph openvpn_users\n"; + print "graph_title OpenVPN traffic\n"; + print "graph_args --base 1024 --lower-limit 0\n"; + print "graph_vlabel Bytes Out (-) / In (+) per \${graph_period}\n"; + print "graph_category openvpn\n"; + print "in.label recv\n"; + print "in.type DERIVE\n"; + print "in.min 0\n"; + print "in.graph no\n"; + print "in.cdef in,1,*\n"; + print "out.label Bps\n"; + print "out.type DERIVE\n"; + print "out.min 0\n"; + print "out.negative in\n"; + print "out.cdef out,1,*\n"; + + while () { + next if ($_ =~ /CLIENT LIST/ || $_ =~ /Updated/ || $_ =~ /Common Name/); + last if ($_ =~ /ROUTING TABLE/); + + # client,IP:port,in,out,D M N hour Y + my @values = split(',', $_); + my $name = $values[0]; + my $fieldname = clean_fieldname($name); + + print "multigraph openvpn_users.$fieldname\n"; + print "graph_title OpenVPN traffic for $name\n"; + print "graph_args --base 1024 --lower-limit 0\n"; + print "graph_vlabel Bytes Out (-) / In (+) per \${graph_period}\n"; + print "graph_category openvpn\n"; + print "in.label recv\n"; + print "in.type DERIVE\n"; + print "in.min 0\n"; + print "in.graph no\n"; + print "in.cdef in,1,*\n"; + print "out.label Bps\n"; + print "out.type DERIVE\n"; + print "out.min 0\n"; + print "out.negative in\n"; + print "out.cdef out,1,*\n"; + } + close FILE; + + exit 0; } -GetValues () { - if grep -q ^$USER $statusfile; then - awk -F , '/^'"$USER"'/ {print $1"_in.value "$3 "\n"$1"_out.value "$4}' $statusfile - else - echo "${USER}_in.value -1" - echo "${USER}_out.value -1" - fi +sub autoconf { + if (-e $statusfile) { + print "yes\n"; + exit 0; + } else { + print "no\n"; + exit 1; + } } +sub report { + my %in; + my %out; + my $tot_in = 0; + my $tot_out = 0; -# Setup config -if [ "$1" = "config" ]; then - echo "graph_title Root Graph of Openvpn Traffic by CN" - echo "graph_args --base 1024 --lower-limit 0" - echo "graph_vlabel Bytes Out (-) / In (+) per \${graph_period}" - echo "graph_category openvpn" - echo "graph_info This graph shows the bandwidth usage in Bytes/s. It prepulates the graph lines from a list of user CN's in the plugin conf file, this must correlate with the values in the ${statusfile} log file." - for USER in $userlist - do - FieldAttrib - done - for USER in $userlist - do - echo "multigraph openvpn_multi.${USER}" - echo "graph_title ${USER} sub-graph" - echo "graph_category openvpn" - echo "graph_info This graph shows the bandwidth usage in bytes/s. It prepulates the graph lines from a list of user CN's in the plugin conf file, this must correlate with the values in the ${statusfile} log file." - FieldAttrib - done - exit 0 -fi; + my %previous_state = restore_state(); + my %new_state; -# Get .values for root graph -for USER in $userlist -do - GetValues -done + open FILE, $statusfile or die $!; + while () { + next if ($_ =~ /CLIENT LIST/ || $_ =~ /Updated/ || $_ =~ /Common Name/); + last if ($_ =~ /ROUTING TABLE/); -# Get .values for sub-graphs -for USER in $userlist -do - echo "multigraph openvpn_multi.${USER}" - GetValues -done + # client,IP:port,in,out,D M N hour Y + my @values = split(',', $_); + my $name = $values[0]; + + my $in = 0; + my $out = 0; + if (exists $previous_state{$name."_in"} && exists $previous_state{$name."_out"}) { + my $old_in = $previous_state{$name."_in"}; + my $old_out = $previous_state{$name."_out"}; + if ($old_in <= $values[2] && $old_out <= $values[3]) { + $in = $values[2] - $old_in; + $out = $values[3] - $old_out; + } + } + + $in{$name} = $in; + $out{$name} = $out; + $tot_in += $in; + $tot_out += $out; + + $new_state{$name."_in"} = $values[2]; + $new_state{$name."_out"} = $values[3]; + } + close FILE; + + print "multigraph openvpn_users\n"; + if (exists $previous_state{"total.in"} && exists $previous_state{"total.out"}) { + my $old_tot_in = $previous_state{"total.in"}; + my $old_tot_out = $previous_state{"total.out"}; + if ($old_tot_in <= $tot_in && $old_tot_out <= $tot_out) { + print "in.value $tot_in\n"; + print "out.value $tot_out\n"; + } else { + print "in.value 0\n"; + print "out.value 0\n"; + } + } else { + print "in.value 0\n"; + print "out.value 0\n"; + } + + $new_state{"total.in"} = $tot_in; + $new_state{"total.out"} = $tot_out; + + save_state(%new_state); + + for my $name (keys %in) { + my $in = $in{$name}; + my $out = $out{$name}; + my $fieldname = clean_fieldname($name); + + print "multigraph openvpn_users.$fieldname\n"; + print "in.value $in\n"; + print "out.value $out\n"; + } + + exit 0; +} + +if ($ARGV[0]) { + my $arg = $ARGV[0]; + my %funcs = ( + config => \&config, + autoconf => \&autoconf, + ); + + if (exists $funcs{$arg}) { + $funcs{$arg}->(); + } else { + die "Unknown argument '$arg'\n"; + } +} else { + report(); +}