Page 1 of 2

NCPA comma separated arguments for plugin not working

Posted: Wed Feb 02, 2022 6:46 am
by sgomeztd
Hi,

I'm trying to run a powershell on NCPA 2.3.1 but I'm having trouble making it accept a command separeted list of arguments. The powershell run directly will be like

Code: Select all

PS C:\Program Files (x86)\Nagios\NCPA\plugins> .\get_dhcp_stats.ps1 -w 10 -c 5 -e 10.108.6.0, 10.112.96.0, 10.112.10.0,10.121.10.0, 10.121.63.0, 10.121.123.64, 10.118.255.0
It works OK when the -e argument have a single IP like

Code: Select all

-M 'plugins/get_dhcp_stats.ps1' -q 'args=-w 10 -c 5 -e 10.178.255.0'
But when I try to put multiple IP address I get "not enough values to unpack (expected 2, got 1)" as it seems it does not like the commas inside the args because If i removed the commas and put just one IP after the other separated by spaces, the ncpa does not complain but the script will not work as it needs a comma separated list.

This is what I'm trying

Code: Select all

-M 'plugins/get_dhcp_stats.ps1' -q 'args=-w 10 -c 5', 'args= -e 10.108.6.0, 10.112.96.0, 10.112.10.0, 10.121.10.0,10.121.63.0, 10.121.123.64, 10.118.255.0'
I also tried using backlash to scape the commas but I still get the not enough values error.

Code: Select all

-M 'plugins/get_dhcp_stats.ps1' -q 'args=-w 10 -c 5 -e 10.108.6.0\, 10.112.96.0\, 10.112.10.0\, 10.121.10.0\, 10.121.63.0\, 10.121.123.64\, 10.118.255.0' 

Re: NCPA comma separated arguments for plugin not working

Posted: Thu Feb 03, 2022 10:00 am
by ssax
Does this work?

Code: Select all

-M 'plugins/get_dhcp_stats.ps1' -a 'w 10 -c 5 -e 10.108.6.0, 10.112.96.0, 10.112.10.0, 10.121.10.0, 10.121.63.0, 10.121.123.64, 10.118.255.0'
Please PM or attach the ps1 file as well.

Re: NCPA comma separated arguments for plugin not working

Posted: Fri Feb 04, 2022 9:59 am
by sgomeztd
Hi,

I did PM you the script. Now I'm having issues with a different Powershel script that only works on 64 bit and none of the solution I found on the forum seems to work..... is being a nightmware migrate from nsclient to ncpa agent :cry:

Re: NCPA comma separated arguments for plugin not working

Posted: Fri Feb 04, 2022 5:47 pm
by ssax
I apologize that you're having a hard time with the migration.

You'll need to switch the ncpa.cfg back then if other ps1 scripts are failing.

The PM you sent is missing the plugin, please make sure to click the Add the File button after selecting the file with the Browse button or else it won't attach.

Thank you!

Re: NCPA comma separated arguments for plugin not working

Posted: Mon Feb 07, 2022 3:31 am
by sgomeztd
HI,

I didn't realized that it shows an error when trying to upload a .ps1 file so I will just paste it here.
ssax wrote:I apologize that you're having a hard time with the migration.
You'll need to switch the ncpa.cfg back then if other ps1 scripts are failing.
We are deploying a new Nagios XI server to replace our old one and migrating at the same time from nsclient to ncpa but the format to pass arguments for the scripts is diferents. Sometimes we can copy it as we had just putting single quotes, others we need to change it a little bit but now I'm fthing with another script that only works on 64 bit that do not want to work but I'm starting to think there is something with that script.

Code: Select all

<#

.SYNOPSIS
    Nagios Powershell Plugin to verify free address by scope in Server applying threshold specified as arguments 

.DESCRIPTION
    The script will print the general status as a summary at first line then the complete information.
	Exception option as been added as optional parameter. Also display format and information about scope name

.EXAMPLE

    ./get_dhcp_stats.ps1  -w <warning threshold> -c <critical threshold> -e (<array of exception scopes by ScopeID>)

    As a Nagios Plugin:
        
        Create command in local nscp configuration: 
            nsclient.ini :   dhcpserver=get_dhcp_stats.ps1 %ARG1%
        
        then try it from Nagios Server
            ./check_nrpe  -H Server1.domain -c dhcpserver -a "-w <warning threshold> -c <critical threshold> -e (<array of exception scopes by ScopeID>"


