-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest-reload
executable file
·131 lines (106 loc) · 3.14 KB
/
request-reload
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
#!/usr/bin/env perl
#
# $Id$
#
use strict;
use HOSTDB;
use Config::IniFiles;
use JEvent;
use XML::Simple;
use Getopt::Long;
use vars qw ($opt_source $opt_requestor $opt_requestorhost $opt_debug $opt_help);
my $res = GetOptions (
"source=s",
"requestor=s",
"requestorhost=s",
"debug",
"help"
);
my $debug = defined ($opt_debug);
my @searchfor = @ARGV;
if ($#searchfor == -1 || $opt_help) {
die (<<EOH);
Syntax: $0 [options] zone or subnet ...
Options :
--source string Claim 'string' as source of the reload request in the JEvent message
--requestor user Set requestor (should be username)
--requestorhost host Set requestor host
EOH
}
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my (@subnets, @zones);
foreach my $a (@ARGV) {
if ($hostdb->is_valid_ip ($a)) {
if ($a =~ /\.0+$/) {
# prolly intended subnet /24
$a .= '/24';
} else {
die ("$0: Argument '$a' is an IP address, not a subnet\n");
}
}
if ($hostdb->is_valid_subnet ($a)) {
my $s = $hostdb->findsubnet ($a);
if (defined ($s)) {
my $n = $s->subnet (); # make sure we get correctly formatted name
push (@subnets, $n);
} else {
die ("$0: Subnet '$a' not found\n");
}
} elsif ($hostdb->clean_domainname ($a)) {
my $z = $hostdb->findzonebyname ($a);
if (defined ($z)) {
my $n = $z->zonename (); # make sure we get correctly formatted name
if ($z->delegated () eq 'Y') {
warn ("$0: Skipping delegated zone '$n'\n");
next;
}
push (@zones, $n);
} else {
die ("$0: Zone '$a' not found\n");
}
} else {
die ("$0: Argument '$a' is neither subnet nor domain\n");
}
}
my $hostdbini = $hostdb->inifile ();
my $jevent_ini = Config::IniFiles->new(-file=> HOSTDB::get_inifile('JEvent'));
my $je = JEvent->new (Config => $jevent_ini);
$je->Connect ();
my $requestor = $opt_requestor;
if (! $requestor) {
$requestor = getpwuid($<);
$requestor = "uid $<" unless ($requestor);
}
my $requestorhost = $opt_requestorhost || Sys::Hostname::hostname();
my $source = $opt_source || 'request-reload';
request_reload ($je, $source, $requestor, $requestorhost, \@subnets, \@zones);
sub request_reload
{
my $je = shift;
my $source = shift;
my $requestor = shift;
my $requestorhost = shift;
my $subnets_ref = shift;
my $zones_ref = shift;
my $i = localtime () . " request-reload[$$]";
my %data = ('type' => 'activate-request',
'source' => $source,
'requestor' => $requestor,
'requestor-host' => $requestorhost,
'items' => {
'zone' => [sort @$zones_ref],
'subnet' => [@$subnets_ref]
}
);
my %XMLoptions = (RootName => 'hostdb',
AttrIndent => 1
);
my $xml = XMLout(\%data, %XMLoptions);
warn ("JEvent XML :\n$xml\n\n") if ($debug);
$je->Publish(Content => $xml) or die ("$0: Failed publishing event\n");
print ("Requested reload of the following subnet(s) :\n ", join ("\n ", sort @$subnets_ref), "\n\n");
print ("Requested reload of the following zone(s) :\n ", join ("\n ", sort @$zones_ref), "\n\n");
return 1;
}