PDA

View Full Version : Export List of Devices in CSV Format (API Usage Example)


rajib
08-12-2009, 06:06 AM
This is a sample Perl script that uses BVE API server to retrieve list of configured devices and prints it as comma-separated list. The output includes the department name, device name, device address and a flag indicating whether the device is suspended.

In order to run this script, place the script under plugin/utils directory on the BVE server and execute the following commands:

Linux/Solaris:

su
cd /usr/local/traverse
etc/bveapi.init start
plugin/utils/exportDeviceList.pl --user ABC --pass XYZ


Windows:

C:
cd "\Program Files\Traverse"
net start nvbveapi
plugin\utils\exportDeviceList.pl --user ABC --pass XYZ


ABC is a user in Traverse and XYZ is the corresponding password. If you specify superuser as the login, all devices for all departments will be returned. Standard corporate disclaimer applicable.

lauraj
03-17-2010, 01:24 AM
Attached is a modification to the above Perl script to also output the tests provisioned on each device as an additional field. The usage is the same.

In order to run this script, place the script under plugin/utils directory on the BVE server and execute the following commands:

Linux/Solaris:


su
cd /usr/local/traverse
etc/bveapi.init start
plugin/utils/exportDeviceTestList.pl --user ABC --pass XYZ


Windows:


C:
cd "\Program Files\Traverse"
net start nvbveapi
plugin\utils\exportDeviceTestList.pl --user ABC --pass XYZ


Standard corporate disclaimer applicable.

charbs
02-28-2011, 06:11 PM
This script is great but I'd like more test details.
I've tweaked it and added some extra device and test fields but it doesn't provide all the info I need for the tests in that some fields where I know should have some values are blank so I'm wondering if my parameters are wrong. I know that not all tests are equal so I've chosen common fields to all of them in order for my export to Excel to work well and also added a title print line.
If you have a chance to look at it and tweak it, it would be appreciated.
Thanks
Charbs

#!/usr/bin/perl
#

##
#
# NOTICE:
# Copyright 2009 Zyrion, Inc. All Rights Reserved.
# This software is the proprietary information of
# Zyrion, Inc. Use is subject to license terms.
#
# DESCRIPTION:
# command line tool to generate list of devices and related tests
#
##

use File::Basename;
use Cwd 'abs_path';
use lib (abs_path(dirname($0)),
abs_path(dirname($0)) . "/../../lib/perl",
abs_path(dirname($0)) . "/../../../src/perl/modules",
abs_path(dirname($0)) . "/../../../fcots/perl/");
use Getopt::Long;
use NetVigil qw(Provisioning);
use NetVigil::Debug;

## variables -------------------------------------------------------------
##