#>
[CmdletBinding()]
param( 
        [Parameter(Mandatory=$True)]
        [int]$w ,
        [Parameter(Mandatory=$True)]
        [int]$c ,
        [Parameter(Mandatory=$False)]
        [String[]]$e
     )

# Import modules 
Import-Module DhcpServer 
$Server=([System.Net.Dns]::GetHostByName((hostname.exe)).HostName).tolower();
$nagmessage = "Name, ScopeID , AddressesFree , PercentageInUse , ReservedAddress , Status`n"
$nagexit = 0
$nagcexit = 0
$nagwexit = 0
$nagomsg="OK `n"
$nagcmsg="CRITICAL :"
$nagwmsg="WARNING :"
$nagexcmsg="EXCEPTIONS : Please notice below scopes are marked as exceptions and not being checked`n"
ForEach($exc in $e)
{
    $ScopeName = Get-DhcpServerv4Scope -ScopeId $exc | select Name
    $nagexcmsg += "          (Name: " + $ScopeName.Name + " , ScopeID: " + $exc + ")`n"
}
$ScopeList = Get-DhcpServerv4Scope -ComputerName $Server 
ForEach($Scope in $ScopeList.ScopeID)  
{ 
    if (!($e -contains $Scope)){
    
        $ScopeStats = Get-DhcpServerv4ScopeStatistics -ComputerName $Server -ScopeId $Scope | Select ScopeID,AddressesFree,AddressesInUse,PercentageInUse,ReservedAddress 
        $ScopeName = Get-DhcpServerv4Scope -ScopeId $Scope | select Name
        if ( $ScopeStats.AddressesFree -lt $c)
        {
            $nagmessage += " " + $ScopeName.Name + " , " + $ScopeStats.ScopeID + " , " +  $ScopeStats.AddressesFree + " , " + $ScopeStats.PercentageInUse + ", " + $ScopeStats.ReservedAddress +" , CRIT`n" 
            $nagcmsg += "( Name: " + $ScopeName.Name + ", ScopeID: "  + $ScopeStats.ScopeID + ", Number of free addresses: " +  $ScopeStats.AddressesFree  + ")"
            $nagcexit = 2
        } ElseIf ($ScopeStats.AddressesFree -lt $w)
        {
            $nagmessage +=  " " + $ScopeName.Name + " , " + $ScopeStats.ScopeID + " , " +  $ScopeStats.AddressesFree + " , " + $ScopeStats.PercentageInUse + " , " + $ScopeStats.ReservedAddress + " , WARN`n" 
            $nagwmsg += "( Name: " + $ScopeName.Name + ", ScopeID: "  + $ScopeStats.ScopeID + ", Number of free addresses: " +  $ScopeStats.AddressesFree  + ")"
            $nagwexit = 1
    
        } else {
            $nagmessage += " " + $ScopeName.Name + " , " + $ScopeStats.ScopeID + " , " +  $ScopeStats.AddressesFree + " , " + $ScopeStats.PercentageInUse + " , " + $ScopeStats.ReservedAddress  + " ,  OK `n"
        }   
    }   
} 

if ( $nagcexit -eq 2)
{
    Write-Host $nagcmsg
    Write-Host
    Write-Host $nagwmsg
    if ($e.Count -ne 0){
        Write-Host
        Write-Host $nagexcmsg
    }   
    Write-Host
    Write-Host $nagmessage
    Exit $nagcexit
} elseif ($nagwexit -eq 1)
{
    Write-Host $nagwmsg
    if ($e.Count -ne 0){
        Write-Host
        Write-Host $nagexcmsg
    }
    Write-Host
    Write-Host $nagmessage
    Exit $nagwexit
} else 
{
    Write-Host $nagomsg
    if ($e.Count -ne 0){
        Write-Host
        Write-Host $nagexcmsg
    }
    Write-Host
    Write-Host $nagmessage
    Exit 0
}

Re: NCPA comma separated arguments for plugin not working

Posted: Mon Feb 07, 2022 6:25 pm
by ssax
Try this one:

Code: Select all

/usr/local/nagios/libexec/check_ncpa.py -H X.X.X.X -t 'yourtoken' -M 'plugins/get_dhcp_stats.ps1' -a '-w 10 -c 5 -e "11.11.11.0, 11.11.12.0"'
Which would be this:

Code: Select all

-M 'plugins/get_dhcp_stats.ps1' -a 'w 10 -c 5 -e "10.108.6.0, 10.112.96.0, 10.112.10.0, 10.121.10.0, 10.121.63.0, 10.121.123.64, 10.118.255.0"'

