NAME

Net::SolarWinds::CookBook - General User Documentation

DESCRIPTION

This is a general goto guide on how to use the Net::SolarWinds modules. Most of the heavy lifting is done by the Net::SolarWinds::REST::Batch module.

HowTo(s)

  • How to enable logging

    use Net::SolarWinds::REST::Batch;
    use Net::SolarWinds::Log;
    
    my $log=new Net::SolarWinds::Log('/var/log/script.log');
    my $rest=new Net::SolarWinds::REST::Batch(log=>$log);
  • How to find a node

    use Net::SolarWinds::REST::Batch;
    use Data::Dumper;
    
    my $rest=new Net::SolarWinds::REST::Batch;
    
    # use the hostname method
    my $result=$rest->get_node("hostname");
    if($result) {
      my $data=$result->get_data;
      print Dumper($data);
    } else {
      print "Error Fetching: hostname, error was: $result\n";
    }
    
    # use the id method method
    my $result=$rest->get_node(212);
    if($result) {
      my $data=$result->get_data;
      print Dumper($data);
    } else {
      print "Error Fetching node by id: 212, error was: $result\n";
    }
    
    # use the ip address method
    my $result=$rest->get_node("192.168.1.74");
    if($result) {
      my $data=$result->get_data;
      print Dumper($data);
    } else {
      print "Error Fetching: 192.168.1.74, error was: $result\n";
    }
  • Adding an interface

    To add an interface, you will need to do a number of things. 1. Get the NodeID, 2. Poll the interfaces on the device, 3. Add the interface. Note: The pollers added by SolarWinds are incosnistent, you may want to audit and add any missing pollers.

    So here is how to add an interface

      use Net::SolarWinds::REST::Batch;
      use Net::SolarWinds::Log;
    
      # its always best to enable logging!
      my $log=new Net::SolarWinds::Log('/var/log/script.log');
      my $rest=new Net::SolarWinds::REST::Batch(log=>$log);
    
      # Node id
      my $nodeid=212;
    
      # fetch our best guess when it comes to default poller maps
      my $pm={};
      if(my $result=$rest->getPollerInterfaceMap($nodeid)) {
        $pm=$result->get_data;
      } else {
        $log->log_warn("Failed to get poller map for $nodeid");
      }
    
      # discover the interfaces
      my $result=$rest->DiscoverInterfacesOnNode($nodeid);
      my $ifname='eth0';
      
      if($result) {
        foreach my $int (@{$result->get_data->{DiscoveredInterfaces}}) {
    
          # never try to add an interface that all ready exists!
          next if $int->{InterfaceID}!=0;
    
          next unless $int->{Manageable};
    
          # The actual name may never be litteral, so we fix that here
          my $caption=$int->{Caption};
          # clean up the returned data
          $caption=~ s/\s\x{b7}.*$//s;
          my ($desc,$ifname)=split /\s-\s/,$caption;
          my $matched;
    
          # one of the above strings will contain our litteral interface name
          foreach my $string ($caption,$desc,$ifname) {
            next unless defined($string);
    	if($string eq $ifname) {
    	  $matched=$string;
    	  last;
    	}
          }
    
          next unless defined($matched);
          my $result=$rest->NodeInterfaceAddDefaultPollers($nodeid,[$int]);
          if($result) {
            my $intid=$result->get_data->{DiscoveredInterfaces}->[0]->{InterfaceID};
    	my $result=$rest->NodeInterfaceCustomProperties($nodeid,$intid,{Some=>"Custom Property"});
    	$log->log_error("Failed to set custom property for $ifname") unless $result;
    
    	# now we add any pollers that were missed by default
    
    	# fetch the interface map
    	my $result=$result=$rest->get_poller_map($intid,'I');
    	my $map=$result->get_data;
    
    	if(exists $pm->{$int->{ifType}}) {
    	  my $list=$pm->{$int->{ifType}};
    	  foreach my $poller (@{$list}) {
    	    next if exists $map->{$poller};
    	    my $result=$rest->add_poller($intid,'I',$poller);
    	    $log->log_error("Failed to add $poller to interface: $intid error was $result") unless $result;
    	  }
    	}
          } else {
            $log->log_error("Failed to add $ifname error was: $result");
          }
        }
      }

AUTHOR

Michael Shipper