Debian Codegraph @

src/os/aix/local/mode/process.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 os::aix::local::mode::process;
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 return sprintf(
33 'Process [command => %s] [arg: %s] [state: %s] [elapsed => %s]',
34 $self->{result_values}->{cmd},
35 $self->{result_values}->{args},
36 $self->{result_values}->{state},
37 $self->{result_values}->{elapsed}
38 );
39 }
40
41 sub set_counters {
42 my ($self, %options) = @_;
43
44 $self->{maps_counters_type} = [
45 { name => 'global', type => 0 },
46 { name => 'alarms', type => 2, message_multiple => '0 process problem detected', format_output => '%s process(es) problem(s) detected',
47 display_counter_problem => { nlabel => 'processes.alerts.count', min => 0 },
48 group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ]
49 }
50 ];
51
52 $self->{maps_counters}->{global} = [
53 { label => 'total', nlabel => 'processes.total.count', set => {
54 key_values => [ { name => 'total' } ],
55 output_template => 'Number of current processes: %s',
56 perfdatas => [
57 { value => 'total', template => '%s', min => 0 },
58 ],
59 }
60 },
61 ];
62
63 $self->{maps_counters}->{alarm} = [
64 { label => 'status', threshold => 0, set => {
65 key_values => [
66 { name => 'ppid' }, { name => 'state' },
67 { name => 'elapsed' }, { name => 'cmd' }, { name => 'args' }
68 ],
69 closure_custom_output => $self->can('custom_status_output'),
70 closure_custom_perfdata => sub { return 0; },
71 closure_custom_threshold_check => \&catalog_status_threshold,
72 }
73 },
74 ];
75 }
76
77 sub new {
78 my ($class, %options) = @_;
79 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
80 bless $self, $class;
81
82 $options{options}->add_options(arguments => {
83 'filter-command:s' => { name => 'filter_command' },
84 'filter-arg:s' => { name => 'filter_arg' },
85 'filter-state:s' => { name => 'filter_state' },
86 'filter-ppid:s' => { name => 'filter_ppid' },
87 'warning-status:s' => { name => 'warning_status', default => '' },
88 'critical-status:s' => { name => 'critical_status', default => '' }
89 });
90
91 return $self;
92 }
93
94 sub check_options {
95 my ($self, %options) = @_;
96 $self->SUPER::check_options(%options);
97
98 $self->change_macros(macros => ['warning_status', 'critical_status']);
99 }
100
101 my %state_map = (
102 Z => 'Canceled',
103 O => 'Nonexistent',
104 A => 'Active',
105 W => 'Swapped',
106 I => 'Idle',
107 T => 'Stopped',
108 R => 'Running',
109 S => 'Sleeping',
110 );
111
112 sub get_time_seconds {
113 my ($self, %options) = @_;
114
115 my $time = $options{value};
116 # Format: [[dd-]hh:]mm:ss
117 my @values = split /:/, $time;
118 my ($seconds, $min, $lpart) = (pop @values, pop @values, pop @values);
119 my $total_seconds_elapsed = $seconds + ($min * 60);
120 if (defined($lpart)) {
121 my ($day, $hour) = split /-/, $lpart;
122 if (!defined($hour)) {
123 $hour = $day;
124 $day = undef;
125 }
126 if (defined($hour)) {
127 $total_seconds_elapsed += ($hour * 60 * 60);
128 }
129 if (defined($day)) {
130 $total_seconds_elapsed += ($day * 86400);
131 }
132 }
133
134 return $total_seconds_elapsed;
135 }
136
137 sub manage_selection {
138 my ($self, %options) = @_;
139
140 my ($stdout) = $options{custom}->execute_command(
141 command => 'ps',
142 command_options => '-e -o state -o ===%t===%p===%P=== -o comm:50 -o ===%a 2>&1'
143 );
144
145 $self->{alarms}->{global} = { alarm => {} };
146 my @lines = split /\n/, $stdout;
147 my $line = shift @lines;
148 foreach my $line (@lines) {
149 next if ($line !~ /^(.*?)===(.*?)===(.*?)===(.*?)===(.*?)===(.*)$/);
150 my ($state, $elapsed, $pid, $ppid, $cmd, $args) = (
151 centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2), centreon::plugins::misc::trim($3),
152 centreon::plugins::misc::trim($4), centreon::plugins::misc::trim($5), centreon::plugins::misc::trim($6)
153 );
154
155 next if (defined($self->{option_results}->{filter_command}) && $self->{option_results}->{filter_command} ne '' &&
156 $cmd !~ /$self->{option_results}->{filter_command}/);
157 next if (defined($self->{option_results}->{filter_arg}) && $self->{option_results}->{filter_arg} ne '' &&
158 $args !~ /$self->{option_results}->{filter_arg}/);
159 next if (defined($self->{option_results}->{filter_state}) && $self->{option_results}->{filter_state} ne '' &&
160 $state_map{$state} !~ /$self->{option_results}->{filter_state}/i);
161 next if (defined($self->{option_results}->{filter_ppid}) && $self->{option_results}->{filter_ppid} ne '' &&
162 $ppid !~ /$self->{option_results}->{filter_ppid}/);
163
164 $self->{alarms}->{global}->{alarm}->{$pid} = {
165 ppid => $ppid,
166 state => $state_map{$state},
167 elapsed => $self->get_time_seconds(value => $elapsed),
168 cmd => $cmd,
169 args => $args
170 };
171 }
172
173 $self->{global} = { total => scalar(keys %{$self->{alarms}->{global}->{alarm}}) };
174 }
175
176 1;
177
178 __END__
179
180 =head1 MODE
181
182 Check AIX processes.
183 Command used: ps -e -o state -o ===%t===%p===%P=== -o comm:50 -o ===%a 2>&1
184
185 =over 8
186
187 =item B<--filter-command>
188
189 Filter process commands (regexp can be used).
190
191 =item B<--filter-arg>
192
193 Filter process arguments (regexp can be used).
194
195 =item B<--filter-ppid>
196
197 Filter process ppid (regexp can be used).
198
199 =item B<--filter-state>
200
201 Filter process states (regexp can be used).
202 You can use: 'Canceled', 'Nonexistent', 'Active',
203 'Swapped', 'Idle', 'Stopped', 'Running', 'Sleeping'.
204
205 =item B<--warning-status>
206
207 Define the conditions to match for the status to be WARNING (default: '')
208 You can use the following variables: %{ppid}, %{state}, %{elapsed}, %{cmd}, %{args}
209
210 =item B<--critical-status>
211
212 Define the conditions to match for the status to be CRITICAL (default: '').
213 You can use the following variables: %{ppid}, %{state}, %{elapsed}, %{cmd}, %{args}
214
215 =item B<--warning-*> B<--critical-*>
216
217 Thresholds.
218 Can be: 'total'.
219
220 =back
221
222 =cut