Debian Codegraph @

src/apps/mq/vernemq/restapi/mode/clusters.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::mq::vernemq::restapi::mode::clusters;
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 catalog_status_calc);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 return 'status: ' . $self->{result_values}->{status};
33 }
34
35 sub prefix_cluster_output {
36 my ($self, %options) = @_;
37
38 return "Cluster '" . $options{instance_value}->{display} . "' ";
39 }
40
41 sub set_counters {
42 my ($self, %options) = @_;
43
44 $self->{maps_counters_type} = [
45 { name => 'global', type => 0 },
46 { name => 'clusters', type => 1, cb_prefix_output => 'prefix_cluster_output', message_multiple => 'All clusters are ok', skipped_code => { -10 => 1 } },
47 ];
48
49 $self->{maps_counters}->{global} = [
50 { label => 'running', nlabel => 'clusters.running.count', display_ok => 0, set => {
51 key_values => [ { name => 'running' } ],
52 output_template => 'current clusters running: %s',
53 perfdatas => [
54 { value => 'running', template => '%s', min => 0 }
55 ]
56 }
57 },
58 { label => 'notrunning', nlabel => 'clusters.notrunning.count', display_ok => 0, set => {
59 key_values => [ { name => 'notrunning' } ],
60 output_template => 'current clusters not running: %s',
61 perfdatas => [
62 { value => 'notrunning', template => '%s', min => 0 }
63 ]
64 }
65 }
66 ];
67
68 $self->{maps_counters}->{clusters} = [
69 { label => 'status', threshold => 0, set => {
70 key_values => [ { name => 'status' }, { name => 'display' } ],
71 closure_custom_calc => \&catalog_status_calc,
72 closure_custom_output => $self->can('custom_status_output'),
73 closure_custom_perfdata => sub { return 0; },
74 closure_custom_threshold_check => \&catalog_status_threshold
75 }
76 }
77 ];
78 }
79
80 sub new {
81 my ($class, %options) = @_;
82 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
83 bless $self, $class;
84
85 $options{options}->add_options(arguments => {
86 'filter-name:s' => { name => 'filter_name' },
87 'unknown-status:s' => { name => 'unknown_status', default => '' },
88 'warning-status:s' => { name => 'warning_status', default => '' },
89 'critical-status:s' => { name => 'critical_status', default => '%{status} eq "notRunning"' },
90 });
91
92 return $self;
93 }
94
95 sub check_options {
96 my ($self, %options) = @_;
97 $self->SUPER::check_options(%options);
98
99 $self->change_macros(macros => ['warning_status', 'critical_status', 'unknown_status']);
100 }
101
102 sub manage_selection {
103 my ($self, %options) = @_;
104
105 my $clusters = $options{custom}->request_api(endpoint => '/cluster/show');
106
107 $self->{global} = { running => 0, notrunning => 0 };
108 $self->{clusters} = {};
109 foreach (@{$clusters->{table}}) {
110 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
111 $_->{Node} !~ /$self->{option_results}->{filter_name}/) {
112 $self->{output}->output_add(long_msg => "skipping cluster '" . $_->{Node} . "': no matching filter.", debug => 1);
113 next;
114 }
115
116 $_->{Running} ? $self->{global}->{running}++ : $self->{global}->{notrunning}++;
117 $self->{clusters}->{$_->{Node}} = {
118 display => $_->{Node},
119 status => $_->{Running} ? 'running' : 'notRunning'
120 };
121 }
122
123 if (scalar(keys %{$self->{clusters}}) <= 0) {
124 $self->{output}->add_option_msg(short_msg => "No cluster found");
125 $self->{output}->option_exit();
126 }
127 }
128
129 1;
130
131 __END__
132
133 =head1 MODE
134
135 Check clusters.
136
137 =over 8
138
139 =item B<--filter-name>
140
141 Filter cluster name (can be a regexp).
142
143 =item B<--unknown-status>
144
145 Define the conditions to match for the status to be UNKNOWN.
146 You can use the following variables: %{status}, %{display}
147
148 =item B<--warning-status>
149
150 Define the conditions to match for the status to be WARNING.
151 You can use the following variables: %{status}, %{display}
152
153 =item B<--critical-status>
154
155 Define the conditions to match for the status to be CRITICAL (default: '%{status} eq "notRunning"').
156 You can use the following variables: %{status}, %{display}
157
158 =item B<--warning-*> B<--critical-*>
159
160 Thresholds.
161 Can be: 'running', 'notrunning'.
162
163 =back
164
165 =cut