From 0d7954617c33b4bb240b391dce526b1df10a32a3 Mon Sep 17 00:00:00 2001 From: Lord Hepipud Date: Tue, 17 Sep 2019 17:56:56 +0200 Subject: [PATCH] Added proper exception handling for Cache write failures --- lib/core/cache/Set-IcingaCacheData.psm1 | 7 ++++++- lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/core/cache/Set-IcingaCacheData.psm1 b/lib/core/cache/Set-IcingaCacheData.psm1 index 7595410..8c66a88 100644 --- a/lib/core/cache/Set-IcingaCacheData.psm1 +++ b/lib/core/cache/Set-IcingaCacheData.psm1 @@ -28,5 +28,10 @@ function Set-IcingaCacheData() } } - Set-Content -Path $CacheFile -Value (ConvertTo-Json -InputObject $cacheData -Depth 100) | Out-Null; + try { + Set-Content -Path $CacheFile -Value (ConvertTo-Json -InputObject $cacheData -Depth 100) | Out-Null; + } catch { + Exit-IcingaThrowException -InputString $_.Exception -CustomMessage (Get-IcingaCacheDir) -StringPattern 'System.UnauthorizedAccessException' -ExceptionType 'Permission' -ExceptionThrown $IcingaExceptions.Permission.CacheFolder; + Exit-IcingaThrowException -CustomMessage $_.Exception -ExceptionType 'Unhandled' -Force; + } } diff --git a/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 b/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 index ea508dd..ae8c890 100644 --- a/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 +++ b/lib/icinga/exception/Icinga_IcingaExceptionEnums.psm1 @@ -6,6 +6,7 @@ [hashtable]$Permission = @{ PerformanceCounter = 'A Plugin failed to fetch Performance Counter information. This may be caused when the used Service User is not permitted to access these information. To fix this, please add the User the Icinga Agent is running on into the "Performance Log Users" group and restart the service.'; + CacheFolder = "A plugin failed to write new data into the configured cache directory. Please update the permissions of this folder to allow write access for the user the Icinga Service is running with or use another folder as cache directory."; }; [hashtable]$Inputs = @{