#!/usr/bin/perl
# Converter script for adding existing OpenVPN N2N connections to collectd
# Used for core update 89

my %ovpnconfig=();

require '/var/ipfire/general-functions.pl';

open(COLLECTDVPN, ">${General::swroot}/ovpn/collectd.vpn") or die "Unable to open collectd.vpn: $!";
print COLLECTDVPN "Loadplugin openvpn\n";
print COLLECTDVPN "\n";
print COLLECTDVPN "<Plugin openvpn>\n";
print COLLECTDVPN "Statusfile \"/var/run/ovpnserver.log\"\n";

&General::readhasharray("/var/ipfire/ovpn/ovpnconfig", \%ovpnconfig);
foreach my $key (keys %ovpnconfig) {
	if ($ovpnconfig{$key}[0] eq 'on' && $ovpnconfig{$key}[3] eq 'net') {
		print COLLECTDVPN "Statusfile \"/var/run/openvpn/$ovpnconfig{$key}[1]-n2n\"\n";
	}
}

print COLLECTDVPN "</Plugin>\n";
close(COLLECTDVPN);

# Reload collectd afterwards
system("/usr/local/bin/collectdctrl restart &>/dev/null");
