#!/bin/perl -l 
##
## This version of the "massage" program was written entirely in perl to allow
## more manipulation of the final output. This version of massage can handle
## groups and schedules having blank spaces whereas the original was defeated
## by that situation. Correct usage of this script is:
#
#			./perlmassage | sort > myfile
##
## The script creates one comma delimited file which is sent to stdout. The
## list columns are: client name, schedule, group, save set, group start time,
## and daily backup level (seven columns Sun thru Sat).
##
## The script is setup only to work on clients with a weekly backup rotation.
## The script puts only one group and one save set per line in the final output.
## Thus if your client configuration has five databases all listed in one
## client (i.e. five save sets) being backed up twice daily (i.e. two groups)
## the final output will have ten lines for this client.
##
## I created this script to allow all this data to be easily brought into a
## spreadsheet, massaged, beautified, and presented to the muckity-mucks.
##
##
## The first line of the script should produce output similar to:
#
#			name: server1;
#			schedule: Standard_Wed;
#			group: Standard_Wed;
#			save set: All;
#
#			name: server2;
#			schedule: Standard_Sat;
#			group: Standard_Sat;
#			save set: All;
#
## and in this exact order, which my system does each time auto-magically.
## Test your system first to see if yours behaves like mine, if it doesn't
## some mods will be necessary. There are some assumptions in my script
## but with little (hopefully) or some (probably) mods it should work for
## you.
##
## Note: Having commas in your group, schedule, server, or save set names is a
## bad idea and will cause unpredictable script output.
##
## Ed Arizona 19-Feb-2004
##


#*** Get NSR Groups***
open NSRADMIN, "echo \"show name; start time\nprint type: nsr group\n\nquit\n\"| nsradmin | egrep -v \"visual|NetWorker\" | sed -e 's/nsradmin>//g' -e 's/^[ ]*//' | ";

while ( <NSRADMIN> ) {
  next if /^$/;
  chomp;
  tr /";//sd;
  ($type,$value)=split /: /;
  $key=$value if ( /name/ ); 
  $groups{$key}=$value;
}
close NSRADMIN;

#*** Print NSR Groups (optional)***
#foreach ( keys %groups ) { printf "%s:%s\n",$_,$groups{$_}; }
#foreach ( keys %groups ) { printf "%s\n",$_; }


#*** Get NSR Schedules ***
open NSRADMIN, "echo \"show name; action\nprint period: week;type: nsr schedule \n\nquit\n\"| nsradmin | egrep -v \"visual|NetWorker\" | sed -e 's/nsradmin>//g' -e 's/^[ ]*//' | ";

while ( <NSRADMIN> ) {
  next if /^$/;
  chomp;
  tr /";//sd;
  ($type,$value)=split /: /;
  $key=$value if ( /name/ ); 
  $value=~y/ /,/;
  $schedules{$key}=$value;
}
close NSRADMIN;

#*** Print NSR Schedules (optional)***
#foreach ( keys %schedules ) { printf "%s:%s\n",$_,$schedules{$_}; }
#foreach ( keys %schedules ) { print $_; }


#*** Get NSR Clients ***
open NSRADMIN, "echo \"show name; group; schedule; save set\nprint type: nsr client\n\" | nsradmin | egrep -v \"visual|NetWorker\" | sed -e 's/nsradmin>//g' -e 's/^[ ]*//' -e 's/save set/saveset/'  | ";

while ( <NSRADMIN> ) {
  chomp;
  while ( ! /^$/ ) {
    tr /";//sd;
#*** for records where group or save set span multiple lines ***
    if ( /: / ) { 
      ($key,$value)=split /: /; 
    } else {
      $value=$_;
    }

    $value =~ s/, /,/g;
    $servers{$key} .= $value;


#*** Get next record for inner loop ***
    $_=<NSRADMIN>;
    chomp;

  }

@groups=split /,/,$servers{group};
@savesets=split /,/,$servers{saveset};

foreach $group ( @groups ) {
  foreach $saveset (@savesets ) {
    printf "%s,%s,%s,%s,%s,%s\n",$servers{name},$servers{schedule},$group,$saveset,$groups{$group},$schedules{$servers{schedule}};
    }
  }
%servers="";
}
close NSRADMIN;
exit;
