Discover from CSV
This script shows how to initiate a Discover/Rewalk from a CSV file. The CSV file could be generated from an external IPAM system, for example.
The format of the CSV input file is:
- IPv4 or IPv6 address
- SNMP Parameters (same format as Discover/Rewalk Settings)
- (Optional) Hostname
Place your CSV file in /tmp/devices.csv on the AKIPS server and run this script.
NOTE: This script will run a Discover/Rewalk for each unique set of SNMP credentials in your CSV file.
# File format:
# ipaddr,SNMP parameters[,hostname]
# e.g.
# 10.1.8.250,version 2 community foobar
# 10.1.8.251,version 3 user fred sha password aes256 password,atlanta-ro
#
sub custom_discover_import_csv
{
my $DEVICES_CSV = "/tmp/devices.csv";
my $IN;
my $line;
my @domains;
my %cfg;
my %ip2host;
my %ip2name;
my $discover_lock;
return if (not -e $DEVICES_CSV);
# Get a lock on the discover
$discover_lock = process_lock ($DISCOVER_LOCK, LOCK_EX)
or EXIT_FATAL ("Failed to get a lock");
# Open discover log file
discover_log ("discover-script.log");
open ($IN, "<", $DEVICES_CSV) or EXIT_FATAL ("Can't open $DEVICES_CSV: $!");
unlink $DEVICES_CSV;
@domains = config_load_domains ();
while ($line = <$IN>) {
chomp $line;
my ($ipaddr, $snmp_param, $hostname) = split (",", $line, 3);
trim $snmp_param;
$cfg{$snmp_param}{$ipaddr} = 1;
if (defined $hostname and $hostname ne "") {
$ip2host{$ipaddr} = config_strip_sysname ($hostname, @domains);
}
}
close $IN;
for my $snmp_param (sort keys %cfg) {
if (discover_scan ($snmp_param, keys %{ $cfg{$snmp_param} }) > 0) {
discover_config ();
}
}
# Close discover log file
stdout_log_close ();
# Set hostnames
if (scalar keys %ip2host > 0) {
for my $ip (keys %ip2host) {
if (defined $ip2name{$ip} and $ip2name{$ip} ne $ip2host{$ip}) {
my $from_name = $ip2name{$ip}{name};
my $to_name = $ip2host{$ip};
config_rename_device ($from_name, $to_name);
}
}
}
process_unlock ($discover_lock);
}