Reply

NetMRI API Index for Infrastructure Devices

Posts: 75
5412     0

Hi,

I am working on getting some data out of NetMRI using the API.  I want to be able to query and get a list of all the infrastructure devices, but I need to change the limit to 3000, and the sorting to sort by DeviceIPDotted. 

Here is what I have so far:

https://netmriIPaddress/api/2.9/infra_devices/index

Any help would be appreciated.

Susan

 

https://netmriIPaddress/api/2

Adviser
Posts: 353
5412     0

https://netmriIPaddress/api/2.9/infra_devices/index?limit=3000&sort=DeviceIPDotted

 

Usually you really want to sort by DeviceIPNumeric though, that will give you the normal expected sort for IP addresses.

 

John

 

John,

Posts: 75
5412     0

John,

Good call on the sorting.  One more question.  With the index option, can I choose the fields I want returned, or do I need to use a different query type?

Susan

 

I believe you can use the

Adviser
Posts: 353
5412     0

I believe you can use the "select" parameter to returns specific fields:

https://netmriIPaddress/api/2.9/infra_devices/index?limit=3000&sort=DeviceIPDotted&select=DeviceID,DeviceName,DeviceIPDotted

I think DeviceID may always be included, as well as any custom fields that are defined, regardless of the select list.

If in the documentation page, you click on the "InfraDevice" link under "Model", it will describe all the various fields. Anything marked [M] is a method call an would need to go in a "methods" parameter list, not "select".

Also, note that using your browser, you will get XML. If you use a different tool (like curl), you will get JSON. The default return type is JSON, but the browser includes an "Accepts" HTTP header that switches the preference to XML. curl, etc. will not include that header automatically. To FORCE the type, append "index" with .json or .xml depending on the datatype you want.

 

Final Script

Posts: 75
5412     0

Thought I would post my final script in  case anyone was interested.  This takes a particular device group in NetMRI.  It grabs the Name, IP, Type and Hostname and outputs it to a CVS file.

 

#!/usr/bin/perl
use Data::Dumper;
use LWP;
use URI;
use JSON;

$USER="USER";
$PASS="PASSWORD";
$SERVER="SERVER";

my $browser=LWP::UserAgent->new();

#Turn off SSL verification (Either method works)
#$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;
$browser->ssl_opts(verify_hostname => 0);

#Open up a cookie jar in a local file which only exists for the length of runtime
$browser->cookie_jar({ file => ".$$.cookies.txt" });

#Build our authentication request
$url = URI->new("https://$SERVER/api/2.9/authenticate.json");
$url->query_form(
 username => "$USER",
 password => "$PASS"
);

#Authenticate, places cookies in the cookie jar automatically
my $response=$browser->get($url);

#In this example we will get all of the devices and then print out their IPs
#Build the WAPI URL by passing options to query_form
$url = URI->new("https://$SERVER/api/2.9/infra_devices/index?limit=3000&sort=DeviceIPNumeric&select=DeviceName,DeviceIPDotted,DeviceType,DeviceVendor&DeviceGroupID=38");

#Execute the request
my $response=$browser->get($url);

#Convert JSON data to Perl data object
$ref=from_json($response->content);

my @devices = @{ $ref->{'infra_devices'} };
foreach my $f ( @devices ) {
 my $DeviceName = $f->{ 'DeviceName' }; 
 my $DeviceIP = $f->{ 'DeviceIPDotted' };
 my $DeviceVendor = $f->{ 'DeviceVendor' };
 my $DeviceType = $f->{ 'DeviceType' };

 print "$DeviceName,$DeviceIP,$DeviceType,$DeviceVendor\n";
}
exit;

Nice! Is there a reason you

Adviser
Posts: 353
5412     0

Nice! Is there a reason you are using LWP directly instead of the NetMRI::API framework?

Response

Posts: 75
5412     0

I wanted to use Perl to call the REST API because it was easier to use REST to get the data (or to test getting the data).  From there, the Perl script was created as a collaboration with some collegues.  This way I can use the basic Perl script to do other types of data manipulation in the future.  If you can improve upon what I did, please post the full answer. Smiley Happy

Susan

I see.

Adviser
Posts: 353
5412     0

I see.

For NetMRI, the "PAPI" is in fact just a wrapper on top of the Core API (ie, the REST API). So, if you install the NetMRI API package (downloadable from the appliance in the API docs section), it will take care of a lot of these details for you. So, your script would look somethign like:

 

#!/usr/bin/perl                                                                                                                                
use Data::Dumper;                                                                                                                              
                                                                                                                                               
use NetMRI::API;                                                                                                                               
                                                                                                                                               
                                                                                                                                               
# You can also create a ~/.netmri.yml file and put these values in that file. That way,                                                        
# they are not hardcoded into the script, and they do not have to be passed on the                                                             
# command line (which anyone can see via ps -ef)                                                                                               
# Be sure to chmod 600 ~/.netmri.yml so no one can read it.                                                                                    
#                                                                                                                                              
$USER="USER";                                                                                                                                  
$PASS="PASSWORD";                                                                                                                              
$SERVER="netmri";                                                                                                                              

# Turn off SSL verification                                                                                                                    
# You can also create your own LWP UA and pass it into NetMRI::API                                                                             
# see perldoc NetMRI::API                                                                                                                      
                                                                                                                                               
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0;                                                                                                          
                                                                                                                                               
my $client = NetMRI::API->new(                                                                                                                 
          api_version => "2.9",                                                                                                                
          username    => $USER,                                                                                                                
          password    => $PASS,                                                                                                                
          url         => "https://$SERVER"                                                                                                     
        );                                                                                                                                     
                                                                                                                                               
                                                                                                                                               
# In list context, the index method will automatically                                                                                         
# page through the results according to the start/current/limit/total fields                                                                   
# that is, it will make one server call per page to retrieve all pages                                                                         
                                                                                                                                               
my @devices = $client->broker->infra_device->index({                                                                                           
                sort   => "DeviceIPNumeric",                                                                                                   
                select => "DeviceName,DeviceIPDotted,DeviceType,DeviceVendor",                                                                 
                DeviceGroupID => "38"                                                                                                          
                });                                                                                                                            
                                                                                                                                               
foreach my $f ( @devices ) {                                                                                                                   
 my $DeviceName = $f->DeviceName;                                                                                                              
 my $DeviceIP = $f->DeviceIPDotted;                                                                                                            
 my $DeviceVendor = $f->DeviceVendor;                                                                                                          
 my $DeviceType = $f->DeviceType;                                                                                                              
                                                                                                                                               
 print "$DeviceName,$DeviceIP,$DeviceType,$DeviceVendor\n";                                                                                    
}                                                                                                                                              
exit;                                                                                                                                          
Showing results for 
Search instead for 
Did you mean: 

Recommended for You