2017-03-08 13:20:00 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
# (c) Ártica ST 2014 <info@artica.es>
|
|
|
|
# Module for network topology discovery.
|
|
|
|
|
2017-03-13 11:13:45 +01:00
|
|
|
package PandoraFMS::Recon::Base;
|
2017-03-08 13:20:00 +01:00
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
# Default lib dir for RPM and DEB packages
|
|
|
|
use NetAddr::IP;
|
2020-04-16 12:39:55 +02:00
|
|
|
use IO::Socket::INET;
|
2017-03-08 13:20:00 +01:00
|
|
|
use POSIX qw/ceil/;
|
2020-03-16 23:52:20 +01:00
|
|
|
use Socket qw/inet_aton/;
|
|
|
|
|
|
|
|
use lib '/usr/lib/perl5';
|
|
|
|
use PandoraFMS::Tools;
|
2017-03-13 11:13:45 +01:00
|
|
|
use PandoraFMS::Recon::NmapParser;
|
|
|
|
use PandoraFMS::Recon::Util;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2019-03-25 17:32:18 +01:00
|
|
|
# Constants.
|
|
|
|
use constant {
|
2020-03-18 10:28:50 +01:00
|
|
|
STEP_SCANNING => 1,
|
2020-03-25 18:02:58 +01:00
|
|
|
STEP_CAPABILITIES => 7,
|
2020-03-18 10:28:50 +01:00
|
|
|
STEP_AFT => 2,
|
|
|
|
STEP_TRACEROUTE => 3,
|
|
|
|
STEP_GATEWAY => 4,
|
2020-03-18 19:31:58 +01:00
|
|
|
STEP_MONITORING => 5,
|
2020-03-19 18:33:04 +01:00
|
|
|
STEP_PROCESSING => 6,
|
2020-03-18 10:28:50 +01:00
|
|
|
STEP_STATISTICS => 1,
|
|
|
|
STEP_APP_SCAN => 2,
|
|
|
|
STEP_CUSTOM_QUERIES => 3,
|
|
|
|
DISCOVERY_HOSTDEVICES => 0,
|
|
|
|
DISCOVERY_HOSTDEVICES_CUSTOM => 1,
|
|
|
|
DISCOVERY_CLOUD_AWS => 2,
|
|
|
|
DISCOVERY_APP_VMWARE => 3,
|
|
|
|
DISCOVERY_APP_MYSQL => 4,
|
|
|
|
DISCOVERY_APP_ORACLE => 5,
|
|
|
|
DISCOVERY_CLOUD_AWS_EC2 => 6,
|
|
|
|
DISCOVERY_CLOUD_AWS_RDS => 7,
|
|
|
|
DISCOVERY_CLOUD_AZURE_COMPUTE => 8,
|
2020-09-22 16:55:46 +02:00
|
|
|
DISCOVERY_CLOUD_GCP_COMPUTE_ENGINE => 13,
|
2020-03-18 10:28:50 +01:00
|
|
|
DISCOVERY_DEPLOY_AGENTS => 9,
|
|
|
|
DISCOVERY_APP_SAP => 10,
|
2020-06-11 14:53:34 +02:00
|
|
|
DISCOVERY_APP_DB2 => 11,
|
2020-09-22 16:55:46 +02:00
|
|
|
DISCOVERY_APP_MICROSOFT_SQL_SERVER => 12,
|
2020-03-25 18:02:58 +01:00
|
|
|
DISCOVERY_REVIEW => 0,
|
2020-03-18 10:28:50 +01:00
|
|
|
DISCOVERY_STANDARD => 1,
|
|
|
|
DISCOVERY_RESULTS => 2,
|
2020-03-26 13:44:08 +01:00
|
|
|
WMI_UNREACHABLE => 1,
|
|
|
|
WMI_BAD_PASSWORD => 2,
|
|
|
|
WMI_GENERIC_ERROR => 3,
|
|
|
|
WMI_OK => 0,
|
2019-03-25 17:32:18 +01:00
|
|
|
};
|
|
|
|
|
2019-09-24 19:07:55 +02:00
|
|
|
# $DEVNULL
|
2017-03-23 14:33:06 +01:00
|
|
|
my $DEVNULL = ($^O eq 'MSWin32') ? '/Nul' : '/dev/null';
|
|
|
|
|
2017-03-08 13:20:00 +01:00
|
|
|
# Some useful OIDs.
|
2018-05-21 15:03:54 +02:00
|
|
|
our $ATPHYSADDRESS = ".1.3.6.1.2.1.3.1.1.2";
|
2017-03-08 13:20:00 +01:00
|
|
|
our $DOT1DBASEBRIDGEADDRESS = ".1.3.6.1.2.1.17.1.1.0";
|
|
|
|
our $DOT1DBASEPORTIFINDEX = ".1.3.6.1.2.1.17.1.4.1.2";
|
|
|
|
our $DOT1DTPFDBADDRESS = ".1.3.6.1.2.1.17.4.3.1.1";
|
|
|
|
our $DOT1DTPFDBPORT = ".1.3.6.1.2.1.17.4.3.1.2";
|
|
|
|
our $IFDESC = ".1.3.6.1.2.1.2.2.1.2";
|
2017-03-15 08:46:11 +01:00
|
|
|
our $IFHCINOCTECTS = ".1.3.6.1.2.1.31.1.1.1.6";
|
|
|
|
our $IFHCOUTOCTECTS = ".1.3.6.1.2.1.31.1.1.1.10";
|
2017-03-08 13:20:00 +01:00
|
|
|
our $IFINDEX = ".1.3.6.1.2.1.2.2.1.1";
|
|
|
|
our $IFINOCTECTS = ".1.3.6.1.2.1.2.2.1.10";
|
|
|
|
our $IFOPERSTATUS = ".1.3.6.1.2.1.2.2.1.8";
|
|
|
|
our $IFOUTOCTECTS = ".1.3.6.1.2.1.2.2.1.16";
|
2018-05-21 15:03:54 +02:00
|
|
|
our $IFTYPE = ".1.3.6.1.2.1.2.2.1.3";
|
2017-03-08 13:20:00 +01:00
|
|
|
our $IPENTADDR = ".1.3.6.1.2.1.4.20.1.1";
|
|
|
|
our $IFNAME = ".1.3.6.1.2.1.31.1.1.1.1";
|
|
|
|
our $IFPHYSADDRESS = ".1.3.6.1.2.1.2.2.1.6";
|
|
|
|
our $IPADENTIFINDEX = ".1.3.6.1.2.1.4.20.1.2";
|
2018-05-21 15:03:54 +02:00
|
|
|
our $IPNETTOMEDIAPHYSADDRESS = ".1.3.6.1.2.1.4.22.1.2";
|
2017-03-08 13:20:00 +01:00
|
|
|
our $IPROUTEIFINDEX = ".1.3.6.1.2.1.4.21.1.2";
|
|
|
|
our $IPROUTENEXTHOP = ".1.3.6.1.2.1.4.21.1.7";
|
|
|
|
our $IPROUTETYPE = ".1.3.6.1.2.1.4.21.1.8";
|
|
|
|
our $PRTMARKERINDEX = ".1.3.6.1.2.1.43.10.2.1.1";
|
|
|
|
our $SYSDESCR = ".1.3.6.1.2.1.1.1";
|
|
|
|
our $SYSSERVICES = ".1.3.6.1.2.1.1.7";
|
|
|
|
our $SYSUPTIME = ".1.3.6.1.2.1.1.3";
|
|
|
|
our $VTPVLANIFINDEX = ".1.3.6.1.4.1.9.9.46.1.3.1.1.18.1";
|
2020-03-16 23:52:20 +01:00
|
|
|
our $PEN_OID = ".1.3.6.1.2.1.1.2.0";
|
2017-03-08 13:20:00 +01:00
|
|
|
|
|
|
|
our @ISA = ("Exporter");
|
2019-03-25 17:32:18 +01:00
|
|
|
our %EXPORT_TAGS = ( 'all' => [qw( )] );
|
2017-03-08 13:20:00 +01:00
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
|
|
|
|
our @EXPORT = qw(
|
2019-03-25 17:32:18 +01:00
|
|
|
$DOT1DBASEBRIDGEADDRESS
|
|
|
|
$DOT1DBASEPORTIFINDEX
|
|
|
|
$DOT1DTPFDBADDRESS
|
|
|
|
$DOT1DTPFDBPORT
|
|
|
|
$IFDESC
|
|
|
|
$IFHCINOCTECTS
|
|
|
|
$IFHCOUTOCTECTS
|
|
|
|
$IFINDEX
|
|
|
|
$IFINOCTECTS
|
|
|
|
$IFOPERSTATUS
|
|
|
|
$IFOUTOCTECTS
|
|
|
|
$IPADENTIFINDEX
|
|
|
|
$IPENTADDR
|
|
|
|
$IFNAME
|
|
|
|
$IPNETTOMEDIAPHYSADDRESS
|
|
|
|
$IFPHYSADDRESS
|
|
|
|
$IPADENTIFINDEX
|
|
|
|
$IPROUTEIFINDEX
|
|
|
|
$IPROUTENEXTHOP
|
|
|
|
$IPROUTETYPE
|
|
|
|
$PRTMARKERINDEX
|
|
|
|
$SYSDESCR
|
|
|
|
$SYSSERVICES
|
|
|
|
$SYSUPTIME
|
2017-03-08 13:20:00 +01:00
|
|
|
);
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Create a new ReconTask object.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub new {
|
2020-03-18 10:28:50 +01:00
|
|
|
my $class = shift;
|
|
|
|
|
|
|
|
my $self = {
|
|
|
|
|
|
|
|
# Known aliases (multiple IP addresses for the same host.
|
|
|
|
aliases => {},
|
|
|
|
|
|
|
|
# Keep our own ARP cache to connect hosts to switches/routers.
|
|
|
|
arp_cache => {},
|
|
|
|
|
|
|
|
# Found children.
|
|
|
|
children => {},
|
|
|
|
|
|
|
|
# Working SNMP community for each device.
|
|
|
|
community_cache => {},
|
|
|
|
|
|
|
|
# Cache of deviced discovered.
|
|
|
|
dicovered_cache => {},
|
|
|
|
|
|
|
|
# Connections between devices.
|
|
|
|
connections => {},
|
|
|
|
|
|
|
|
# Devices by type.
|
|
|
|
hosts => [],
|
|
|
|
routers => [],
|
|
|
|
switches => [],
|
|
|
|
|
|
|
|
# Found interfaces.
|
|
|
|
ifaces => {},
|
|
|
|
|
|
|
|
# Found parents.
|
|
|
|
parents => {},
|
|
|
|
|
|
|
|
# Route cache.
|
|
|
|
routes => [],
|
|
|
|
default_gw => undef,
|
|
|
|
|
|
|
|
# SNMP query cache.
|
|
|
|
snmp_cache => {},
|
|
|
|
|
|
|
|
# Globally enable/disable SNMP scans.
|
|
|
|
snmp_enabled => 1,
|
|
|
|
|
|
|
|
# Globally enable/disable WMI scans.
|
|
|
|
wmi_enabled => 0,
|
2020-04-30 15:00:25 +02:00
|
|
|
|
|
|
|
# Globally enable/disable RCMD scans.
|
|
|
|
rcmd_enabled => 0,
|
|
|
|
rcmd_timeout => 4,
|
|
|
|
rcmd_timeout_bin => '/usr/bin/timeout',
|
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
auth_strings_array => [],
|
|
|
|
wmi_timeout => 3,
|
|
|
|
timeout_cmd => '',
|
|
|
|
|
|
|
|
# Switch to switch connections. Used to properly connect hosts
|
|
|
|
# that are connected to a switch wich is in turn connected to another switch,
|
|
|
|
# since the hosts will show up in the latter's switch AFT too.
|
|
|
|
switch_to_switch => {},
|
|
|
|
|
|
|
|
# Visited devices (initially empty).
|
|
|
|
visited_devices => {},
|
|
|
|
|
2020-04-16 14:06:41 +02:00
|
|
|
# Inverse relationship for visited devices (initially empty).
|
|
|
|
addresses => {},
|
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Per device VLAN cache.
|
|
|
|
vlan_cache => {},
|
|
|
|
vlan_cache_enabled => 1, # User configuration. Globally disables the VLAN cache.
|
|
|
|
__vlan_cache_enabled__ => 0, # Internal state. Allows us to enable/disable the VLAN cache on a per SNMP query basis.
|
|
|
|
|
|
|
|
# Configuration parameters.
|
|
|
|
all_ifaces => 0,
|
|
|
|
communities => [],
|
|
|
|
icmp_checks => 2,
|
|
|
|
icmp_timeout => 2,
|
|
|
|
id_os => 0,
|
|
|
|
id_network_profile => 0,
|
|
|
|
nmap => '/usr/bin/nmap',
|
|
|
|
parent_detection => 1,
|
|
|
|
parent_recursion => 5,
|
|
|
|
os_detection => 0,
|
|
|
|
recon_timing_template => 3,
|
|
|
|
recon_ports => '',
|
|
|
|
resolve_names => 0,
|
|
|
|
snmp_auth_user => '',
|
|
|
|
snmp_auth_pass => '',
|
|
|
|
snmp_auth_method => '',
|
|
|
|
snmp_checks => 2,
|
|
|
|
snmp_privacy_method => '',
|
|
|
|
snmp_privacy_pass => '',
|
|
|
|
snmp_security_level => '',
|
|
|
|
snmp_timeout => 2,
|
|
|
|
snmp_version => 1,
|
|
|
|
subnets => [],
|
|
|
|
autoconfiguration_enabled => 0,
|
|
|
|
|
|
|
|
# Store progress summary - Discovery progress view.
|
|
|
|
step => 0,
|
|
|
|
c_network_name => '',
|
|
|
|
c_network_percent => 0.0,
|
|
|
|
summary => {
|
|
|
|
SNMP => 0,
|
|
|
|
WMI => 0,
|
|
|
|
discovered => 0,
|
|
|
|
alive => 0,
|
|
|
|
not_alive => 0
|
|
|
|
},
|
|
|
|
@_,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
# Perform some sanity checks.
|
|
|
|
die("No subnet was specified.") unless defined($self->{'subnets'});
|
|
|
|
|
|
|
|
$self = bless($self, $class);
|
|
|
|
|
|
|
|
# Check SNMP params id SNMP is enabled
|
|
|
|
if ($self->{'snmp_enabled'}) {
|
|
|
|
|
|
|
|
# Check SNMP version
|
|
|
|
if ( $self->{'snmp_version'} ne '1'
|
|
|
|
&& $self->{'snmp_version'} ne '2'
|
|
|
|
&& $self->{'snmp_version'} ne '2c'
|
|
|
|
&& $self->{'snmp_version'} ne '3') {
|
|
|
|
$self->{'snmp_enabled'} = 0;
|
|
|
|
$self->call('message', "SNMP version " . $self->{'snmp_version'} . " not supported (only 1, 2, 2c and 3).", 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Check the version 3 parameters
|
|
|
|
if ($self->{'snmp_version'} eq '3') {
|
|
|
|
|
|
|
|
# Fixed some vars
|
|
|
|
$self->{'communities'} = [];
|
|
|
|
|
|
|
|
# SNMP v3 checks
|
|
|
|
if ( $self->{'snmp_security_level'} ne 'noAuthNoPriv'
|
|
|
|
&&$self->{'snmp_security_level'} ne 'authNoPriv'
|
|
|
|
&&$self->{'snmp_security_level'} ne 'authPriv') {
|
|
|
|
$self->{'snmp_enabled'} = 0;
|
|
|
|
$self->call('message', "Invalid SNMP security level " . $self->{'snmp_security_level'} . ".", 5);
|
|
|
|
}
|
|
|
|
if ($self->{'snmp_privacy_method'} ne 'DES' && $self->{'snmp_privacy_method'} ne 'AES') {
|
|
|
|
$self->{'snmp_enabled'} = 0;
|
|
|
|
$self->call('message', "Invalid SNMP privacy method " . $self->{'snmp_privacy_method'} . ".", 5);
|
|
|
|
}
|
|
|
|
if ($self->{'snmp_auth_method'} ne 'MD5' && $self->{'snmp_auth_method'} ne 'SHA') {
|
|
|
|
$self->{'snmp_enabled'} = 0;
|
|
|
|
$self->call('message', "Invalid SNMP authentication method " . $self->{'snmp_auth_method'} . ".", 5);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
# Fixed some vars
|
|
|
|
$self->{'snmp_auth_user'} = '';
|
|
|
|
$self->{'snmp_auth_pass'} = '';
|
|
|
|
$self->{'snmp_auth_method'} = '';
|
|
|
|
$self->{'snmp_privacy_method'} = '';
|
|
|
|
$self->{'snmp_privacy_pass'} = '';
|
|
|
|
$self->{'snmp_security_level'} = '';
|
|
|
|
|
|
|
|
# Disable SNMP scans if no community was given.
|
|
|
|
if (ref($self->{'communities'}) ne "ARRAY" || scalar(@{$self->{'communities'}}) == 0) {
|
|
|
|
$self->{'snmp_enabled'} = 0;
|
|
|
|
$self->call('message', "There is no SNMP community configured.", 5);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Prepare auth array.
|
|
|
|
# WMI could be launched with '-N' - no pass - argument.
|
|
|
|
if ($self->{'wmi_enabled'} == 1){
|
|
|
|
if (defined($self->{'auth_strings_str'})) {
|
|
|
|
@{$self->{'auth_strings_array'}} = split(',', $self->{'auth_strings_str'});
|
|
|
|
}
|
|
|
|
|
|
|
|
# Timeout available only in linux environments.
|
|
|
|
if ($^O =~ /lin/i && defined($self->{'plugin_exec'}) && defined($self->{'wmi_timeout'})) {
|
|
|
|
$self->{'timeout_cmd'} = $self->{'plugin_exec'}.' '.$self->{'wmi_timeout'}.' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Remove all snmp related values if disabled
|
|
|
|
if (!$self->{'snmp_enabled'}) {
|
|
|
|
$self->{'communities'} = [];
|
|
|
|
$self->{'snmp_auth_user'} = '';
|
|
|
|
$self->{'snmp_auth_pass'} = '';
|
|
|
|
$self->{'snmp_auth_method'} = '';
|
|
|
|
$self->{'snmp_privacy_method'} = '';
|
|
|
|
$self->{'snmp_privacy_pass'} = '';
|
|
|
|
$self->{'snmp_security_level'} = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Add an address to a device.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub add_addresses($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $ip_address) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'visited_devices'}->{$device}->{'addr'}->{$ip_address} = '';
|
2020-04-16 14:06:41 +02:00
|
|
|
|
|
|
|
# Inverse relationship.
|
|
|
|
$self->{'addresses'}{$ip_address} = $device;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-04-16 14:06:41 +02:00
|
|
|
# Update IP references.
|
|
|
|
if (ref($self->{'agents_found'}{$device}) eq 'HASH') {
|
|
|
|
my @addresses = $self->get_addresses($device);
|
|
|
|
$self->{'agents_found'}{$device}{'other_ips'} = \@addresses;
|
|
|
|
$self->call('message', 'New IP detected for '.$device.': '.$ip_address, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Get main address from given address (multi addressed devices).
|
|
|
|
################################################################################
|
|
|
|
sub get_main_address($$) {
|
|
|
|
my ($self, $addr) = @_;
|
|
|
|
|
|
|
|
return $self->{'addresses'}{$addr};
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Add a MAC/IP address to the ARP cache.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub add_mac($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $mac, $ip_addr) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$mac = parse_mac($mac);
|
|
|
|
$self->{'arp_cache'}->{$mac} = $ip_addr;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Add an interface/MAC to the interface cache.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub add_iface($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $iface, $mac) = @_;
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$iface =~ s/"//g;
|
|
|
|
$self->{'ifaces'}->{$mac} = $iface;
|
2017-03-23 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Discover connectivity from address forwarding tables.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub aft_connectivity($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $switch) = @_;
|
|
|
|
my (%mac_temp, @aft_temp);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return unless ($self->is_snmp_discovered($switch));
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->enable_vlan_cache();
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the address forwarding table (AFT) of each switch.
|
|
|
|
my @aft;
|
|
|
|
foreach my $mac ($self->snmp_get_value_array($switch, $DOT1DTPFDBADDRESS)) {
|
|
|
|
push(@aft, parse_mac($mac));
|
|
|
|
}
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Search for matching entries.
|
|
|
|
foreach my $aft_mac (@aft) {
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Do we know who this is?
|
|
|
|
my $host = $self->get_ip_from_mac($aft_mac);
|
|
|
|
next unless defined($host) and $host ne '';
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the name of the host interface if available.
|
|
|
|
my $host_if_name = $self->get_iface($aft_mac);
|
2020-03-25 18:02:58 +01:00
|
|
|
$host_if_name = defined($host_if_name) ? $host_if_name : 'Host Alive';
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the interface associated to the port were we found the MAC address.
|
|
|
|
my $switch_if_name = $self->get_if_from_aft($switch, $aft_mac);
|
|
|
|
next unless defined($switch_if_name) and ($switch_if_name ne '');
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Do not connect a host to a switch twice using the same interface.
|
|
|
|
# The switch is probably connected to another switch.
|
|
|
|
next if ($self->is_switch_connected($host, $host_if_name));
|
|
|
|
$self->mark_switch_connected($host, $host_if_name);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# The switch and the host are already connected.
|
|
|
|
next if ($self->are_connected($switch, $switch_if_name, $host, $host_if_name));
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Connect!
|
|
|
|
$self->mark_connected($switch, $switch_if_name, $host, $host_if_name);
|
|
|
|
$self->call('message', "Switch $switch (if $switch_if_name) is connected to host $host (if $host_if_name).", 5);
|
|
|
|
}
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->disable_vlan_cache();
|
2017-03-23 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Return 1 if the given devices are connected to each other, 0 otherwise.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub are_connected($$$$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $dev_1, $if_1, $dev_2, $if_2) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check for aliases!
|
|
|
|
$dev_1 = $self->{'aliases'}->{$dev_1} if defined($self->{'aliases'}->{$dev_1});
|
|
|
|
$dev_2 = $self->{'aliases'}->{$dev_2} if defined($self->{'aliases'}->{$dev_2});
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-19 14:11:09 +01:00
|
|
|
# Use Host Alive modules when interfaces are unknown.
|
|
|
|
$if_1 = "Host Alive" if $if_1 eq '';
|
|
|
|
$if_2 = "Host Alive" if $if_2 eq '';
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if ( defined($self->{'connections'}->{"${dev_1}\t${if_1}\t${dev_2}\t${if_2}"})
|
|
|
|
||defined($self->{'connections'}->{"${dev_2}\t${if_2}\t${dev_1}\t${if_1}"})) {
|
|
|
|
return 1;
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 0;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-16 23:52:20 +01:00
|
|
|
################################################################################
|
2020-03-26 13:44:08 +01:00
|
|
|
# Initialize tmp pool for addr.
|
|
|
|
# Already discovered by scan_subnet. Registration only.
|
2020-03-16 23:52:20 +01:00
|
|
|
################################################################################
|
|
|
|
sub icmp_discovery($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $addr) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-05-13 16:43:20 +02:00
|
|
|
# Create an agent for the device and add it to the list of known hosts.
|
|
|
|
push(@{$self->{'hosts'}}, $addr);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-05-13 16:43:20 +02:00
|
|
|
# Create an agent for the device and add it to the list of known hosts.
|
|
|
|
$self->add_agent($addr);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-19 14:11:09 +01:00
|
|
|
$self->add_module($addr,
|
2020-03-18 10:28:50 +01:00
|
|
|
{
|
|
|
|
'ip_target' => $addr,
|
|
|
|
'name' => "Host Alive",
|
|
|
|
'description' => '',
|
2020-03-18 19:31:58 +01:00
|
|
|
'type' => 'remote_icmp_proc',
|
2020-03-18 10:28:50 +01:00
|
|
|
'id_modulo' => 2,
|
|
|
|
}
|
|
|
|
);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Discover as much information as possible from the given device using SNMP.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub snmp_discovery($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Have we already visited this device?
|
|
|
|
return if ($self->is_visited($device));
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Mark the device as visited.
|
|
|
|
$self->mark_visited($device);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Are SNMP scans enabled?
|
|
|
|
if ($self->{'snmp_enabled'} == 1) {
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Try to find the MAC with an ARP request.
|
|
|
|
$self->get_mac_from_ip($device);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check if the device responds to SNMP.
|
|
|
|
if ($self->snmp_responds($device)) {
|
|
|
|
$self->{'summary'}->{'SNMP'} += 1;
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Fill the VLAN cache.
|
|
|
|
$self->find_vlans($device);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Guess the device type.
|
|
|
|
$self->guess_device_type($device);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Find aliases for the device.
|
|
|
|
$self->find_aliases($device);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Find interfaces for the device.
|
|
|
|
$self->find_ifaces($device);
|
2018-05-21 15:03:54 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check remote ARP caches.
|
|
|
|
$self->remote_arp($device);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get PEN.
|
|
|
|
$self->snmp_pen($device);
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Try to call the given function on the given object.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub call {
|
2020-03-18 10:28:50 +01:00
|
|
|
my $self = shift;
|
|
|
|
my $func = shift;
|
|
|
|
my @params = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if ($self->can($func)) {
|
|
|
|
$self->$func(@params);
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Disable the VLAN cache.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub disable_vlan_cache($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'__vlan_cache_enabled__'} = 0;
|
2017-03-23 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Enable the VLAN cache.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub enable_vlan_cache($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return if ($self->{'vlan_cache_enabled'} == 0);
|
|
|
|
$self->{'__vlan_cache_enabled__'} = 1;
|
2017-03-23 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Connect the given hosts to its gateway.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub gateway_connectivity($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $host) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my $gw = $self->get_gateway($host);
|
|
|
|
return unless defined($gw);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check for aliases!
|
|
|
|
$host = $self->{'aliases'}->{$host} if defined($self->{'aliases'}->{$host});
|
|
|
|
$gw = $self->{'aliases'}->{$gw} if defined($self->{'aliases'}->{$gw});
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Same host, different IP addresses.
|
|
|
|
return if ($host eq $gw);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->call('message', "Host $host is reached via gateway $gw.", 5);
|
|
|
|
$self->mark_connected($gw, '', $host, '');
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Find IP address aliases for the given device.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub find_aliases($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get ARP cache.
|
|
|
|
my @ip_addresses = $self->snmp_get_value_array($device, $IPENTADDR);
|
|
|
|
foreach my $ip_address (@ip_addresses) {
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Skip broadcast and localhost addresses.
|
|
|
|
next if ($ip_address =~ m/\.255$|\.0$|127\.0\.0\.1$/);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Sometimes we find the same IP address we had.
|
|
|
|
next if ($ip_address eq $device);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->add_addresses($device, $ip_address);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Try to find the MAC with an ARP request.
|
|
|
|
$self->get_mac_from_ip($ip_address);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->call('message', "Found address $ip_address for host $device.", 5);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Is this address an alias itself?
|
|
|
|
$device = $self->{'aliases'}->{$device} if defined($self->{'aliases'}->{$device});
|
|
|
|
next if ($ip_address eq $device);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Link the two addresses.
|
|
|
|
$self->{'aliases'}->{$ip_address} = $device;
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Find all the interfaces for the given host.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub find_ifaces($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Does it respond to SNMP?
|
|
|
|
return unless ($self->is_snmp_discovered($device));
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my @output = $self->snmp_get_value_array($device, $PandoraFMS::Recon::Base::IFINDEX);
|
|
|
|
foreach my $if_index (@output) {
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
next unless ($if_index =~ /^[0-9]+$/);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Ignore virtual interfaces.
|
|
|
|
next if ($self->get_if_type($device, $if_index) eq '53');
|
2018-05-21 15:03:54 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the MAC.
|
|
|
|
my $mac = $self->get_if_mac($device, $if_index);
|
|
|
|
next unless (defined($mac) && $mac ne '');
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Save it.
|
|
|
|
$self->add_mac($mac, $device);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the name of the network interface.
|
|
|
|
my $if_name = $self->snmp_get_value($device, "$PandoraFMS::Recon::Base::IFNAME.$if_index");
|
|
|
|
next unless defined($if_name);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Save it.
|
|
|
|
$self->add_iface($if_name, $mac);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->call('message', "Found interface $if_name MAC $mac for host $device.", 5);
|
|
|
|
}
|
2017-03-23 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Find the device's VLANs and fill the VLAN cache.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub find_vlans ($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
|
|
|
my %vlan_hash;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
foreach my $vlan ($self->snmp_get_value_array($device, $VTPVLANIFINDEX)) {
|
|
|
|
next if $vlan eq '0';
|
|
|
|
$vlan_hash{$vlan} = 1;
|
|
|
|
}
|
|
|
|
my @vlans = keys(%vlan_hash);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'vlan_cache'}->{$device} = [];
|
|
|
|
push(@{$self->{'vlan_cache'}->{$device}}, @vlans) if (scalar(@vlans) > 0);
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Return the addresses of the given device as an array.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_addresses($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if (defined($self->{'visited_devices'}->{$device})) {
|
|
|
|
return keys(%{$self->{'visited_devices'}->{$device}->{'addr'}});
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# By default return the given address.
|
|
|
|
return ($device);
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Return a device structure from an IP address.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_device($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $addr) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if (defined($self->{'visited_devices'}->{$addr})) {
|
|
|
|
return $self->{'visited_devices'}->{$addr};
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return undef;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Get the SNMP community of the given device. Returns undef if no community was found.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_community($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return '' if ($self->{'snmp_version'} eq "3");
|
2018-09-17 14:36:52 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if (defined($self->{'community_cache'}->{$device})) {
|
|
|
|
return $self->{'community_cache'}->{$device};
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return '';
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Return the connection hash.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_connections($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return $self->{'connections'};
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-25 18:02:58 +01:00
|
|
|
################################################################################
|
|
|
|
# Return the PEN associated to target host.
|
|
|
|
################################################################################
|
|
|
|
sub get_pen($$) {
|
|
|
|
my ($self, $host) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-25 18:02:58 +01:00
|
|
|
return undef unless ref($self->{'pen'}) eq 'HASH';
|
|
|
|
|
|
|
|
return $self->{'pen'}->{$host};
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Return the parent relationship hash.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub get_parents($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self) = @_;
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return $self->{'parents'};
|
2017-03-23 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Get the type of the given device.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_device_type($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if (defined($self->{'visited_devices'}->{$device})) {
|
2020-03-30 14:28:10 +02:00
|
|
|
if (defined($self->{'visited_devices'}->{$device}->{'type'})) {
|
|
|
|
return $self->{'visited_devices'}->{$device}->{'type'};
|
|
|
|
} else {
|
|
|
|
$self->{'visited_devices'}->{$device}->{'type'} = 'host';
|
|
|
|
}
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Assume 'host' by default.
|
|
|
|
return 'host';
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Return all known hosts that are not switches or routers.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_hosts($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return $self->{'hosts'};
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Add an interface/MAC to the interface cache.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub get_iface($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $mac) = @_;
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return undef unless defined($self->{'ifaces'}->{$mac});
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return $self->{'ifaces'}->{$mac};
|
2017-03-23 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Get an interface name from an AFT entry. Returns undef on error.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_if_from_aft($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $switch, $mac) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the port associated to the MAC.
|
|
|
|
my $port = $self->snmp_get_value($switch, "$DOT1DTPFDBPORT." . mac_to_dec($mac));
|
|
|
|
return '' unless defined($port);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the interface index associated to the port.
|
|
|
|
my $if_index = $self->snmp_get_value($switch, "$DOT1DBASEPORTIFINDEX.$port");
|
|
|
|
return '' unless defined($if_index);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the interface name.
|
|
|
|
my $if_name = $self->snmp_get_value($switch, "$IFNAME.$if_index");
|
|
|
|
return "if$if_index" unless defined($if_name);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$if_name =~ s/"//g;
|
|
|
|
return $if_name;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Get an interface name from an IP address.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_if_from_ip($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $ip_addr) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the port associated to the IP address.
|
|
|
|
my $if_index = $self->snmp_get_value($device, "$IPROUTEIFINDEX.$ip_addr");
|
|
|
|
return '' unless defined($if_index);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the name of the interface associated to the port.
|
|
|
|
my $if_name = $self->snmp_get_value($device, "$IFNAME.$if_index");
|
|
|
|
return '' unless defined($if_name);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$if_name =~ s/"//g;
|
|
|
|
return $if_name;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Get an interface name from a MAC address.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_if_from_mac($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $mac) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the port associated to the IP address.
|
|
|
|
my @output = $self->snmp_get($device, $IFPHYSADDRESS);
|
|
|
|
foreach my $line (@output) {
|
|
|
|
chomp($line);
|
|
|
|
next unless $line =~ /^IFPHYSADDRESS.(\S+)\s+=\s+\S+:\s+(.*)$/;
|
|
|
|
my ($if_index, $if_mac) = ($1, $2);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Make sure the MAC addresses match.
|
|
|
|
next unless (mac_matches($mac, $if_mac) == 1);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Pupulate the ARP cache.
|
|
|
|
$self->add_mac($mac, $device);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the name of the interface associated to the port.
|
|
|
|
my $if_name = $self->snmp_get_value($device, "$IFNAME.$if_index");
|
|
|
|
return '' unless defined($if_name);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$if_name =~ s/"//g;
|
|
|
|
return $if_name;
|
|
|
|
}
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return '';
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Get an interface name from a port number. Returns '' on error.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub get_if_from_port($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $switch, $port) = @_;
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the interface index associated to the port.
|
|
|
|
my $if_index = $self->snmp_get_value($switch, "$DOT1DBASEPORTIFINDEX.$port");
|
|
|
|
return '' unless defined($if_index);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the interface name.
|
|
|
|
my $if_name = $self->snmp_get_value($switch, "$IFNAME.$if_index");
|
|
|
|
return "if$if_index" unless defined($if_name);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$if_name =~ s/"//g;
|
|
|
|
return $if_name;
|
2017-03-23 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Returns the IP address of the given interface (by index).
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_if_ip($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $if_index) = @_;
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my @output = $self->snmp_get($device, $IPADENTIFINDEX);
|
|
|
|
foreach my $line (@output) {
|
|
|
|
chomp($line);
|
|
|
|
return $1 if ($line =~ m/^$IPADENTIFINDEX.(\S+)\s+=\s+\S+:\s+$if_index$/);
|
|
|
|
}
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return '';
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Returns the MAC address of the given interface (by index).
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_if_mac($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $if_index) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my $mac = $self->snmp_get_value($device, "$IFPHYSADDRESS.$if_index");
|
|
|
|
return '' unless defined($mac);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Clean-up the MAC address.
|
|
|
|
$mac = parse_mac($mac);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return $mac;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-05-21 15:03:54 +02:00
|
|
|
# Returns the type of the given interface (by index).
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-05-21 15:03:54 +02:00
|
|
|
sub get_if_type($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $if_index) = @_;
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my $type = $self->snmp_get_value($device, "$IFTYPE.$if_index");
|
|
|
|
return '' unless defined($type);
|
2018-05-21 15:03:54 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return $type;
|
2018-05-21 15:03:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Get an IP address from the ARP cache given the MAC address.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_ip_from_mac($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $mac_addr) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if (defined($self->{'arp_cache'}->{$mac_addr})) {
|
|
|
|
return $self->{'arp_cache'}->{$mac_addr};
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return undef;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-03-25 17:32:18 +01:00
|
|
|
# Attemtps to find
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub get_mac_from_ip($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $host) = @_;
|
|
|
|
my $mac = undef;
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
eval {
|
|
|
|
$mac = `arping -c 1 $host 2>$DEVNULL`;
|
|
|
|
$mac = undef unless ($? == 0);
|
|
|
|
};
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return unless defined($mac);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
($mac) = $mac =~ /\[(.*?)\]/ if defined($mac);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Clean-up the MAC address.
|
|
|
|
chomp($mac);
|
|
|
|
$mac = parse_mac($mac);
|
|
|
|
$self->add_mac($mac, $host);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->call('message', "Found MAC $mac for host $host in the local ARP cache.", 5);
|
2017-03-23 14:33:06 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Get a port number from an AFT entry. Returns undef on error.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub get_port_from_aft($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $switch, $mac) = @_;
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get the port associated to the MAC.
|
|
|
|
my $port = $self->snmp_get_value($switch, "$DOT1DTPFDBPORT." . mac_to_dec($mac));
|
|
|
|
return '' unless defined($port);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return $port;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Fill the route cache.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_routes($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Empty the current route cache.
|
|
|
|
$self->{'routes'} = [];
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Parse route's output.
|
|
|
|
my @output = `route -n 2>$DEVNULL`;
|
|
|
|
foreach my $line (@output) {
|
|
|
|
chomp($line);
|
|
|
|
if ($line =~ /^0\.0\.0\.0\s+(\d+\.\d+\.\d+\.\d+).*/) {
|
|
|
|
$self->{'default_gw'} = $1;
|
|
|
|
} elsif ($line =~ /^(\d+\.\d+\.\d+\.\d+)\s+(\d+\.\d+\.\d+\.\d+)\s+(\d+\.\d+\.\d+\.\d+).*/) {
|
|
|
|
push(@{$self->{'routes'}}, { dest => $1, gw => $2, mask => $3 });
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Replace 0.0.0.0 with the default gateway's IP.
|
|
|
|
return unless defined($self->{'default_gw'});
|
|
|
|
foreach my $route (@{$self->{'routes'}}) {
|
|
|
|
$route->{gw} = $self->{'default_gw'} if ($route->{'gw'} eq '0.0.0.0');
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Get the gateway to reach the given host.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_gateway($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $host) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Look for a specific route to the given host.
|
|
|
|
foreach my $route (@{$self->{'routes'}}) {
|
|
|
|
if (subnet_matches($host, $route->{'dest'}, $route->{'mask'})) {
|
|
|
|
return $route->{'gw'};
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Return the default gateway.
|
|
|
|
return $self->{'default_gw'} if defined($self->{'default_gw'});
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Ops!
|
|
|
|
return undef;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Return a pointer to an array containing configured subnets.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_subnets($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return $self->{'subnets'};
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Get an array of all the visited devices.
|
|
|
|
# NOTE: This functions returns the whole device structures, not just address
|
|
|
|
# like get_hosts, get_switches, get_routers and get_all_devices.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_visited_devices($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return $self->{'visited_devices'};
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Returns an array of found VLAN IDs.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub get_vlans($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Disabled in verison 3
|
|
|
|
return () if ($self->{'snmp_version'} eq "3");
|
2018-09-17 14:36:52 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Is the VLAN cache disabled?
|
|
|
|
return () unless ($self->{'__vlan_cache_enabled__'} == 1);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return () unless defined($self->{'vlan_cache'}->{$device});
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return @{$self->{'vlan_cache'}->{$device}};
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Guess the type of the given device.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub guess_device_type($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
|
|
|
|
|
|
|
# Get the value of sysServices.
|
|
|
|
my $services = $self->snmp_get_value($device, "$SYSSERVICES.0");
|
|
|
|
return unless defined($services);
|
|
|
|
|
|
|
|
# Check the individual bits.
|
|
|
|
my @service_bits = split('', unpack('b8', pack('C', $services)));
|
|
|
|
|
|
|
|
# Check for layer 2 connectivity support.
|
|
|
|
my $bridge_mib = $self->snmp_get_value($device, $DOT1DBASEBRIDGEADDRESS);
|
|
|
|
|
|
|
|
# L2?
|
|
|
|
my $device_type;
|
|
|
|
if ($service_bits[1] == 1) {
|
|
|
|
|
|
|
|
# L3?
|
|
|
|
if ($service_bits[2] == 1) {
|
|
|
|
|
|
|
|
# Bridge MIB?
|
|
|
|
if (defined($bridge_mib)) {
|
|
|
|
$device_type = 'switch';
|
|
|
|
} else {
|
|
|
|
|
|
|
|
# L7?
|
|
|
|
if ($service_bits[6] == 1) {
|
|
|
|
$device_type = 'host';
|
|
|
|
} else {
|
|
|
|
$device_type = 'router';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
|
|
|
|
# Bridge MIB?
|
|
|
|
if (defined($bridge_mib)) {
|
|
|
|
$device_type = 'switch';
|
|
|
|
} else {
|
|
|
|
$device_type = 'host';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
|
|
|
|
# L3?
|
|
|
|
if ($service_bits[2] == 1) {
|
|
|
|
|
|
|
|
# L4?
|
|
|
|
if ($service_bits[3] == 1) {
|
|
|
|
$device_type = 'switch';
|
|
|
|
} else {
|
|
|
|
|
|
|
|
# L7?
|
|
|
|
if ($service_bits[6] == 1) {
|
|
|
|
$device_type = 'host';
|
|
|
|
} else {
|
|
|
|
$device_type = 'router';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
|
|
|
|
# Printer MIB?
|
|
|
|
my $printer_mib = $self->snmp_get_value($device, $PRTMARKERINDEX);
|
|
|
|
if (defined($printer_mib)) {
|
|
|
|
$device_type = 'printer';
|
|
|
|
} else {
|
|
|
|
$device_type = 'host';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set the type of the device.
|
|
|
|
$self->set_device_type($device, $device_type);
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-05-21 15:03:54 +02:00
|
|
|
# Return 1 if the given device has children.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-05-21 15:03:54 +02:00
|
|
|
sub has_children($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2018-05-21 15:03:54 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check for aliases!
|
|
|
|
$device = $self->{'aliases'}->{$device} if defined($self->{'aliases'}->{$device});
|
2018-05-21 15:03:54 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 1 if (defined($self->{'children'}->{$device}));
|
2018-05-21 15:03:54 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 0;
|
2018-05-21 15:03:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Return 1 if the given device has a parent.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub has_parent($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check for aliases!
|
|
|
|
$device = $self->{'aliases'}->{$device} if defined($self->{'aliases'}->{$device});
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 1 if (defined($self->{'parents'}->{$device}));
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 0;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Returns 1 if the device belongs to one of the scanned subnets.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub in_subnet($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
|
|
|
$device = ip_to_long($device);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# No subnets specified.
|
|
|
|
return 1 if (scalar(@{$self->{'subnets'}}) <= 0);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
foreach my $subnet (@{$self->{'subnets'}}) {
|
|
|
|
if (subnet_matches($device, $subnet)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 0;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Check for switches that are connected to other switches/routers and show
|
|
|
|
# up in a switch/router's port.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub is_switch_connected($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $iface) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check for aliases!
|
|
|
|
$device = $self->{'aliases'}->{$device} if defined($self->{'aliases'}->{$device});
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 1 if defined($self->{'switch_to_switch'}->{"${device}\t${iface}"});
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 0;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Returns 1 if the given device has already been visited, 0 otherwise.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub is_visited($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check for aliases!
|
|
|
|
$device = $self->{'aliases'}->{$device} if defined($self->{'aliases'}->{$device});
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if (defined($self->{'visited_devices'}->{$device})) {
|
|
|
|
return 1;
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 0;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-09-17 14:36:52 +02:00
|
|
|
# Returns 1 if the given device has responded successfully to a snmp request
|
|
|
|
# Returns 0 otherwise.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-09-17 14:36:52 +02:00
|
|
|
sub is_snmp_discovered($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2018-09-17 14:36:52 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check if device is into discovered cache
|
|
|
|
return (defined($self->{'discovered_cache'}->{$device})) ? 1 : 0;
|
2018-09-17 14:36:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Mark the given devices as connected to each other on the given interfaces.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub mark_connected($$;$$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $parent, $parent_if, $child, $child_if) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check for aliases!
|
|
|
|
$parent = $self->{'aliases'}->{$parent} if defined($self->{'aliases'}->{$parent});
|
|
|
|
$child = $self->{'aliases'}->{$child} if defined($self->{'aliases'}->{$child});
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Use ping modules when interfaces are unknown.
|
|
|
|
$parent_if = "Host Alive" if $parent_if eq '';
|
|
|
|
$child_if = "Host Alive" if $child_if eq '';
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Do not connect devices using ping modules. A parent-child relationship is enough.
|
|
|
|
if ($parent_if ne "Host Alive" || $child_if ne "Host Alive") {
|
|
|
|
$self->{'connections'}->{"${parent}\t${parent_if}\t${child}\t${child_if}"} = 1;
|
|
|
|
$self->call('connect_agents', $parent, $parent_if, $child, $child_if);
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Prevent parent-child loops.
|
|
|
|
if (!defined($self->{'parents'}->{$parent})
|
|
|
|
||$self->{'parents'}->{$parent} ne $child) {
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# A parent-child relationship is always created to help complete the map with
|
|
|
|
# layer 3 information.
|
|
|
|
$self->{'parents'}->{$child} = $parent;
|
|
|
|
$self->{'children'}->{$parent} = $child;
|
|
|
|
$self->call('set_parent', $child, $parent);
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Mark the given switch as having a connection on the given interface.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub mark_switch_connected($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $iface) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check for aliases!
|
|
|
|
$device = $self->{'aliases'}->{$device} if defined($self->{'aliases'}->{$device});
|
|
|
|
$self->{'switch_to_switch'}->{"${device}\t${iface}"} = 1;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Mark the given device as visited.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub mark_visited($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'visited_devices'}->{$device} = {
|
|
|
|
'addr' => { $device => '' },
|
|
|
|
'type' => 'host'
|
|
|
|
};
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-09-17 14:36:52 +02:00
|
|
|
# Mark the given device as snmp discovered.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-09-17 14:36:52 +02:00
|
|
|
sub mark_discovered($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2018-09-17 14:36:52 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'discovered_cache'}->{$device} = 1;
|
2018-09-17 14:36:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-09-17 14:36:52 +02:00
|
|
|
# Validate the configuration for the given device.
|
|
|
|
# Returns 1 if successfull snmp contact, 0 otherwise.
|
|
|
|
# Updates the SNMP community cache on v1, v2 and v2c.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub snmp_responds($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 1 if($self->is_snmp_discovered($device));
|
2018-09-17 14:36:52 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return ($self->{'snmp_version'} eq "3")
|
|
|
|
? $self->snmp_responds_v3($device)
|
|
|
|
: $self->snmp_responds_v122c($device);
|
2018-09-17 14:36:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-09-17 14:36:52 +02:00
|
|
|
# Looks for a working SNMP community for the given device. Returns 1 if one is
|
|
|
|
# found, 0 otherwise. Updates the SNMP community cache.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-09-17 14:36:52 +02:00
|
|
|
sub snmp_responds_v122c($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
foreach my $community (@{$self->{'communities'}}) {
|
2017-03-14 08:27:44 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Clean blanks.
|
|
|
|
$community =~ s/\s+//g;
|
2017-03-14 08:27:44 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my $command = $self->snmp_get_command($device, ".0", $community);
|
|
|
|
`$command`;
|
|
|
|
if ($? == 0) {
|
|
|
|
$self->set_community($device, $community);
|
|
|
|
$self->mark_discovered($device);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 0;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2018-09-17 14:36:52 +02:00
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-09-17 14:36:52 +02:00
|
|
|
# Validate the SNMP v3 configuration for a device.
|
|
|
|
# Returns 1 if successfull snmp contact, 0 otherwise.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-09-17 14:36:52 +02:00
|
|
|
sub snmp_responds_v3($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2018-09-17 14:36:52 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my $command = $self->snmp_get_command($device, ".0");
|
|
|
|
`$command`;
|
2018-09-17 14:36:52 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if ($? == 0) {
|
|
|
|
$self->mark_discovered($device);
|
|
|
|
return 1;
|
|
|
|
}
|
2018-09-17 14:36:52 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return 0;
|
2018-09-17 14:36:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-05-21 15:03:54 +02:00
|
|
|
# Parse the local ARP cache.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-05-21 15:03:54 +02:00
|
|
|
sub local_arp($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self) = @_;
|
2018-05-21 15:03:54 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my @output = `arp -an 2>$DEVNULL`;
|
|
|
|
foreach my $line (@output) {
|
|
|
|
next unless ($line =~ m/\((\S+)\) at ([0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+)/);
|
|
|
|
$self->add_mac(parse_mac($2), $1);
|
|
|
|
}
|
2018-05-21 15:03:54 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-05-21 15:03:54 +02:00
|
|
|
# Parse remote SNMP ARP caches.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-05-21 15:03:54 +02:00
|
|
|
sub remote_arp($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Try to learn more MAC addresses from the device's ARP cache.
|
|
|
|
my @output = $self->snmp_get($device, $IPNETTOMEDIAPHYSADDRESS);
|
|
|
|
foreach my $line (@output) {
|
|
|
|
next unless ($line =~ /^$IPNETTOMEDIAPHYSADDRESS\.\d+\.(\S+)\s+=\s+\S+:\s+(.*)$/);
|
|
|
|
my ($ip_addr, $mac_addr) = ($1, $2);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Skip broadcast, net and local addresses.
|
|
|
|
next if ($ip_addr =~ m/\.255$|\.0$|127\.0\.0\.1$/);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$mac_addr = parse_mac($mac_addr);
|
|
|
|
$self->add_mac($mac_addr, $ip_addr);
|
|
|
|
$self->call('message', "Found MAC $mac_addr for host $ip_addr in the ARP cache of host $device.", 5);
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Look in atPhysAddress for MAC addresses too.
|
|
|
|
@output = $self->snmp_get($device, $ATPHYSADDRESS);
|
|
|
|
foreach my $line (@output) {
|
|
|
|
next unless ($line =~ m/^$ATPHYSADDRESS\.\d+\.\d+\.(\S+)\s+=\s+\S+:\s+(.*)$/);
|
|
|
|
my ($ip_addr, $mac_addr) = ($1, $2);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Skip broadcast, net and local addresses.
|
|
|
|
next if ($ip_addr =~ m/\.255$|\.0$|127\.0\.0\.1$/);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$mac_addr = parse_mac($mac_addr);
|
|
|
|
$self->add_mac($mac_addr, $ip_addr);
|
|
|
|
$self->call('message', "Found MAC $mac_addr for host $ip_addr in the ARP cache (atPhysAddress) of host $device.", 5);
|
|
|
|
}
|
2018-05-21 15:03:54 +02:00
|
|
|
}
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2020-03-16 23:52:20 +01:00
|
|
|
# Add agent to pool (will be registered at the end of the scan).
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2020-03-16 23:52:20 +01:00
|
|
|
sub prepare_agent($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $addr) = @_;
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-04-16 14:06:41 +02:00
|
|
|
# Avoid multi-ip agent. No reference, is first encounter.
|
|
|
|
my $main_address = $self->get_main_address($addr);
|
|
|
|
return unless is_empty($main_address);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-05-13 16:43:20 +02:00
|
|
|
# Resolve hostnames.
|
|
|
|
my $host_name = (($self->{'resolve_names'} == 1) ? gethostbyaddr(inet_aton($addr), AF_INET) : $addr);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-05-13 16:43:20 +02:00
|
|
|
# Fallback to device IP if host name could not be resolved.
|
|
|
|
$host_name = $addr if (!defined($host_name) || $host_name eq '');
|
2020-04-16 12:39:55 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'agents_found'} = {} if ref($self->{'agents_found'}) ne 'HASH';
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Already initialized.
|
2020-04-30 16:53:45 +02:00
|
|
|
return if ref($self->{'agents_found'}->{$addr}) eq 'HASH';
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-04-16 14:06:41 +02:00
|
|
|
my @addresses = $self->get_addresses($addr);
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'agents_found'}->{$addr} = {
|
|
|
|
'agent' => {
|
2020-04-16 12:39:55 +02:00
|
|
|
'nombre' => $host_name,
|
2020-03-18 10:28:50 +01:00
|
|
|
'direccion' => $addr,
|
2020-04-16 12:39:55 +02:00
|
|
|
'alias' => $host_name,
|
2020-03-18 10:28:50 +01:00
|
|
|
},
|
2020-04-16 14:06:41 +02:00
|
|
|
'other_ips' => \@addresses,
|
2020-03-18 10:28:50 +01:00
|
|
|
'pen' => $self->{'pen'}{$addr},
|
|
|
|
'modules' => [],
|
|
|
|
};
|
2020-03-16 23:52:20 +01:00
|
|
|
}
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-16 23:52:20 +01:00
|
|
|
################################################################################
|
|
|
|
# Add agent to pool (will be registered at the end of the scan).
|
|
|
|
################################################################################
|
|
|
|
sub add_agent($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $addr) = @_;
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-04-08 16:06:39 +02:00
|
|
|
# Avoid create empty agents.
|
|
|
|
return if is_empty($addr);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->prepare_agent($addr);
|
2020-03-16 23:52:20 +01:00
|
|
|
}
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-16 23:52:20 +01:00
|
|
|
################################################################################
|
|
|
|
# Add module to agent (tmp pool) (will be registered at the end of the scan).
|
|
|
|
################################################################################
|
2020-03-19 14:11:09 +01:00
|
|
|
sub add_module($$$) {
|
|
|
|
my ($self, $agent, $data) = @_;
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->prepare_agent($agent);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-19 14:11:09 +01:00
|
|
|
$self->{'agents_found'}->{$agent}->{'modules'} = {}
|
|
|
|
unless ref($self->{'agents_found'}->{$agent}->{'modules'}) eq 'HASH';
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-19 14:11:09 +01:00
|
|
|
# Test module. Is it well defined?
|
|
|
|
return unless ref($data) eq 'HASH' && defined($data->{'name'})
|
|
|
|
&& $data->{'name'} ne '';
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-04-06 18:04:53 +02:00
|
|
|
# Test module. Is it success? Some components have MIB name instead OID.
|
|
|
|
$self->{'translate_snmp'} = 1;
|
2020-03-25 18:02:58 +01:00
|
|
|
my $rs = $self->call('test_module', $agent, $data);
|
2020-04-06 18:04:53 +02:00
|
|
|
$self->{'translate_snmp'} = 0;
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-25 18:02:58 +01:00
|
|
|
return unless is_enabled($rs);
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-19 14:11:09 +01:00
|
|
|
$self->{'agents_found'}->{$agent}->{'modules'}{$data->{'name'}} = $data;
|
2020-03-18 10:28:50 +01:00
|
|
|
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-16 23:52:20 +01:00
|
|
|
################################################################################
|
|
|
|
# Test target address (methods).
|
|
|
|
################################################################################
|
|
|
|
sub test_capabilities($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $addr) = @_;
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->icmp_discovery($addr);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if (is_enabled($self->{'snmp_enabled'})) {
|
|
|
|
# SNMP discovery.
|
|
|
|
$self->snmp_discovery($addr);
|
|
|
|
}
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# WMI discovery.
|
|
|
|
if (is_enabled($self->{'wmi_enabled'})) {
|
|
|
|
# Add wmi scan if enabled.
|
2020-03-26 13:44:08 +01:00
|
|
|
$self->wmi_discovery($addr);
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
2019-04-10 20:40:03 +02:00
|
|
|
|
2020-05-04 09:52:15 +02:00
|
|
|
# RCMD discovery.
|
2020-04-30 15:00:25 +02:00
|
|
|
if (is_enabled($self->{'rcmd_enabled'})) {
|
|
|
|
# Add wmi scan if enabled.
|
|
|
|
$self->rcmd_discovery($addr);
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
# Scan the given subnet.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-23 14:33:06 +01:00
|
|
|
sub scan_subnet($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self) = @_;
|
|
|
|
my $progress = 1;
|
|
|
|
|
|
|
|
my @subnets = @{$self->get_subnets()};
|
|
|
|
foreach my $subnet (@subnets) {
|
|
|
|
$self->{'c_network_percent'} = 0;
|
|
|
|
$self->{'c_network_name'} = $subnet;
|
|
|
|
$self->call('update_progress', ceil($progress));
|
|
|
|
|
|
|
|
# Clean blanks.
|
|
|
|
$subnet =~ s/\s+//g;
|
|
|
|
|
|
|
|
my $net_addr = new NetAddr::IP($subnet);
|
|
|
|
if (!defined($net_addr)) {
|
|
|
|
$self->call('message', "Invalid network: $subnet", 3);
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Save the network and broadcast addresses.
|
|
|
|
my $network = $net_addr->network();
|
|
|
|
my $broadcast = $net_addr->broadcast();
|
|
|
|
|
|
|
|
my @hosts = map { (split('/', $_))[0] } $net_addr->hostenum;
|
|
|
|
my $total_hosts = scalar(@hosts);
|
|
|
|
my %hosts_alive = ();
|
|
|
|
|
|
|
|
# By default 200, (20 * 10)
|
|
|
|
my $host_block_size = $self->{'block_size'};
|
|
|
|
|
2020-04-01 18:16:57 +02:00
|
|
|
$host_block_size = 50 unless defined($self->{'block_size'});
|
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# The first 50% of the recon task approx.
|
2020-03-18 19:31:58 +01:00
|
|
|
my $step = 25.0 / scalar(@subnets) / (($total_hosts / $host_block_size)+1);
|
2020-03-18 10:28:50 +01:00
|
|
|
my $subnet_step = 50.0 / (($total_hosts / $host_block_size)+1);
|
|
|
|
|
|
|
|
for (my $block_index=0;
|
|
|
|
$block_index < $total_hosts;
|
|
|
|
$block_index += $host_block_size
|
|
|
|
) {
|
|
|
|
# Update the recon task
|
|
|
|
# Increase self summary.alive hosts.
|
|
|
|
$self->call('message', "Searching for hosts (".$block_index." / ".$total_hosts.")", 5);
|
|
|
|
my $to = $host_block_size + $block_index;
|
|
|
|
$to = $total_hosts if $to >= $total_hosts;
|
|
|
|
|
|
|
|
my $c_block_size = $to - $block_index;
|
|
|
|
my @block = pandora_block_ping(
|
|
|
|
{
|
|
|
|
'fping' => $self->{'fping'},
|
2020-03-19 14:11:09 +01:00
|
|
|
'networktimeout' => 0.5 # use fping defaults
|
2020-03-18 10:28:50 +01:00
|
|
|
},
|
|
|
|
@hosts[$block_index .. $to - 1]
|
|
|
|
);
|
|
|
|
|
|
|
|
# check alive hosts in current block
|
|
|
|
%hosts_alive = (
|
|
|
|
%hosts_alive,
|
|
|
|
map {chomp; $_ => 1} @block
|
|
|
|
);
|
|
|
|
|
|
|
|
$self->{'summary'}->{'not_alive'} += $c_block_size - (scalar @block);
|
|
|
|
$self->{'summary'}->{'alive'} += scalar @block;
|
|
|
|
|
|
|
|
# Update progress.
|
|
|
|
$progress += $step;
|
|
|
|
$self->{'c_network_percent'} += $subnet_step;
|
|
|
|
|
|
|
|
# Populate.
|
|
|
|
$self->call('update_progress', ceil($progress));
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update progress.
|
|
|
|
$self->call('message', "Searching for hosts (".$total_hosts." / ".$total_hosts.")", 5);
|
|
|
|
$progress = ceil($progress);
|
|
|
|
$self->{'c_network_percent'} = 50;
|
|
|
|
|
|
|
|
# Populate.
|
|
|
|
$self->call('update_progress', ceil($progress));
|
|
|
|
|
|
|
|
$total_hosts = scalar keys %hosts_alive;
|
2020-04-08 13:01:11 +02:00
|
|
|
if ($total_hosts == 0) {
|
|
|
|
# Populate.
|
|
|
|
$self->{'c_network_percent'} += 50;
|
|
|
|
$self->call('update_progress', ceil($progress)+25);
|
|
|
|
next;
|
|
|
|
}
|
2020-03-18 19:31:58 +01:00
|
|
|
$step = 25.0 / scalar(@subnets) / $total_hosts;
|
2020-03-18 10:28:50 +01:00
|
|
|
$subnet_step = 50.0 / $total_hosts;
|
2020-03-25 18:02:58 +01:00
|
|
|
|
|
|
|
$self->{'step'} = STEP_CAPABILITIES;
|
2020-03-18 10:28:50 +01:00
|
|
|
foreach my $addr (keys %hosts_alive) {
|
|
|
|
# Increase self summary.alive hosts.
|
|
|
|
$self->call('message', "Scanning host: $addr", 5);
|
2020-03-25 18:02:58 +01:00
|
|
|
$self->{'c_network_name'} = $addr;
|
2020-03-18 10:28:50 +01:00
|
|
|
|
|
|
|
# Update progress.
|
|
|
|
$progress += $step;
|
|
|
|
$self->{'c_network_percent'} += $subnet_step;
|
|
|
|
|
|
|
|
# Populate.
|
|
|
|
$self->call('update_progress', ceil($progress));
|
|
|
|
|
2020-05-04 11:13:15 +02:00
|
|
|
# Filter by port (if enabled).
|
|
|
|
if (!is_empty($self->{'recon_ports'})) {
|
|
|
|
next unless $self->call("tcp_scan", $addr) > 0;
|
|
|
|
}
|
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Enable/ disable capabilities.
|
|
|
|
$self->test_capabilities($addr);
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-04-10 20:40:03 +02:00
|
|
|
# Perform a Cloud scan
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-04-10 20:40:03 +02:00
|
|
|
sub cloud_scan($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my $self = shift;
|
|
|
|
my ($progress, $step);
|
2019-04-10 20:40:03 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my $type = '';
|
2019-04-10 20:40:03 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if ( $self->{'task_data'}->{'type'} == DISCOVERY_CLOUD_AWS_EC2
|
|
|
|
|| $self->{'task_data'}->{'type'} == DISCOVERY_CLOUD_AWS_RDS) {
|
|
|
|
$type = 'Aws';
|
|
|
|
} else {
|
2019-11-05 15:38:17 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Unrecognized task type.
|
|
|
|
$self->call('message', 'Unrecognized task type', 1);
|
|
|
|
$self->call('update_progress', -1);
|
|
|
|
return;
|
|
|
|
}
|
2019-04-10 20:40:03 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Initialize cloud object.
|
|
|
|
my $cloudObj = PandoraFMS::Recon::Util::enterprise_new(
|
|
|
|
'PandoraFMS::Recon::Cloud::'.$type,
|
|
|
|
[
|
|
|
|
task_data => $self->{'task_data'},
|
|
|
|
aws_access_key_id => $self->{'aws_access_key_id'},
|
|
|
|
aws_secret_access_key => $self->{'aws_secret_access_key'},
|
|
|
|
cloud_util_path => $self->{'cloud_util_path'},
|
|
|
|
creds_file => $self->{'creds_file'},
|
|
|
|
parent => $self
|
|
|
|
]
|
2019-04-10 20:40:03 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
);
|
2019-04-10 20:40:03 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if (!$cloudObj) {
|
2019-11-05 15:38:17 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Failed to initialize, check Cloud credentials or anything.
|
|
|
|
$self->call('message', 'Unable to initialize PandoraFMS::Recon::Cloud::'.$type, 3);
|
|
|
|
} else {
|
2019-11-05 15:38:17 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Let Cloud object manage scan.
|
|
|
|
$cloudObj->scan();
|
|
|
|
}
|
2019-04-10 20:40:03 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Update progress.
|
|
|
|
# Done!
|
|
|
|
$self->{'step'} = '';
|
|
|
|
$self->call('update_progress', -1);
|
2019-04-10 20:40:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-11-04 19:52:38 +01:00
|
|
|
# Performs a database scan.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-11-04 19:52:38 +01:00
|
|
|
sub database_scan($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $type, $obj, $global_percent, $targets) = @_;
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my @data;
|
|
|
|
my @modules;
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my $dbObjCfg = $obj->get_config();
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'summary'}->{'discovered'} += 1;
|
|
|
|
$self->{'summary'}->{'alive'} += 1;
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
push @modules,
|
|
|
|
{
|
|
|
|
name => $type . ' connection',
|
|
|
|
type => 'generic_proc',
|
|
|
|
data => 1,
|
|
|
|
description => $type . ' availability'
|
|
|
|
};
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Analyze.
|
|
|
|
$self->{'step'} = STEP_STATISTICS;
|
|
|
|
$self->{'c_network_percent'} = 30;
|
|
|
|
$self->call('update_progress', $global_percent + (30 / (scalar @$targets)));
|
|
|
|
$self->{'c_network_name'} = $obj->get_host();
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Retrieve connection statistics.
|
|
|
|
# Retrieve uptime statistics
|
|
|
|
# Retrieve query stats
|
|
|
|
# Retrieve connections
|
|
|
|
# Retrieve innodb
|
|
|
|
# Retrieve cache
|
|
|
|
$self->{'c_network_percent'} = 50;
|
|
|
|
$self->call('update_progress', $global_percent + (50 / (scalar @$targets)));
|
|
|
|
push @modules, $obj->get_statistics();
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Custom queries.
|
|
|
|
$self->{'step'} = STEP_CUSTOM_QUERIES;
|
|
|
|
$self->{'c_network_percent'} = 80;
|
|
|
|
$self->call('update_progress', $global_percent + (80 / (scalar @$targets)));
|
|
|
|
push @modules, $obj->execute_custom_queries();
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if (defined($dbObjCfg->{'scan_databases'})
|
|
|
|
&& "$dbObjCfg->{'scan_databases'}" eq "1") {
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Skip database scan in Oracle tasks
|
|
|
|
next if defined($self->{'type'}) && $self->{'type'} == DISCOVERY_APP_ORACLE;
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-06-11 14:53:34 +02:00
|
|
|
# Skip database scan in DB2 tasks
|
|
|
|
next if defined($self->{'type'}) && $self->{'type'} == DISCOVERY_APP_DB2;
|
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my $__data = $obj->scan_databases();
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if (ref($__data) eq "ARRAY") {
|
|
|
|
if (defined($dbObjCfg->{'agent_per_database'})
|
|
|
|
&& $dbObjCfg->{'agent_per_database'} == 1) {
|
2019-11-05 15:38:17 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Agent per database detected.
|
|
|
|
push @data, @{$__data};
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
} else {
|
2019-11-05 15:38:17 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Merge modules into engine agent.
|
|
|
|
my @_modules = map {
|
|
|
|
map { $_ }
|
|
|
|
@{$_->{'module_data'}}
|
|
|
|
} @{$__data};
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
push @modules, @_modules;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 19:52:38 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return {
|
|
|
|
'modules' => \@modules,
|
|
|
|
'data' => \@data
|
|
|
|
};
|
2019-11-04 19:52:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-04-09 18:33:02 +02:00
|
|
|
# Perform an Application scan.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-04-09 18:33:02 +02:00
|
|
|
sub app_scan($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self) = @_;
|
|
|
|
my ($progress, $step);
|
|
|
|
|
|
|
|
my $type = '';
|
|
|
|
my $db_scan = 0;
|
|
|
|
|
|
|
|
# APP object initialization.
|
|
|
|
if ($self->{'task_data'}->{'type'} == DISCOVERY_APP_MYSQL) {
|
|
|
|
$type = 'MySQL';
|
|
|
|
} elsif ($self->{'task_data'}->{'type'} == DISCOVERY_APP_ORACLE) {
|
|
|
|
$type = 'Oracle';
|
2020-06-11 14:53:34 +02:00
|
|
|
} elsif ($self->{'task_data'}->{'type'} == DISCOVERY_APP_DB2) {
|
|
|
|
$type = 'DB2';
|
2020-06-17 13:41:09 +02:00
|
|
|
} elsif ($self->{'task_data'}->{'type'} == DISCOVERY_APP_MICROSOFT_SQL_SERVER) {
|
|
|
|
$type = 'MSSQL';
|
2020-03-18 10:28:50 +01:00
|
|
|
} elsif ($self->{'task_data'}->{'type'} == DISCOVERY_APP_SAP) {
|
|
|
|
$type = 'SAP';
|
|
|
|
} else {
|
|
|
|
# Unrecognized task type.
|
|
|
|
$self->call('message', 'Unrecognized task type', 1);
|
|
|
|
$self->call('update_progress', -1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @targets = split /,/, $self->{'task_data'}->{'subnet'};
|
|
|
|
|
|
|
|
my $global_step = 100 / (scalar @targets);
|
|
|
|
my $global_percent = 0;
|
|
|
|
my $i = 0;
|
|
|
|
foreach my $target (@targets) {
|
|
|
|
if ( !defined($target)
|
|
|
|
|| $target eq ''
|
|
|
|
|| $target =~ /^#/) {
|
|
|
|
# Ignore empty target or commented one.
|
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
my @data;
|
|
|
|
my @modules;
|
|
|
|
|
|
|
|
$self->{'step'} = STEP_APP_SCAN;
|
|
|
|
$self->{'c_network_name'} = $target;
|
|
|
|
$self->{'c_network_percent'} = 0;
|
|
|
|
|
|
|
|
# Send message
|
|
|
|
$self->call('message', 'Checking target ' . $target, 10);
|
|
|
|
|
|
|
|
# Force target acquirement.
|
|
|
|
$self->{'task_data'}->{'dbhost'} = $target;
|
|
|
|
$self->{'task_data'}->{'target_index'} = $i++;
|
|
|
|
|
|
|
|
# Update progress
|
|
|
|
$self->{'c_network_percent'} = 10;
|
|
|
|
$self->call('update_progress', $global_percent + (10 / (scalar @targets)));
|
|
|
|
|
|
|
|
# Connect to target.
|
|
|
|
my $obj = PandoraFMS::Recon::Util::enterprise_new(
|
|
|
|
'PandoraFMS::Recon::Applications::'.$type,
|
|
|
|
{
|
|
|
|
%{$self->{'task_data'}},
|
|
|
|
'target' => $target,
|
|
|
|
'pa_config' => $self->{'pa_config'},
|
|
|
|
'parent' => $self
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
if (defined($obj)) {
|
|
|
|
|
|
|
|
# Verify if object is connected. If cannot connect to current target
|
|
|
|
# return with module.
|
|
|
|
if (!$obj->is_connected()) {
|
|
|
|
$self->call('message', 'Cannot connect to target ' . $target, 3);
|
|
|
|
$global_percent += $global_step;
|
|
|
|
$self->{'c_network_percent'} = 90;
|
|
|
|
|
|
|
|
# Update progress
|
|
|
|
$self->call('update_progress', $global_percent + (90 / (scalar @targets)));
|
|
|
|
$self->{'summary'}->{'not_alive'} += 1;
|
|
|
|
push @modules, {
|
|
|
|
name => $type . ' connection',
|
|
|
|
type => 'generic_proc',
|
|
|
|
data => 0,
|
|
|
|
description => $type . ' availability'
|
|
|
|
};
|
|
|
|
|
|
|
|
} else {
|
|
|
|
#
|
|
|
|
# $results is always a hash with:
|
|
|
|
# @modules => 'global' modules.
|
|
|
|
# @data => {
|
|
|
|
# 'agent_data' => {}
|
|
|
|
# 'module_data' => []
|
|
|
|
# }
|
|
|
|
my $results;
|
|
|
|
|
|
|
|
# Scan connected obj.
|
|
|
|
if ( $self->{'task_data'}->{'type'} == DISCOVERY_APP_MYSQL
|
2020-06-11 14:53:34 +02:00
|
|
|
|| $self->{'task_data'}->{'type'} == DISCOVERY_APP_ORACLE
|
|
|
|
|| $self->{'task_data'}->{'type'} == DISCOVERY_APP_DB2
|
2020-06-17 14:20:15 +02:00
|
|
|
|| $self->{'task_data'}->{'type'} == DISCOVERY_APP_MICROSOFT_SQL_SERVER
|
2020-06-11 14:53:34 +02:00
|
|
|
) {
|
2020-03-18 10:28:50 +01:00
|
|
|
|
|
|
|
# Database.
|
|
|
|
$results = $self->database_scan($type, $obj, $global_percent, \@targets);
|
|
|
|
|
|
|
|
} elsif ($self->{'task_data'}->{'type'} == DISCOVERY_APP_SAP) {
|
|
|
|
|
|
|
|
# SAP scan
|
|
|
|
$results = $obj->scan();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add results.
|
|
|
|
if (ref($results) eq 'HASH') {
|
|
|
|
if (defined($results->{'modules'})) {
|
|
|
|
push @modules, @{$results->{'modules'}};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defined($results->{'data'})) {
|
|
|
|
push @data, @{$results->{'data'}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Put engine agent at the beginning of the list.
|
|
|
|
my $version = $obj->get_version();
|
|
|
|
unshift @data, {
|
|
|
|
'agent_data' => {
|
|
|
|
'agent_name' => $obj->get_agent_name(),
|
|
|
|
'os' => $type,
|
|
|
|
'os_version' => (defined($version) ? $version : 'Discovery'),
|
|
|
|
'interval' => $self->{'task_data'}->{'interval_sweep'},
|
|
|
|
'id_group' => $self->{'task_data'}->{'id_group'},
|
|
|
|
'address' => $obj->get_host(),
|
|
|
|
'description' => '',
|
|
|
|
},
|
|
|
|
'module_data' => \@modules,
|
|
|
|
};
|
|
|
|
|
|
|
|
$self->call('create_agents', \@data);
|
|
|
|
|
|
|
|
# Destroy item.
|
|
|
|
undef($obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
$global_percent += $global_step;
|
|
|
|
$self->{'c_network_percent'} = 100;
|
|
|
|
$self->call('update_progress', $global_percent);
|
|
|
|
}
|
|
|
|
|
|
|
|
# Update progress.
|
|
|
|
# Done!
|
|
|
|
$self->{'step'} = '';
|
|
|
|
$self->call('update_progress', -1);
|
2019-04-03 18:13:19 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-16 20:56:47 +02:00
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-07-16 20:56:47 +02:00
|
|
|
# Perform a deployment scan.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-07-16 20:56:47 +02:00
|
|
|
sub deploy_scan($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my $self = shift;
|
|
|
|
my ($progress, $step);
|
2019-07-16 20:56:47 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my $type = '';
|
2019-07-16 20:56:47 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Initialize deployer object.
|
|
|
|
my $deployer = PandoraFMS::Recon::Util::enterprise_new(
|
|
|
|
'PandoraFMS::Recon::Deployer',
|
|
|
|
[
|
|
|
|
task_data => $self->{'task_data'},
|
|
|
|
parent => $self
|
|
|
|
]
|
2019-07-16 20:56:47 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
);
|
2019-07-16 20:56:47 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if (!$deployer) {
|
2019-11-05 15:38:17 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Failed to initialize, check Cloud credentials or anything.
|
|
|
|
$self->call('message', 'Unable to initialize PandoraFMS::Recon::Deployer', 3);
|
|
|
|
} else {
|
2019-11-05 15:38:17 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Let deployer object manage scan.
|
|
|
|
$deployer->scan();
|
|
|
|
}
|
2019-07-16 20:56:47 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Update progress.
|
|
|
|
# Done!
|
|
|
|
$self->{'step'} = '';
|
|
|
|
$self->call('update_progress', -1);
|
2019-07-16 20:56:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Perform a network scan.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub scan($) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self) = @_;
|
|
|
|
my ($progress, $step) = 1, 0;
|
|
|
|
|
|
|
|
# 1%
|
|
|
|
$self->call('update_progress', 1);
|
|
|
|
|
|
|
|
if (defined($self->{'task_data'})) {
|
|
|
|
if ( $self->{'task_data'}->{'type'} == DISCOVERY_APP_MYSQL
|
|
|
|
|| $self->{'task_data'}->{'type'} == DISCOVERY_APP_ORACLE
|
2020-06-11 14:53:34 +02:00
|
|
|
|| $self->{'task_data'}->{'type'} == DISCOVERY_APP_DB2
|
2020-06-17 13:41:09 +02:00
|
|
|
|| $self->{'task_data'}->{'type'} == DISCOVERY_APP_MICROSOFT_SQL_SERVER
|
2020-03-18 10:28:50 +01:00
|
|
|
|| $self->{'task_data'}->{'type'} == DISCOVERY_APP_SAP) {
|
|
|
|
# Application scan.
|
|
|
|
$self->call('message', "Scanning application ...", 6);
|
|
|
|
return $self->app_scan();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($self->{'task_data'}->{'type'} == DISCOVERY_CLOUD_AWS_RDS) {
|
|
|
|
|
|
|
|
# Cloud scan.
|
|
|
|
return $self->cloud_scan();
|
|
|
|
}
|
|
|
|
|
|
|
|
if($self->{'task_data'}->{'type'} == DISCOVERY_DEPLOY_AGENTS) {
|
|
|
|
return $self->deploy_scan();
|
|
|
|
}
|
|
|
|
}
|
2020-03-18 19:31:58 +01:00
|
|
|
|
2020-03-25 18:02:58 +01:00
|
|
|
if(defined($self->{'task_data'}{'review_mode'})
|
|
|
|
&& $self->{'task_data'}{'review_mode'} == DISCOVERY_RESULTS
|
2020-03-18 10:28:50 +01:00
|
|
|
) {
|
|
|
|
# Use Cached results.
|
2020-03-25 18:02:58 +01:00
|
|
|
$self->{'step'} = STEP_PROCESSING;
|
2020-03-18 19:31:58 +01:00
|
|
|
$self->call('report_scanned_agents');
|
|
|
|
|
|
|
|
# Done!
|
|
|
|
$self->{'step'} = '';
|
|
|
|
$self->call('update_progress', -1);
|
|
|
|
return;
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Find devices.
|
2020-03-18 19:31:58 +01:00
|
|
|
$self->call('message', "[1/6] Scanning the network...", 3);
|
2020-03-25 18:02:58 +01:00
|
|
|
$self->{'c_network_name'} = '';
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'step'} = STEP_SCANNING;
|
|
|
|
$self->call('update_progress', $progress);
|
|
|
|
|
|
|
|
$self->scan_subnet();
|
|
|
|
# Read the local ARP cache.
|
|
|
|
$self->local_arp();
|
|
|
|
|
|
|
|
# Get a list of found hosts.
|
|
|
|
my @hosts = @{$self->get_hosts()};
|
|
|
|
if (scalar(@hosts) > 0 && $self->{'parent_detection'} == 1) {
|
|
|
|
|
|
|
|
# Delete previous connections.
|
|
|
|
$self->call('delete_connections');
|
|
|
|
|
2020-04-13 17:50:18 +02:00
|
|
|
# Connectivity from address forwarding tables.
|
|
|
|
$self->call('message', "[2/6] Finding address forwarding table connectivity...", 3);
|
2020-03-25 18:02:58 +01:00
|
|
|
$self->{'c_network_name'} = '';
|
2020-04-13 17:50:18 +02:00
|
|
|
$self->{'step'} = STEP_AFT;
|
|
|
|
($progress, $step) = (50, 10.0 / scalar(@hosts)); # From 50% to 60%.
|
|
|
|
for (my $i = 0; defined($hosts[$i]); $i++) {
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->call('update_progress', $progress);
|
|
|
|
$progress += $step;
|
2020-04-13 17:50:18 +02:00
|
|
|
$self->aft_connectivity($hosts[$i]);
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Connect hosts that are still unconnected using traceroute.
|
2020-03-18 19:31:58 +01:00
|
|
|
$self->call('message', "[3/6] Finding traceroute connectivity.", 3);
|
2020-03-25 18:02:58 +01:00
|
|
|
$self->{'c_network_name'} = '';
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'step'} = STEP_TRACEROUTE;
|
2020-03-18 19:31:58 +01:00
|
|
|
($progress, $step) = (60, 10.0 / scalar(@hosts)); # From 60% to 70%.
|
2020-03-18 10:28:50 +01:00
|
|
|
foreach my $host (@hosts) {
|
|
|
|
$self->call('update_progress', $progress);
|
|
|
|
$progress += $step;
|
|
|
|
next if ($self->has_parent($host) || $self->has_children($host));
|
|
|
|
$self->traceroute_connectivity($host);
|
|
|
|
}
|
|
|
|
|
2020-04-13 17:50:18 +02:00
|
|
|
# Connect hosts that are still unconnected using known gateways.
|
|
|
|
$self->call('message', "[4/6] Finding host to gateway connectivity.", 3);
|
2020-03-25 18:02:58 +01:00
|
|
|
$self->{'c_network_name'} = '';
|
2020-04-13 17:50:18 +02:00
|
|
|
$self->{'step'} = STEP_GATEWAY;
|
|
|
|
($progress, $step) = (70, 10.0 / scalar(@hosts)); # From 70% to 80%.
|
|
|
|
$self->get_routes(); # Update the route cache.
|
|
|
|
foreach my $host (@hosts) {
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->call('update_progress', $progress);
|
|
|
|
$progress += $step;
|
2020-04-13 17:50:18 +02:00
|
|
|
next if ($self->has_parent($host));
|
|
|
|
$self->gateway_connectivity($host);
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 16:43:20 +02:00
|
|
|
# Apply monitoring templates
|
|
|
|
$self->call('message', "[5/6] Applying monitoring.", 3);
|
2020-03-19 18:33:04 +01:00
|
|
|
$self->{'step'} = STEP_MONITORING;
|
2020-05-13 16:43:20 +02:00
|
|
|
$self->call('apply_monitoring', $self);
|
2020-03-18 10:28:50 +01:00
|
|
|
|
|
|
|
# Print debug information on found devices.
|
|
|
|
$self->call('message', "[Summary]", 3);
|
|
|
|
foreach my $host (@hosts) {
|
|
|
|
my $device = $self->get_device($host);
|
|
|
|
next unless defined($device);
|
|
|
|
|
|
|
|
# Print device information.
|
|
|
|
my $dev_info = "Device: " . $device->{'type'} . " (";
|
|
|
|
foreach my $ip_address ($self->get_addresses($host)) {
|
|
|
|
$dev_info .= "$ip_address,";
|
|
|
|
}
|
|
|
|
chop($dev_info);
|
|
|
|
$dev_info .= ')';
|
|
|
|
$self->call('message', $dev_info, 3);
|
|
|
|
}
|
|
|
|
|
2020-05-13 16:43:20 +02:00
|
|
|
# Apply monitoring templates
|
|
|
|
$self->call('message', "[6/6] Processing results.", 3);
|
2020-03-19 18:33:04 +01:00
|
|
|
$self->{'step'} = STEP_PROCESSING;
|
2020-03-18 10:28:50 +01:00
|
|
|
# Send agent information to Database (Discovery) or XML (satellite.).
|
2020-03-30 14:28:10 +02:00
|
|
|
$self->call('report_scanned_agents');
|
|
|
|
|
|
|
|
if(defined($self->{'task_data'}{'review_mode'})
|
|
|
|
&& $self->{'task_data'}{'review_mode'} == DISCOVERY_STANDARD
|
|
|
|
) {
|
|
|
|
# Send agent information to Database (Discovery) or XML (satellite.).
|
|
|
|
$self->call('report_scanned_agents', 1);
|
|
|
|
}
|
2020-03-16 23:52:20 +01:00
|
|
|
|
2020-03-18 19:31:58 +01:00
|
|
|
# Done!
|
|
|
|
$self->{'step'} = '';
|
|
|
|
$self->call('update_progress', -1);
|
|
|
|
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Set an SNMP community for the given device.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub set_community($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $community) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'community_cache'}->{$device} = $community;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Set the type of the given device.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub set_device_type($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $type) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'visited_devices'}->{$device}->{'type'} = $type;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-16 23:52:20 +01:00
|
|
|
################################################################################
|
|
|
|
# Calculate
|
|
|
|
################################################################################
|
|
|
|
sub snmp_pen($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $addr) = @_;
|
2020-03-16 23:52:20 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->{'pen'} = {} if ref($self->{'pen'}) ne 'HASH';
|
2020-03-16 23:52:20 +01:00
|
|
|
|
2020-03-25 18:02:58 +01:00
|
|
|
$self->{'pen'}{$addr} = $self->snmp_get_value($addr, $PEN_OID);
|
2020-03-16 23:52:20 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
if(defined($self->{'pen'}{$addr})) {
|
|
|
|
($self->{'pen'}{$addr}) = $self->{'pen'}{$addr} =~ /\.\d+\.\d+\.\d+\.\d+\.\d+\.\d+\.(\d+?)\./
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Performs an SNMP WALK and returns the response as an array.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub snmp_get($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $oid) = @_;
|
|
|
|
my @output;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return () unless defined $self->is_snmp_discovered($device);
|
|
|
|
my $community = $self->get_community($device);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check the SNMP query cache first.
|
|
|
|
if (defined($self->{'snmp_cache'}->{"${device}_${oid}"})) {
|
|
|
|
return @{$self->{'snmp_cache'}->{"${device}_${oid}"}};
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Check VLANS.
|
|
|
|
my @vlans = $self->get_vlans($device);
|
|
|
|
if (scalar(@vlans) == 0) {
|
|
|
|
my $command = $self->snmp_get_command($device, $oid, $community);
|
|
|
|
@output = `$command`;
|
|
|
|
}else {
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Handle duplicate lines.
|
|
|
|
my %output_hash;
|
|
|
|
foreach my $vlan (@vlans) {
|
|
|
|
my $command = $self->snmp_get_command($device, $oid, $community, $vlan);
|
|
|
|
foreach my $line (`$command`) {
|
|
|
|
$output_hash{$line} = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
push(@output, keys(%output_hash));
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Update the SNMP query cache.
|
|
|
|
$self->{'snmp_cache'}->{"${device}_${oid}"} = [@output];
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return @output;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-09-17 14:36:52 +02:00
|
|
|
# Get the snmpwalk command seing version 1, 2, 2c or 3.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2018-09-17 14:36:52 +02:00
|
|
|
sub snmp_get_command {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $oid, $community, $vlan) = @_;
|
|
|
|
$vlan = defined($vlan) ? "\@" . $vlan : '';
|
2018-09-17 14:36:52 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my $command = "snmpwalk -M$DEVNULL -r$self->{'snmp_checks'} -t$self->{'snmp_timeout'} -v$self->{'snmp_version'} -On -Oe ";
|
|
|
|
if ($self->{'snmp_version'} eq "3") {
|
|
|
|
if ($self->{'community'}) { # Context
|
2021-02-04 16:41:47 +01:00
|
|
|
$command .= " -N \'$self->{'community'}\' ";
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
|
|
|
$command .= " -l$self->{'snmp_security_level'} ";
|
|
|
|
if ($self->{'snmp_security_level'} ne "noAuthNoPriv") {
|
2021-02-04 16:41:47 +01:00
|
|
|
$command .= " -u$self->{'snmp_auth_user'} -a $self->{'snmp_auth_method'} -A \'$self->{'snmp_auth_pass'}\' ";
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
|
|
|
if ($self->{'snmp_security_level'} eq "authPriv") {
|
2021-02-04 16:41:47 +01:00
|
|
|
$command .= " -x$self->{'snmp_privacy_method'} -X \'$self->{'snmp_privacy_pass'}\' ";
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-03-23 16:15:57 +01:00
|
|
|
$command .= " -c\'$community\'$vlan ";
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
2018-09-17 14:36:52 +02:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return "$command $device $oid 2>$DEVNULL";
|
2018-09-17 14:36:52 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Performs an SNMP WALK and returns the value of the given OID. Returns undef
|
|
|
|
# on error.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub snmp_get_value($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $oid) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-04-06 18:04:53 +02:00
|
|
|
my $effective_oid = $oid;
|
2020-04-28 13:26:57 +02:00
|
|
|
if (is_enabled($self->{'translate_snmp'}) && $oid !~ /^[\.\d]+$/) {
|
2020-04-06 18:04:53 +02:00
|
|
|
$effective_oid = `snmptranslate $oid -On 2>$DEVNULL`;
|
2020-04-28 13:26:57 +02:00
|
|
|
$effective_oid =~ s/[\r\n]//g;
|
2020-04-06 18:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
my @output = $self->snmp_get($device, $effective_oid);
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
foreach my $line (@output) {
|
2020-04-28 13:26:57 +02:00
|
|
|
$line =~ s/[\r\n]//g;
|
2020-10-09 14:50:37 +02:00
|
|
|
return $1 if ($line =~ /^\.{0,1}$effective_oid\s+=\s+\S+:\s+(.*)/);
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return undef;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Performs an SNMP WALK and returns an array of values.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub snmp_get_value_array($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $oid) = @_;
|
|
|
|
my @values;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my @output = $self->snmp_get($device, $oid);
|
|
|
|
foreach my $line (@output) {
|
|
|
|
chomp($line);
|
2020-10-09 14:50:37 +02:00
|
|
|
push(@values, $1) if ($line =~ /^\.{0,1}$oid\S*\s+=\s+\S+:\s+(.*)$/);
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return @values;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
# Performs an SNMP WALK and returns a hash of values.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub snmp_get_value_hash($$$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $device, $oid) = @_;
|
|
|
|
my %values;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my @output = $self->snmp_get_value_array($device, $oid);
|
|
|
|
foreach my $line (@output) {
|
|
|
|
$values{$line} = '';
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return %values;
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-10 10:36:37 +01:00
|
|
|
# Connect the given host to its parent using traceroute.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2017-03-08 13:20:00 +01:00
|
|
|
sub traceroute_connectivity($$) {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $host) = @_;
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Perform a traceroute.
|
|
|
|
my $nmap_args = '-nsP -PE --traceroute --max-retries '.$self->{'icmp_checks'}.' --host-timeout '.$self->{'icmp_timeout'}.'s -T'.$self->{'recon_timing_template'};
|
|
|
|
my $np = PandoraFMS::Recon::NmapParser->new();
|
|
|
|
eval {$np->parsescan($self->{'nmap'}, $nmap_args, ($host));};
|
|
|
|
return if ($@);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Get hops to the host.
|
|
|
|
my ($h) = $np->all_hosts();
|
|
|
|
return unless defined($h);
|
|
|
|
my @hops = $h->all_trace_hops();
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Skip the target host.
|
|
|
|
pop(@hops);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Reverse the host order (closest hosts first).
|
|
|
|
@hops = reverse(@hops);
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Look for parents.
|
|
|
|
my $device = $host;
|
|
|
|
for (my $i = 0; $i < $self->{'parent_recursion'}; $i++) {
|
2020-04-08 16:06:39 +02:00
|
|
|
next if is_empty($hops[$i]);
|
2020-03-18 10:28:50 +01:00
|
|
|
my $parent = $hops[$i]->ipaddr();
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Create an agent for the parent.
|
|
|
|
$self->add_agent($parent);
|
2017-03-23 14:33:06 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
$self->call('message', "Host $device is one hop away from host $parent.", 5);
|
|
|
|
$self->mark_connected($parent, '', $device, '');
|
2017-03-08 13:20:00 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
# Move on to the next hop.
|
|
|
|
$device = $parent;
|
|
|
|
}
|
2017-03-08 13:20:00 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-02-13 14:56:10 +01:00
|
|
|
# Returns the credentials with which the host responds to WMI queries or
|
|
|
|
# undef if it does not respond to WMI.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2020-03-26 13:44:08 +01:00
|
|
|
sub wmi_credentials {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $target) = @_;
|
2020-03-26 13:44:08 +01:00
|
|
|
return $self->{'wmi_auth'}{$target};
|
2019-02-13 14:56:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 14:33:56 +01:00
|
|
|
################################################################################
|
|
|
|
# Returns the credentials KEY with which the host responds to WMI queries or
|
|
|
|
# undef if it does not respond to WMI.
|
|
|
|
################################################################################
|
|
|
|
sub wmi_credentials_key {
|
2020-05-13 16:43:20 +02:00
|
|
|
my ($self, $target) = @_;
|
|
|
|
return $self->{'wmi_auth_key'}{$target};
|
2020-03-26 14:33:56 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
################################################################################
|
|
|
|
# Calculate WMI credentials for target, 1 if calculated, undef if cannot
|
|
|
|
# connect to target. Credentials could be empty (-N)
|
|
|
|
################################################################################
|
|
|
|
sub wmi_credentials_calculation {
|
|
|
|
my ($self, $target) = @_;
|
|
|
|
|
|
|
|
# Test empty credentials.
|
2020-04-07 18:50:56 +02:00
|
|
|
my @output = `$self->{'timeout_cmd'}$self->{'wmi_client'} -N //$target "SELECT * FROM Win32_ComputerSystem" 2>$DEVNULL`;
|
2020-03-26 13:44:08 +01:00
|
|
|
my $rs = $self->wmi_output_check($?, @output);
|
|
|
|
|
|
|
|
if ($rs == WMI_OK) {
|
|
|
|
$self->{'wmi_auth'}{$target} = '';
|
2020-03-26 14:33:56 +01:00
|
|
|
$self->{'wmi_auth_key'}{$target} = '';
|
2020-03-26 13:44:08 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($rs == WMI_UNREACHABLE) {
|
|
|
|
# Target does not respond.
|
|
|
|
$self->{'wmi'}{$target} = 0;
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Test all credentials selected.
|
|
|
|
foreach my $key_index (@{$self->{'auth_strings_array'}}) {
|
|
|
|
my $cred = $self->call('get_credentials', $key_index);
|
|
|
|
next if ref($cred) ne 'HASH';
|
|
|
|
|
|
|
|
my $auth = $cred->{'username'}.'%'.$cred->{'password'};
|
|
|
|
next if $auth eq '%';
|
|
|
|
|
2020-04-07 18:50:56 +02:00
|
|
|
@output = `$self->{'timeout_cmd'}$self->{'wmi_client'} -U $auth //$target "SELECT * FROM Win32_ComputerSystem" 2>$DEVNULL`;
|
2020-03-26 13:44:08 +01:00
|
|
|
|
|
|
|
my $rs = $self->wmi_output_check($?, @output);
|
|
|
|
|
|
|
|
if ($rs == WMI_OK) {
|
|
|
|
$self->{'wmi_auth'}{$target} = $auth;
|
2020-03-26 14:33:56 +01:00
|
|
|
$self->{'wmi_auth_key'}{$target} = $key_index;
|
2020-03-26 13:44:08 +01:00
|
|
|
$self->{'wmi'}{$target} = 1;
|
|
|
|
$self->{'summary'}->{'WMI'} += 1;
|
|
|
|
$self->call('message', "[".$target."] WMI available.", 10);
|
|
|
|
return 1;
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
if ($rs == WMI_UNREACHABLE) {
|
|
|
|
# Target does not respond.
|
|
|
|
$self->call('message', "[".$target."] WMI unreachable.", 10);
|
|
|
|
$self->{'wmi'}{$target} = 0;
|
|
|
|
return undef;
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return undef;
|
2019-02-13 14:56:10 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 15:00:25 +02:00
|
|
|
################################################################################
|
|
|
|
# Returns the credentials with which the host responds to WMI queries or
|
|
|
|
# undef if it does not respond to WMI.
|
|
|
|
################################################################################
|
|
|
|
sub rcmd_credentials {
|
|
|
|
my ($self, $target) = @_;
|
|
|
|
return $self->{'rcmd_auth'}{$target};
|
|
|
|
}
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-04-30 15:00:25 +02:00
|
|
|
################################################################################
|
|
|
|
# Returns the credentials KEY with which the host responds to WMI queries or
|
|
|
|
# undef if it does not respond to WMI.
|
|
|
|
################################################################################
|
|
|
|
sub rcmd_credentials_key {
|
2020-05-13 16:43:20 +02:00
|
|
|
my ($self, $target) = @_;
|
|
|
|
return $self->{'rcmd_auth_key'}{$target};
|
2020-04-30 15:00:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
# Calculate WMI credentials for target, 1 if calculated, undef if cannot
|
|
|
|
# connect to target. Credentials could be empty (-N)
|
|
|
|
################################################################################
|
|
|
|
sub rcmd_credentials_calculation {
|
|
|
|
my ($self, $target) = @_;
|
|
|
|
|
|
|
|
my $rcmd = PandoraFMS::Recon::Util::enterprise_new(
|
2020-05-13 16:43:20 +02:00
|
|
|
'PandoraFMS::RemoteCmd',[{
|
2020-04-30 15:00:25 +02:00
|
|
|
'psexec' => $self->{'parent'}->{'pa_config'}->{'psexec'},
|
|
|
|
'winexe' => $self->{'parent'}->{'pa_config'}->{'winexe'},
|
|
|
|
'plink' => $self->{'parent'}->{'pa_config'}->{'plink'}
|
|
|
|
}]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!$rcmd) {
|
|
|
|
# Library not available.
|
|
|
|
$self->call('message', "PandoraFMS::RemoteCmd library not available", 10);
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:02:07 +02:00
|
|
|
my $os = $self->{'os_cache'}{$target};
|
|
|
|
$os = $self->call('guess_os', $target, 1) if is_empty($os);
|
2020-05-13 16:43:20 +02:00
|
|
|
$rcmd->set_host($target);
|
2020-05-13 17:02:07 +02:00
|
|
|
$rcmd->set_os($os);
|
|
|
|
|
|
|
|
$self->{'os_cache'}{$target} = $os;
|
2020-04-30 15:00:25 +02:00
|
|
|
|
|
|
|
# Test all credentials selected.
|
|
|
|
foreach my $key_index (@{$self->{'auth_strings_array'}}) {
|
|
|
|
my $cred = $self->call('get_credentials', $key_index);
|
|
|
|
next if ref($cred) ne 'HASH';
|
2020-05-13 16:43:20 +02:00
|
|
|
$rcmd->clean_ssh_lib();
|
|
|
|
|
|
|
|
my $username;
|
|
|
|
my $domain;
|
|
|
|
|
|
|
|
if($cred->{'username'} =~ /^(.*?)\\(.*)$/) {
|
|
|
|
$domain = $1;
|
|
|
|
$username = $2;
|
|
|
|
} else {
|
|
|
|
$username = $cred->{'username'};
|
|
|
|
}
|
|
|
|
|
|
|
|
$rcmd->set_credentials(
|
|
|
|
{
|
|
|
|
'user' => $username,
|
|
|
|
'pass' => $cred->{'password'},
|
|
|
|
'domain' => $domain
|
|
|
|
}
|
|
|
|
);
|
2020-04-30 15:00:25 +02:00
|
|
|
|
|
|
|
$rcmd->set_timeout(
|
|
|
|
$self->{'rcmd_timeout_bin'},
|
|
|
|
$self->{'rcmd_timeout'}
|
|
|
|
);
|
|
|
|
|
2020-05-13 16:43:20 +02:00
|
|
|
my $result;
|
|
|
|
eval {
|
|
|
|
$result = $rcmd->rcmd('echo 1');
|
2020-04-30 15:00:25 +02:00
|
|
|
chomp($result);
|
|
|
|
my $out = '';
|
|
|
|
$out = $result if !is_empty($result);
|
2020-05-13 17:02:07 +02:00
|
|
|
$self->call('message', "Trying [".$key_index."] in [". $target."] [".$os."]: [$out]", 10);
|
2020-05-13 16:43:20 +02:00
|
|
|
};
|
|
|
|
if ($@) {
|
2020-05-13 17:02:07 +02:00
|
|
|
$self->call('message', "Failed while trying [".$key_index."] in [". $target."] [".$os."]:" . @_, 10);
|
2020-05-13 16:43:20 +02:00
|
|
|
}
|
2020-04-30 15:00:25 +02:00
|
|
|
|
|
|
|
if (!is_empty($result) && $result == "1") {
|
|
|
|
$self->{'rcmd_auth'}{$target} = $cred;
|
|
|
|
$self->{'rcmd_auth_key'}{$target} = $key_index;
|
|
|
|
$self->{'rcmd'}{$target} = 1;
|
|
|
|
$self->{'summary'}->{'RCMD'} += 1;
|
|
|
|
$self->call('message', "RCMD available for $target", 10);
|
|
|
|
return 1;
|
2020-05-13 16:43:20 +02:00
|
|
|
} else {
|
2020-05-13 17:02:07 +02:00
|
|
|
$self->call('message', "Last error ($target|$os|$result) was [".$rcmd->get_last_error()."]", 10);
|
2020-04-30 15:00:25 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 16:43:20 +02:00
|
|
|
}
|
2020-04-30 15:00:25 +02:00
|
|
|
|
2020-05-13 16:43:20 +02:00
|
|
|
# Not found.
|
|
|
|
return 0;
|
2020-04-30 15:00:25 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2020-03-26 13:44:08 +01:00
|
|
|
# Tests wmi capability for addr.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2020-03-26 13:44:08 +01:00
|
|
|
sub wmi_discovery {
|
|
|
|
my ($self, $addr) = @_;
|
2020-03-18 10:28:50 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
# Initialization.
|
|
|
|
$self->{'wmi'} = {} unless ref($self->{'wmi'}) eq 'HASH';
|
2020-03-18 10:28:50 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
# Calculate credentials.
|
|
|
|
$self->wmi_credentials_calculation($addr);
|
2020-03-18 10:28:50 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
}
|
2020-03-18 10:28:50 +01:00
|
|
|
|
2020-04-30 15:00:25 +02:00
|
|
|
################################################################################
|
|
|
|
# Tests credentials against addr.
|
|
|
|
################################################################################
|
|
|
|
sub rcmd_discovery {
|
2020-05-13 16:43:20 +02:00
|
|
|
my ($self, $addr) = @_;
|
2020-04-30 15:00:25 +02:00
|
|
|
|
2020-05-13 16:43:20 +02:00
|
|
|
# Initialization.
|
|
|
|
$self->{'rcmd'} = {} unless ref($self->{'rcmd'}) eq 'HASH';
|
2020-04-30 15:00:25 +02:00
|
|
|
|
2020-05-13 16:43:20 +02:00
|
|
|
# Calculate credentials.
|
|
|
|
$self->rcmd_credentials_calculation($addr);
|
2020-04-30 15:00:25 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
################################################################################
|
|
|
|
# Extra: WMI imported methods. DO NOT EXPORT TO AVOID DOUBLE DEF.
|
|
|
|
################################################################################
|
2020-03-18 10:28:50 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
################################################################################
|
|
|
|
# Validate wmi output. (err code and messages).
|
|
|
|
################################################################################
|
|
|
|
sub wmi_output_check {
|
|
|
|
my ($self, $rc, @output) = @_;
|
|
|
|
if ($? != 0) {
|
|
|
|
# Something went wrong.
|
|
|
|
if (defined($output[-1]) && $output[-1] =~ /NTSTATUS: (.*)/) {
|
|
|
|
my $err = $1;
|
|
|
|
$self->{'last_wmi_error'} = $err;
|
2020-03-18 10:28:50 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
if ($err =~ /NT_STATUS_IO_TIMEOUT/
|
|
|
|
|| $err =~ /NT_STATUS_CONNECTION_REFUSED/
|
|
|
|
) {
|
|
|
|
# Fail.
|
|
|
|
return WMI_UNREACHABLE;
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
if ($err =~ /NT_STATUS_ACCESS_DENIED/) {
|
|
|
|
return WMI_BAD_PASSWORD;
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
2020-03-26 13:44:08 +01:00
|
|
|
}
|
2020-03-18 10:28:50 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
# Fail.
|
|
|
|
return WMI_GENERIC_ERROR;
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
2020-03-16 23:52:20 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
# Ok.
|
|
|
|
return WMI_OK;
|
2019-02-13 14:56:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2020-03-26 13:44:08 +01:00
|
|
|
# Performs a wmi get requests and returns the response as an array.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2020-03-26 13:44:08 +01:00
|
|
|
sub wmi_get {
|
|
|
|
my ($self, $target, $query) = @_;
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
return () unless $self->wmi_responds($target);
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
return $self->wmi_get_command($target, $self->{'wmi_auth'}{$target}, $query);
|
|
|
|
}
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-02-13 14:56:10 +01:00
|
|
|
# Performs a wmi get requests and returns the response as an array.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2020-03-26 13:44:08 +01:00
|
|
|
sub wmi_get_command {
|
2020-03-18 10:28:50 +01:00
|
|
|
my ($self, $target, $auth, $query) = @_;
|
2019-03-25 17:32:18 +01:00
|
|
|
|
2020-04-07 18:50:56 +02:00
|
|
|
return () if is_empty($target);
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my @output;
|
|
|
|
if (defined($auth) && $auth ne '') {
|
2020-04-14 11:57:18 +02:00
|
|
|
$auth =~ s/'/\'/g;
|
|
|
|
@output = `$self->{'timeout_cmd'}"$self->{'wmi_client'}" -U '$auth' //$target "$query" 2>$DEVNULL`;
|
2020-03-18 10:28:50 +01:00
|
|
|
}else {
|
2020-04-07 18:50:56 +02:00
|
|
|
@output = `$self->{'timeout_cmd'}"$self->{'wmi_client'}" -N //$target "$query" 2>$DEVNULL`;
|
2020-03-18 10:28:50 +01:00
|
|
|
}
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
my $rs = $self->wmi_output_check($?, @output);
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
if ($rs == WMI_OK) {
|
|
|
|
return @output;
|
|
|
|
}
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-04-07 18:50:56 +02:00
|
|
|
my $err = $self->{'last_wmi_error'};
|
|
|
|
$err = 'Not OK, empty error' if is_empty($err);
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
$self->call(
|
|
|
|
'message',
|
2020-04-07 18:50:56 +02:00
|
|
|
"[".$target."] WMI error: ".$err,
|
2020-03-26 13:44:08 +01:00
|
|
|
10
|
|
|
|
);
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
return ();
|
|
|
|
}
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
################################################################################
|
|
|
|
# Checks if target is reachable using wmi.
|
|
|
|
################################################################################
|
|
|
|
sub wmi_responds {
|
2020-05-13 16:43:20 +02:00
|
|
|
my ($self, $target) = @_;
|
|
|
|
return 1 if is_enabled($self->{'wmi'}{$target});
|
|
|
|
return 0;
|
2019-02-13 14:56:10 +01:00
|
|
|
}
|
|
|
|
|
2020-04-30 15:00:25 +02:00
|
|
|
################################################################################
|
|
|
|
# Checks if target is reachable using rcmd.
|
|
|
|
################################################################################
|
|
|
|
sub rcmd_responds {
|
2020-05-13 16:43:20 +02:00
|
|
|
my ($self, $target) = @_;
|
|
|
|
return 1 if is_enabled($self->{'rcmd'}{$target});
|
|
|
|
return 0;
|
2019-02-13 14:56:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-02-13 14:56:10 +01:00
|
|
|
# Performs a WMI request and returns the requested column of the first row.
|
|
|
|
# Returns undef on error.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-02-13 14:56:10 +01:00
|
|
|
sub wmi_get_value {
|
2020-03-26 13:44:08 +01:00
|
|
|
my ($self, $target, $query, $column) = @_;
|
2020-03-18 10:28:50 +01:00
|
|
|
my @result;
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
my @output = $self->wmi_get($target, $query);
|
2020-03-18 10:28:50 +01:00
|
|
|
return undef unless defined($output[2]);
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
my $line = $output[2];
|
|
|
|
chomp($line);
|
|
|
|
my @columns = split(/\|/, $line);
|
|
|
|
return undef unless defined($columns[$column]);
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return $columns[$column];
|
2019-02-13 14:56:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-02-13 14:56:10 +01:00
|
|
|
# Performs a WMI request and returns row values for the requested column
|
|
|
|
# in an array.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-02-13 14:56:10 +01:00
|
|
|
sub wmi_get_value_array {
|
2020-03-26 13:44:08 +01:00
|
|
|
my ($self, $target, $query, $column) = @_;
|
2020-03-18 10:28:50 +01:00
|
|
|
my @result;
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-26 13:44:08 +01:00
|
|
|
my @output = $self->wmi_get($target, $query);
|
2020-03-18 10:28:50 +01:00
|
|
|
foreach (my $i = 2; defined($output[$i]); $i++) {
|
|
|
|
my $line = $output[$i];
|
|
|
|
chomp($line);
|
|
|
|
my @columns = split(/\|/, $line);
|
|
|
|
next unless defined($columns[$column]);
|
|
|
|
push(@result, $columns[$column]);
|
|
|
|
}
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2020-03-18 10:28:50 +01:00
|
|
|
return @result;
|
2019-02-13 14:56:10 +01:00
|
|
|
}
|
|
|
|
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-02-13 14:56:10 +01:00
|
|
|
# END: WMI imported methods.
|
2020-03-11 13:35:31 +01:00
|
|
|
################################################################################
|
2019-02-13 14:56:10 +01:00
|
|
|
|
2017-03-08 13:20:00 +01:00
|
|
|
1;
|
|
|
|
__END__
|
|
|
|
|