Re: NCPA comma separated arguments for plugin not working

Posted: Mon Feb 14, 2022 11:19 am
by sgomeztd
ssax wrote:Try this one:

Code: Select all

/usr/local/nagios/libexec/check_ncpa.py -H X.X.X.X -t 'yourtoken' -M 'plugins/get_dhcp_stats.ps1' -a '-w 10 -c 5 -e "11.11.11.0, 11.11.12.0"'
Which would be this:

Code: Select all

-M 'plugins/get_dhcp_stats.ps1' -a 'w 10 -c 5 -e "10.108.6.0, 10.112.96.0, 10.112.10.0, 10.121.10.0, 10.121.63.0, 10.121.123.64, 10.118.255.0"'
I have started trying running the command directly on the nagios server shell instead of the GUI to troublehsoot. Running it as you mention I get the following that as you can see puts an extra " on the call

Code: Select all

~$/usr/local/nagios/libexec/check_ncpa.py -H 10.100.64.190 -t 'TOKEN' -P 5693 -M 'plugins/get_dhcp_stats.ps1' -a '-w 10 -c 5 -e "10.108.6.0,10.112.96.0"'
Get-DhcpServerv4Scope : Cannot process argument transformation on parameter
'ScopeId'. Cannot convert value ""10.108.6.0,10.112.96.0"" to type
"System.Net.IPAddress[]". Error: "Cannot convert value
""10.108.6.0,10.112.96.0"" to type "System.Net.IPAddress". Error: "An invalid
IP address was specified.""
At C:\Program Files (x86)\Nagios\NCPA\plugins\get_dhcp_stats.ps1:51 char:49
+     $ScopeName = Get-DhcpServerv4Scope -ScopeId $exc | select Name
+                                                 ~~~~
    + CategoryInfo          : InvalidData: (:) [Get-DhcpServerv4Scope], Parame
   terBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-DhcpSer
   verv4Scope
If I run it without the quotes I get that is putting everything between quotes anyway.

Code: Select all

$/usr/local/nagios/libexec/check_ncpa.py -H 10.100.64.190 -t 'TOKEN' -P 5693 -M 'plugins/get_dhcp_stats.ps1' -a '-w 10 -c 5 -e 10.108.6.0,10.112.96.0'
Get-DhcpServerv4Scope : Cannot process argument transformation on parameter
'ScopeId'. Cannot convert value "10.108.6.0,10.112.96.0" to type
"System.Net.IPAddress[]". Error: "Cannot convert value
"10.108.6.0,10.112.96.0" to type "System.Net.IPAddress". Error: "An invalid IP
address was specified.""
At C:\Program Files (x86)\Nagios\NCPA\plugins\get_dhcp_stats.ps1:51 char:49
+     $ScopeName = Get-DhcpServerv4Scope -ScopeId $exc | select Name
For some reason is adding an extra pair of " that I do not need. I have been trying with different combination of quotes, spaces, commans, braquets, etc but nothing seeems to do the trick. I Also saw here https://stackoverflow.com/questions/132 ... ommandline they suggest using Powershell -command instead of Powershell -File but so far I have not managed to make it work

Re: NCPA comma separated arguments for plugin not working

Posted: Tue Feb 15, 2022 10:46 am
by ssax
I was only able to get it to work by modifying the plugin and removing the double-quotes and the spaces between the IPs:

Code: Select all

/usr/local/nagios/libexec/check_ncpa.py -H X.X.X.X -t 'yourtoken' -M 'plugins/get_dhcp_stats.ps1' -a '-w 10 -c 5 -e 11.11.11.0,11.11.12.0'

Code: Select all

<#

.SYNOPSIS
	Nagios Powershell Plugin to verify free address by scope in Server applying threshold specified as arguments

.DESCRIPTION
	The script will print the general status as a summary at first line then the complete information.
   Exception option as been added as optional parameter. Also display format and information about scope name

.EXAMPLE

	./get_dhcp_stats.ps1  -w <warning threshold> -c <critical threshold> -e (<array of exception scopes by ScopeID>)

	As a Nagios Plugin:
	   
		Create command in local nscp configuration:
			nsclient.ini :   dhcpserver=get_dhcp_stats.ps1 %ARG1%
	   
		then try it from Nagios Server
			./check_nrpe  -H Server1.domain -c dhcpserver -a "-w <warning threshold> -c <critical threshold> -e (<array of exception scopes by ScopeID>"


