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()
{
param(
[string]$IP
$IP
);
$IPArray = $IP.Split(' ');
$IPList = @();
foreach ($entry in $IPArray) {
if ($entry -like '*.*') {
$IPList += ConvertTo-IcingaIPv4BinaryString -IP $entry;
} elseif ($entry -like '*:*') {
$IPList += ConvertTo-IcingaIPv6BinaryString -IP $entry;
} else {
return 'Invalid IP was provided!';
}
if ($IP -like '*.*') {
$IP = ConvertTo-IcingaIPv4BinaryString -IP $IP;
} elseif ($IP -like '*:*') {
$IP = ConvertTo-IcingaIPv6BinaryString -IP $IP;
} else {
return 'Invalid IP was provided!';
}
return $IPList;
return $IP;
}

View file

@ -21,18 +21,24 @@
function ConvertTo-IcingaIPv4BinaryString()
{
param(
[string]$IP
);
param(
[string]$IP
);
$IP = $IP -split '\.' | ForEach-Object {
[System.Convert]::ToString($_, 2).PadLeft(8, '0');
}
$IP = $IP -join '';
$IP = $IP -replace '\s','';
return @{
'value' = $IP;
'name' = 'IPv4'
try {
$IP = $IP -split '\.' | ForEach-Object {
[System.Convert]::ToString($_, 2).PadLeft(8, '0');
}
$IP = $IP -join '';
$IP = $IP -replace '\s','';
} catch {
# Todo: Should we handle errors? It might happen due to faulty routes or unhandled route config
# 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) {
[string]$RegexPattern = [string]::Format("^.{{{0}}}", $Route.Value.Mask);
[string]$ToBeMatched = $Route.Value."Binary IP String";
if ($null -eq $ToBeMatched) {
continue;
}
$Match1=[regex]::Matches($ToBeMatched, $RegexPattern).Value;
$Match2=[regex]::Matches($destinationIP.Value, $RegexPattern).Value;
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)) {
$ExternalInterfaces[$ExternalInterface].count += 1;
} else {