Networker

Re: [Networker] identifying storage nodes and their clients.

2005-07-19 15:23:41
Subject: Re: [Networker] identifying storage nodes and their clients.
From: Tim Mooney <mooney AT DOGBERT.CC.NDSU.NODAK DOT EDU>
To: NETWORKER AT LISTSERV.TEMPLE DOT EDU
Date: Tue, 19 Jul 2005 14:19:40 -0500
In regard to: Re: [Networker] identifying storage nodes and their clients.,...:

This script is so close.  I have a many to many relationship for storage
nodes and clients.  For example, the output of the nsradmin script:

So, what I'm going for then is something that would do a parent ->
children relationship.  Where the desired final output from a script of
the prior nsradmin output would be something like:

StorageNode    Clients
============   =============================
client1        client1
client4        client4
client6        client4, client10
client7        client4
client5        client2
client3        client2, client8
client9        client8
nsrserverhost  client4, client8, client10

Haven't quite figured out how to write that.  Any suggestions?

With the right tools, it's not difficult.  What tools are you comfortable
using?  Something that supports associative arrays and/or other data
structures would be ideal.  I would use perl, but someone that's more
comfortable with modern ksh (which has associative arrays) or awk could
accomplish the same thing with those tools.

The basic algorithm is

- every time you see a client name, remember it for later.
- every time you see a blank line, forget your current client name.
- every time you see a list of storage nodes (possibly just one),
  loop over each storage node in the list, and add the client name
  that you're remembering to the list of clients for the current storage
  node.

I mocked up a crude script in perl that does this and seems to work.
If you're not comfortable with perl, it's going to be pretty opaque,
though.  It took me about 20 minutes to write, and 15 of those were spent
swearing at perl's notation for references and data structures.  ;-)

It's after my sig, but again, it's not going to be much use to you if
perl's not your thing.  I disavow any association with the script, it's
minimally tested, and completely unsupported.


Tim
--
Tim Mooney                              mooney AT dogbert.cc.ndsu.NoDak DOT edu
Information Technology Services         (701) 231-1076 (Voice)
Room 242-J6, IACC Building              (701) 231-8541 (Fax)
North Dakota State University, Fargo, ND 58105-5164


#! /usr/bin/perl -w

#
# expects input to be in the format generated from the query:
#
#echo "
#        show name; storage nodes
#        print type: NSR client
#        " | nsradmin -s $SERVER -i -
#
# Run that and pipe it into this script, or dump it to a file and use
# redirection to get it to this script.
#

while ($line=<>) {
        chomp($line);
        if ($line =~ /^\s*$/) {
                $client='';
                next;
        }

        # cannot be multivalued
        if ($line =~ /^\s*name: (.*)\s*$/) {
                $client=$1;
                next;
        }

        # CAN be multivalued
        if ($line =~ /^\s*storage nodes: (.*)\s*$/) {
                @snode_list = split(/, /, $1);
                foreach $snode (@snode_list) {
                        if (exists($snodes{$snode})) {
                                # add element to array ref.
                                $snode_array_ref = $snodes{$snode};
                                push(@$snode_array_ref, $client);
                        } else {
                                # create new array ref.
                                $snodes{$snode} = [ $client ];
                        }
                }
                next;
        }
}

format STDOUT_TOP=
StorageNode       Clients
===============   ======================================
.

format STDOUT=
@<<<<<<<<<<<<<<   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$snode,           $client_list
.

#
# Done building our mapping, spit it out.
#
foreach $snode (sort(keys(%snodes))) {
        # could be done as one step, looks like line noise.
        $snode_array_ref = $snodes{$snode};
        $client_list = join(', ', @$snode_array_ref);

        write;
}

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