-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-dhcp-config
executable file
·305 lines (244 loc) · 7.92 KB
/
generate-dhcp-config
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/local/bin/perl -w
#
# $Id$
#
use strict;
use Config::IniFiles;
#use lib 'blib/lib';
use HOSTDB;
use Getopt::Std;
use vars qw ($opt_h $opt_d $opt_o $opt_q $opt_R);
use FileHandle;
getopts ('hdo:qR');
sub usage
{
die (<<EOD);
Syntax: $0 [options] [subnet ...]
subnet is the subnet in slash notation (eg. 192.168.1.0/24)
options :
-d debug
-o dir output to dir
-q quiet
-R remove old files for subnet
EOD
}
usage () if ($opt_h);
my $debug = defined ($opt_d);
my $quiet = defined ($opt_q);
my $remove_old = defined ($opt_R);
my $output_dir = $opt_o || '.';
$output_dir =~ s/\/$//o; # remove trailing slash
die ("$0: Output dir '$output_dir' is not a directory\n") if (! -d $output_dir);
my $hostdb = HOSTDB::DB->new (inifile => HOSTDB::get_inifile (),
debug => $debug
);
my @subnet_list = @ARGV;
push (@subnet_list, '*') if (0 == @subnet_list);
my (@subnets);
my $all_subnets = 0;
if (grep (/^\*$/, @subnet_list)) {
@subnets = $hostdb->findallsubnets ();
$all_subnets = 1;
} else {
foreach my $subnet_name (@subnet_list) {
push (@subnets, $hostdb->findsubnet ($subnet_name));
}
}
usage () if (! @subnets);
my $failed = 0;
my %all_generated_files;
foreach my $subnet (@subnets) {
die ("$0: Empty subnet, oh no.\n") if (! $subnet);
my %generated_files;
$failed = 1 if (! print_dhcp_config ($hostdb, $subnet, $output_dir, \%generated_files, $debug, $quiet));
if (! $failed and ! $all_subnets) {
$failed = 1 if (! remove_old_files ($subnet, $output_dir, \%generated_files,
$remove_old, $debug, $quiet));
}
last if ($failed);
foreach my $k (keys %generated_files) {
$all_generated_files{$k} = $generated_files{$k};
}
}
if (! $failed and $all_subnets) {
$failed = 1 if (! remove_old_files (undef, $output_dir, \%all_generated_files, $remove_old, $debug, $quiet));
}
exit ($failed);
sub print_dhcp_config
{
my $hostdb = shift;
my $subnet = shift;
my $output_dir = shift;
my $generated_files_ref = shift;
my $debug = shift;
my $quiet = shift;
my @subnet_profiles = split (',', $subnet->profilelist ());
my $subnet_name = $subnet->subnet ();
my $subnet_id = $subnet->id ();
if (! grep (/^default$/, @subnet_profiles)) {
warn ("$0: No 'default' profile for subnet '$subnet_name', skipping subnet.\n");
return undef;
}
my (@ranges, $range_start, $range_end, %hosts);
foreach my $host ($hostdb->findhostbyiprange ($subnet->netaddr (), $subnet->broadcast ())) {
if ($host->dhcpmode () eq 'DYNAMIC') {
next if ($host->dhcpstatus () eq 'DISABLED');
# XXX $host->profile () is not considered when dhcpmode is dynamic - should it?
$range_start = $host->ip () if (! $range_start);
$range_end = $host->ip ();
next;
} else {
my $id = $host->id ();
my $ip = $host->ip ();
my $hostname = $host->hostname () || 'NULL';
if ($range_start) {
# before this host, we were processing a range of dynamic hosts. finish that range.
push (@ranges, "${range_start} ${range_end}");
$range_start = '';
}
# continue processing this host
my $profile = $host->profile ();
if (! grep (/^$profile$/, @subnet_profiles)) {
warn ("$0: Host $id ($ip, $hostname) profile $profile not listed for subnet $subnet_name, skipping subnet.");
return undef;
}
if (! $hostdb->is_valid_profilename ($profile)) {
warn ("$0: Invalid profile name '$profile' for host $id ($ip, $hostname), skipping subnet.\n");
return undef;
} else {
push (@{$hosts{$profile}}, $host);
}
}
}
# check if we were constructing a range statement when the loop above ended
if ($range_start) {
push (@ranges, "${range_start} ${range_end}");
$range_start = '';
}
my $fn = "${subnet_name}_id${subnet_id}_options";
my $OUT = new FileHandle;
$fn =~ s#/#-#go; # replace slash(es) with - (dash)
print ("Building $fn\n") unless ($quiet);
my $outfile = "$output_dir/$fn";
open ($OUT, "> $outfile.new") or warn ("$0: Could not open outfile '$outfile.new' for writing: $!\n"), return undef;
if (-f $outfile) {
# copy leading comments from existing file
open (IN, "< $outfile") or warn ("$0: Could not open existing '$outfile' for reading: $!\n"), return undef;
while (my $t = <IN>) {
if ($t =~ /^#/) {
print ($OUT $t);
next;
} elsif ($t =~ /^\s*$/) {
print ($OUT $t);
last;
}
last;
}
close (IN);
} else {
# new file
print ($OUT "# \$Id\$\n# DON'T EDIT THIS FILE, IT IS AUTO GENERATED AND WILL GET OVERWRITTEN\n#\n\n");
}
my $t;
foreach $t (@ranges) {
print ($OUT " range $t;\n") or warn ("$0: Could not write to file '$outfile.new': $!\n"), return undef; ;
}
close ($OUT);
rename ("$outfile.new", $outfile) or warn ("$0: Could not rename '$outfile.new' to '$outfile': $!\n"), return undef;
$generated_files_ref->{$fn} = 1;
foreach my $profile (@subnet_profiles) {
my $fn = "${subnet_name}_id${subnet_id}-${profile}";
$fn =~ s#/#-#go; # replace slash(es) with - (dash)
print ("Building $fn\n") unless ($quiet);
my $outfile = "$output_dir/$fn";
my $OUT = new FileHandle;
open ($OUT, "> $outfile.new") or warn ("$0: Could not open outfile '$outfile.new' for writing: $!\n"), return undef;
if (-f $outfile) {
# copy leading comments from existing file
open (IN, "< $outfile") or warn ("$0: Could not open existing '$outfile' for reading: $!\n"), return undef;
while (my $t = <IN>) {
if ($t =~ /^#/) {
print ($OUT $t);
next;
} elsif ($t =~ /^\s*$/) {
print ($OUT $t);
last;
}
last;
}
close (IN);
} else {
# new file
print ($OUT "# \$Id\$\n# DON'T EDIT THIS FILE, IT IS AUTO GENERATED AND WILL GET OVERWRITTEN\n#\n\n");
}
my $t_count = 0;
foreach my $host (@{$hosts{$profile}}) {
print_host ($OUT, $host, $debug) or warn ("$0: Could not write to file '$outfile.new': $!\n"), return undef;
$t_count++;
}
warn ("$0: No hosts in $subnet_name profile $profile\n") if (! $t_count and ! $quiet);
$generated_files_ref->{$fn} = 1;
close ($OUT);
rename ("$outfile.new", $outfile) or warn ("$0: Could not rename '$outfile.new' to '$outfile': $!\n");
}
return 1;
}
sub remove_old_files
{
my $subnet = shift;
my $output_dir = shift;
my $generated_files_ref = shift;
my $do_remove = shift;
my $debug = shift;
my $quiet = shift;
opendir (DIR, $output_dir) or warn ("$0: Could not opendir () '$output_dir': $!\n"), return 0;
my @files;
if (defined ($subnet)) {
my $file_regexp = $subnet->subnet () . "_id" . $subnet->id () . "-";
$file_regexp =~ s#/#-#go; # replace slash(es) with - (dash)
@files = grep { /^$file_regexp/ } readdir DIR;
} else {
@files = readdir DIR;
}
close (DIR);
foreach my $fn (@files) {
next if ($fn eq "filelist");
my $file = "$output_dir/$fn";
next unless (-f $file); # skip non-files
if (! defined ($generated_files_ref->{$fn})) {
if ($do_remove) {
print ("Removing '$file'\n");
unlink ($file) or warn ("$0: Could not unlink () '$file': $!\n"), return 0;
} else {
print ("NOT removing stale file '$file'\n") unless ($quiet);
}
}
}
return 1;
}
sub print_host
{
my $OUT = shift;
my $host = shift;
my $debug = shift;
my $hostname = $host->hostname () || 'NULL';
my $id = $host->id ();
my $ip = $host->ip ();
my $mac = $host->mac_address ();
if ($host->dhcpstatus () eq 'DISABLED') {
warn ("Host with ID $id (IP $ip, hostname $hostname) has DHCP DISABLED, skipping\n") if ($debug);
return 1;
}
if (! defined ($ip)) {
warn ("Host with ID $id (hostname $hostname) has NO IP ADDRESS, skipping\n") if ($debug);
return 1;
}
if (! defined ($mac)) {
warn ("Host with ID $id (hostname $hostname) has NO MAC ADDRESS, skipping\n") if ($debug);
return 1;
}
print ($OUT <<EOD) or return undef;
host ${hostname}_${id} { hardware ethernet $mac; fixed-address $ip; option host-name \"$hostname\"; }
EOD
return 1;
}