-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_linux_cpu
executable file
·219 lines (173 loc) · 5.82 KB
/
check_linux_cpu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/perl
##################
#
# Check the CPU on a Linux host
#
# Usage: check_linux_cpu -w "idle>90,system>0"
#
# Copyright (C) 2023 Bernd Arnold
# https://github.com/wopfel/monitoring-plugins
#
# Required modules:
# - Data::Dumper (when requesting verbose output)
# - Getopt::Long (for command line option parsing)
#
##################
use strict;
use warnings;
my $require_datadumper_rc = eval { require Data::Dumper; Data::Dumper->import; 1; };
use Getopt::Long;
# Sub routines
sub print_help();
my $opt_verbose;
my $opt_warn;
my $opt_help;
my $answer = "";
my $crit_count = 0;
my $warn_count = 0;
my $status = "Unknown";
my @perfdata = ();
my @additional = ();
# Process command line options
GetOptions( "v|verbose" => \$opt_verbose,
"w|warn=s" => \$opt_warn,
"help" => \$opt_help,
)
or do { print STDERR "Error in command line argument processing. Try '$0 -help'.\n"; exit 1; };
if ( $opt_help ) {
print_help();
exit 10; # Outside of the official monitoring plugin's exit codes
}
my $stat_filename = "/proc/stat";
my %time_series = ();
# Get the cpu values 2 times for measuring the difference
for my $iteration ( 0, 1 ) {
open FILE, "<", $stat_filename or die "Error: Cannot open file '$stat_filename'.";
while ( <FILE> ) {
if ( /^cpu/) {
my @values = split /\s+/;
my $object = shift @values;
push @{ $time_series{ $iteration }{ $object } }, @values;
}
}
close FILE or die "Error: Cannot close file '$stat_filename'.";
# Sleep some seconds between iterations
sleep 3 if $iteration == 0;
}
my %total_time = ();
my %diff_time = ();
for my $iteration ( 0, 1 ) {
$total_time{ $iteration } = 0;
my $element = 0;
for ( @{ $time_series{ $iteration }{ 'cpu' } } ) {
$total_time{ $iteration } += $_;
$diff_time{ 'cpu' }{ $element } = -$_ if $iteration == 0;
$diff_time{ 'cpu' }{ $element } += $_ if $iteration == 1;
$element++;
}
}
# That should be round about the sleep time times 100 (300 for 3 second sleep)
my $diff_time_total = $total_time{1} - $total_time{0};
if ( $opt_verbose ) {
print "Diff time:\n";
print Dumper( \%diff_time );
print "Time series:\n";
print Dumper( \%time_series );
}
my $user_time = $diff_time{ 'cpu' }{ 0 };
my $nice_time = $diff_time{ 'cpu' }{ 1 };
my $system_time = $diff_time{ 'cpu' }{ 2 };
my $idle_time = $diff_time{ 'cpu' }{ 3 };
my $iowait_time = $diff_time{ 'cpu' }{ 4 };
my $irq_time = $diff_time{ 'cpu' }{ 5 };
my $softirq_time = $diff_time{ 'cpu' }{ 6 };
my $user_pct = $user_time / $diff_time_total * 100;
my $nice_pct = $nice_time / $diff_time_total * 100;
my $system_pct = $system_time / $diff_time_total * 100;
my $idle_pct = $idle_time / $diff_time_total * 100;
my $iowait_pct = $iowait_time / $diff_time_total * 100;
my $irq_pct = $irq_time / $diff_time_total * 100;
my $softirq_pct = $softirq_time / $diff_time_total * 100;
###
for my $warn_check ( split /,/, $opt_warn // "" ) {
print "Warn check: $warn_check\n" if $opt_verbose;
if ( my ( $obj, $cmp, $value ) = $warn_check =~ /^([a-z]+)(\>)(\d+)$/ ) {
print "$obj $cmp $value\n" if $opt_verbose;
if ( $obj eq "user" and $cmp eq ">" ) {
if ( $user_pct > $value ) {
$warn_count++;
}
} elsif ( $obj eq "nice" and $cmp eq ">" ) {
if ( $nice_pct > $value ) {
$warn_count++;
}
} elsif ( $obj eq "system" and $cmp eq ">" ) {
if ( $system_pct > $value ) {
$warn_count++;
}
} elsif ( $obj eq "idle" and $cmp eq ">" ) {
if ( $idle_pct > $value ) {
$warn_count++;
}
} elsif ( $obj eq "iowait" and $cmp eq ">" ) {
if ( $iowait_pct > $value ) {
$warn_count++;
}
} elsif ( $obj eq "irq" and $cmp eq ">" ) {
if ( $irq_pct > $value ) {
$warn_count++;
}
} elsif ( $obj eq "softirq" and $cmp eq ">" ) {
if ( $softirq_pct > $value ) {
$warn_count++;
}
} else {
die "Wrong parameter format. Try 'idle>70'";
}
} else {
die "Wrong parameter format. Try 'idle>70'";
}
}
$answer .= sprintf "CPUs %.2f %% idle, %.2f %% user, %.2f %% system, %.2f %% iowait. ", $idle_pct, $user_pct, $system_pct, $iowait_pct;
push @perfdata, "cpu_idle=$idle_pct";
push @perfdata, "cpu_user=$user_pct";
push @perfdata, "cpu_nice=$nice_pct";
push @perfdata, "cpu_system=$system_pct";
push @perfdata, "cpu_iowait=$iowait_pct";
push @perfdata, "cpu_irq=$irq_pct";
push @perfdata, "cpu_softirq=$softirq_pct";
###
if ( $crit_count ) {
$status = "Critical";
} elsif ( $warn_count ) {
$status = "Warning";
} else {
$status = "Ok";
}
print "$status: ";
print "$answer\n";
print "| " . join(" ", @perfdata) . "\n" if @perfdata;
print join("\n", @additional) . "\n" if @additional;
exit 2 if $status eq "Critical";
exit 1 if $status eq "Warning";
exit 0 if $status eq "Ok";
exit 3; # 'Unknown' return code
# Sub routine
sub print_help() {
print STDERR <<~"END"
Check linux CPU utilization.
Usage: $0 -w WARNDEF
Example: $0 -w system>50
Warns if system cpu utilization is above 50 %
Parameters:
-h, --help Show this help text
-v, --verbose Verbose output (for debugging)
-w, --warn DEF Warn definition (see below)
Warn defintion:
ELEMENT>VALUE[,ELEMENT>VALUE[...]]
Gives a warning result if ELEMENT is above VALUE percent CPU utilization.
Multiple defintions, comma separated, are allowed.
Example: idle>90,system>40
Gives a warning, if idle util > 90 %, or system util > 40 %, or both.
END
}