Debian Codegraph @

src/hardware/devices/cisco/ces/restapi/mode/diagnostics.pm

raw
1 #
2 # Copyright 2018 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 hardware::devices::cisco::ces::restapi::mode::diagnostics;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use POSIX;
28 use centreon::plugins::misc;
29 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
30
31 sub custom_status_output {
32 my ($self, %options) = @_;
33
34 return sprintf(
35 'alarm [level: %s] [type: %s] [description: %s]',
36 $self->{result_values}->{level},
37 $self->{result_values}->{type},
38 $self->{result_values}->{description}
39 );
40 }
41
42 sub set_counters {
43 my ($self, %options) = @_;
44
45 $self->{maps_counters_type} = [
46 { name => 'alarms', type => 2, message_multiple => '0 problem(s) detected', display_counter_problem => { label => 'alerts', min => 0 },
47 group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ]
48 }
49 ];
50
51 $self->{maps_counters}->{alarm} = [
52 { label => 'status', threshold => 0, set => {
53 key_values => [ { name => 'description' }, { name => 'level' }, { name => 'type' } ],
54 closure_custom_output => $self->can('custom_status_output'),
55 closure_custom_perfdata => sub { return 0; },
56 closure_custom_threshold_check => \&catalog_status_threshold
57 }
58 }
59 ];
60 }
61
62 sub new {
63 my ($class, %options) = @_;
64 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
65 bless $self, $class;
66
67 $options{options}->add_options(arguments => {
68 'filter-msg:s' => { name => 'filter_msg' },
69 'legacy-tc' => { name => 'legacy_tc' },
70 'warning-status:s' => { name => 'warning_status', default => '%{level} =~ /warning|minor/i' },
71 'critical-status:s' => { name => 'critical_status', default => '%{level} =~ /critical|major/i' }
72 });
73
74 return $self;
75 }
76
77 sub check_options {
78 my ($self, %options) = @_;
79 $self->SUPER::check_options(%options);
80
81 $self->change_macros(macros => ['warning_status', 'critical_status']);
82 }
83
84 #<Command>
85 #<DiagnosticsResult status="OK">
86 # <Message item="1">
87 # <Description item="1">System is in standby mode.</Description>
88 # <Level item="1">Skipped</Level>
89 # <References item="1"/>
90 # <Type item="1">SelectedVideoInputSourceConnected</Type>
91 # </Message>
92 #</DiagnosticsResult>
93 #</Command>
94
95 sub manage_selection {
96 my ($self, %options) = @_;
97
98 my $post = '<Command><Diagnostics><Run><ResultSet>Alerts</ResultSet></Run></Diagnostics></Command>';
99 my $label = 'DiagnosticsRunResult';
100 if (defined($self->{option_results}->{legacy_tc})) {
101 $post = '<Command><SystemUnit><Diagnostics><Run><ResultSet>Alerts</ResultSet></Run></Diagnostics></SystemUnit></Command>';
102 $label = 'DiagnosticsResult';
103 }
104 my $result = $options{custom}->request_api(
105 method => 'POST',
106 url_path => '/putxml',
107 query_form_post => $post,
108 ForceArray => ['Message']
109 );
110
111 $self->{alarms}->{global} = { alarm => {} };
112 foreach (@{$result->{$label}->{Message}}) {
113 my $description = ref($_->{Description}) eq 'HASH' ? $_->{Description}->{content} : $_->{Description};
114 my $level = ref($_->{Level}) eq 'HASH' ? $_->{Level}->{content} : $_->{Level};
115 my $type = ref($_->{Type}) eq 'HASH' ? $_->{Type}->{content} : $_->{Type};
116
117 if (defined($self->{option_results}->{filter_msg}) && $self->{option_results}->{filter_msg} ne '' &&
118 $description !~ /$self->{option_results}->{filter_msg}/) {
119 $self->{output}->output_add(long_msg => "skipping '" . $description . "': no matching filter.", debug => 1);
120 next;
121 }
122
123 $self->{alarms}->{global}->{alarm}->{ $_->{item} } = {
124 description => $description,
125 level => $level,
126 type => $type
127 };
128 }
129 }
130
131 1;
132
133 __END__
134
135 =head1 MODE
136
137 Check diagnostic messages.
138 Since TC 6.0: SystemUnit Diagnostics Run (use --legacy-tc option)
139 Since CE 8.0: Diagnostics Run
140
141 =over 8
142
143 =item B<--filter-msg>
144
145 Filter by message (can be a regexp).
146
147 =item B<--legacy-tc>
148
149 Use old legacy command.
150
151 =item B<--warning-status>
152
153 Define the conditions to match for the status to be WARNING (default: '%{level} =~ /warning|minor/i')
154 You can use the following variables: %{description}, %{level}, %{type}
155
156 =item B<--critical-status>
157
158 Define the conditions to match for the status to be CRITICAL (default: '%{level} =~ /critical|major/i').
159 You can use the following variables: %{description}, %{level}, %{type}
160
161 =back
162
163 =cut