{
no warnings 'qw';
my $my_name = basename($0);
my $my_version = (qw$Revision: #1 $)[1];
}

## parse cli parameters & validate -------------------------------------------------
##

GetOptions ('host=s' => \$opt_host,
'username=s' => \$opt_user,
'password=s' => \$opt_pass,
'device=s' => \$opt_device,
'version' => \$opt_vers,
'help' => \$opt_help,
'debug+' => \$DEBUG);

if ($opt_vers) { &show_version; &exit_now(0); }
if ($opt_help) { &show_usage; }
if (! $opt_user) {
&error("no username specified");
&show_usage;
}
$opt_host = $opt_host || "127.0.0.1"; # default host (localhost)
$opt_pass = $opt_pass || ""; # default password (blank)
$opt_device = $opt_device || '*'; # default device filter (all)

## beginning of program ------------------------------------------------
##

my $bve_online = 0;
my $BVE = new NetVigil::Provisioning( DEBUG => $DEBUG,
Host => $opt_host,
Port => "7661");

if (! $BVE->Login( user => $opt_user, password => $opt_pass)) {
&error("failed to log into bve api server",
"reason: " . $BVE->GetErrorMsg);
&exit_now(1);
}
$bve_online = 1;

if (! $BVE->ListDevice(deviceName => $opt_device)) {
&error("failed to retrieve list of devices",
"reason: " . $BVE->GetErrorMsg);
&exit_now(1);
}

my $RESULT_COUNT = $BVE->GetResultCount();
unless ($RESULT_COUNT) {
&error("no matching devices were found using search criteria");
&exit_now(1);
}

&debug("search returned ${RESULT_COUNT} matching device(s) ...");

# store device information into temporary array
#
my %DEVICE_LIST = ();
my %DEVICE_INFO = ();
my $RESULT_REF = $BVE->GetResultRef();
foreach my $device_serial (keys %{ $RESULT_REF }) {
my $device_name = $RESULT_REF->{$device_serial}->{devicename};
my $department_name = $RESULT_REF->{$device_serial}->{accountname};
$DEVICE_LIST{$department_name}->{$device_name}->{address} = $RESULT_REF->{$device_serial}->{address};
$DEVICE_LIST{$department_name}->{$device_name}->{status} = $RESULT_REF->{$device_serial}->{issuspended};
$DEVICE_LIST{$department_name}->{$device_name}->{parent} = $RESULT_REF->{$device_serial}->{parentnames};
$DEVICE_LIST{$department_name}->{$device_name}->{type} = $RESULT_REF->{$device_serial}->{devicetype};
$DEVICE_LIST{$department_name}->{$device_name}->{snmpcom} = $RESULT_REF->{$device_serial}->{snmpcid};
$DEVICE_LIST{$department_name}->{$device_name}->{snmpver} = $RESULT_REF->{$device_serial}->{snmpversion};
$DEVICE_LIST{$department_name}->{$device_name}->{agentcom} = $RESULT_REF->{$device_serial}->{agentcommunity};
$DEVICE_LIST{$department_name}->{$device_name}->{agentprt} = $RESULT_REF->{$device_serial}->{agentport};
$DEVICE_LIST{$department_name}->{$device_name}->{agentver} = $RESULT_REF->{$device_serial}->{agentversion};
$DEVICE_LIST{$department_name}->{$device_name}->{modl} = $RESULT_REF->{$device_serial}->{model};
$DEVICE_LIST{$department_name}->{$device_name}->{vendr} = $RESULT_REF->{$device_serial}->{vendor};
$DEVICE_LIST{$department_name}->{$device_name}->{comnt} = $RESULT_REF->{$device_serial}->{comment};

}


#testName

# print device and test list in CSV format
#
# print header line
#
print "\"department_name\",";
print "\"device_name\",";
print "\"address\",";
print "\"status\",";
print "\"parent\",";
print "\"type\",";
print "\"snmpcommunity\",";
print "\"snmpversion\",";
print "\"agentcommunity\",";
print "\"agentport\",";
print "\"agentversion\",";
print "\"model\",";
print "\"vendor\",";
print "\"comment\",";
print "\"testname\",";
print "\"testtype\",";
print "\"subType\",";
print "\"interval\",";
print "\"warningThreshold\",";
print "\"criticalThreshold\",";
print "\"slaThreshold\",";
print "\"units\",";
print "\"actionName\",";
print "\"scheduleName\",";
print "\"isSuspended\"";
print "\n";

foreach my $department_name (sort keys %DEVICE_LIST) {
foreach my $device_name (sort keys %{ $DEVICE_LIST{$department_name} }) {
# retrieve list of tests from device
#
$BVE->ListTest(deviceName=>"${device_name}", testName=>'*');
my $RESULT_REF = $BVE->GetResultRef();

# no tests found on device
#
if ($RESULT_REF eq "") {
print "\"${department_name}\",";
print "\"${device_name}\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{address} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{status} . "\",";
print "UNCONFIGURED";
print "\n";
}
foreach $serial_num (keys %{ $RESULT_REF }) {
$testname = $RESULT_REF->{$serial_num}->{testname};
$testtype = $RESULT_REF->{$serial_num}->{testtype};
$subType = $RESULT_REF->{$serial_num}->{subType};
$interval = $RESULT_REF->{$serial_num}->{interval};
$warningThreshold = $RESULT_REF->{$serial_num}->{warningThreshold};
$criticalThreshold = $RESULT_REF->{$serial_num}->{criticalThreshold};
$slaThreshold = $RESULT_REF->{$serial_num}->{slaThreshold};
$units = $RESULT_REF->{$serial_num}->{units};
$actionName = $RESULT_REF->{$serial_num}->{actionName};
$scheduleName = $RESULT_REF->{$serial_num}->{scheduleName};
$isSuspended = $RESULT_REF->{$serial_num}->{isSuspended};

print "\"${department_name}\",";
print "\"${device_name}\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{address} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{status} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{parent} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{type} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{snmpcom} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{snmpver} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{agentcom} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{agentprt} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{agentver} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{modl} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{vendr} . "\",";
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{comnt} . "\",";
print "\"" . $testname . "\",";
print "\"" . $testtype . "\",";
print "\"" . $subType . "\",";
print "\"" . $interval . "\",";
print "\"" . $warningThreshold . "\",";
print "\"" . $criticalThreshold . "\",";
print "\"" . $slaThreshold . "\",";
print "\"" . $units . "\",";
print "\"" . $actionName . "\",";
print "\"" . $scheduleName . "\",";
print "\"" . $isSuspended . "\"";
print "\n";
}
}
}

# all done
#
&exit_now(0);

# sub routines ---------------------------------------------------------
#

sub show_version {

print "${my_name} (version: ${my_version})\n";

}

sub show_usage {

print <<eofUsage

usage: ${my_name} [ --host=< addr > ]
--username=login_id [ --password=login_pw ]
[ --device=<wildcard> ]

--host = ip address or full host name of bve
--username = log into bve api server as this user
--password = password for the specified user
--device = device name filter

eofUsage
;
&exit_now(1);
}

sub exit_now {

my ($return_value) = @_;
if ($bve_online) { $BVE->Logout; }
exit $return_value;

}

rajib
03-07-2011, 08:21 AM
Hi,

I assume you are having difficulty with exporting the test thresholds, action profile, etc? The Perl API exposes parameter values in hash/associative arrays with the key names that match BVE API parameter names. However, the key names will always be in lowercase regardless of how they appear in the BVE API. In other words, you should be able to access the missing parameters by changing:
$warningThreshold = $RESULT_REF->{$serial_num}->{warningThreshold};
to
$warningThreshold = $RESULT_REF->{$serial_num}->{warningthreshold};
and so on.

- Rajib

charbs
03-07-2011, 02:12 PM
That solved it !! :-)
Thanks Rajib !

