mirror of
https://github.com/Icinga/icinga-powershell-framework.git
synced 2025-12-20 23:00:35 -05:00
Code cleanup
This commit is contained in:
parent
c3063d23fa
commit
47b5f2327c
1 changed files with 45 additions and 278 deletions
|
|
@ -101,6 +101,51 @@ function Import-IcingaLib()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Publish-IcingaModuleManifests()
|
||||||
|
{
|
||||||
|
param(
|
||||||
|
[string]$Module
|
||||||
|
);
|
||||||
|
|
||||||
|
[string]$ManifestDir = Join-Path -Path $PSScriptRoot -ChildPath 'manifests';
|
||||||
|
[string]$ModuleFile = [string]::Format('{0}.psd1', $Module);
|
||||||
|
[string]$PSDFile = Join-Path -Path $ManifestDir -ChildPath $ModuleFile;
|
||||||
|
|
||||||
|
if (Test-Path $PSDFile) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
New-ModuleManifest -path $PSDFile -ModuleVersion 1.0 -Author $env:USERNAME -CompanyName 'Icinga GmbH' -Copyright '(c) 2019 Icinga GmbH. All rights reserved.' -PowerShellVersion 4.0;
|
||||||
|
$Content = Get-Content $PSDFile;
|
||||||
|
$NewContent = @();
|
||||||
|
|
||||||
|
foreach ($line in $Content) {
|
||||||
|
if ([string]::IsNullOrEmpty($line)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($line[0] -eq '#') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($line.Contains('#')) {
|
||||||
|
$line = $line.Substring(0, $line.IndexOf('#'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$tmpLine = $line;
|
||||||
|
while ($tmpLine.Contains(' ')) {
|
||||||
|
$tmpLine = $tmpLine.Replace(' ', '');
|
||||||
|
}
|
||||||
|
if ([string]::IsNullOrEmpty($tmpLine)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$NewContent += $line;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set-Content -Path $PSDFile -Value $NewContent;
|
||||||
|
}
|
||||||
|
|
||||||
function Get-IcingaPluginDir()
|
function Get-IcingaPluginDir()
|
||||||
{
|
{
|
||||||
return (Join-Path -Path $PSScriptRoot -ChildPath 'lib\plugins\');
|
return (Join-Path -Path $PSScriptRoot -ChildPath 'lib\plugins\');
|
||||||
|
|
@ -120,281 +165,3 @@ function Get-IcingaPowerShellConfigDir()
|
||||||
{
|
{
|
||||||
return (Join-Path -Path $PSScriptRoot -ChildPath 'config');
|
return (Join-Path -Path $PSScriptRoot -ChildPath 'config');
|
||||||
}
|
}
|
||||||
|
|
||||||
function Install-Icinga()
|
|
||||||
{
|
|
||||||
[string]$command = Get-Icinga-Command('setup');
|
|
||||||
return &$command;
|
|
||||||
}
|
|
||||||
|
|
||||||
function Get-Icinga-Setup()
|
|
||||||
{
|
|
||||||
[string]$command = Get-Icinga-Command('setup');
|
|
||||||
return &$command -IsAgentIntalled $TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
function Get-Icinga-Service()
|
|
||||||
{
|
|
||||||
$Icinga2.Service.Status();
|
|
||||||
}
|
|
||||||
|
|
||||||
function Start-Icinga-Service()
|
|
||||||
{
|
|
||||||
$Icinga2.Service.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
function Stop-Icinga-Service()
|
|
||||||
{
|
|
||||||
$Icinga2.Service.Stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
function Restart-Icinga-Service()
|
|
||||||
{
|
|
||||||
$Icinga2.Service.Restart();
|
|
||||||
}
|
|
||||||
|
|
||||||
function Install-Icinga-Service()
|
|
||||||
{
|
|
||||||
[CmdletBinding()]
|
|
||||||
param(
|
|
||||||
[string]$IcingaServicePath = ''
|
|
||||||
)
|
|
||||||
$Icinga2.Service.Install($IcingaServicePath);
|
|
||||||
}
|
|
||||||
|
|
||||||
function Uninstall-Icinga-Service()
|
|
||||||
{
|
|
||||||
$Icinga2.Service.Uninstall();
|
|
||||||
}
|
|
||||||
|
|
||||||
function Start-Icinga-Daemon
|
|
||||||
{
|
|
||||||
[CmdletBinding()]
|
|
||||||
param(
|
|
||||||
[Switch]$NoConsole = $FALSE
|
|
||||||
);
|
|
||||||
|
|
||||||
if ((Get-Icinga-Setup) -eq $FALSE) {
|
|
||||||
$Icinga2.Log.Write(
|
|
||||||
$Icinga2.Enums.LogState.Warning,
|
|
||||||
'The agent seems to be not configured on this system. Please run "Install-Icinga" and try again.'
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($NoConsole) {
|
|
||||||
$Icinga2.Log.DisableConsole();
|
|
||||||
}
|
|
||||||
|
|
||||||
$Icinga2.TCPDaemon.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
function Stop-Icinga-Daemon()
|
|
||||||
{
|
|
||||||
if ((Get-Icinga-Setup) -eq $FALSE) {
|
|
||||||
$Icinga2.Log.Write(
|
|
||||||
$Icinga2.Enums.LogState.Warning,
|
|
||||||
'The agent seems to be not configured on this system. Please run "Install-Icinga" and try again.'
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$Icinga2.TCPDaemon.Stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
function Start-Icinga-Checker
|
|
||||||
{
|
|
||||||
[CmdletBinding()]
|
|
||||||
param(
|
|
||||||
[Switch]$NoConsole = $FALSE
|
|
||||||
);
|
|
||||||
|
|
||||||
if ((Get-Icinga-Setup) -eq $FALSE) {
|
|
||||||
$Icinga2.Log.Write(
|
|
||||||
$Icinga2.Enums.LogState.Warning,
|
|
||||||
'The agent seems to be not configured on this system. Please run "Install-Icinga" and try again.'
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($NoConsole) {
|
|
||||||
$Icinga2.Log.DisableConsole();
|
|
||||||
}
|
|
||||||
|
|
||||||
$Icinga2.Checker.Start();
|
|
||||||
}
|
|
||||||
|
|
||||||
function Stop-Icinga-Checker
|
|
||||||
{
|
|
||||||
if ((Get-Icinga-Setup) -eq $FALSE) {
|
|
||||||
$Icinga2.Log.Write(
|
|
||||||
$Icinga2.Enums.LogState.Warning,
|
|
||||||
'The agent seems to be not configured on this system. Please run "Install-Icinga" and try again.'
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$Icinga2.Checker.Stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
<#
|
|
||||||
# This function allows us to easily call core modules by simply
|
|
||||||
# providing the name of the module we want to load
|
|
||||||
#>
|
|
||||||
function Get-Icinga-Command()
|
|
||||||
{
|
|
||||||
[CmdletBinding()]
|
|
||||||
param(
|
|
||||||
[string]$command = ''
|
|
||||||
);
|
|
||||||
|
|
||||||
[string]$command = [string]::Format('core\{0}.ps1', $command);
|
|
||||||
|
|
||||||
return (Join-Path $PSScriptRoot -ChildPath $command);
|
|
||||||
}
|
|
||||||
|
|
||||||
<#
|
|
||||||
# Execute checks based on a filter or execute all of them
|
|
||||||
#>
|
|
||||||
function New-Icinga-Monitoring()
|
|
||||||
{
|
|
||||||
param(
|
|
||||||
[array]$Include = @(),
|
|
||||||
[array]$Exclude = @(),
|
|
||||||
[switch]$ListModules = $FALSE,
|
|
||||||
$Config = $null
|
|
||||||
);
|
|
||||||
|
|
||||||
if ((Get-Icinga-Setup) -eq $FALSE) {
|
|
||||||
$Icinga2.Log.Write(
|
|
||||||
$Icinga2.Enums.LogState.Warning,
|
|
||||||
'The agent seems to be not configured on this system. Please run "Install-Icinga" and try again.'
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
[string]$command = Get-Icinga-Command('monitoring');
|
|
||||||
return &$command -Include $Include -Exclude $Exclude -ListModules $ListModules -Config $Config -AgentRoot $Icinga2.App.RootPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
<#
|
|
||||||
# Retreive Performance Counter from our Windows System
|
|
||||||
#>
|
|
||||||
function Get-Icinga-Counter()
|
|
||||||
{
|
|
||||||
param(
|
|
||||||
# Allows to specify the full path of a counter to fetch data. Example '\Processor(*)\% Processor Time'
|
|
||||||
[string]$Counter = '',
|
|
||||||
# Allows to fetch all counters of a specific category, like 'Processor'
|
|
||||||
[string]$ListCounter = '',
|
|
||||||
# Provide an array of counters we check in a bulk '\Processor(*)\% Processor Time', '\Processor(*)\% c1 time'"
|
|
||||||
[array]$CounterArray = @(),
|
|
||||||
# List all available Performance Counter Categories on a system
|
|
||||||
[switch]$ListCategories = $FALSE,
|
|
||||||
# By default counters will wait globally for 500 milliseconds. With this we can skip it. Use with caution!
|
|
||||||
[switch]$SkipWait = $FALSE,
|
|
||||||
# These arguments apply to CreateStructuredPerformanceCounterTable
|
|
||||||
# This is the category name we want to create a structured output
|
|
||||||
# Example: 'Network Interface'
|
|
||||||
[string]$CreateStructuredOutputForCategory = '',
|
|
||||||
# This is the hashtable of Performance Counters, created by
|
|
||||||
# PerformanceCounterArray
|
|
||||||
[hashtable]$StructuredCounterInput = @{},
|
|
||||||
# This argument is just a helper to replace certain strings within
|
|
||||||
# a instance name with simply nothing.
|
|
||||||
# Example: 'HarddiskVolume1' => '1'
|
|
||||||
[array]$StructuredCounterInstanceCleanup = @()
|
|
||||||
);
|
|
||||||
|
|
||||||
if ((Get-Icinga-Setup) -eq $FALSE) {
|
|
||||||
$Icinga2.Log.Write(
|
|
||||||
$Icinga2.Enums.LogState.Warning,
|
|
||||||
'The agent seems to be not configured on this system. Please run "Install-Icinga" and try again.'
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
[string]$command = Get-Icinga-Command('perfcounter');
|
|
||||||
return (&$command `
|
|
||||||
-Counter $Counter `
|
|
||||||
-ListCounter $ListCounter `
|
|
||||||
-CounterArray $CounterArray `
|
|
||||||
-ListCategories $ListCategories `
|
|
||||||
-SkipWait $SkipWait `
|
|
||||||
-CreateStructuredOutputForCategory $CreateStructuredOutputForCategory `
|
|
||||||
-StructuredCounterInput $StructuredCounterInput `
|
|
||||||
-StructuredCounterInstanceCleanup $StructuredCounterInstanceCleanup
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
<#
|
|
||||||
# Get a single config key of Icinga 2 or the entire configuration
|
|
||||||
#>
|
|
||||||
function Get-Icinga-Config()
|
|
||||||
{
|
|
||||||
param(
|
|
||||||
[string]$Key = '',
|
|
||||||
[switch]$ListConfig = $FALSE
|
|
||||||
);
|
|
||||||
|
|
||||||
[string]$command = Get-Icinga-Command('config');
|
|
||||||
return &$command -GetConfig $Key -ListConfig $ListConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
function Set-Icinga-Config()
|
|
||||||
{
|
|
||||||
param(
|
|
||||||
[string]$Key = '',
|
|
||||||
[Object]$Value = ''
|
|
||||||
);
|
|
||||||
|
|
||||||
[string]$command = Get-Icinga-Command('config');
|
|
||||||
return &$command -AddKey $Key -AddValue $Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
function Remove-Icinga-Config()
|
|
||||||
{
|
|
||||||
param(
|
|
||||||
[string]$Key = ''
|
|
||||||
);
|
|
||||||
|
|
||||||
[string]$command = Get-Icinga-Command('config');
|
|
||||||
return &$command -RemoveConfig $Key;
|
|
||||||
}
|
|
||||||
|
|
||||||
function New-Icinga-Config()
|
|
||||||
{
|
|
||||||
[string]$command = Get-Icinga-Command('config');
|
|
||||||
return &$command -Reload $TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
function Get-Icinga-Lib()
|
|
||||||
{
|
|
||||||
param([string]$Include);
|
|
||||||
|
|
||||||
[string]$IncludeFile = (Join-Path $PSScriptRoot -ChildPath (
|
|
||||||
[string]::Format(
|
|
||||||
'\core\include\{0}.ps1',
|
|
||||||
$Include
|
|
||||||
)));
|
|
||||||
|
|
||||||
if (-Not (Test-Path $IncludeFile)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (& $IncludeFile);
|
|
||||||
}
|
|
||||||
|
|
||||||
function Get-Icinga-Object()
|
|
||||||
{
|
|
||||||
return $Icinga2;
|
|
||||||
}
|
|
||||||
|
|
||||||
# Initialise base configuration for our module
|
|
||||||
<#
|
|
||||||
$Icinga2 = & (Join-Path -Path $PSScriptRoot -ChildPath '\core\init.ps1') `
|
|
||||||
-RootDirectory $PSScriptRoot `
|
|
||||||
-ModuleName $MyInvocation.MyCommand.Name;
|
|
||||||
|
|
||||||
Export-ModuleMember @Icinga2;
|
|
||||||
#>
|
|
||||||
Loading…
Reference in a new issue