From d0dcc0c1287e87167f46a1ed93991d1322056870 Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Thu, 13 Feb 2020 12:32:56 +0100 Subject: [PATCH] Adds Icinga Agent object list wrapper implementation Fixes #44 --- .../finders/Find-IcingaAgentObjects.psm1 | 40 +++++++++++++++++++ .../getters/Get-IcingaAgentObjectList.psm1 | 7 ++++ .../writers/Write-IcingaAgentObjectList.psm1 | 14 +++++++ 3 files changed, 61 insertions(+) create mode 100644 lib/core/icingaagent/finders/Find-IcingaAgentObjects.psm1 create mode 100644 lib/core/icingaagent/getters/Get-IcingaAgentObjectList.psm1 create mode 100644 lib/core/icingaagent/writers/Write-IcingaAgentObjectList.psm1 diff --git a/lib/core/icingaagent/finders/Find-IcingaAgentObjects.psm1 b/lib/core/icingaagent/finders/Find-IcingaAgentObjects.psm1 new file mode 100644 index 0000000..8b74c7b --- /dev/null +++ b/lib/core/icingaagent/finders/Find-IcingaAgentObjects.psm1 @@ -0,0 +1,40 @@ +function Find-IcingaAgentObjects() +{ + param( + $Find = @(), + $OutFile = $null + ); + + if ($Find.Length -eq 0) { + throw 'Please specify content you want to look for'; + } + + [array]$ObjectList = (Get-IcingaAgentObjectList).Split("`r`n"); + [int]$lineIndex = 0; + [array]$Result = @(); + + foreach ($line in $ObjectList) { + if ([string]::IsNullOrEmpty($line)) { + continue; + } + + foreach ($entry in $Find) { + if ($line -like $entry) { + [string]$ResultLine = [string]::Format( + 'Line #{0} => "{1}"', + $lineIndex, + $line + ); + $Result += $ResultLine; + } + } + + $lineIndex += 1; + } + + if ([string]::IsNullOrEmpty($OutFile)) { + Write-Output $Result; + } else { + Set-Content -Path $OutFile -Value $Result; + } +} diff --git a/lib/core/icingaagent/getters/Get-IcingaAgentObjectList.psm1 b/lib/core/icingaagent/getters/Get-IcingaAgentObjectList.psm1 new file mode 100644 index 0000000..f79299d --- /dev/null +++ b/lib/core/icingaagent/getters/Get-IcingaAgentObjectList.psm1 @@ -0,0 +1,7 @@ +function Get-IcingaAgentObjectList() +{ + $Binary = Get-IcingaAgentBinary; + $ObjectList = Start-IcingaProcess -Executable $Binary -Arguments 'object list'; + + return $ObjectList.Message; +} diff --git a/lib/core/icingaagent/writers/Write-IcingaAgentObjectList.psm1 b/lib/core/icingaagent/writers/Write-IcingaAgentObjectList.psm1 new file mode 100644 index 0000000..ea2615b --- /dev/null +++ b/lib/core/icingaagent/writers/Write-IcingaAgentObjectList.psm1 @@ -0,0 +1,14 @@ +function Write-IcingaAgentObjectList() +{ + param( + [string]$Path + ); + + if ([string]::IsNullOrEmpty($Path)) { + throw 'Please specify a path to write the Icinga objects to'; + } + + $ObjectList = Get-IcingaAgentObjectList; + + Set-Content -Path $Path -Value $ObjectList; +}