Script to Automatically run Spectrum Integrated Discovery Profiles

Document created by RenatoBR on Jul 18, 2014Last modified by RenatoBR on Dec 17, 2016
Version 3Show Document
  • View in full screen mode

The integration of Discovery Profiles with Spectrum IPDomain collection is a nice feature. However the minimum period of 24 hours for automatic discoveries is not good enough for me. So I create a this script to run every 5 minutes via CRON.

To save resources in CAPC it only execute the Discovery Profile if the list of IPs since the last run has changed.

 

#!/opt/perl5/perls/perl-5.16.3/bin/perl use strict; use warnings; use autodie; use REST::Client; use JSON; use URI::Escape; use XML::LibXML; use Digest::MD5 qw(md5_hex); use Config::Tiny;  our $VERSION = 0.1;  my $hostcada          = 'http://hostcada:8581'; my $url               = '/rest/discoveryprofiles/'; my @discoveryProfiles = ( 28540, 28543 ); #I have 2 IP Domains, those are the IDs of ther Discovery Profiles my $configfile        = '/etc/DiscoveryProfile.conf';  my $Config = Config::Tiny->read($configfile); $Config = Config::Tiny->new unless $Config;  my $client = REST::Client->new(); $client->setHost($hostcada); $client->addHeader( 'Accept',       'application/xml' ); $client->addHeader( 'Content-Type', 'application/xml; charset=UTF-8' ); $client->GET($url);  if ( $client->responseCode() ne '200' ) {     print $client->responseCode() . "\n" . $client->responseContent() . "\n";     exit 1; }  my $doc = XML::LibXML->load_xml( string => $client->responseContent() ); my $root = $doc->documentElement();  my $write_conf = 0;  for my $id (@discoveryProfiles) {     my ($node) = $root->findnodes("/DiscoveryProfileList/DiscoveryProfile[ID=$id]");     my $name = $node->findnodes('./Item/Name/text()')->[0]->data;     my @iplist    = map { $_->textContent() } $node->findnodes('./IPListList/IPList');     my $instance  = $node->findnodes('./MostRecentInstance/text()')->[0]->data;     my $md5       = md5_hex(@iplist);     my $conf_md5  = $Config->{$name}->{md5} || "";     my $conf_pmd5 = $Config->{$name}->{pmd5} || "";      if ( ( "$md5" ne "$conf_md5" ) and ( "$md5" ne "$conf_pmd5" ) and @iplist ) {         runDiscovery($id);         $Config->{$name}->{md5}  = $md5;         $Config->{$name}->{pmd5} = $conf_md5;         $write_conf              = 1;     } }  if ($write_conf) {     $Config->write($configfile); }  sub runDiscovery {     my $id  = shift;     my $xml = "<DiscoveryProfile  version='1.0.0'><RunStatus>START</RunStatus></DiscoveryProfile>";     my $url = "/rest/discoveryprofiles/$id";     $client->PUT( $url, $xml ); }   

Attachments

    Outcomes