NetWorker Basics – Policy Tree and Status

The nsrpolicy tool is remarkably powerful, allowing you to create edit and view policy details rapidly from the command line on your NetWorker server. In this post I’ll show you a simple Perl script that you can use to automatically display a “configuration tree” of your policies, and in doing so also show any currently running workflow.

The flow of the script is as follows:

  1. Use “nsrpolicy policy list” to generate a list of the configured policies.
  2. For each policy returned, display the policy name and:
    1. Run “nsrpolicy workflow list -p policyName” to get a list of all the workflows within the policy.
    2. Run “nsrpolicy workflow display -p policyName -w workflowName” to get some basic configuration details for the workflow (enabled, auto start and start time).
    3. Run “nsrpolicy monitor -p policyName -w workflowName” to find out whether the workflow is running.
    4. Print the workflow and the gathered details.
    5. Run “nsrpolicy action list -p policyName -w workflowName” to grab and print a list of the actions within the workflow.

Running the script in my lab, I get the following output:

[Sun Oct 17 14:04:37]

## /nsr/scripts  
## root@orilla 

$ ./policytree.pl 
Adhoc
  -> CritRebuild 
     Enabled [Y] Auto [N] Time [21:00]
     - backup
  -> LegacyCent 
     Enabled [N] Auto [N] Time [21:00]
     - backup
  -> LegacyTest 
     Enabled [Y] Auto [N] Time [21:00]
     - backup
Infrastructure
  -> Servers 
     Enabled [Y] Auto [Y] Time [23:00]
     - backup
  -> Virtual (Active)
     Enabled [Y] Auto [Y] Time [21:00]
     - backup
     - clone
NAS
  -> DJW 
     Enabled [Y] Auto [Y] Time [15:00]
     - backup
  -> DJW GBD Archive ()
     Enabled [Y] Auto [N] Time [08:00]
     - backup
  -> Finance 
     Enabled [Y] Auto [Y] Time [21:00]
     - backup
  -> Homeshare 
     Enabled [Y] Auto [Y] Time [20:45]
     - backup
  -> PMdG 
     Enabled [Y] Auto [Y] Time [23:30]
     - backup
Server Protection
  -> NMC server backup 
     Enabled [Y] Auto [Y] Time [14:00]
     - NMC server backup
  -> Server backup 
     Enabled [Y] Auto [Y] Time [10:00]
     - Server db backup
     - Expiration

The script I wrote for this looks like the following:

#!/usr/bin/perl -w

use strict;

my @policies = ();

if (open(NSRP,"nsrpolicy policy list |")) {
	while (<NSRP>) {
		my $line = $_;
		chomp $line;
		push(@policies,$line);
	}
	close(NSRP);
}

if (@policies+0 > 0) {
	foreach my $policy (sort {$a cmp $b} @policies) {
		my @workflows = ();
		print "$policy\n";
		
		# Get the workflows for this policy.
		if (open(NSRW,"nsrpolicy workflow list -p \'$policy\' |")) {
			while (<NSRW>) {
				my $line = $_;
				chomp $line;
				push(@workflows,$line);
			}
			close(NSRW);
			if (@workflows+0 > 0) {
				foreach my $workflow (sort {$a cmp $b} @workflows) {
					my $enabled = "N";
					my $autostart = "N";
					my $startTime = " ";
					
					if (open(NSRW,"nsrpolicy workflow display -p \'$policy\' -w \'$workflow\' |")) {
						while (<NSRW>) {
							my $line = $_;
							chomp $line;
							if ($line =~ /\"workflowAutostartEnabled\": (.*),/) {
								my $as = $1;
								if ($as eq "true") {
									$autostart = "Y";
								}
							}
							if ($line =~ /\"workflowEnabled\": (.*),/) {
								my $en = $1;
								if ($en eq "true") {
									$enabled = "Y";
								}
							}
							if ($line =~ /\"workflowStarttime\": \"(.*)\"/) {
								$startTime = $1;
							}
						}
						close(NSRW);
					}
					my $wfd = "Enabled [$enabled] Auto [$autostart] Time [$startTime]";
					my $state = "";
					if (open(NSRM,"nsrpolicy monitor -p \'$policy\' -w \'$workflow\' --non-tabular 2>&1 |")) {
						while (<NSRM>) {
							my $line = $_;
							chomp $line;
							if ($line =~ /^\s+job state:(.*)/) {
								$state = $1;
								last;
							}
						}
						close(NSRM);
						if ($state eq "COMPLETED" || $state eq "") {
							$state = "";
						} else {
							$state="(" . ucfirst(lc($state)) . ")";
						}
					}
					
					print "  -> $workflow $state\n     $wfd\n";
					if (open(NSRA,"nsrpolicy action list -p \'$policy\' -w \'$workflow\' |")) {
						while (<NSRA>) {
							my $line = $_;
							chomp $line;
							print "     - $line\n";
						}
						close(NSRA);
					}
				}
			}
		}
	}
}

There’s a lot more you could do with this of course, but hopefully what you see from the above is how trivial it is to leverage the nsrpolicy to grab out useful details from the command line.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.