Thursday, February 15, 2007

Server list creation

While I was working at International Paper I became accustomed to a server tracking file called 'masterfile'. I used it many times in scripts to parse out data about individual servers or compile lists of servers to administer. Since then I have used a similar format here at Grumman to track my boxes and run scripts against them and have put together this Perl script to do it. The syntax can use Perl regular expressions to make it simple to create your lists. You will need Term::ANSIColor module from CPAN or delete the colored output and remove the module from the code. Also your masterfile should be '::' separated with the hostname as the first field. If you would like to see a dummy masterfile email me.

Use this script at your own will I assume no responsibility.

#!/usr/bin/perl

# master.pl - script to parse masterfile
#
# M.Baxter
# Version .90
######################################
######################################

# Complain about undeclared variables
use strict;
use warnings;

# Used modules
use Getopt::Long;
use File::Basename;
use Term::ANSIColor;

# Initialise global variables
our($opt_h, $opt_l, $opt_f, $opt_v, $opt_e);
our $script = basename($0);

# Declare subroutines
sub init;
sub usage;
sub list_servers;
sub full_master;

# Get the options
Getopt::Long::Configure('bundling');
GetOptions(
'v|verbose' => \$opt_v,
'h|help' => \$opt_h,
'f|full' => \$opt_f,
'l|list=s' => \$opt_l,
'e|exclude=s' => \$opt_e,
);

# Main
($opt_h) && usage;
($opt_f) || ($opt_l) || usage;
($opt_f) && full_master;
($opt_l) && list_servers;

# Subroutines
sub usage {
print `clear`, "\n\n";
print "Usage: \n\n";
print " -h : This Help Message\n";
print " -f : full masterfile in csv format created in scripts home dir.\n";
print " -l : list to be run i.e linux|sun|aix or any regular expression to search for.\n";
print " -e : server or regular expression to exclude from list.\n";
print " -v : Verbose output to full and list options.\n\n";
print color('bold green'), "example: $script -l linux -v\n\n\n";
print color('reset');
exit;
}

sub list_servers {
open (MASTER, "/usr/global/configs/masterfile") or die "cant open masterfile: $! \n";
while () {
if (/$opt_l/i) {
if($opt_e) {
if(! $opt_v) {
my @host = split(/:/);
print color('bold green'), "$host[0]\n" if(!m/$opt_e/i and !m/^#/);
} else {
print if(!m/$opt_e/i and !m/^#/);
}
} else {
if(! $opt_v) {
my @host = split(/:/);
print color('bold green'), "$host[0]\n" if(!m/^#/);
} else {
print if(!m/^#/);
}

}
}
}
close (MASTER);
print color('reset'), "\n";
exit;
}

sub full_master {
open (MASTERCSV, ">/usr/global/configs/masterfile.csv") or die "cant open masterfile.csv: $! \n";
open (MASTER, "/usr/global/configs/masterfile") or die "cant open masterfile: $! \n";
print MASTERCSV "Hostname,MachineType,Distribution,Application,Location,serialno,MACHINEMODEL,";
print MASTERCSV "CPUno,OSLEVEL,MEMORY,Startup_Order,Console\n";
while () {
s/:/,/g;
if (!m/^#/) {
print $_ if $opt_v;
print MASTERCSV;
}
}
close (MASTERCSV);
close (MASTER);
exit;
}

1 comment:

Anonymous said...

Thanks for writing this.