Debian Codegraph @

src/os/solaris/local/mode/cpu.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::solaris::local::mode::cpu;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::statefile;
28
29 sub new {
30 my ($class, %options) = @_;
31 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
32 bless $self, $class;
33
34 $options{options}->add_options(arguments => {
35 'warning:s' => { name => 'warning' },
36 'critical:s' => { name => 'critical' }
37 });
38
39 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
40 return $self;
41 }
42
43 sub check_options {
44 my ($self, %options) = @_;
45 $self->SUPER::init(%options);
46
47 if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) {
48 $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
49 $self->{output}->option_exit();
50 }
51 if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) {
52 $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'.");
53 $self->{output}->option_exit();
54 }
55
56 $self->{statefile_cache}->check_options(%options);
57 }
58
59 sub run {
60 my ($self, %options) = @_;
61
62 my ($stdout) = $options{custom}->execute_command(
63 command => 'kstat',
64 command_options => '-n sys 2>&1'
65 );
66
67 $self->{statefile_cache}->read(statefile => 'cache_solaris_local_' . $options{custom}->get_identifier() . '_' . $self->{mode});
68 my $old_timestamp = $self->{statefile_cache}->get(name => 'last_timestamp');
69 my $datas = {};
70 $datas->{last_timestamp} = time();
71
72 $self->{output}->output_add(
73 severity => 'OK',
74 short_msg => "CPUs usages are ok."
75 );
76 my @output_cpu_instance = split("instance", $stdout);
77 shift @output_cpu_instance;
78 foreach (@output_cpu_instance) {
79 /:\s.*?(\d+)/;
80 my $cpu_number = $1;
81 /.*?cpu_ticks_idle\s.*?(\d+).*?cpu_ticks_kernel\s.*?(\d+).*?cpu_ticks_user\s.*?(\d+)/ms;
82 $datas->{'cpu_idle_' . $cpu_number} = $1;
83 $datas->{'cpu_system_' . $cpu_number} = $2;
84 $datas->{'cpu_user_' . $cpu_number} = $3;
85
86 if (!defined($old_timestamp)) {
87 next;
88 }
89 my $old_cpu_idle = $self->{statefile_cache}->get(name => 'cpu_idle_' . $cpu_number);
90 my $old_cpu_system = $self->{statefile_cache}->get(name => 'cpu_system_' . $cpu_number);
91 my $old_cpu_user = $self->{statefile_cache}->get(name => 'cpu_user_' . $cpu_number);
92 if (!defined($old_cpu_system) || !defined($old_cpu_idle) || !defined($old_cpu_user)) {
93 next;
94 }
95
96 if ($datas->{'cpu_idle_' . $cpu_number} < $old_cpu_idle) {
97 # We set 0. Has reboot.
98 $old_cpu_user = 0;
99 $old_cpu_idle = 0;
100 $old_cpu_system = 0;
101 }
102
103 my $total_elapsed = ($datas->{'cpu_idle_' . $cpu_number} + $datas->{'cpu_user_' . $cpu_number} + $datas->{'cpu_system_' . $cpu_number}) - ($old_cpu_user + $old_cpu_idle + $old_cpu_system);
104 my $idle_elapsed = $datas->{'cpu_idle_' . $cpu_number} - $old_cpu_idle;
105 my $cpu_ratio_usetime = 100 * $idle_elapsed / $total_elapsed;
106 $cpu_ratio_usetime = 100 - $cpu_ratio_usetime;
107
108 my $exit_code = $self->{perfdata}->threshold_check(
109 value => $cpu_ratio_usetime,
110 threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]
111 );
112
113 $self->{output}->output_add(long_msg => sprintf("CPU %d %.2f%%", $cpu_number, $cpu_ratio_usetime));
114 if (!$self->{output}->is_status(litteral => 1, value => $exit_code, compare => 'ok')) {
115 $self->{output}->output_add(
116 severity => $exit_code,
117 short_msg => sprintf("CPU %d %.2f%%", $cpu_number, $cpu_ratio_usetime)
118 );
119 }
120 $self->{output}->perfdata_add(
121 label => 'cpu_' . $cpu_number, unit => '%',
122 value => sprintf("%.2f", $cpu_ratio_usetime),
123 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'),
124 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'),
125 min => 0, max => 100
126 );
127 }
128
129 $self->{statefile_cache}->write(data => $datas);
130 if (!defined($old_timestamp)) {
131 $self->{output}->output_add(
132 severity => 'OK',
133 short_msg => "Buffer creation..."
134 );
135 }
136
137 $self->{output}->display();
138 $self->{output}->exit();
139 }
140
141 1;
142
143 __END__
144
145 =head1 MODE
146
147 Check system CPUs
148
149 Command used: 'kstat -n sys 2>&1'
150
151 =over 8
152
153 =item B<--warning>
154
155 Warning threshold in percent.
156
157 =item B<--critical>
158
159 Critical threshold in percent.
160
161 =back
162
163 =cut