Skip to content

Commit ee03d74

Browse files
authored
Merge pull request #198 from Icinga:feature/add_support_to_flush_api_directory_by_cmdlet
Feature: Adds Cmdlet to flush Icinga Agent API directory
2 parents 3c06c5e + 6004373 commit ee03d74

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

doc/06-Framework-Usage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ The Icinga PowerShell Framework ships with a bunch of Cmdlets for monitoring, me
1717
* [Run Icinga Agent as other Service User](frameworkusage/33-Run-Icinga-Agent-As-Other-Service-User.md)
1818
* [Install/Update Icinga Agent](frameworkusage/35-Install-Update-Icinga-Agent.md)
1919
* [Uninstall Icinga Agent](frameworkusage/34-Uninstall-Icinga-Agent.md)
20+
* [Flush Icinga Agent API Directory](frameworkusage/36-Flush-Icinga-Agent-API-Directory.md)

doc/31-Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1414
### Enhancements
1515

1616
* [#193](https://github.com/Icinga/icinga-powershell-framework/pull/193) Adds optional support for adding milliseconds to `Get-IcingaUnixTime` with the `-Milliseconds` argument for more detailed time comparison
17+
* [#198](https://github.com/Icinga/icinga-powershell-framework/pull/198) Adds support to flush the content of the Icinga Agent API directory with a single Cmdlet `Clear-IcingaAgentApiDirectory`
1718

1819
## 1.3.1 (2021-02-04)
1920

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Flush Icinga Agent API Directory
2+
3+
In some cases it might be helpful or required to flush the local Icinga Agent API directory from the disk. To assist with this process, there is a simple Cmdlet available.
4+
5+
## Delete the API Directory Content
6+
7+
To flush the content, you will have to stop the Icinga Agent service and run the following Cmdlet within an Icinga Shell:
8+
9+
```powershell
10+
Clear-IcingaAgentApiDirectory
11+
```
12+
13+
## Delete API Directory with automated Icinga Agent handling
14+
15+
In case you want to automate the process, you can add the `-Force` argument which will stop the Icinga Agent service for you, flush the API directory content and start the Icinga Agent afterwards again:
16+
17+
```powershell
18+
Clear-IcingaAgentApiDirectory -Force
19+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<#
2+
.SYNOPSIS
3+
Clears the entire content of the Icinga Agent API directory located
4+
at Program Data\icinga2\var\lib\icinga2\api\
5+
.DESCRIPTION
6+
Clears the entire content of the Icinga Agent API directory located
7+
at Program Data\icinga2\var\lib\icinga2\api\
8+
.FUNCTIONALITY
9+
Clears the entire content of the Icinga Agent API directory located
10+
at Program Data\icinga2\var\lib\icinga2\api\
11+
.EXAMPLE
12+
PS>Clear-IcingaAgentApiDirectory;
13+
.EXAMPLE
14+
PS>Clear-IcingaAgentApiDirectory -Force;
15+
.PARAMETER Force
16+
In case the Icinga Agent service is running while executing the command,
17+
the force argument will ensure the service is stopped before the API
18+
directory is flushed and restarted afterwards
19+
.INPUTS
20+
System.Boolean
21+
.LINK
22+
https://github.com/Icinga/icinga-powershell-framework
23+
#>
24+
25+
function Clear-IcingaAgentApiDirectory()
26+
{
27+
param (
28+
[switch]$Force = $FALSE
29+
);
30+
31+
$IcingaService = (Get-IcingaServices -Service icinga2).icinga2;
32+
$ApiDirectory = (Join-Path -Path $Env:ProgramData -ChildPath 'icinga2\var\lib\icinga2\api\');
33+
34+
if ((Test-Path $ApiDirectory) -eq $FALSE) {
35+
Write-IcingaConsoleError 'The Icinga Agent API directory is not present on this system. Please check if the Icinga Agent is installed';
36+
return;
37+
}
38+
39+
if ($IcingaService.configuration.Status.raw -eq 4 -And $Force -eq $FALSE) {
40+
Write-IcingaConsoleError 'The API directory can not be deleted while the Icinga Agent is running. Use the "-Force" argument to stop the service, flush the directory and restart the service again.';
41+
return;
42+
}
43+
44+
if ($IcingaService.configuration.Status.raw -eq 4) {
45+
Stop-IcingaService icinga2;
46+
Start-Sleep -Seconds 1;
47+
}
48+
49+
Write-IcingaConsoleNotice 'Flushing Icinga Agent API directory';
50+
Remove-ItemSecure -Path (Join-Path -Path $ApiDirectory -ChildPath '*') -Recurse -Force | Out-Null;
51+
Start-Sleep -Seconds 1;
52+
53+
if ($IcingaService.configuration.Status.raw -eq 4) {
54+
Start-IcingaService icinga2;
55+
}
56+
}

0 commit comments

Comments
 (0)