Debian Codegraph @

src/apps/openvpn/omi/mode/serverusage.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and application monitoring for
6 # service performance.
7 #
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 # http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 #
20
21 package apps::openvpn::omi::mode::serverusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Digest::MD5 qw(md5_hex);
28
29 sub set_counters {
30 my ($self, %options) = @_;
31
32 $self->{maps_counters_type} = [
33 { name => 'global', type => 0 }
34 ];
35
36 $self->{maps_counters}->{global} = [
37 { label => 'num-clients', nlabel => 'clients.current.count',set => {
38 key_values => [ { name => 'num_clients' } ],
39 output_template => 'Current Clients: %s',
40 perfdatas => [
41 { label => 'num_clients', template => '%s', min => 0 }
42 ]
43 }
44 },
45 { label => 'traffic-in', nlabel => 'server.traffic.in.bitspersecond',set => {
46 key_values => [ { name => 'traffic_in', per_second => 1 } ],
47 output_change_bytes => 2,
48 output_template => 'Traffic In: %s %s/s',
49 perfdatas => [
50 { label => 'traffic_in', template => '%.2f', min => 0, unit => 'b/s' }
51 ]
52 }
53 },
54 { label => 'traffic-out', nlabel => 'server.traffic.out.bitspersecond', set => {
55 key_values => [ { name => 'traffic_out', per_second => 1 } ],
56 output_change_bytes => 2,
57 output_template => 'Traffic Out: %s %s/s',
58 perfdatas => [
59 { label => 'traffic_out', template => '%.2f', min => 0, unit => 'b/s' }
60 ]
61 }
62 }
63 ];
64 }
65
66 sub new {
67 my ($class, %options) = @_;
68 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
69 bless $self, $class;
70
71 $options{options}->add_options(arguments => {
72 });
73
74 return $self;
75 }
76
77 sub manage_selection {
78 my ($self, %options) = @_;
79
80 my $result = $options{custom}->command(cmd => 'load-stats');
81 # SUCCESS: nclients=6,bytesin=7765329961,bytesout=18435500727
82
83 $self->{global} = { num_clients => 0, traffic_in => 0, traffic_out => 0 };
84 if ($result =~ /nclients=(\d+),bytesin=(\d+),bytesout=(\d+)/) {
85 $self->{global} = { num_clients => $1, traffic_in => $2 * 8, traffic_out => $3 * 8 };
86 }
87
88 #status
89 #OpenVPN CLIENT LIST
90 #Updated,Thu Jan 10 16:05:32 2019
91 #Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
92 #plop1,xxxx.xxx.xxx.xxxx:56702,104525967,29529209,Thu Jan 10 08:53:46 2019
93 #plop2,xxx.xxxx.xxx.xxx:39374,1814536,8630412,Thu Jan 10 15:27:13 2019
94 #plop3,xxx.xxxx.xxxx.xxxx:62866,8208936,85352252,Thu Jan 10 08:14:49 2019
95 #ROUTING TABLE
96 #Virtual Address,Common Name,Real Address,Last Ref
97 #10.8.1.xxx,plop1,xxx.xx.xx.xxx:53725,Thu Jan 10 16:05:31 2019
98 #...
99 #GLOBAL STATS
100 #Max bcast/mcast queue length,9
101 #END
102 if ($self->{output}->is_verbose()) {
103 $result = $options{custom}->command(cmd => 'status');
104 if ($result =~ /OpenVPN CLIENT LIST\n(.*?)ROUTING TABLE/ms) {
105 my @users = split /\n/, $1;
106 splice @users, 0, 2;
107 foreach (@users) {
108 my ($user) = split /,/;
109 $self->{output}->add_option_msg(long_msg => "user '$user' connected");
110 }
111 }
112 }
113
114 $self->{cache_name} = 'openvpn_' . $self->{mode} . '_' . $options{custom}->get_connect_info() . '_' .
115 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')) . '_' .
116 (defined($self->{option_results}->{filter_name}) ? md5_hex($self->{option_results}->{filter_name}) : md5_hex('all'));
117 }
118
119 1;
120
121 __END__
122
123 =head1 MODE
124
125 Check server usage.
126
127 =over 8
128
129 =item B<--warning-*> B<--critical-*>
130
131 Thresholds.
132 Can be: 'num-clients', 'traffic-in', 'traffic-out'.
133
134 =back
135
136 =cut