Debian Codegraph @

src/network/stonesoft/snmp/mode/rejectedpackets.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 network::stonesoft::snmp::mode::rejectedpackets;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::statefile;
28
29 sub new {
30 my ($class, %options) = @_;
31 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
32 bless $self, $class;
33
34 $options{options}->add_options(arguments =>
35 {
36 "warning:s" => { name => 'warning' },
37 "critical:s" => { name => 'critical' },
38 });
39
40 $self->{statefile_value} = centreon::plugins::statefile->new(%options);
41
42 return $self;
43 }
44
45 sub check_options {
46 my ($self, %options) = @_;
47 $self->SUPER::init(%options);
48
49 if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) {
50 $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
51 $self->{output}->option_exit();
52 }
53 if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) {
54 $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
55 $self->{output}->option_exit();
56 }
57
58 $self->{statefile_value}->check_options(%options);
59 }
60
61 sub run {
62 my ($self, %options) = @_;
63 $self->{snmp} = $options{snmp};
64 $self->{hostname} = $self->{snmp}->get_hostname();
65 $self->{snmp_port} = $self->{snmp}->get_port();
66
67 if ($self->{snmp}->is_snmpv1()) {
68 $self->{output}->add_option_msg(short_msg => "Can't check SNMP 64 bits counters with SNMPv1.");
69 $self->{output}->option_exit();
70 }
71
72 my $new_datas = {};
73 $self->{statefile_value}->read(statefile => "stonesoft_" . $self->{hostname} . '_' . $self->{snmp_port} . '_' . $self->{mode});
74
75 my $oid_fwRejected = '.1.3.6.1.4.1.1369.5.2.1.9.0';
76 my $result = $self->{snmp}->get_leef(oids => [$oid_fwRejected], nothing_quit => 1);
77
78 my $rejected_packets = $result->{$oid_fwRejected};
79 $new_datas->{rejected_packets} = $rejected_packets;
80 $new_datas->{last_timestamp} = time();
81
82 my $old_datas = {};
83 $old_datas->{old_timestamp} = $self->{statefile_value}->get(name => 'last_timestamp');
84 $old_datas->{old_rejected_packets} = $self->{statefile_value}->get(name => 'rejected_packets');
85 if (!defined($old_datas->{old_rejected_packets}) || $new_datas->{rejected_packets} < $old_datas->{old_rejected_packets}) {
86 # We set 0. Has reboot.
87 $old_datas->{old_rejected_packets} = 0;
88 }
89
90 if (defined($old_datas->{old_timestamp})) {
91 my $time_delta = $new_datas->{last_timestamp} - $old_datas->{old_timestamp};
92 if ($time_delta <= 0) {
93 $time_delta = 1;
94 }
95
96 my $rejected = $new_datas->{rejected_packets} - $old_datas->{old_rejected_packets};
97 my $rejected_per_sec = $rejected / $time_delta;
98
99 my $exit = $self->{perfdata}->threshold_check(value => $rejected_per_sec, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
100
101 $self->{output}->output_add(severity => $exit,
102 short_msg => sprintf("Packets Rejected : %.2f /s [%i packets]",
103 $rejected_per_sec, $rejected));
104
105 $self->{output}->perfdata_add(label => 'rejected_packets_per_sec',
106 value => sprintf("%.2f", $rejected_per_sec),
107 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
108 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'),
109 min => 0);
110
111 } else {
112 $self->{output}->output_add(severity => 'OK',
113 short_msg => "Buffer creation...");
114 }
115
116 $self->{statefile_value}->write(data => $new_datas);
117
118 $self->{output}->display();
119 $self->{output}->exit();
120 }
121
122 1;
123
124 __END__
125
126 =head1 MODE
127
128 Check rejected packets per second by firewall.
129
130 =over 8
131
132 =item B<--warning>
133
134 Warning threshold for blocked packets per second.
135
136 =item B<--critical>
137
138 Critical threshold for blocked packets per second.
139
140 =back
141
142 =cut
143