-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_cpu_temp.pl
executable file
·159 lines (138 loc) · 4.28 KB
/
check_cpu_temp.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
my $sensors = '/usr/bin/sensors';
my $sensors_detect = '/usr/sbin/sensors-detect';
my %E_STATE = (
'OK' => 0,
'WARNING' => 1,
'CRITICAL' => 2,
'UNKNOWN' => 3,
);
my ( $warning, $critical, $sudo, $help );
GetOptions(
'w|warning=i' => \$warning,
'c|critical=i' => \$critical,
'sensors' => \$sensors,
'sensors-detect' => \$sensors_detect,
's|sudo' => \$sudo,
'h|help' => \$help,
);
return usage() if $help;
sub usage {
print <<"END_USAGE";
Usage:
$0 [ options ]
Options:
-w|--warning - WARNING_CPU_TEMP. Default value
-c|--critical - CRITICAL_CPU_TEMP
-s|--sudo - Use sudo for sensors and sensors-detect commands
-h|--help - this help information
--sensors - Set path to sensors util. Default /usr/sbin/sensors
--sensors-detect - Set path to sensors-detect util. Default /usr/sbin/sensors-detect
WARNING_CPU_TEMP - default value from sensors output, if not def = 80;
CRITICAL_CPU_TEMP - default value from sensors output, if not def = 100;
Plugin depends from lm_sensors package.
END_USAGE
exit $E_STATE{'UNKNOWN'};
}
# validate args and prepare data
if ( $> and !$sudo ) {
print "you are not root\n";
exit $E_STATE{'UNKNOWN'};
}
my $no_bin;
if ( !-X $sensors ) {
$no_bin .= "sensors not found, please set path to sensors\n";
}
if ( !-X $sensors_detect ) {
$no_bin .= "sensors-detect not found, please set path to sensors-detect\n";
}
if ($no_bin){
print "$no_bin\n";
usage();
}
my $exit_status = 'OK';
my $status_string = '';
my $perfdata_string = '';
my $reg_t = qr/\+([\d\.]+)/;
my $reg_dg = qr/\xc2\xb0C/;
my $reg = qr/$reg_t$reg_dg\s+?(\(high\s=\s$reg_t$reg_dg,\s+crit\s=\s$reg_t$reg_dg\))/;
sub return_status {
my $request_status = shift;
if ( $request_status eq 'WARNING' ) {
return if $exit_status eq 'CRITICAL';
}
if ( $request_status eq 'UNKNOWN' ) {
return if $exit_status eq 'WARNING';
return if $exit_status eq 'CRITICAL';
}
$exit_status = $request_status;
}
sub make_sensors_conf {
system "echo -e | sudo $sensors_detect > /dev/null";
}
# sensors | grep "°C" | sed -r 's/:[ ]+[+]?([0-9.]+)°C.*$/=\1/;s/ /_/g;
my @sensors_data = `$sensors | grep "°C\\|coretemp-isa"`;
if (!grep { /^(Physical|Core)/ } grep {/$reg/} @sensors_data){
if ( -f '/etc/sysconfig/lm_sensors' ){
my $conf = `cat /etc/sysconfig/lm_sensors`;
if ($conf =~ m/Run sensors-detect to generate this config file/) {
make_sensors_conf;
@sensors_data = `$sensors | grep "°C\\|coretemp-isa"`;
}
elsif (!$sudo) {
system 'mv /etc/sysconfig/lm_sensors{,.bkp}';
make_sensors_conf;
@sensors_data = `$sensors | grep "°C\\|coretemp-isa"`;
system 'mv /etc/sysconfig/lm_sensors{.bkp,}';
}
}
else{
make_sensors_conf;
@sensors_data = `$sensors | grep "°C\\|coretemp-isa"`;
}
}
my %cpus;
my $cpu = 0;
for my $line ( @sensors_data ){
my ($name, $data);
($name, $data) = split /:\s+/, $line;
$name =~ s/\s+//g;
$cpu = sprintf("%d", $1)
if $line =~ m/coretemp-isa-/
&& $line =~ m/(\d+)$/;
next if $name !~ m/^\s?(Physical|Core)/;
if ( $cpus{$cpu}{$name} ){
foreach my $cp (sort keys %cpus){
if ($cpus{$cp}{$name}){
$cpu ++;
last;
}
}
}
$cpus{$cpu}{$name} = 1;
my ($temp, $crit, $warn);
$data =~ $reg;
($temp, $warn, $crit) = ( int $1, int $3, int $4);
$warning = $warning ? $warning :
defined $warn ? $warn : 80;
$critical = $critical ? $critical :
defined $crit ? $crit : 100;
$name = "cpu$cpu.". lc $name;
$perfdata_string .= "$name=$temp;$warning;$critical;; ";
if ( $temp >= $critical ){
$status_string .= "$name: temperature $temp is critical\n";
return_status('CRITICAL');
}
elsif ( $temp >= $warning ){
$status_string .= "$name: temperature $temp is warning\n";
return_status('WARNING');
}
}
print "$exit_status";
print ": \n$status_string" if $status_string;
print "|$perfdata_string\n";
exit $E_STATE{$exit_status};
1;