#>
[CmdletBinding()]
param(
		[Parameter(Mandatory=$True)]
		[int]$w ,
		[Parameter(Mandatory=$True)]
		[int]$c ,
		[Parameter(Mandatory=$False)]
		[String[]]$e
	 )

# Import modules
Import-Module DhcpServer
$Server=([System.Net.Dns]::GetHostByName((hostname.exe)).HostName).tolower();
$nagmessage = "Name, ScopeID , AddressesFree , PercentageInUse , ReservedAddress , Status`n"
$nagexit = 0
$nagcexit = 0
$nagwexit = 0
$nagomsg="OK `n"
$nagcmsg="CRITICAL :"
$nagwmsg="WARNING :"
$nagexcmsg="EXCEPTIONS : Please notice below scopes are marked as exceptions and not being checked`n"
$list = $e.split(',');
ForEach($exc in $list)
{
	[IPAddress]$exc.Trim()
	$ScopeName = Get-DhcpServerv4Scope -ScopeId $exc | select Name
	$nagexcmsg += "          (Name: " + $ScopeName.Name + " , ScopeID: " + $exc + ")`n"
}
$ScopeList = Get-DhcpServerv4Scope -ComputerName $Server
ForEach($Scope in $ScopeList.ScopeID) 
{
	if (!($e -contains $Scope)){
   
		$ScopeStats = Get-DhcpServerv4ScopeStatistics -ComputerName $Server -ScopeId $Scope | Select ScopeID,AddressesFree,AddressesInUse,PercentageInUse,ReservedAddress
		$ScopeName = Get-DhcpServerv4Scope -ScopeId $Scope | select Name
		if ( $ScopeStats.AddressesFree -lt $c)
		{
			$nagmessage += " " + $ScopeName.Name + " , " + $ScopeStats.ScopeID + " , " +  $ScopeStats.AddressesFree + " , " + $ScopeStats.PercentageInUse + ", " + $ScopeStats.ReservedAddress +" , CRIT`n"
			$nagcmsg += "( Name: " + $ScopeName.Name + ", ScopeID: "  + $ScopeStats.ScopeID + ", Number of free addresses: " +  $ScopeStats.AddressesFree  + ")"
			$nagcexit = 2
		} ElseIf ($ScopeStats.AddressesFree -lt $w)
		{
			$nagmessage +=  " " + $ScopeName.Name + " , " + $ScopeStats.ScopeID + " , " +  $ScopeStats.AddressesFree + " , " + $ScopeStats.PercentageInUse + " , " + $ScopeStats.ReservedAddress + " , WARN`n"
			$nagwmsg += "( Name: " + $ScopeName.Name + ", ScopeID: "  + $ScopeStats.ScopeID + ", Number of free addresses: " +  $ScopeStats.AddressesFree  + ")"
			$nagwexit = 1
   
		} else {
			$nagmessage += " " + $ScopeName.Name + " , " + $ScopeStats.ScopeID + " , " +  $ScopeStats.AddressesFree + " , " + $ScopeStats.PercentageInUse + " , " + $ScopeStats.ReservedAddress  + " ,  OK `n"
		}   
	}   
}

if ( $nagcexit -eq 2)
{
	Write-Host $nagcmsg
	Write-Host
	Write-Host $nagwmsg
	if ($e.Count -ne 0){
		Write-Host
		Write-Host $nagexcmsg
	}   
	Write-Host
	Write-Host $nagmessage
	Exit $nagcexit
} elseif ($nagwexit -eq 1)
{
	Write-Host $nagwmsg
	if ($e.Count -ne 0){
		Write-Host
		Write-Host $nagexcmsg
	}
	Write-Host
	Write-Host $nagmessage
	Exit $nagwexit
} else
{
	Write-Host $nagomsg
	if ($e.Count -ne 0){
		Write-Host
		Write-Host $nagexcmsg
	}
	Write-Host
	Write-Host $nagmessage
	Exit 0
}

Re: NCPA comma separated arguments for plugin not working

Posted: Wed Feb 16, 2022 5:21 am
by sgomeztd
Hi Ssax,

By adding that split you mention and passing the arguments like a coma separated list it work OK from the Nagios GUI. The script was not working properly as the line " if (!($list -contains $Scope)){" also had to be updated but after that minor change now it works OK.

I will take this into account as there is other scripts i'm migrating from Nsclint to ncpa that are also not working.

Re: NCPA comma separated arguments for plugin not working

Posted: Wed Feb 16, 2022 7:21 pm
by ssax
Okay thanks for the update! Let us know when we're okay to lock this up and mark it as resolved.