-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_ups_apc
177 lines (142 loc) · 3.78 KB
/
check_ups_apc
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
#!/bin/perl
# Erstellt 19.09.2022, Bernd Arnold
# Getestet mit einer APC USV Pro 900
use strict;
use warnings;
my $perfdata = "";
my $errorcount = 0;
my $warningcount = 0;
my @addinfo = ();
# # apcaccess status
# APC : 001,036,0872
# DATE : 2022-09-19 17:40:01 +0200
# HOSTNAME : proxmox4
# VERSION : 3.14.14 (31 May 2016) debian
# UPSNAME : APC900
# CABLE : USB Cable
# DRIVER : USB UPS Driver
# UPSMODE : Stand Alone
# STARTTIME: 2022-09-19 17:27:21 +0200
# MODEL : Back-UPS RS 900G
# STATUS : ONLINE NOBATT
# LINEV : 234.0 Volts
# LOADPCT : 0.0 Percent
# BCHARGE : 20.0 Percent
# TIMELEFT : 177.5 Minutes
# MBATTCHG : 5 Percent
# MINTIMEL : 3 Minutes
# MAXTIME : 0 Seconds
# SENSE : Medium
# LOTRANS : 176.0 Volts
# HITRANS : 294.0 Volts
# ALARMDEL : 30 Seconds
# BATTV : 27.1 Volts
# LASTXFER : No transfers since turnon
# NUMXFERS : 0
# TONBATT : 0 Seconds
# CUMONBATT: 0 Seconds
# XOFFBATT : N/A
# SELFTEST : NO
# STATFLAG : 0x01000008
# SERIALNO : ***
# BATTDATE : 2016-04-08
# NOMINV : 230 Volts
# NOMBATTV : 24.0 Volts
# NOMPOWER : 540 Watts
# FIRMWARE : 879.L4 .I USB FW:L4
# END APC : 2022-09-19 17:40:03 +0200
# When communication is lost:
# # apcaccess status
# APC : 001,018,0441
# DATE : 2024-11-03 22:07:42 +0100
# HOSTNAME : proxmox4
# VERSION : 3.14.14 (31 May 2016) debian
# UPSNAME : APC900
# CABLE : USB Cable
# DRIVER : USB UPS Driver
# UPSMODE : Stand Alone
# STARTTIME: 2024-11-03 22:07:32 +0100
# STATUS : COMMLOST
# MBATTCHG : 5 Percent
# MINTIMEL : 3 Minutes
# MAXTIME : 0 Seconds
# NUMXFERS : 0
# TONBATT : 0 Seconds
# CUMONBATT: 0 Seconds
# XOFFBATT : N/A
# STATFLAG : 0x05000100
# END APC : 2024-11-04 18:36:59 +0100
my @output = `apcaccess status`;
if ( $? != 0 ) {
print "Error when calling apcaccess.\n";
exit 3;
}
my %upsdata;
for my $line ( @output ) {
chomp $line;
my ( $item, $value ) = $line =~ /^(.*?) *: (.*?) *$/;
#print "<<<$item>>>\n";
#print "<<<$line>>>";
$upsdata{ $item } = $value;
}
# STATUS : COMMLOST
if ( defined $upsdata{'STATUS'} and $upsdata{'STATUS'} eq "COMMLOST" ) {
print "UNKNOWN: Communication to UPS lost.\n";
exit 3;
}
#print @output;
#print $upsdata{'LOADPCT'};
# LOADPCT : 0.0 Percent
if ( defined $upsdata{'LOADPCT'} ) {
my $load_pct = $upsdata{'LOADPCT'};
$load_pct =~ s/ Perc.*//;
$perfdata .= "loadpct=" . $load_pct . " ";
}
# BCHARGE : 20.0 Percent
if ( defined $upsdata{'BCHARGE'} ) {
my $battcharge_pct = $upsdata{'BCHARGE'};
$battcharge_pct =~ s/ Perc.*//;
if ( $battcharge_pct < 40 ) {
$errorcount++;
push @addinfo, "[CRITICAL] less than 40 % charged";
} elsif ( $battcharge_pct < 80 ) {
$warningcount++;
push @addinfo, "[WARNING] less than 80 % charged";
}
$perfdata .= "battchargepct=" . $battcharge_pct . " ";
}
# TIMELEFT : 177.5 Minutes
if ( defined $upsdata{'TIMELEFT'} ) {
my $timeleftminutes = $upsdata{'TIMELEFT'};
if ( $timeleftminutes =~ / Minutes$/ ) {
$timeleftminutes =~ s/ Minutes//;
$perfdata .= "timeleftminutes=" . $timeleftminutes . " ";
} else {
print "Cannot parse TIMELEFT.";
$warningcount++;
}
}
# STATUS : ONLINE NOBATT
# STATUS : COMMLOST
my $usvstatus = $upsdata{'STATUS'};
if ( $usvstatus ne "ONLINE" ) {
$errorcount++;
push @addinfo, "UPS not online";
}
my $statustext;
$statustext = "Communication to UPS lost" if $usvstatus eq "COMMLOST";
$statustext = "On battery" if $usvstatus eq "ONBATT";
$statustext = "Off" if $usvstatus eq "";
if ( $statustext ) {
print "USV-Status: $statustext ($usvstatus). ";
} else {
print "USV-Status: $usvstatus. ";
}
print $upsdata{'BCHARGE'} . " charged. ";
print "| $perfdata" if $perfdata;
print "\n";
print join ("\n", @addinfo) . "\n" if @addinfo;
exit 2 if $errorcount > 0;
exit 1 if $warningcount > 0;
exit 0 if $errorcount + $warningcount == 0;
exit 3;