Debian Codegraph @

src/centreon/common/cisco/standard/snmp/mode/hsrp.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 centreon::common::cisco::standard::snmp::mode::hsrp;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27
28 my %map_row_status = (
29 1 => 'active',
30 2 => 'notInService',
31 3 => 'notReady',
32 4 => 'createAndGo',
33 5 => 'createAndWait',
34 6 => 'destroy'
35 );
36 my %map_states = (
37 1 => 'initial',
38 2 => 'learn',
39 3 => 'listen',
40 4 => 'speak',
41 5 => 'standby',
42 6 => 'active',
43 );
44
45 sub new {
46 my ($class, %options) = @_;
47 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
48 bless $self, $class;
49
50 $options{options}->add_options(arguments => {
51 'role:s' => { name => 'role', default => 'primary' },
52 'filter-vrid:s' => { name => 'filter_vrid' },
53 });
54
55 return $self;
56 }
57
58 sub check_options {
59 my ($self, %options) = @_;
60 $self->SUPER::init(%options);
61
62 if ($self->{option_results}->{role} !~ /^primary|secondary$/) {
63 $self->{output}->add_option_msg(short_msg => "You must use either primary either secondary for --role option");
64 $self->{output}->option_exit();
65 }
66 }
67
68 sub run {
69 my ($self, %options) = @_;
70 $self->{snmp} = $options{snmp};
71
72 my $vridout = '';
73 my $oid_cHsrpGrpStandbyState = ".1.3.6.1.4.1.9.9.106.1.2.1.1.15"; # HSRP Oper Status
74 my $oid_cHsrpGrpEntryRowStatus = ".1.3.6.1.4.1.9.9.106.1.2.1.1.17"; # HSRP Admin Status
75
76 my $results = $self->{snmp}->get_multiple_table(oids =>
77 [
78 { oid => $oid_cHsrpGrpStandbyState },
79 { oid => $oid_cHsrpGrpEntryRowStatus },
80 ],
81 nothing_quit => 1
82 );
83
84 $self->{output}->output_add(severity => 'OK',
85 short_msg => sprintf("Router is in its expected state : '%s'", $self->{option_results}->{role}));
86 foreach my $oid (keys %{$results->{$oid_cHsrpGrpStandbyState}}) {
87 $oid =~ /(\d+\.\d+)$/;
88 my $vrid = $1;
89
90 if (defined($self->{option_results}->{filter_vrid}) && $self->{option_results}->{filter_vrid} ne '' &&
91 $vrid !~ /$self->{option_results}->{filter_vrid}/) {
92 $self->{output}->output_add(long_msg => "skipping vrid '" . $vrid . "': no matching filter.", debug => 1);
93 next;
94 }
95
96 my $operState = $results->{$oid_cHsrpGrpEntryRowStatus}->{$oid_cHsrpGrpEntryRowStatus . "." . $vrid};
97 my $adminState = $results->{$oid_cHsrpGrpStandbyState}->{$oid};
98
99 $self->{output}->output_add(long_msg => sprintf("[Vrid : %s] [Admin Status is '%s'] [Oper Status is '%s']",
100 $vrid, $map_states{$adminState}, $map_row_status{$operState}));
101
102 if ($map_row_status{$operState} !~ /^active$/i) {
103 $self->{output}->output_add(severity => 'CRITICAL',
104 short_msg => sprintf("VRID %s operational state is '%s'", $vrid, $map_row_status{$operState}));
105 }
106
107 if (($self->{option_results}->{role} eq 'primary' && $map_states{$adminState} !~ /^active$/) ||
108 ($self->{option_results}->{role} eq 'secondary' && $map_states{$adminState} !~ /^standby$/)) {
109 $vridout .= sprintf("(VRID %s is '%s')", $vrid, $map_states{$adminState});
110 }
111 }
112
113 if ($vridout ne '') {
114 $self->{output}->output_add(severity => 'CRITICAL',
115 short_msg => sprintf("%s - Router isn't in the expected state (%s)", $vridout, $self->{option_results}->{role}));
116 }
117
118 $self->{output}->display();
119 $self->{output}->exit();
120 }
121
122 1;
123
124 __END__
125
126 =head1 MODE
127
128 Check Cisco HSRP (CISCO-HSRP-MIB). Trigger a critical if not in the expected state or if a VRID is not in an active state.
129
130 =over 8
131
132 =item B<--filter-vrid>
133
134 Filter VRID (can be a regexp).
135
136 =item B<--role>
137
138 If role is 'primary', an error if HSRPs are 'standby' states.
139 If role is 'secondary', an error if HSRPs are 'active' states. (default: 'primary')
140
141 =back
142
143 =cut
144