nv-l

Re: Extracting just a single field from objdb???

1999-11-17 18:35:04
Subject: Re: Extracting just a single field from objdb???
From: "Boyles, Gary P" <gary.p.boyles AT INTEL DOT COM>
To: nv-l AT lists.tivoli DOT com
Date: Wed, 17 Nov 1999 15:35:04 -0800
Jeff,
Let me know how it works.  I'm interested in whether its
portable or not.
Gary Boyles

-----Original Message-----
From: Jeff Fitzwater [mailto:jfitz AT PRINCETON DOT EDU]
Sent: Wednesday, November 17, 1999 1:17 PM
To: NV-L AT UCSBVM.UCSB DOT EDU
Subject: Re: Extracting just a single field from objdb???


I'll give it a spin.
Thank you

Jeff Fitzwater

"Boyles, Gary P" wrote:
>
> Jeff,
> Here's a perl-code example that I wrote awhile back.  It runs on either
> NetView AIX or NT.  It does what you want.  If you don't want temp-files
> lying around... always use the "once" cache flag.
>
> To use it (for your example) type...
>
> get_nv_field_info.pl yourNode  "TopM Interface List" once
>
> For generic usage:
>
> get_nv_field_info.pl -h
>
> It should work for just about any field.
>
> Regards,
>
> Gary Boyles
>
>
----------------------------------------------------------------------------
> --
>
> #!/usr/bin/perl
>
############################################################################
> ##
> # Procedure:    get_nv_field_info.pl
> #
> #
> # Description:
> # ===========
> # Get specific field-information from the ovw database  (via ovobjprint)
> #
> # This program uses the idea of a cache-file with ovobjprint.  It runs
> # ovobjprint, and then saves the info to a file (c:\temp or /tmp).  It
> # will then read follow-on information from the file, until you tell it
> # to read new info, or until the file is 20 minutes old.
> #
> # Inputs:
> # =======
> # 1)  Node-Name  2)  Field-To-Get  3)  cache-flag (new,use,delete,once)
> #
> # Modifications:
> #
> # Date          Who     Reason
> # ===========   ===
> ======================================================
>
############################################################################
> ##
>
> $True           = "1";          # Perl standard for true/false
> $False          = "0";
>
> $prgName        = 'get_nv_field_info';
> $prgVer         = 'V1.0-01';
>
> #print "\n$prgName $prgVer \n\n";
>
> #++++++++++
> # Initialize Variables
>
############################################################################
> ##
> @rtnList        = ();                   # Used for subroutine returns
>
> $thisNode       = "";                   # Current Node
> $thisField      = "";                   # NetView Field
>
> #++++++++++
> # Get arguments
>
############################################################################
> ##
> $thisNode  = $ARGV[0];          # Node Name
> $thisField = $ARGV[1];          # NetView Field
> $cacheFlag = $ARGV[2];          # Cache Flag
>
> if    ( "$cacheFlag" ne "use"           &&
>         "$cacheFlag" ne "delete"        &&
>         "$cacheFlag" ne "once"          &&
>         "$cacheFlag" ne "new"   )       { $cacheFlag = "use"; }
>
> #++++++++++
> # Print out usage message
>
############################################################################
> ##
> if ( $ARGV[0] eq "-h" || $ARGV[0] eq "" || $ARGV[0] eq "help")
>   {
>   print "\n$prgName $prgVer \n\n";
>   print "Usage:  $prgFile Node Field-Info \n\n";
>   print "Usage:  $prgFile gxx.nobody.com isIPRouter  [cacheFlag]\n";
>   print "Usage:  $prgFile gxx.nobody.com sysLocation \n";
>   print "Usage:  $prgFile gxx.nobody.com sysLocation new \n\n";
>   print "Cache Flag Options = new, use, once, delete \n";
>   print "Cache Flag Default = use \n\n";
>   exit(1);
>   }
>
> #++++++++++
> # Get ovobjprint information
>
############################################################################
> ##
> ($errStat, @rtnList) = &get_NV_field_info("$thisNode","$thisField",
> $cacheFlag);
>
> foreach $aLine (@rtnList)
>   {
>   print "$aLine\n";
>   }
>
> exit(0);
>
>
############################################################################
> ##
> # Subroutine:   get_NV_field_info
> #
> # Input:        1)  Node  2) Field-To-Get  3)  Cache-Flag:
> #                     new    - get new  info and don't delete after use
> #                     once   - get new  info then delete after use
> #                     use    - get info from cache (if cache is present)
> #                     delete - just delete the cache (don't do anything
> else)
> #
> # Outputs:      Status + String containing field info
> #
> # Description:
> # Get the information from the ovw database for a field (for any object)
>
############################################################################
> ##
> sub get_NV_field_info
> {
>   my ($the_OS)  = $ENV{"OS"};
>   my ($nv_drive)        = $ENV{"NV_DRIVE"};
>   my ($uniqueKey)       = $$;
>   my ($First_Line_Flag) = "$True";
>
>   my ($errStat) = "$False";
>   my (@rtnList) = ();
>
>   my ($cacheFile);
>   my ($fileName)        = "";
>   my ($inLine)          = "";
>   my ($xChar)           = "";
>   my ($xLine)           = "";
>
>   my ($create_time)     = 0;
>   my ($now_time)        = 0;
>
>   my (@nodeTbl)         = ();
>   my (@pLine)           = ();
>   my (@aLine)           = ();
>   my ($f_ptr)           = 0;
>   my ($c_ptr)           = 0;
>   my ($l_ctr)           = 0;
>
>   my ($fieldFlag)       = "";
>
>   my ($the_Node)  = "$_[0]";            # Node-name
>   my ($the_Field) = "$_[1]";            # OVW-field to get
>   my ($cacheFlag) = "$_[2]";            # Info from
> cache...new/once/use/delete
>
> if ($cacheFlag eq "" ) { $cacheFlag = "use"; }
>
> #++++++++++
> # Configure OS-dependent stuff
> #########################################################
> if ("$prgName" eq "") { $prgName = "Get_NV_Field_Info"; }
>
> $fileName = "Get_NV_field_info.$the_Node";
>
> #++++++++++
> # Setup path & command-names
>
############################################################################
> ###
> if ( "$the_OS" eq "Windows_NT" )
>   {
>   $ovObj_cmd    = "$nv_drive"."\\usr\\OV\\bin\\ovobjprint -s";
>   $cacheFile    = "C:"."\\temp\\"."$fileName";
>   }
> else
>   {
>   $ovObj_cmd    = "/usr/OV/bin/ovobjprint -s";
>   $cacheFile    = "/tmp/"."$fileName";
>   }
>
> #++++++++++
> # What are we supposed to do:
> #   1)  Just delete the file-cache for that node.
> #   2)  Read from the cache (if it exists)
> #       If it doesn't exist... read info into cache-file
> #       IF its over 15 minutes old -- re-build it.
> #   3)  Just get new info and put it into the cache
> #########################################################
> if ( $cacheFlag eq "delete" )
>   {
>   unlink($cacheFile);
>   return ("$True","Cache_File_Deleted");
>   }
> elsif ($cacheFlag eq "use" )
>   {
>   if (-e $cacheFile)                            # Cache-file exists
>     {
>     @rtnList      = stat($cacheFile);
>     $create_time  = $rtnList[10];
>     $now_time     = time;
>     if ( ($now_time - $create_time) > 1200 )    # But if its old -- reload
> it
>       {
>       unlink($cacheFile);
>       $errStat = `$ovObj_cmd $the_Node > $cacheFile`;
>       }
>     }
>   else                                          # Cache-file doesn't exist
>     {
>     $errStat = `$ovObj_cmd $the_Node > $cacheFile`; # So create it
>     }
>   }
> else                                            # Here we're requesting
new
> one
>   {
>   $errStat = `$ovObj_cmd $the_Node > $cacheFile`;
>   }
>
> #++++++++++
> # Extract the specific field-info from the cache-file
> #########################################################
> open (IN_LIST, "$cacheFile");           # Open temp-file
>
> $fieldFlag = "$False";
>
> $l_ctr = 0;
> while (<IN_LIST>)               # Process entire file... looking for
> field-info
>   {
>   $l_ctr++;                     # Keep track of number of lines
>   chop;
>   ~s/^\s*//;                    # Remove leading spaces (needed for perl4)
>
>   $inLine = "$_";
>   @pLine = split(/\s+/,"$inLine");
>
>   if ( $fieldFlag eq "$True" )  # Get by FIELD... then search for
field-info
>     {
>     $aLine[0] = "$_";           # Get the line of text
>     $f_ptr = index ("$aLine[0]", "$the_Field");
>     if ( $f_ptr != -1 )         # Exit loop if this is THE line.
>       {
>       $c_ptr = 1;
>       while (<IN_LIST>)
>         {
>         chop;
>         ~s/^\s*//;                      # Remove leading spaces
>         if (substr("$_",0,1) eq "\"")   # Check for double-quote
>           {
>           push (@aLine, "$_");          # so push it onto our list.
>           }
>         else
>           {
>           last;
>           }
>         }
>       last;
>       }                                 # Field-flag if
>     }                                   # Field-flag if
>   else                          # Loop until FIELD keyword is hit
>     {
>     if ( "$pLine[0]" eq "FIELD" ) { $fieldFlag = "$True"; }
>     }
>   }
>
> close (IN_LIST);                # Close  temp-file
>
> #++++++++++
> # If object-info is incomplete -- exit with error
> #########################################################
> if ($l_ctr < 4)
>   {
>   unlink($cacheFile);
>   return("$False","$False");
>   }
>
> #++++++++++
> # Delete cache after use (if asked to)
> #########################################################
> if ( $cacheFlag eq "once" ) { unlink ($cacheFile); }
>
> #++++++++++
> # Process the field-info
> #########################################################
> @rtnList = ();                  # Init return-array again
>
> if ( $f_ptr != -1 )             # If its a valid-field... get the info
>   {
>   $f_ptr = $f_ptr + length ( "$the_Field") + 1; # Get past field-tag
>
>   $aLine[0] = substr ( "$aLine[0]", $f_ptr ); # Get info (with leading
> spaces)
>
>   foreach $xField (@aLine)
>     {
>     $_     = $xField;
>     tr/\"/ /;                   # Convert double-quotes to white-space
char
>     ~s/^\t*//;                  # Remove leading tabs
>     ~s/^\s*//;                  # Remove leading spaces
>     s/\s*$//;                   # Remove trailing spaces
>
>     @pLine = split(/\s+/,"$_"); # Parse into components (perl4 format)
>
>     push (@rtnList, "@pLine");  # Push field-info onto list
>     }
>   }
> else
>   {
>   push (@rtnList, "");          # Push null-info onto list
>   }
>
> return ( "$True", @rtnList );           # Return field-info
>
> }
>
> 1;