Debian Codegraph @

src/os/hpux/snmp/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::hpux::snmp::mode::process;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27
28 my %map_process_status = (
29 1 => 'sleep',
30 2 => 'run',
31 3 => 'stop',
32 4 => 'zombie',
33 5 => 'other',
34 6 => 'idle',
35 );
36
37 sub new {
38 my ($class, %options) = @_;
39 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
40 bless $self, $class;
41
42 $options{options}->add_options(arguments =>
43 {
44 "warning:s" => { name => 'warning', },
45 "critical:s" => { name => 'critical', },
46 "process-cmd:s" => { name => 'process_cmd', },
47 "regexp-cmd" => { name => 'regexp_cmd', },
48 "process-status:s" => { name => 'process_status', default => 'run' },
49 });
50
51 return $self;
52 }
53
54 sub check_options {
55 my ($self, %options) = @_;
56 $self->SUPER::init(%options);
57
58 if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) {
59 $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
60 $self->{output}->option_exit();
61 }
62 if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) {
63 $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
64 $self->{output}->option_exit();
65 }
66 }
67
68 my $oids = {
69 status => '.1.3.6.1.4.1.11.2.3.1.4.2.1.19', # processStatus
70 cmd => '.1.3.6.1.4.1.11.2.3.1.4.2.1.22', # processCmd
71 };
72
73 sub run {
74 my ($self, %options) = @_;
75 $self->{snmp} = $options{snmp};
76
77 my $oid2check_filter = 'status';
78 # To have a better order
79 foreach (('cmd', 'status')) {
80 if (defined($self->{option_results}->{'process_' . $_}) && $self->{option_results}->{'process_' . $_} ne '') {
81 $oid2check_filter = $_;
82 last;
83 }
84 }
85 # Build other
86 my $mores_filters = {};
87 my $more_oids = [];
88 foreach (keys %$oids) {
89 if ($_ ne $oid2check_filter && defined($self->{option_results}->{'process_' . $_}) && $self->{option_results}->{'process_' . $_} ne '') {
90 push @{$more_oids}, $oids->{$_};
91 $mores_filters->{$_} = 1;
92 }
93 }
94
95 my $oids_multiple_table = [ { oid => $oids->{$oid2check_filter} } ];
96
97 $self->{results} = $self->{snmp}->get_multiple_table(oids => $oids_multiple_table);
98 my $result = $self->{results}->{$oids->{$oid2check_filter}};
99 my $instances_keep = {};
100 foreach my $key ($self->{snmp}->oid_lex_sort(keys %{$result})) {
101 my $option_val = $self->{option_results}->{'process_' . $oid2check_filter};
102
103 if ($oid2check_filter eq 'status') {
104 if ($map_process_status{$result->{$key}} =~ /$option_val/) {
105 $key =~ /\.([0-9]+)$/;
106 $instances_keep->{$1} = 1;
107 }
108 } elsif ((defined($self->{option_results}->{'regexp_' . $oid2check_filter}) && $result->{$key} =~ /$option_val/)
109 || (!defined($self->{option_results}->{'regexp_' . $oid2check_filter}) && $result->{$key} eq $option_val)) {
110 $key =~ /\.([0-9]+)$/;
111 $instances_keep->{$1} = 1;
112 }
113 }
114
115 my $result2;
116 if (scalar(keys %$instances_keep) > 0) {
117 if (scalar(@$more_oids) > 0) {
118 $self->{snmp}->load(oids => $more_oids, instances => [ keys %$instances_keep ]);
119 $result2 = $self->{snmp}->get_leef();
120 }
121
122 foreach my $key (keys %$instances_keep) {
123 my $value = ($oid2check_filter eq 'status') ? $map_process_status{$result->{$oids->{$oid2check_filter} . '.' . $key}} : $result->{$oids->{$oid2check_filter} . '.' . $key};
124 my $long_value = '[ ' . $oid2check_filter . ' => ' . $value . ' ]';
125 my $deleted = 0;
126 foreach (keys %$mores_filters) {
127 my $opt_val = $self->{option_results}->{'process_' . $_};
128 $value = ($_ eq 'status') ? $map_process_status{$result2->{$oids->{$_} . '.' . $key}} : $result2->{$oids->{$_} . '.' . $key};
129
130 if ($_ eq 'status') {
131 if ($value !~ /$opt_val/) {
132 delete $instances_keep->{$key};
133 $deleted = 1;
134 last;
135 }
136 } elsif ((defined($self->{option_results}->{'regexp_' . $_}) && $value !~ /$opt_val/)
137 || (!defined($self->{option_results}->{'regexp_' . $_}) && $value ne $opt_val)) {
138 delete $instances_keep->{$key};
139 $deleted = 1;
140 last;
141 }
142
143 $long_value .= ' [ ' . $_ . ' => ' . $value . ' ]';
144 }
145
146 if ($deleted == 0) {
147 $self->{output}->output_add(long_msg => 'Process: ' . $long_value);
148 }
149 }
150 }
151
152 my $num_processes_match = scalar(keys(%$instances_keep));
153 my $exit = $self->{perfdata}->threshold_check(value => $num_processes_match, threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
154 $self->{output}->output_add(severity => $exit,
155 short_msg => "Number of current processes running: $num_processes_match");
156 $self->{output}->perfdata_add(label => 'nbproc',
157 value => $num_processes_match,
158 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
159 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'),
160 min => 0);
161
162 $self->{output}->display();
163 $self->{output}->exit();
164 }
165
166 1;
167
168 __END__
169
170 =head1 MODE
171
172 Monitor the number of system processes.
173
174 =over 8
175
176 =item B<--warning>
177
178 Warning threshold (process count).
179
180 =item B<--critical>
181
182 Critical threshold (process count).
183
184 =item B<--process-cmd>
185
186 Define the command of the processes to check.
187
188 =item B<--regexp-cmd>
189
190 Allows to use regexp to filter process command (with option --process-cmd).
191
192 =item B<--process-status>
193
194 Define the status(es) of the processes to check (default: 'run'). Can be a regexp.
195
196 =back
197
198 =cut