Networker

Re: [Networker] Handling Savegroup Completion Notices

2007-07-09 10:22:06
Subject: Re: [Networker] Handling Savegroup Completion Notices
From: John Stoffel <john.stoffel AT TAEC.TOSHIBA DOT COM>
To: NETWORKER AT LISTSERV.TEMPLE DOT EDU
Date: Mon, 9 Jul 2007 10:19:39 -0400
Here's a simple script to parse the current status of savegroups on a
networker server(s).

#!/usr/local/bin/perl -w
#
# Legato Networker Savegroup status report - Checks whether
# savegroup(s) are running or not on a bunch of servers.

use strict;
$|++;

# Query for savegrp status
#
# 
# show name;status;last start;last end; autostart
# print type: NSR group

my $query = "show name;status;last start;last end;autostart\nprint type: NSR gro
up";

require 5.000;
use Getopt::Long;

my $nsradmin = "/usr/sbin/nsradmin";
my $server = "foo";
my $debug = 0;
my $help = 0;
my $verbose = 0;
my %status;

my @servers = qw( foo bar );

my $ret = GetOptions(
                     'd', \$debug,
                     'h', \$help,
                     's=s', \$server,
                     'v', \$verbose,
                    );

if (! -x $nsradmin ) {
  die "Error!  Can't find $nsradmin.  Please login to a system that has this ins
talled.";
}

print STDERR "DEBUG mode: $debug\n" if ($debug);

print "Checking servers...\n  " if $verbose;
foreach my $s (sort @servers) {
  print "$s " if $verbose;
  &getsgrpstatus($s);
}
print "\n...done.\n" if $verbose;

&showstatus;

exit;

#---------------------------------------------------------------------
sub getsgrpstatus {
  
  my $server = shift @_;
  
  my $sg;

  print " getsgstatus()\n" if $debug;

  my $q = $query;
  
  my @res = &runquery($server,$q);
  
  foreach (@res) {
    chomp;
    next if (m/^$/);

    if (m/name: (.*);/) {
      $sg = $1;
    }

    if (m/status: (.*);/) {
      $status{$server}{$sg}{'STATUS'} = $1;
    }

    if (m/autostart: (.*);/) {
      $status{$server}{$sg}{'AUTOSTART'} = $1;
    }

    if (m/start: (.*);/) {
      $status{$server}{$sg}{'BEGAN'} = $1;
    }

    if (m/end: (.*);/) {
      $status{$server}{$sg}{'ENDED'} = $1;
    }
  }
}

#---------------------------------------------------------------------
sub showstatus {
  foreach my $s (sort keys %status) {
    foreach my $g (sort keys %{$status{$s}}) {

      # Skip if the savegroup didn't start automatically 
      next if ($status{$s}{$g}{'AUTOSTART'} eq 'Disabled');

      # Skip if the savegroup is idle (i.e. completed it's run)
      next if ($status{$s}{$g}{'STATUS'} eq 'idle');

      print "\nserver:     $s\n";
      print " savegroup: $g\n";
      foreach my $k (sort keys %{$status{$s}{$g}}) {
        print "  $k:\t$status{$s}{$g}{$k}\n";
      }
    }
  }
}

#---------------------------------------------------------------------
sub runquery {

  my $s = shift @_;
  my $q = shift @_;
  
  print "runquery()\n" if $debug;
  
  my @res;
  my $qf = "/tmp/nsr_query.$$";
  
  if ($q =~ m/^\s*$/) {
    die "Can't run an empty query!\n\n";
  }
  
  open(QF,">$qf") || 
    die "Error!  Can't create $qf: $!\n";
  print QF "$q\n";
  close(QF);

  # Now run the query and parse the input nicely...
  open(AQ,"$nsradmin -s $s -i $qf |") || 
    die "Error!  Couldn't run $nsradmin query: $!\n";
  @res = <AQ>;
  close(AQ);
  unlink $qf;
  return @res;
}

To sign off this list, send email to listserv AT listserv.temple DOT edu and 
type "signoff networker" in the body of the email. Please write to 
networker-request AT listserv.temple DOT edu if you have any problems with this 
list. You can access the archives at 
http://listserv.temple.edu/archives/networker.html or
via RSS at http://listserv.temple.edu/cgi-bin/wa?RSS&L=NETWORKER