Debian Codegraph @

src/network/barracuda/cloudgen/snmp/mode/serverservice.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::barracuda::cloudgen::snmp::mode::serverservice;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 my $msg = "status is '" . $self->{result_values}->{status} . "'";
33 return $msg;
34 }
35
36 sub custom_status_calc {
37 my ($self, %options) = @_;
38
39 $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_serverServiceState'};
40 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
41 return 0;
42 }
43
44 sub set_counters {
45 my ($self, %options) = @_;
46
47 $self->{maps_counters_type} = [
48 { name => 'services', type => 1, cb_prefix_output => 'prefix_services_output', message_multiple => 'All server services are ok' }
49 ];
50
51 $self->{maps_counters}->{services} = [
52 { label => 'status', threshold => 0, set => {
53 key_values => [ { name => 'serverServiceState' }, { name => 'display' } ],
54 closure_custom_calc => $self->can('custom_status_calc'),
55 closure_custom_output => $self->can('custom_status_output'),
56 closure_custom_perfdata => sub { return 0; },
57 closure_custom_threshold_check => \&catalog_status_threshold,
58 }
59 },
60 ];
61 }
62
63 sub prefix_services_output {
64 my ($self, %options) = @_;
65
66 return "Server service '" . $options{instance_value}->{display} . "' ";
67 }
68
69 sub new {
70 my ($class, %options) = @_;
71 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
72 bless $self, $class;
73
74 $options{options}->add_options(arguments => {
75 'filter-name:s' => { name => 'filter_name' },
76 'warning-status:s' => { name => 'warning_status', default => '' },
77 'critical-status:s' => { name => 'critical_status', default => '%{status} !~ /^started$/i' },
78 });
79
80 return $self;
81 }
82
83 sub check_options {
84 my ($self, %options) = @_;
85 $self->SUPER::check_options(%options);
86
87 $self->change_macros(macros => ['warning_status', 'critical_status']);
88 }
89
90 my %map_status = (
91 -1 => 'unknown',
92 0 => 'stopped',
93 1 => 'started',
94 2 => 'blocked',
95 4 => 'removed',
96 );
97
98 my $oid_serverServiceName = '.1.3.6.1.4.1.10704.1.1.1.1';
99 my $mapping = {
100 serverServiceState => { oid => '.1.3.6.1.4.1.10704.1.1.1.2', map => \%map_status },
101 };
102
103 sub manage_selection {
104 my ($self, %options) = @_;
105
106 if ($options{snmp}->is_snmpv1()) {
107 $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3.");
108 $self->{output}->option_exit();
109 }
110
111 my $snmp_result = $options{snmp}->get_table(oid => $oid_serverServiceName, nothing_quit => 1);
112 $self->{services} = {};
113 foreach my $oid (keys %{$snmp_result}) {
114 $oid =~ /^$oid_serverServiceName\.(.*)$/;
115 my $instance = $1;
116 $snmp_result->{$oid} =~ s/\\//g;
117 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
118 $snmp_result->{$oid} !~ /$self->{option_results}->{filter_name}/) {
119 $self->{output}->output_add(long_msg => "skipping service '" . $snmp_result->{$oid} . "'.", debug => 1);
120 next;
121 }
122
123 $self->{services}->{$instance} = { display => $snmp_result->{$oid} };
124 }
125
126 $options{snmp}->load(oids => [$mapping->{serverServiceState}->{oid}], instances => [keys %{$self->{services}}], instance_regexp => '^(.*)$');
127 $snmp_result = $options{snmp}->get_leef(nothing_quit => 1);
128
129 foreach (keys %{$self->{services}}) {
130 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_);
131
132 foreach my $name (keys %$mapping) {
133 $self->{services}->{$_}->{$name} = $result->{$name};
134 }
135 }
136
137 if (scalar(keys %{$self->{services}}) <= 0) {
138 $self->{output}->add_option_msg(short_msg => "No services found.");
139 $self->{output}->option_exit();
140 }
141 }
142
143 1;
144
145 __END__
146
147 =head1 MODE
148
149 Check server services status.
150
151 =over 8
152
153 =item B<--warning-status>
154
155 Define the conditions to match for the status to be WARNING.
156 You can use the following variables: %{status}, %{display}
157
158 =item B<--critical-status>
159
160 Define the conditions to match for the status to be CRITICAL (default: '%{status} !~ /^started$/i').
161 You can use the following variables: %{status}, %{display}
162
163 =item B<--filter-name>
164
165 Filter by service name (can be a regexp).
166
167 =back
168
169 =cut