Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / vpn / openvpn_multi @ ae4e85ab

Historique | Voir | Annoter | Télécharger (3,83 ko)

1
#!/usr/bin/perl -w
2

    
3
=head1 NAME
4

    
5
openvpn_multi - Plugin for monitoring OpenVPN users traffic
6

    
7
=head1 CONFIGURATION
8

    
9
  [openvpn_multi]
10
    user root
11
    env.statusfile /var/log/openvpn.status
12

    
13
=head1 AUTHOR
14

    
15
Copyright 2013 Pierre Schweitzer <pierre@reactos.org>
16

    
17
=head1 LICENSE
18

    
19
GNU GPL v2 or any later version
20

    
21
=head1 MAGIC MARKERS
22

    
23
 #%# family=auto
24
 #%# capabilities=autoconf
25

    
26
=cut
27

    
28
use strict;
29
use Munin::Common::Defaults;
30
use Munin::Plugin;
31

    
32
my $statusfile = ($ENV{'statusfile'} || '/var/log/openvpn.status');
33

    
34
sub config {
35
	open FILE, $statusfile or die $!;
36

    
37
	print "multigraph openvpn_users\n";
38
	print "graph_title OpenVPN traffic\n";
39
	print "graph_args --base 1024 --lower-limit 0\n";
40
	print "graph_vlabel Bytes Out (-) / In (+) per \${graph_period}\n";
41
	print "graph_category network\n";
42
	print "in.label recv\n";
43
	print "in.type DERIVE\n";
44
	print "in.min 0\n";
45
	print "in.graph no\n";
46
	print "in.cdef in,1,*\n";
47
	print "out.label Bps\n";
48
	print "out.type DERIVE\n";
49
	print "out.min 0\n";
50
	print "out.negative in\n";
51
	print "out.cdef out,1,*\n";
52

    
53
	while (<FILE>) {
54
		next if ($_ =~ /CLIENT LIST/ || $_ =~ /Updated/ || $_ =~ /Common Name/);
55
		last if ($_ =~ /ROUTING TABLE/);
56

    
57
		# client,IP:port,in,out,D M N hour Y
58
		my @values = split(',', $_);
59
		my $name = $values[0];
60
		my $fieldname = clean_fieldname($name);
61

    
62
		print "multigraph openvpn_users.$fieldname\n";
63
		print "graph_title OpenVPN traffic for $name\n";
64
		print "graph_args --base 1024 --lower-limit 0\n";
65
		print "graph_vlabel Bytes Out (-) / In (+) per \${graph_period}\n";
66
		print "graph_category network\n";
67
		print "in.label recv\n";
68
		print "in.type DERIVE\n";
69
		print "in.min 0\n";
70
		print "in.graph no\n";
71
		print "in.cdef in,1,*\n";
72
		print "out.label Bps\n";
73
		print "out.type DERIVE\n";
74
		print "out.min 0\n";
75
		print "out.negative in\n";
76
		print "out.cdef out,1,*\n";
77
	}
78
	close FILE;
79

    
80
	exit 0;
81
}
82

    
83
sub autoconf {
84
	if (-e $statusfile) {
85
		print "yes\n";
86
		exit 0;
87
	} else {
88
		print "no\n";
89
		exit 1;
90
	}
91
}
92

    
93
sub report {
94
	my %in;
95
	my %out;
96
	my $tot_in = 0;
97
	my $tot_out = 0;
98

    
99
	my %previous_state = restore_state();
100
	my %new_state;
101

    
102
	open FILE, $statusfile or die $!;
103
	while (<FILE>) {
104
		next if ($_ =~ /CLIENT LIST/ || $_ =~ /Updated/ || $_ =~ /Common Name/);
105
		last if ($_ =~ /ROUTING TABLE/);
106

    
107
		# client,IP:port,in,out,D M N hour Y
108
		my @values = split(',', $_);
109
		my $name = $values[0];
110

    
111
		my $in = 0;
112
		my $out = 0;
113
		if (exists $previous_state{$name."_in"} && exists $previous_state{$name."_out"}) {
114
			my $old_in = $previous_state{$name."_in"};
115
			my $old_out = $previous_state{$name."_out"};
116
			if ($old_in <= $values[2] && $old_out <= $values[3]) {
117
				$in = $values[2] - $old_in;
118
				$out = $values[3] - $old_out;
119
			}
120
		}
121

    
122
		$in{$name} = $in;
123
		$out{$name} = $out;
124
		$tot_in += $in;
125
		$tot_out += $out;
126

    
127
		$new_state{$name."_in"} = $values[2];
128
		$new_state{$name."_out"} = $values[3];
129
	}
130
	close FILE;
131

    
132
	print "multigraph openvpn_users\n";
133
	if (exists $previous_state{"total.in"} && exists $previous_state{"total.out"}) {
134
		my $old_tot_in = $previous_state{"total.in"};
135
		my $old_tot_out = $previous_state{"total.out"};
136
		if ($old_tot_in <= $tot_in && $old_tot_out <= $tot_out) {
137
			print "in.value $tot_in\n";
138
			print "out.value $tot_out\n";
139
		} else {
140
			print "in.value 0\n";
141
			print "out.value 0\n";
142
		}
143
	}  else {
144
		print "in.value 0\n";
145
		print "out.value 0\n";
146
	}
147

    
148
	$new_state{"total.in"} = $tot_in;
149
	$new_state{"total.out"} = $tot_out;
150

    
151
	save_state(%new_state);
152

    
153
	for my $name (keys %in) {
154
		my $in = $in{$name};
155
		my $out = $out{$name};
156
		my $fieldname = clean_fieldname($name);
157

    
158
		print "multigraph openvpn_users.$fieldname\n";
159
		print "in.value $in\n";
160
		print "out.value $out\n";
161
	}
162

    
163
	exit 0;
164
}
165

    
166
if ($ARGV[0]) {
167
	my $arg = $ARGV[0];
168
	my %funcs = (
169
		config   => \&config,
170
		autoconf => \&autoconf,
171
	);
172

    
173
	if (exists $funcs{$arg}) {
174
		$funcs{$arg}->();
175
	} else {
176
		die "Unknown argument '$arg'\n";
177
	}
178
} else {
179
	report();
180
}