line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
########################################################################### |
2
|
|
|
|
|
|
|
# # |
3
|
|
|
|
|
|
|
# Nagios::Cmd::Read # |
4
|
|
|
|
|
|
|
# Written by Albert Tobey # |
5
|
|
|
|
|
|
|
# Copyright 2003, Albert P Tobey # |
6
|
|
|
|
|
|
|
# # |
7
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify it # |
8
|
|
|
|
|
|
|
# under the terms of the GNU General Public License as published by the # |
9
|
|
|
|
|
|
|
# Free Software Foundation; either version 2, or (at your option) any # |
10
|
|
|
|
|
|
|
# later version. # |
11
|
|
|
|
|
|
|
# # |
12
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful, but # |
13
|
|
|
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of # |
14
|
|
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # |
15
|
|
|
|
|
|
|
# General Public License for more details. # |
16
|
|
|
|
|
|
|
# # |
17
|
|
|
|
|
|
|
########################################################################### |
18
|
|
|
|
|
|
|
package Nagios::Cmd::Read; |
19
|
21
|
|
|
21
|
|
20622
|
use vars qw( @ISA $do_lock $do_seek ); |
|
21
|
|
|
|
|
63
|
|
|
21
|
|
|
|
|
2142
|
|
20
|
21
|
|
|
21
|
|
126
|
use Fcntl qw(:flock SEEK_SET); |
|
21
|
|
|
|
|
21
|
|
|
21
|
|
|
|
|
3024
|
|
21
|
21
|
|
|
21
|
|
126
|
use Carp; |
|
21
|
|
|
|
|
42
|
|
|
21
|
|
|
|
|
1134
|
|
22
|
21
|
|
|
21
|
|
105
|
use Symbol; |
|
21
|
|
|
|
|
21
|
|
|
21
|
|
|
|
|
1323
|
|
23
|
21
|
|
|
21
|
|
105
|
use POSIX qw( O_RDONLY O_NONBLOCK O_EXCL ); |
|
21
|
|
|
|
|
42
|
|
|
21
|
|
|
|
|
189
|
|
24
|
|
|
|
|
|
|
@ISA = qw( Exporter Nagios::Cmd ); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
$do_lock = undef; |
27
|
|
|
|
|
|
|
$do_seek = undef; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
## |
30
|
|
|
|
|
|
|
## NOTE: the sys* functions are used to better resemble what Nagios actually does |
31
|
|
|
|
|
|
|
## and to avoid problems with buffering. Although seek() is useless when working |
32
|
|
|
|
|
|
|
## with fifo's, it is necessary when using a regular file. This is used for doing |
33
|
|
|
|
|
|
|
## testing and debugging. The seek() does not cause any problems when used on a |
34
|
|
|
|
|
|
|
## fifo. |
35
|
|
|
|
|
|
|
## |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 NAME |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Nagios::Cmd |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head1 DESCRIPTION |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
A module for reading a fifo or regular file similar to the way Nagios does. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 SYNOPSIS |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
use Nagios::Cmd; |
48
|
|
|
|
|
|
|
use Nagios::Cmd::Read; |
49
|
|
|
|
|
|
|
use POSIX qw/mkfifo/; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $fifo = '/var/tmp/test.pipe'; |
52
|
|
|
|
|
|
|
mkfifo( $fifo, 600 ); |
53
|
|
|
|
|
|
|
my $writer = Nagios::Cmd->new( $fifo ); |
54
|
|
|
|
|
|
|
my $reader = Nagios::Cmd::Read->new( $fifo ); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$writer->service_check('SSH', 'localhost', 0, 'version 1 waiting'); |
57
|
|
|
|
|
|
|
print $reader->readcmd(), " was written to $fifo\n"; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 METHODS |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=over 4 |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item new() |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Pass in the name of a fifo to open and read from. The fifo must already exist. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my $reader = Nagios::Cmd::Read->new( $fifo ); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub new { |
72
|
21
|
50
|
|
21
|
1
|
8547
|
croak "$_[1] is not a pipe!" unless ( -p $_[1] ); |
73
|
21
|
|
|
|
|
63
|
new_anyfile(@_); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=item new_anyfile() |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
Same as new, but can be any type of file such as regular files or /dev/null. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my $reader = Nagios::Cmd::Read->new_anyfile( '/tmp/commands.txt' ); |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=cut |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub new_anyfile { |
85
|
21
|
|
|
21
|
1
|
42
|
my( $type, $cmdfile ) = @_; |
86
|
21
|
50
|
|
|
|
315
|
croak "$cmdfile does not exist!" unless ( -e $cmdfile ); |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# open it as much like Nagios does as is possible from perl |
89
|
21
|
|
|
|
|
84
|
my $fh = gensym; |
90
|
21
|
50
|
|
|
|
777
|
sysopen( $fh, $cmdfile, O_RDONLY|O_NONBLOCK|O_EXCL ) |
91
|
|
|
|
|
|
|
|| croak "could not sysopen $cmdfile for reading: $!"; |
92
|
|
|
|
|
|
|
|
93
|
21
|
|
|
|
|
126
|
bless \$fh, $type; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item readcmd() |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Read a single command. Just like in Nagios, the filehandle is kept open for the |
99
|
|
|
|
|
|
|
duration of the program. If the target file is a regular file, sysseek() will |
100
|
|
|
|
|
|
|
be used to rewind to the top of the file (which may not be what you want). |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
To turn of seeking for regular files, call $object->seek(undef);. |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=cut |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub readcmd { |
107
|
819
|
|
|
819
|
1
|
966
|
my $self = shift; |
108
|
|
|
|
|
|
|
|
109
|
819
|
50
|
|
|
|
1428
|
flock( $$self, LOCK_EX ) if ( $do_lock ); |
110
|
|
|
|
|
|
|
|
111
|
819
|
50
|
|
|
|
1239
|
sysseek( $$self, 0, SEEK_SET ) if ( $do_seek ); |
112
|
819
|
|
|
|
|
4578
|
my $rv = readline $$self; |
113
|
|
|
|
|
|
|
|
114
|
819
|
50
|
|
|
|
1449
|
flock( $$self, LOCK_UN ) if ( $do_lock ); |
115
|
|
|
|
|
|
|
|
116
|
819
|
|
|
|
|
11907
|
return $rv; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item seek() |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
Turn seek on/off. Default is off. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
$reader->seek(undef); # turn off seeking |
124
|
|
|
|
|
|
|
$reader->seek(1); # turn it back on |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
Nagios::Cmd::Read::seek(undef); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=cut |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub seek { |
131
|
0
|
0
|
|
0
|
1
|
0
|
if ( !-f ${$_[0]} ) { |
|
0
|
|
|
|
|
0
|
|
132
|
0
|
|
|
|
|
0
|
carp "enabling seek for a named pipe or other special file!!!"; |
133
|
|
|
|
|
|
|
} |
134
|
0
|
|
|
|
|
0
|
$do_seek = $_[1] |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=item safe() |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Turn use of flock() on/off. Setting it to a defined value will enable |
140
|
|
|
|
|
|
|
flock()ing of filehandles before reading. Setting to undef turns it off. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
$reader->lock(1); # turn on use of flock() around reads |
143
|
|
|
|
|
|
|
$reader->lock(undef); # turn it off |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Nagios::Cmd::Read::lock(1); |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=cut |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
0
|
0
|
0
|
sub lock { $do_lock = $_[1] } |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=back |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 AUTHOR |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Al Tobey |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=cut |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub DESTROY { |
160
|
21
|
|
|
21
|
|
42
|
close( ${$_[0]} ); |
|
21
|
|
|
|
|
273
|
|
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
1; |