Add support for long-term caching of check metrics

This commit is contained in:
Lord Hepipud 2019-11-03 21:44:29 +01:00
parent b2f5dba4f5
commit 2c4d482e28

View file

@ -46,6 +46,7 @@ function Start-IcingaServiceCheckTask()
$PassedTime = 0; $PassedTime = 0;
$SortedResult = $null; $SortedResult = $null;
$OldData = @{}; $OldData = @{};
$PerfCache = @{};
if (-Not ($IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler.ContainsKey($CheckCommand))) { if (-Not ($IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler.ContainsKey($CheckCommand))) {
$IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler.Add($CheckCommand, [hashtable]::Synchronized(@{})); $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler.Add($CheckCommand, [hashtable]::Synchronized(@{}));
@ -53,6 +54,23 @@ function Start-IcingaServiceCheckTask()
$IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand].Add('average', [hashtable]::Synchronized(@{})); $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand].Add('average', [hashtable]::Synchronized(@{}));
} }
$LoadedCacheData = Get-IcingaCacheData -Space 'sc_daemon' -CacheStore 'checkresult_store' -KeyName $CheckCommand;
if ($null -ne $LoadedCacheData) {
foreach ($entry in $LoadedCacheData.PSObject.Properties) {
$IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['results'].Add(
$entry.name,
[hashtable]::Synchronized(@{})
);
foreach ($item in $entry.Value.PSObject.Properties) {
$IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['results'][$entry.name].Add(
$item.Name,
$item.Value
);
}
}
}
while ($TRUE) { while ($TRUE) {
if ($PassedTime -ge $Interval) { if ($PassedTime -ge $Interval) {
try { try {
@ -63,6 +81,7 @@ function Start-IcingaServiceCheckTask()
foreach ($result in $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['results'].Keys) { foreach ($result in $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['results'].Keys) {
$SortedResult = $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['results'][$result].GetEnumerator() | Sort-Object name -Descending; $SortedResult = $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['results'][$result].GetEnumerator() | Sort-Object name -Descending;
Add-IcingaHashtableItem -Hashtable $OldData -Key $result -Value @{}; Add-IcingaHashtableItem -Hashtable $OldData -Key $result -Value @{};
Add-IcingaHashtableItem -Hashtable $PerfCache -Key ([string]$result) -Value @{};
foreach ($index in $TimeIndexes) { foreach ($index in $TimeIndexes) {
$ObjectCount = 0; $ObjectCount = 0;
@ -74,6 +93,7 @@ function Start-IcingaServiceCheckTask()
$ObjectCount += 1; $ObjectCount += 1;
$ObjectValue += $timeEntry.Value; $ObjectValue += $timeEntry.Value;
Remove-IcingaHashtableItem -Hashtable $OldData[$result] -Key $timeEntry; Remove-IcingaHashtableItem -Hashtable $OldData[$result] -Key $timeEntry;
Add-IcingaHashtableItem -Hashtable $PerfCache[$result] -Key ([string]$timeEntry.Key) -Value ([string]$timeEntry.Value);
} else { } else {
Add-IcingaHashtableItem -Hashtable $OldData[$result] -Key $timeEntry -Value $null; Add-IcingaHashtableItem -Hashtable $OldData[$result] -Key $timeEntry -Value $null;
} }
@ -96,6 +116,8 @@ function Start-IcingaServiceCheckTask()
} }
Set-IcingaCacheData -Space 'sc_daemon' -CacheStore 'checkresult' -KeyName $CheckCommand -Value $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['average']; Set-IcingaCacheData -Space 'sc_daemon' -CacheStore 'checkresult' -KeyName $CheckCommand -Value $IcingaDaemonData.BackgroundDaemon.ServiceCheckScheduler[$CheckCommand]['average'];
# Write collected metrics to disk in case we reload the daemon. We will load them back into the module after reload then
Set-IcingaCacheData -Space 'sc_daemon' -CacheStore 'checkresult_store' -KeyName $CheckCommand -Value $PerfCache;
} catch { } catch {
# Todo: Add error reporting / handling # Todo: Add error reporting / handling
} }
@ -103,6 +125,7 @@ function Start-IcingaServiceCheckTask()
$PassedTime = 0; $PassedTime = 0;
$SortedResult = $null; $SortedResult = $null;
$OldData = @{}; $OldData = @{};
$PerfCache = @{};
} }
$PassedTime += 1; $PassedTime += 1;
Start-Sleep -Seconds 1; Start-Sleep -Seconds 1;