| 1 | ########################################################################### | ||||||||||
| 2 | # # | ||||||||||
| 3 | # Nagios::Cmd::Read # | ||||||||||
| 4 | # Written by Albert Tobey <albert.tobey@priority-health.com> # | ||||||||||
| 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 | ▶ | use vars qw( @ISA $do_lock $do_seek ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 21 | ▶ | use Fcntl qw(:flock SEEK_SET); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 21 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 21 | ▶ | use Symbol; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 21 | ▶ | use POSIX qw( O_RDONLY O_NONBLOCK O_EXCL ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 21 | @ISA = qw( Exporter Nagios::Cmd ); | |||||||||
| 25 | |||||||||||
| 26 | 21 | $do_lock = undef; | |||||||||
| 27 | 21 | $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 | 21 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 72 | 21 | ▶ | croak "$_[1] is not a pipe!" unless ( -p $_[1] ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 21 | 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 | 21 | ▶ | sub new_anyfile { | ||||||||
|
Subroutine
new_anyfile: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||
| 85 | 21 | my( $type, $cmdfile ) = @_; | |||||||||
| 86 | 21 | ▶ | croak "$cmdfile does not exist!" unless ( -e $cmdfile ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | |||||||||||
| 88 | # open it as much like Nagios does as is possible from perl | ||||||||||
| 89 | 21 | my $fh = gensym; | |||||||||
| 90 | 21 | ▶ | sysopen( $fh, $cmdfile, O_RDONLY|O_NONBLOCK|O_EXCL ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | || croak "could not sysopen $cmdfile for reading: $!"; | ||||||||||
| 92 | |||||||||||
| 93 | 21 | 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 | 819 | ▶ | sub readcmd { | ||||||||
|
Subroutine
readcmd: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||
| 107 | 819 | my $self = shift; | |||||||||
| 108 | |||||||||||
| 109 | 819 | ▶ | flock( $$self, LOCK_EX ) if ( $do_lock ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | |||||||||||
| 111 | 819 | ▶ | sysseek( $$self, 0, SEEK_SET ) if ( $do_seek ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 819 | my $rv = readline $$self; | |||||||||
| 113 | |||||||||||
| 114 | 819 | ▶ | flock( $$self, LOCK_UN ) if ( $do_lock ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | |||||||||||
| 116 | 819 | 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 | 0 | ▶ | sub seek { | ||||||||
|
Subroutine
seek: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 131 | 0 | ▶ | if ( !-f ${$_[0]} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 0 | carp "enabling seek for a named pipe or other special file!!!"; | |||||||||
| 133 | } | ||||||||||
| 134 | 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 | ▶ | sub lock { $do_lock = $_[1] } | ||||||||
|
Subroutine
lock: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 150 | |||||||||||
| 151 | =back | ||||||||||
| 152 | |||||||||||
| 153 | =head1 AUTHOR | ||||||||||
| 154 | |||||||||||
| 155 | Al Tobey <tobeya@cpan.org> | ||||||||||
| 156 | |||||||||||
| 157 | =cut | ||||||||||
| 158 | |||||||||||
| 159 | 21 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 160 | 21 | close( ${$_[0]} ); | |||||||||
| 161 | } | ||||||||||
| 162 | |||||||||||
| 163 | 21 | 1; | |||||||||