travisn
03-30-2011, 08:52 AM
Greetings,

When I run either of the Zyrion provided scripts, i get

[root@traverse]# ./plugin/utils/exportDeviceTestList.pl --user usernameX --pass passwordX
-bash: ./plugin/utils/exportDeviceTestList.pl: /usr/bin/perl^M: bad interpreter: No such file or directory

Thanks.

-Travis N

rajib
03-31-2011, 02:12 AM
Travis,

When running the script on Linux, it may be necessary to change line terminators. You can accomplish this by using following command:

dos2unix plugin/utils/exportDeviceTestList.pl

Afterwards you should be able to run the command normally.

- Rajib

sratliff
04-12-2011, 03:32 PM
Hi, Charbs

could you post your final script? I am trying to accomplish the same thing. Thanks.

charbs
04-13-2011, 01:07 PM
Hi Sratliff,

The script is in my first post. The typo which Rajib corrected was in the parameters used in the foreach $serial_num (keys %{ $RESULT_REF })... section. Simply change the test parameters to all lowercase at the end of each line. Keep in mind that not all test parameters are included. I used only the ones who seem to be common to all tests. You might want to use the testlist API to see if others could be usefull to you and tweak the script accordingly.
Enjoy!
Charbs

charbs
06-17-2011, 04:20 PM
One caveat I'm trying to resolve which is to get tag information.

I've inserted the following to the temporary array section:
$DEVICE_LIST{$department_name}->{$device_name}->{tag1} = $RESULT_REF->{$device_serial}->{"tag1 (Tag 1)"};

....and the usual print line:
print "\"" . $DEVICE_LIST{$department_name}->{$device_name}->{tag1} . "\",";

but there's no value.

Does this mean that we can't get tag information or is it just another typo on my part ??

Thanks

Serge

rajib
06-18-2011, 04:29 PM
Hi Serge,

The properies are stored in the hash array in lowercase for consistency. if you change your script like this, the expected information should be available:


$DEVICE_LIST{$department_name}->{$device_name}->{tag1} = $RESULT_REF->{$device_serial}->{"tag1 (tag 1)"};


- Rajib

amishra1983
07-04-2011, 03:13 PM
Rajib,
Should we place the .pl file in util directory or Plugin directory.
Can you please elaborate more.

rajib
07-04-2011, 03:32 PM
Ashish,

The files shuld be extracted under plugin\utils directory - ie. create a new directory named "utils" under the existing "plugin" directory and place the file(s) from the ZIP archive in the newly created directory.

Regards - Rajib

amishra1983
07-04-2011, 04:00 PM
Perfect. Looking fine !!!

amishra1983
07-09-2011, 09:36 AM
Laura,
Can we also get the current test status and what thresholds are set for each test. This gives us complete data for all the customers and then we can play with this.