-
Notifications
You must be signed in to change notification settings - Fork 1
/
housekeep-dnszone
executable file
·139 lines (106 loc) · 3.21 KB
/
housekeep-dnszone
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
132
133
134
135
136
137
138
139
#!/usr/local/bin/perl -w
#
# $Id$
#
# script to check all host entrys and make sure their DNS zone is correct
#
use strict;
use Config::IniFiles;
#use lib 'blib/lib';
use HOSTDB;
use Getopt::Std;
use vars qw ($opt_h $opt_d $opt_q $opt_v $opt_n);
use FileHandle;
getopts ('hdqvn');
my $debug = defined ($opt_d);
my $quiet = defined ($opt_q);
my $verbose = defined ($opt_v);
my $dry_run = defined ($opt_n);
if ($opt_h) {
die(<<EOT);
Syntax: $0 [options] zones
options:
-d debug
-q quiet mode
-v verbose mode
-n dry run
EOT
}
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my (@all_zonenames);
foreach my $zone ($hostdb->findallzones ()) {
push (@all_zonenames, $zone->zonename ());
}
my $changed_count = 0;
my $total_count = 0;
my $failed_count = 0;
foreach my $host ($hostdb->findallhosts ()) {
$total_count++;
my $hostname = $host->hostname ();
if ($host->manual_dnszone () eq 'Y') {
# host has auto-DNS-zone disabled, probably glue
warn ("Skipping host $hostname, manual DNS zone maintenance\n") unless $quiet;
next;
}
next unless defined ($hostname);
do_object ($hostdb, $host, $hostname, 'host', \@all_zonenames, \$changed_count, \$failed_count, $verbose);
}
my $t = '';
$t = '(dry run) ' if ($dry_run);
print ("Changed DNS zone on $changed_count ${t}out of $total_count hosts, $failed_count failed.\n") unless $quiet;
$changed_count = 0;
$total_count = 0;
$failed_count = 0;
foreach my $alias ($hostdb->findallhostaliases ()) {
$total_count++;
my $aliasname = $alias->aliasname ();
next unless defined ($aliasname);
do_object ($hostdb, $alias, $aliasname, 'alias', \@all_zonenames, \$changed_count, \$failed_count, $verbose);
}
$t = '';
$t = '(dry run) ' if ($dry_run);
print ("Changed DNS zone on $changed_count ${t}out of $total_count host aliases, $failed_count failed.\n") unless $quiet;
exit (1) if ($failed_count);
exit (0);
sub do_object
{
my $hostdb = shift;
my $o = shift;
my $name = shift;
my $type = shift;
my $all_zonenames_ref = shift;
my $changed_ref = shift;
my $failed_ref = shift;
my $verbose = shift;
my $res = 1;
my $correct_zonename = $hostdb->findzonenamebyhostname ($name, @{$all_zonenames_ref}) || 'NULL';
my $object_zonename = $o->dnszone ();
my $id = $o->id ();
if (! $object_zonename) {
warn ("$type $id ($name) has no DNS zone set\n") if ($verbose);
return undef;
}
if ($object_zonename ne $correct_zonename) {
warn ("Changing DNS zone on $type $name (id $id) from '$object_zonename' to '$correct_zonename'\n");
my $valid = 1;
$o->dnszone ($correct_zonename) or warn ("Could not change zone from '$object_zonename' to '$correct_zonename' on $type $id ($name): $o->{error}\n"), $valid = 0;
if ($valid) {
$$changed_ref++;
if (! $dry_run) {
if ($o->commit ()) {
warn ("Changed zone for $type $id ($name) from '$object_zonename' to '$correct_zonename'\n") if ($debug);
} else {
warn ("Commit on $type $id ($name) failed: $o->{error}\n");
$$failed_ref++;
$res = 0;
}
}
}
}
if ($object_zonename eq 'NULL' and $verbose) {
warn ("$type $name (id $id) has empty DNS zone\n");
}
return $res;
}