Networker

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

2004-05-07 13:14:04
Subject: Re: [Networker] Need NSRADMIN Output Examples for Script Testing
From: Darren Dunham <ddunham AT TAOS DOT COM>
To: NETWORKER AT LISTMAIL.TEMPLE DOT EDU
Date: Fri, 7 May 2004 09:59:00 -0700
> 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.
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=