Listing 1. Script to Build Configuration File

#!/usr/bin/perl

my $munin_dir = '/etc/munin';
my $munin_config = 'munin.conf';
my $munin_config_temp = 'munin.conf.tmp';
my $node_port = '4949';
my $nmap = "nmap -oG - -p ";
my %subnets = (
                "10.1.1.0/24" => "VLAN1",
                "10.1.5.0/24" => "VLAN5",
                "10.1.6.0/24" => "VLAN6",
             );

# iterate through each subnet and perform the nmap scan
foreach $subnet (keys %subnets){
   open NMAP, "$nmap $node_port $subnet | grep open |"
   &rarrhk;or die "Can't run nmap: $!\n";
   while (&lt;NMAP>){
      $ip = $host = "";
# parse out the hostname and IP address
      /Host: (\d+\.\d+\.\d+\.\d+) \((.*?)\)/;
      $ip = $1; $host = $2;

      next if($ip eq "");

# sometimes nmap doesn't do rDNS properly,
# get it manually in that case
      if($host eq ""){
         $host = `dig -x $ip +short` or $host = $ip;
         chomp $host;
         $host =~ s/\.$//;
      }

      $munin_hosts{$host} = $ip;
   }
   close NMAP;
}

# output to a temp file in case munin
# runs while this script is open
open OUTFILE, "> $munin_dir/$munin_config_temp" or die "Can't open
$munin_dir/$munin_config_temp: $!\n";

# first print out the standard header for the munin file
print OUTFILE &lt;&lt;END_HEAD;
dbdir   /var/lib/munin
htmldir /var/www/munin
logdir  /var/log/munin
rundir  /var/run/munin
tmpldir /etc/munin/templates

END_HEAD

# then print out the config for each host
foreach $host (sort keys %munin_hosts){
   print OUTFILE "\[$host\]\n\taddress $host\n";
   # add any extra munin options for each host here
   print OUTFILE "\n";
}
close OUTFILE;

system("mv $munin_dir/$munin_config_temp $munin_dir/$munin_config");

