This repository has been archived by the owner on Jan 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBird.pm
102 lines (64 loc) · 2.63 KB
/
Bird.pm
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
#!/usr/bin/perl
use strict;
use warnings;
package Bird;
use Exporter qw/import/;
our @EXPORT_OK = qw//;
#-----------------------------------------------------------------------------
# Bird command module
use IO::Socket::UNIX qw/SOCK_STREAM/;
use constant BIRDC_PATH => '/usr/sbin/birdc';
use constant VALID_SETTINGS => (
'restrict',
'socket',
);
sub new {
my ($class, %args) = @_;
my $self = {
'settings' => {
'restrict' => 0,
'socket' => undef,
},
};
# Import any /valid/ settings specified in the constructor
map { $self->{'settings'}->{$_} = $args{$_} if defined($args{$_}) } &VALID_SETTINGS if %args;
bless($self,$class);
return $self;
}
sub long_cmd {
my ($self, $command) = @_;
my @arguments = ( BIRDC_PATH, '-v' );
push( @arguments, '-r' ) if( $self->{'settings'}->{'restrict'} );
push( @arguments, '-s'.$self->{'settings'}->{'socket'} ) if( defined $self->{'settings'}->{'socket'} );
push( @arguments, $command );
open( BIRD, "-|", @arguments ) || die "Connection failed: $@";
my @result = <BIRD>;
close( BIRD );
chomp @result;
# Check the header data
die "Bad 'hello' received" unless( scalar @result > 0 );
die "Bad 'hello' received" unless( shift(@result) =~ m/^0001 BIRD 1\.[\d\.]+ ready\.$/ );
die "Bad 'hello' received" unless( $self->{'settings'}->{'restrict'} && shift(@result) =~ m/^0016 Access restricted$/ );
return @result;
}
sub cmd {
my ($self, $command) = @_;
return pop @{$self->long_cmd($command)};
}
1;
#-----------------------------------------------------------------------------
# POD Documentation
__END__
=head1 NAME
Bird - Library for communicating with the BIRD routing daemon
=head1 DESCRIPTION
Bird provides a simple API to communicate with the BIRD routing daemon.
This module is intended as a drop-in replacement for the Birdctl module available at:
https://github.com/stephank/nagios-bird
Unlike Birdctl, this module uses the commandline tool birdc for interacting with BIRD.
=head1 TODO
Finish writing the POD documentation for this module
=head1 COPYRIGHT AND LICENSE
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.