Networker

Re: [Networker] Need NSRADMIN Output Examples for Script Testing

2004-05-07 13:37:56
Subject: Re: [Networker] Need NSRADMIN Output Examples for Script Testing
From: "Peter L. Buschman" <plblists AT IOTK DOT COM>
To: NETWORKER AT LISTMAIL.TEMPLE DOT EDU
Date: Fri, 7 May 2004 19:37:40 +0200
Darren:

Thank you very much for the example below.  It is quite helpful.  In my
case, reading the resource
files directly is not an option so I have to parse the raw nsradmin
output.  However, I will only be
querying one resource type at a time, which makes things slightly easier.

Here are some observations on the output:

    1. Status messages at the top need to be skipped (Current query set,
Display options, etc.,)
    2. Attribute / value pairs are separated by a : in column 28.
    3. Attribute / value pairs are terminated by a ; and can span 1 or
more lines.
    4. Attribute / value pairs will wrap long output lines with either a \
or ', ' (comma and space)
        as the last character before the newline.
    5. An attribute can have one or more values.  If multiple values are
present, they are separated
        by commas and may or may not be contained in doublequotes (").
    6. Multiple resource records are separated by a blank line.

Compared to NetBackup (where every command has a different output format),
this actually is relatively
straightforward and the parsing algorithm can be re-used for querying all
of the available Networker resources.

I agree that the existing format is a bit convoluted, however, and could be
made more script friendly. ;-)

--PLB

At 06:59 PM 5/7/2004, Darren Dunham wrote:
> All:
>
> I am working on an algorithm for parsing nsradmin output and need some
> real-life examples from
> complex environments to verify the results.

Having written this more than once, here's some tips/code...

#1 Some types aren't always properly cased.  I've seen NSR and nsr both
   in output.

#2 Sometimes it's easier to read nsr.res directly rather than nsradmin
   output.  If you do that, you need to be able to cope with the fact
   that the "resource identifier" line doesn't have a semicolon.

Here's some perl code I've used in the past.  It parses anything that
comes out of 'nsradmin' and puts it all into a single perl hash.  "nsr
client" data is handled specially, since the "name" attribute is not
usually unique.  There might be multiple client instances.  The code
below is just a part of a big program, so it probably won't run without
help, but that's the nsrinfo parse section.  You might find some parts
of it useful.

*sigh*  Wouldn't it be nice if nsrinfo had a simple to parse format for
reading by scripts?  The line continuation stuff makes it more readable,
but harder to parse.

use Text::ParseWords;
[...]

my (%nsr_data, $line, %entry);
[...]

# per-line parse engine is here....
  {
    $line++;
    #print STDERR scalar keys %nsr_data, " $line\n" unless $line % 1000;
    next if /^#/;
    s/^\s+//;  # leading whitespace is irrelevant;

    # hack for reading from nsr.res directly rather than nsradmin output
    # add a semicolon to the "resource identifier" line.
    s/^(resource identifier:.*)\n\Z/$1;\n/;

    if (/^\s*$/)
      {
        # blank line separates entries.
        my $type = $entry{'type'}[0];
        # Danger... hack approaching...
        $type =~ s/^nsr/NSR/;
        my $name = $entry{'name'}[0];

        if ($type eq "NSR client")
          {
            push (@{$nsr_data{$type}{$name}}, { %entry });
          }
        else
          {
            if (exists $nsr_data{$type}{$name})
              {
                #warn "Overwriting entry for $type, $name\n";
                # we'll normally be silent, but this can
                # be uncommented for debugging.
              }
            if (defined $type and
                defined $name)
              {
                # the main NSR entry in nsr.res doesn't
                # have a name attribute.  We can usually
                # ignore it safely
                $nsr_data{$type}{$name} = { %entry };
              }
          }
        %entry = ();
        next;
      }

    if (s/\\\n\Z// or ! s/;\n\Z//)
      {
        # continuation character
        $_ .= <>;
        redo;
      }

    ### Attempt to parse into standard NSR info format
    # 1  line :== key: value;
    # 2  value :==   item [ , item [ , item ...] ]

    my $key;
    unless ( s/(.*?)\s*\:\s*// )
      {
        # optional warnings here..
        warn "Couldn't parse line $line";

        next;
      }

    $key = $1; # if s/(.*?)\s*\:\s*//;
    my @values;

    # Completion and work list are dynamic features that sometimes
    # contain data that can confuse parse_line.
    # Stripping them out doesn't eliminate
    # any configuration information.
    if (defined $entry{type} and
        $entry{type}[0] eq "NSR group" and
        ($key eq "completion" or $key eq "work list"))
      { @values = (); }
    else
      { @values = parse_line('\s*,\s*', 0, $_); }

    $entry{$key} = [ @values ] ;
  }

--
Darren Dunham                                           ddunham AT taos DOT com
Senior Technical Consultant         TAOS            http://www.taos.com/
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >

--
Note: To sign off this list, send a "signoff networker" command via email
to listserv AT listmail.temple DOT edu or visit the list's Web site at
http://listmail.temple.edu/archives/networker.html where you can
also view and post messages to the list.
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=

--
Note: To sign off this list, send a "signoff networker" command via email
to listserv AT listmail.temple DOT edu or visit the list's Web site at
http://listmail.temple.edu/archives/networker.html where you can
also view and post messages to the list.
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=