Fixes exceptions during interface discovery

Fix #38
This commit is contained in:
Christian Stein 2020-02-07 11:06:57 +01:00
parent edacccd75a
commit 9b6941c4b9
3 changed files with 37 additions and 26 deletions

View file

@ -22,21 +22,16 @@
function ConvertTo-IcingaIPBinaryString() function ConvertTo-IcingaIPBinaryString()
{ {
param( param(
[string]$IP $IP
); );
$IPArray = $IP.Split(' '); if ($IP -like '*.*') {
$IPList = @(); $IP = ConvertTo-IcingaIPv4BinaryString -IP $IP;
} elseif ($IP -like '*:*') {
foreach ($entry in $IPArray) { $IP = ConvertTo-IcingaIPv6BinaryString -IP $IP;
if ($entry -like '*.*') { } else {
$IPList += ConvertTo-IcingaIPv4BinaryString -IP $entry; return 'Invalid IP was provided!';
} elseif ($entry -like '*:*') {
$IPList += ConvertTo-IcingaIPv6BinaryString -IP $entry;
} else {
return 'Invalid IP was provided!';
}
} }
return $IPList; return $IP;
} }

View file

@ -21,18 +21,24 @@
function ConvertTo-IcingaIPv4BinaryString() function ConvertTo-IcingaIPv4BinaryString()
{ {
param( param(
[string]$IP [string]$IP
); );
$IP = $IP -split '\.' | ForEach-Object { try {
[System.Convert]::ToString($_, 2).PadLeft(8, '0'); $IP = $IP -split '\.' | ForEach-Object {
} [System.Convert]::ToString($_, 2).PadLeft(8, '0');
$IP = $IP -join ''; }
$IP = $IP -replace '\s',''; $IP = $IP -join '';
$IP = $IP -replace '\s','';
return @{ } catch {
'value' = $IP; # Todo: Should we handle errors? It might happen due to faulty routes or unhandled route config
'name' = 'IPv4' # we throw errors which should have no effect at all
return $null;
}
return @{
'value' = $IP;
'name' = 'IPv4'
} }
} }

View file

@ -113,11 +113,21 @@ function Get-IcingaNetworkInterface()
foreach ($destinationIP in $IPBinStringMaster) { foreach ($destinationIP in $IPBinStringMaster) {
[string]$RegexPattern = [string]::Format("^.{{{0}}}", $Route.Value.Mask); [string]$RegexPattern = [string]::Format("^.{{{0}}}", $Route.Value.Mask);
[string]$ToBeMatched = $Route.Value."Binary IP String"; [string]$ToBeMatched = $Route.Value."Binary IP String";
if ($null -eq $ToBeMatched) {
continue;
}
$Match1=[regex]::Matches($ToBeMatched, $RegexPattern).Value; $Match1=[regex]::Matches($ToBeMatched, $RegexPattern).Value;
$Match2=[regex]::Matches($destinationIP.Value, $RegexPattern).Value; $Match2=[regex]::Matches($destinationIP.Value, $RegexPattern).Value;
If ($Match1 -like $Match2) { If ($Match1 -like $Match2) {
$ExternalInterface = ((Get-NetIPAddress -InterfaceIndex $Route.Value.Interface -AddressFamily $destinationIP.Name).IPAddress); $ExternalInterface = ((Get-NetIPAddress -InterfaceIndex $Route.Value.Interface -AddressFamily $destinationIP.Name -ErrorAction SilentlyContinue).IPAddress);
# If no interface was found -> skip this entry
if ($null -eq $ExternalInterface) {
continue;
}
if ($ExternalInterfaces.ContainsKey($ExternalInterface)) { if ($ExternalInterfaces.ContainsKey($ExternalInterface)) {
$ExternalInterfaces[$ExternalInterface].count += 1; $ExternalInterfaces[$ExternalInterface].count += 1;
} else { } else {