line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hardware::UPS::Perl::General; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
#============================================================================== |
4
|
|
|
|
|
|
|
# package description: |
5
|
|
|
|
|
|
|
#============================================================================== |
6
|
|
|
|
|
|
|
# This package defines the following subroutines to be used in Perl scripts |
7
|
|
|
|
|
|
|
# dealing with an UPS. For a detailed description see the pod documentation |
8
|
|
|
|
|
|
|
# included at the end of this file. |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# Variables: |
11
|
|
|
|
|
|
|
# ---------- |
12
|
|
|
|
|
|
|
# $UPSERROR - the global error text |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
# Subroutines: |
15
|
|
|
|
|
|
|
# ------------ |
16
|
|
|
|
|
|
|
# &InitWE - initializing working environment |
17
|
|
|
|
|
|
|
# &Catch - signal handler |
18
|
|
|
|
|
|
|
# &Error - displaying error messages and exit |
19
|
|
|
|
|
|
|
# &Warning - displaying warning messages |
20
|
|
|
|
|
|
|
# &ManPage - displaying man page of `UPSSCRIPT' |
21
|
|
|
|
|
|
|
# &Version - displaying version information of `UPSSCRIPT' |
22
|
|
|
|
|
|
|
# &SetLogger - setting the logger |
23
|
|
|
|
|
|
|
# &SetPID - setting the PID object |
24
|
|
|
|
|
|
|
# &ConnectUPS - connecting to the UPS |
25
|
|
|
|
|
|
|
# |
26
|
|
|
|
|
|
|
#============================================================================== |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
#============================================================================== |
29
|
|
|
|
|
|
|
# Copyright: |
30
|
|
|
|
|
|
|
#============================================================================== |
31
|
|
|
|
|
|
|
# Copyright (c) 2007 Christian Reile, . All |
32
|
|
|
|
|
|
|
# rights reserved. This program is free software; you can redistribute it |
33
|
|
|
|
|
|
|
# and/or modify it under the same terms as Perl itself. |
34
|
|
|
|
|
|
|
#============================================================================== |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#============================================================================== |
37
|
|
|
|
|
|
|
# Entries for Revision Control: |
38
|
|
|
|
|
|
|
#============================================================================== |
39
|
|
|
|
|
|
|
# Revision : $Revision: 1.15 $ |
40
|
|
|
|
|
|
|
# Author : $Author: creile $ |
41
|
|
|
|
|
|
|
# Last Modified On: $Date: 2007/04/17 19:46:00 $ |
42
|
|
|
|
|
|
|
# Status : $State: Exp $ |
43
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
44
|
|
|
|
|
|
|
# Modifications : |
45
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
46
|
|
|
|
|
|
|
# |
47
|
|
|
|
|
|
|
# $Log: General.pm,v $ |
48
|
|
|
|
|
|
|
# Revision 1.15 2007/04/17 19:46:00 creile |
49
|
|
|
|
|
|
|
# documentation bugfixes. |
50
|
|
|
|
|
|
|
# |
51
|
|
|
|
|
|
|
# Revision 1.14 2007/04/14 09:37:26 creile |
52
|
|
|
|
|
|
|
# documentation update. |
53
|
|
|
|
|
|
|
# |
54
|
|
|
|
|
|
|
# Revision 1.13 2007/04/07 15:18:20 creile |
55
|
|
|
|
|
|
|
# new function ConnectUPS() added; |
56
|
|
|
|
|
|
|
# adaptations to "best practices" style; |
57
|
|
|
|
|
|
|
# update of documentation. |
58
|
|
|
|
|
|
|
# |
59
|
|
|
|
|
|
|
# Revision 1.12 2007/03/13 17:04:09 creile |
60
|
|
|
|
|
|
|
# new subroutines SetLogger() and SetPID(); |
61
|
|
|
|
|
|
|
# prototypes removed; |
62
|
|
|
|
|
|
|
# restarting by catching signal HUP implemented. |
63
|
|
|
|
|
|
|
# |
64
|
|
|
|
|
|
|
# Revision 1.11 2007/03/03 21:14:31 creile |
65
|
|
|
|
|
|
|
# new variable $UPSERROR added; |
66
|
|
|
|
|
|
|
# adaptations to revised Constants.pm. |
67
|
|
|
|
|
|
|
# |
68
|
|
|
|
|
|
|
# Revision 1.10 2007/02/05 20:33:17 creile |
69
|
|
|
|
|
|
|
# pod documentation revised. |
70
|
|
|
|
|
|
|
# |
71
|
|
|
|
|
|
|
# Revision 1.9 2007/02/04 19:10:25 creile |
72
|
|
|
|
|
|
|
# bug fix of pod documentation. |
73
|
|
|
|
|
|
|
# |
74
|
|
|
|
|
|
|
# Revision 1.8 2007/02/04 14:03:31 creile |
75
|
|
|
|
|
|
|
# bug fix in pod documentation. |
76
|
|
|
|
|
|
|
# |
77
|
|
|
|
|
|
|
# Revision 1.7 2007/02/03 16:03:58 creile |
78
|
|
|
|
|
|
|
# all variables moved to new package |
79
|
|
|
|
|
|
|
# Hardware::UPS::Perl::Constants; |
80
|
|
|
|
|
|
|
# subroutine SendMail() incorporated into new package |
81
|
|
|
|
|
|
|
# Hardware::UPs::Perl::Logging; |
82
|
|
|
|
|
|
|
# subroutines WritePIDFile() and DeletePIDFile() removed |
83
|
|
|
|
|
|
|
# because of OO PID file handling; |
84
|
|
|
|
|
|
|
# cleanup for unnecessary packages; |
85
|
|
|
|
|
|
|
# update of documentation. |
86
|
|
|
|
|
|
|
# |
87
|
|
|
|
|
|
|
# Revision 1.6 2007/01/28 21:05:47 creile |
88
|
|
|
|
|
|
|
# exclusion of signal TERM from error handling in subroutine |
89
|
|
|
|
|
|
|
# &Catch(). |
90
|
|
|
|
|
|
|
# |
91
|
|
|
|
|
|
|
# Revision 1.5 2007/01/28 05:26:44 creile |
92
|
|
|
|
|
|
|
# bug fix concerning pod documentation. |
93
|
|
|
|
|
|
|
# |
94
|
|
|
|
|
|
|
# Revision 1.4 2007/01/27 16:08:57 creile |
95
|
|
|
|
|
|
|
# rename to Hardware::UPS::Perl::General; |
96
|
|
|
|
|
|
|
# removal of unnecessary comments; |
97
|
|
|
|
|
|
|
# variables exported prepended by UPS. |
98
|
|
|
|
|
|
|
# |
99
|
|
|
|
|
|
|
# Revision 1.3 2007/01/21 15:05:09 creile |
100
|
|
|
|
|
|
|
# some beautifications. |
101
|
|
|
|
|
|
|
# |
102
|
|
|
|
|
|
|
# Revision 1.2 2007/01/20 16:05:34 creile |
103
|
|
|
|
|
|
|
# subroutine &SendMail() revised |
104
|
|
|
|
|
|
|
# |
105
|
|
|
|
|
|
|
# Revision 1.1 2007/01/20 08:10:54 creile |
106
|
|
|
|
|
|
|
# initial revision |
107
|
|
|
|
|
|
|
# |
108
|
|
|
|
|
|
|
# |
109
|
|
|
|
|
|
|
#============================================================================== |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
#============================================================================== |
112
|
|
|
|
|
|
|
# module preamble: |
113
|
|
|
|
|
|
|
#============================================================================== |
114
|
|
|
|
|
|
|
|
115
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
29
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
BEGIN { |
118
|
1
|
|
|
1
|
|
5
|
use Exporter (); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
119
|
1
|
|
|
1
|
|
4
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
133
|
|
120
|
|
|
|
|
|
|
|
121
|
1
|
|
|
1
|
|
8
|
$VERSION = sprintf( "%d.%02d", q$Revision: 1.15 $ =~ /(\d+)\.(\d+)/ ); |
122
|
|
|
|
|
|
|
|
123
|
1
|
|
|
|
|
12
|
@ISA = qw(Exporter); |
124
|
1
|
|
|
|
|
3
|
@EXPORT = qw( |
125
|
|
|
|
|
|
|
$UPSERROR |
126
|
|
|
|
|
|
|
&InitWE |
127
|
|
|
|
|
|
|
&Catch |
128
|
|
|
|
|
|
|
&Error |
129
|
|
|
|
|
|
|
&Warning |
130
|
|
|
|
|
|
|
&ManPage |
131
|
|
|
|
|
|
|
&Version |
132
|
|
|
|
|
|
|
&SetPID |
133
|
|
|
|
|
|
|
&SetLogger |
134
|
|
|
|
|
|
|
&ConnectUPS |
135
|
|
|
|
|
|
|
); |
136
|
1
|
|
|
|
|
1
|
@EXPORT_OK = qw(); |
137
|
1
|
|
|
|
|
26
|
%EXPORT_TAGS = qw(); |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
1
|
|
|
1
|
|
6
|
use vars @EXPORT, @EXPORT_OK; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
123
|
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
#============================================================================== |
144
|
|
|
|
|
|
|
# end of module preamble |
145
|
|
|
|
|
|
|
#============================================================================== |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
#============================================================================== |
148
|
|
|
|
|
|
|
# packages required: |
149
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
150
|
|
|
|
|
|
|
# |
151
|
|
|
|
|
|
|
# POSIX - Perl interface to IEEE Std 1003.1 |
152
|
|
|
|
|
|
|
# |
153
|
|
|
|
|
|
|
# Hardware::UPS::Perl::Connection - importing Hardware::UPS::Perl connection |
154
|
|
|
|
|
|
|
# Hardware::UPS::Perl::Constants - importing Hardware::UPS::Perl constants |
155
|
|
|
|
|
|
|
# Hardware::UPS::Perl::Driver - importing Hardware::UPS::Perl driver |
156
|
|
|
|
|
|
|
# |
157
|
|
|
|
|
|
|
#============================================================================== |
158
|
|
|
|
|
|
|
|
159
|
1
|
|
|
|
|
9
|
use POSIX qw( |
160
|
|
|
|
|
|
|
:signal_h sigprocmask |
161
|
1
|
|
|
1
|
|
1046
|
); |
|
1
|
|
|
|
|
7267
|
|
162
|
|
|
|
|
|
|
|
163
|
1
|
|
|
1
|
|
1901
|
use Hardware::UPS::Perl::Connection; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
51
|
|
164
|
1
|
|
|
|
|
90
|
use Hardware::UPS::Perl::Constants qw( |
165
|
|
|
|
|
|
|
UPSEXECUTABLE |
166
|
|
|
|
|
|
|
UPSSCRIPT |
167
|
1
|
|
|
1
|
|
958
|
); |
|
1
|
|
|
|
|
3
|
|
168
|
1
|
|
|
1
|
|
806
|
use Hardware::UPS::Perl::Driver; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
1359
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
#============================================================================== |
171
|
|
|
|
|
|
|
# defining exported variables: |
172
|
|
|
|
|
|
|
#============================================================================== |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
$UPSERROR = q{}; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
#============================================================================== |
177
|
|
|
|
|
|
|
# defining user invisible package variables: |
178
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
179
|
|
|
|
|
|
|
# |
180
|
|
|
|
|
|
|
# @SCRIPTARGUMENTS - list of arguments |
181
|
|
|
|
|
|
|
# $LOGGER - the logger object used |
182
|
|
|
|
|
|
|
# $PID - the PID object used |
183
|
|
|
|
|
|
|
# |
184
|
|
|
|
|
|
|
#============================================================================== |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
my @SCRIPTARGUMENTS = ( |
187
|
|
|
|
|
|
|
); |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
my $LOGGER = undef; |
190
|
|
|
|
|
|
|
my $PID = undef; |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
#============================================================================== |
193
|
|
|
|
|
|
|
# defining exported subroutines: |
194
|
|
|
|
|
|
|
#============================================================================== |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
sub InitWE { |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
# subroutine for initializing working environment for Perl scripts |
199
|
|
|
|
|
|
|
# dealing with an UPS |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
# the argument list |
202
|
0
|
|
|
0
|
|
|
@SCRIPTARGUMENTS = @ARGV; |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
# special signal case: hangup detected (restart) |
205
|
|
|
|
|
|
|
# |
206
|
|
|
|
|
|
|
# POSIX unmasks the sigprocmask properly |
207
|
0
|
|
|
|
|
|
my $sigset = POSIX::SigSet->new(); |
208
|
0
|
|
|
|
|
|
my $action = POSIX::SigAction->new(\&Catch, $sigset, &POSIX::SA_NODEFER); |
209
|
|
|
|
|
|
|
|
210
|
0
|
|
|
|
|
|
POSIX::sigaction(&POSIX::SIGHUP, $action); |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
# catching all other signals |
213
|
0
|
|
|
|
|
|
$SIG{ INT } = \&Catch; # Interrupt from keyboard |
214
|
0
|
|
|
|
|
|
$SIG{ QUIT } = \&Catch; # Quit from keyboard |
215
|
0
|
|
|
|
|
|
$SIG{ PIPE } = \&Catch; # Broken pipe: write to pipe with no readers |
216
|
0
|
|
|
|
|
|
$SIG{ TERM } = \&Catch; # Termination signal |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
} # end of subroutine "InitWE" |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
sub Catch { |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
# subroutine for catching signals and performing actions |
223
|
|
|
|
|
|
|
# |
224
|
|
|
|
|
|
|
# parameter: $signal (input) - signal to be caught |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# input as hidden local variable |
227
|
0
|
|
|
0
|
|
|
my $signal = shift; |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# hidden local variables |
230
|
0
|
|
|
|
|
|
my %signalHandler; # the signal handler |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
# setting up the signal handler |
233
|
|
|
|
|
|
|
%signalHandler = ( |
234
|
|
|
|
|
|
|
HUP => sub { # restarting |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
# deactivate signals |
237
|
|
|
|
|
|
|
DEACTIVATE: |
238
|
0
|
|
|
0
|
|
|
for my $sig (qw(HUP INT QUIT PIPE TERM)) { |
239
|
0
|
|
|
|
|
|
$SIG{$sig} = sub {}; |
|
0
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
# restoring signals |
243
|
0
|
|
|
|
|
|
my $s = POSIX::SigSet->new(); |
244
|
0
|
|
|
|
|
|
my $t = POSIX::SigSet->new(); |
245
|
0
|
|
|
|
|
|
sigprocmask(SIG_BLOCK, $s, $t); |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
# deleting PID file |
248
|
0
|
0
|
|
|
|
|
if (defined $PID) { |
249
|
0
|
|
|
|
|
|
$PID->delete(); |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
# restart |
253
|
0
|
0
|
|
|
|
|
if (defined $LOGGER) { |
254
|
0
|
|
|
|
|
|
$LOGGER->info("restarting ...") |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
0
|
0
|
|
|
|
|
exec ${\(UPSEXECUTABLE)} => @SCRIPTARGUMENTS |
|
0
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
or Error("restart failed -- $!"); |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
}, |
261
|
|
|
|
|
|
|
TERM => sub { # normal exit |
262
|
0
|
|
|
0
|
|
|
exit 0; |
263
|
|
|
|
|
|
|
}, |
264
|
0
|
|
|
|
|
|
); |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
# signal handling |
267
|
0
|
0
|
|
|
|
|
if (exists $signalHandler{$signal}) { |
268
|
0
|
|
|
|
|
|
$signalHandler{$signal}->(); |
269
|
|
|
|
|
|
|
} |
270
|
|
|
|
|
|
|
else { |
271
|
0
|
|
|
|
|
|
Error("caught a SIG$signal -- stopping execution"); |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
} # end of subroutine "Catch" |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
sub Error { |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
# subroutine for displaying any error message, cleaning up and exit |
279
|
|
|
|
|
|
|
# |
280
|
|
|
|
|
|
|
# parameter: $errorMessage (input) - error message to be displayed |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
# input as hidden local variable |
283
|
0
|
|
|
0
|
|
|
my $errorMessage = shift; |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
# displaying error message |
286
|
0
|
|
|
|
|
|
print STDERR "${\(UPSSCRIPT)}: $errorMessage\n"; |
|
0
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
# exiting with error |
289
|
0
|
|
|
|
|
|
exit 1; |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
} # end of subroutine "Error" |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
sub Warning { |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
# subroutine for displaying a warning message to STDERR without exiting |
296
|
|
|
|
|
|
|
# the program |
297
|
|
|
|
|
|
|
# |
298
|
|
|
|
|
|
|
# parameter: $warningMessage (input) - warning message to be displayed |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
# input as hidden local variable |
301
|
0
|
|
|
0
|
|
|
my $warningMessage = shift; |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
# displaying warning message |
304
|
0
|
|
|
|
|
|
print STDERR "${\(UPSSCRIPT)}: $warningMessage\n"; |
|
0
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
} # end of subroutine "Warning" |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
sub ManPage { |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
# subroutine for displaying the man page of the calling main program |
311
|
|
|
|
|
|
|
# and exiting without error |
312
|
|
|
|
|
|
|
|
313
|
|
|
|
|
|
|
# displaying man page |
314
|
0
|
|
|
0
|
|
|
CORE::system("pod2man $0 | groff -man -Tlatin1 | less"); |
315
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
# exiting without error |
317
|
0
|
|
|
|
|
|
exit 0; |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
} # end of subroutine "ManPage" |
320
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
sub Version { |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
# subroutine for displaying the version information on the calling Perl |
324
|
|
|
|
|
|
|
# script and exiting without error |
325
|
|
|
|
|
|
|
# |
326
|
|
|
|
|
|
|
# parameter: $version (input) - revision number |
327
|
|
|
|
|
|
|
# $date (input) - revison date |
328
|
|
|
|
|
|
|
# $description (input) - short description of calling script |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
# input as hidden local variables |
331
|
0
|
|
|
0
|
|
|
my $version = shift; |
332
|
0
|
|
|
|
|
|
my $date = shift; |
333
|
0
|
|
|
|
|
|
my $description = shift; |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
# displaying version information |
336
|
0
|
|
|
|
|
|
print <
|
337
|
0
|
|
|
|
|
|
${\(UPSSCRIPT)}, $description |
|
0
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
Version $version, $date |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
Copyright (c) 2007 by Christian Reile |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under the |
344
|
|
|
|
|
|
|
terms of the GNU General Public License as published by the Free Software |
345
|
|
|
|
|
|
|
Foundation. |
346
|
|
|
|
|
|
|
|
347
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but |
348
|
|
|
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
349
|
|
|
|
|
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
350
|
|
|
|
|
|
|
for more details. |
351
|
|
|
|
|
|
|
|
352
|
0
|
|
|
|
|
|
${\(UPSSCRIPT)}: For help, type `${\(UPSSCRIPT)} -h' or `${\(UPSSCRIPT)} --help'. |
|
0
|
|
|
|
|
|
|
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
EOF |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
# exiting without error |
357
|
0
|
|
|
|
|
|
exit 0; |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
} # end of subroutine "Version" |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
sub SetLogger { |
362
|
|
|
|
|
|
|
|
363
|
|
|
|
|
|
|
# subroutine to set the logging object |
364
|
|
|
|
|
|
|
# |
365
|
|
|
|
|
|
|
# parameters: $logger (input) - the logger |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
# input as hidden variable |
368
|
0
|
|
|
0
|
|
|
my $logger = shift; |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
# hidden local variables |
371
|
0
|
|
|
|
|
|
my $refType; # a reference type |
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
# checking logger |
374
|
0
|
0
|
|
|
|
|
if (defined $logger) { |
375
|
0
|
|
|
|
|
|
$refType = ref($logger); |
376
|
0
|
0
|
|
|
|
|
if ($refType ne "Hardware::UPS::Perl::Logging") { |
377
|
0
|
|
|
|
|
|
Error("no logger -- <$refType>"); |
378
|
|
|
|
|
|
|
} |
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
else { |
381
|
0
|
|
|
|
|
|
Error("no logger defined"); |
382
|
|
|
|
|
|
|
} |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
# setting logger |
385
|
0
|
|
|
|
|
|
$LOGGER = $logger; |
386
|
|
|
|
|
|
|
|
387
|
|
|
|
|
|
|
} # end of subroutine "SetLogger" |
388
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
sub SetPID { |
390
|
|
|
|
|
|
|
|
391
|
|
|
|
|
|
|
# subroutine to set the PID object |
392
|
|
|
|
|
|
|
# |
393
|
|
|
|
|
|
|
# parameters: $pidObject (input) - the PID object |
394
|
|
|
|
|
|
|
|
395
|
|
|
|
|
|
|
# input as hidden variable |
396
|
0
|
|
|
0
|
|
|
my $pidObject = shift; |
397
|
|
|
|
|
|
|
|
398
|
|
|
|
|
|
|
# hidden local variables |
399
|
0
|
|
|
|
|
|
my $refType; # a reference type |
400
|
|
|
|
|
|
|
|
401
|
|
|
|
|
|
|
# checking logger |
402
|
0
|
0
|
|
|
|
|
if (defined $pidObject) { |
403
|
0
|
|
|
|
|
|
$refType = ref($pidObject); |
404
|
0
|
0
|
|
|
|
|
if ($refType ne "Hardware::UPS::Perl::PID") { |
405
|
0
|
|
|
|
|
|
Error("no PID object -- <$refType>"); |
406
|
|
|
|
|
|
|
} |
407
|
|
|
|
|
|
|
} |
408
|
|
|
|
|
|
|
else { |
409
|
0
|
|
|
|
|
|
Error("no PID object defined"); |
410
|
|
|
|
|
|
|
} |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
# setting PID object |
413
|
0
|
|
|
|
|
|
$PID = $pidObject; |
414
|
|
|
|
|
|
|
|
415
|
|
|
|
|
|
|
} # end of subroutine "SetPID" |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
sub ConnectUPS { |
418
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
# subroutine to connect to the UPS |
420
|
|
|
|
|
|
|
# |
421
|
|
|
|
|
|
|
# parameters: $options (input) - anonymous hash; options |
422
|
|
|
|
|
|
|
# |
423
|
|
|
|
|
|
|
# The following option keys are recognized: |
424
|
|
|
|
|
|
|
# |
425
|
|
|
|
|
|
|
# Host ($) - the remote host; string; optional |
426
|
|
|
|
|
|
|
# TCPPort ($) - the TCP port to use; required, if host is specified |
427
|
|
|
|
|
|
|
# SerialPort ($) - the serial port to use; required, if host is not |
428
|
|
|
|
|
|
|
# specified |
429
|
|
|
|
|
|
|
# DebugLevel ($) - the debug level; natural number; optional |
430
|
|
|
|
|
|
|
# Driver ($) - the driver; string; required |
431
|
|
|
|
|
|
|
# Logger ($) - Hardware::UPS::Perl::Logging object; the logger to |
432
|
|
|
|
|
|
|
# use; required |
433
|
|
|
|
|
|
|
|
434
|
|
|
|
|
|
|
# input as hidden local variable |
435
|
0
|
|
|
0
|
|
|
my $options = shift; |
436
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
# hidden local variables |
438
|
0
|
|
|
|
|
|
my $refType; # a reference type |
439
|
|
|
|
|
|
|
my $host; # the remote host |
440
|
0
|
|
|
|
|
|
my $port; # the TCP or serial port |
441
|
0
|
|
|
|
|
|
my $debugLevel; # the debug level |
442
|
0
|
|
|
|
|
|
my $driverName; # the name of the driver to use |
443
|
0
|
|
|
|
|
|
my $logger; # the logger to use |
444
|
0
|
|
|
|
|
|
my $connectionType; # the connection type |
445
|
0
|
|
|
|
|
|
my $connectionOptions; # the connection options |
446
|
0
|
|
|
|
|
|
my $connection; # the connection object |
447
|
0
|
|
|
|
|
|
my $driver; # the driver object |
448
|
0
|
|
|
|
|
|
my $ups; # the UPS object |
449
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
# checking options |
451
|
0
|
|
|
|
|
|
$refType = ref($options); |
452
|
0
|
0
|
|
|
|
|
if ($refType ne 'HASH') { |
453
|
0
|
|
|
|
|
|
Error("not a hash reference -- <$refType>"); |
454
|
|
|
|
|
|
|
} |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
# processing options |
457
|
|
|
|
|
|
|
# |
458
|
|
|
|
|
|
|
# the host |
459
|
0
|
|
|
|
|
|
$host = delete $options->{Host}; |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
# the driver |
462
|
0
|
|
|
|
|
|
$driverName = delete $options->{Driver}; |
463
|
0
|
0
|
|
|
|
|
if (!defined $driverName) { |
464
|
0
|
|
|
|
|
|
Error("driver missing"); |
465
|
|
|
|
|
|
|
} |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
# the debug level |
468
|
0
|
|
|
|
|
|
$debugLevel = delete $options->{DebugLevel}; |
469
|
0
|
0
|
|
|
|
|
if (!defined $debugLevel) { |
470
|
0
|
|
|
|
|
|
$debugLevel = 0; |
471
|
|
|
|
|
|
|
} |
472
|
|
|
|
|
|
|
|
473
|
|
|
|
|
|
|
# the logger |
474
|
0
|
|
|
|
|
|
$logger = delete $options->{Logger}; |
475
|
0
|
0
|
|
|
|
|
if (!defined $logger) { |
476
|
0
|
|
|
|
|
|
Error("logger missing"); |
477
|
|
|
|
|
|
|
} |
478
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
# getting connection |
480
|
0
|
0
|
|
|
|
|
if (defined $host) { |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
# remotely via TCP |
483
|
0
|
|
|
|
|
|
$connectionType = "net"; |
484
|
|
|
|
|
|
|
|
485
|
0
|
|
|
|
|
|
$port = delete $options->{TCPPort}; |
486
|
0
|
0
|
|
|
|
|
if (!defined $port) { |
487
|
0
|
|
|
|
|
|
Error("TCP port missing"); |
488
|
|
|
|
|
|
|
} |
489
|
|
|
|
|
|
|
|
490
|
|
|
|
|
|
|
$connectionOptions = { |
491
|
0
|
|
|
|
|
|
Host => $host , |
492
|
|
|
|
|
|
|
TCPPort => $port , |
493
|
|
|
|
|
|
|
Logger => $logger, |
494
|
|
|
|
|
|
|
}; |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
} |
497
|
|
|
|
|
|
|
else { |
498
|
|
|
|
|
|
|
|
499
|
|
|
|
|
|
|
# locally via a serial port |
500
|
0
|
|
|
|
|
|
$connectionType = "serial"; |
501
|
|
|
|
|
|
|
|
502
|
0
|
|
|
|
|
|
$port = delete $options->{SerialPort}; |
503
|
0
|
0
|
|
|
|
|
if (!defined $port) { |
504
|
0
|
|
|
|
|
|
Error("serial port missing"); |
505
|
|
|
|
|
|
|
} |
506
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
$connectionOptions = { |
508
|
0
|
|
|
|
|
|
SerialPort => $port , |
509
|
|
|
|
|
|
|
Logger => $logger, |
510
|
|
|
|
|
|
|
}; |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
} |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
# getting connection |
515
|
0
|
|
|
|
|
|
$connection = Hardware::UPS::Perl::Connection->new({ |
516
|
|
|
|
|
|
|
Type => $connectionType , |
517
|
|
|
|
|
|
|
Options => $connectionOptions, |
518
|
|
|
|
|
|
|
Logger => $logger , |
519
|
|
|
|
|
|
|
}); |
520
|
0
|
0
|
|
|
|
|
if (!defined $connection) { |
521
|
0
|
|
|
|
|
|
Error("creating connection failed -- $UPSERROR"); |
522
|
|
|
|
|
|
|
} |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
# getting driver |
525
|
0
|
|
|
|
|
|
$driver = Hardware::UPS::Perl::Driver->new({ |
526
|
|
|
|
|
|
|
Driver => $driverName, |
527
|
|
|
|
|
|
|
Options => { |
528
|
|
|
|
|
|
|
Connection => $connection, |
529
|
|
|
|
|
|
|
Logger => $logger, |
530
|
|
|
|
|
|
|
}, |
531
|
|
|
|
|
|
|
Logger => $logger, |
532
|
|
|
|
|
|
|
}); |
533
|
0
|
0
|
|
|
|
|
if (!defined $driver) { |
534
|
0
|
|
|
|
|
|
Error("creating driver failed -- $UPSERROR"); |
535
|
|
|
|
|
|
|
} |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
# connecting to UPS |
538
|
0
|
|
|
|
|
|
$ups = $driver->getDriverHandle(); |
539
|
0
|
0
|
|
|
|
|
if (!defined $ups) { |
540
|
0
|
|
|
|
|
|
Error("creating UPS object failed -- ".$driver->getErrorMessage()); |
541
|
|
|
|
|
|
|
} |
542
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
# setting debug level |
544
|
0
|
|
|
|
|
|
$ups->setDebugLevel($debugLevel); |
545
|
|
|
|
|
|
|
|
546
|
|
|
|
|
|
|
# flushing UPS buffer |
547
|
0
|
|
|
|
|
|
$ups->flush(); |
548
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
# returning UPS object |
550
|
0
|
|
|
|
|
|
return $ups; |
551
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
} # end of subroutine "ConnectUPS" |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
#============================================================================== |
555
|
|
|
|
|
|
|
# package return: |
556
|
|
|
|
|
|
|
#============================================================================== |
557
|
|
|
|
|
|
|
1; |
558
|
|
|
|
|
|
|
|
559
|
|
|
|
|
|
|
__END__ |