Page 1 of 1

powershell errors

Posted: Fri Jul 11, 2014 8:10 am
by MichielvM
Hi all, I have been messing around with this powershell script http://exchange.nagios.org/directory/Pl ... rV/details
The idea is to monitor Hyper-V failover disks.

From the commandline on the host I get this line working:

Code: Select all

PS C:\Program Files\NSClient++\scripts> .\check_csv_freespace.ps1 -n ZWEB_CSV01 -w 80 -c 90
OK - ZWEB_CSV01 total: 240GB, used: 81GB (33%), free: 159GB (66%) | 'ZWEB_CSV01 used_space'=81Gb;192;216;0;240
Copied the script to the nsclient++\scripts directory.
The ncp.ini puzzles me. Is that the same as the nsclient.ini?
I assumed it is, so I went ahead and made these changes to it.

Code: Select all

[/settings/external scripts/wrappings]
bat = scripts\\%SCRIPT% %ARGS%
ps1 = cmd /c echo scripts\\%SCRIPT% %ARGS%; exit($lastexitcode) | powershell.exe -command -
command[check_csv_freespace]=cmd /c echo .check_csv_freespace.ps1 -n $ARG1$ -w $ARG2$ -c $ARG3$ | powershell.exe -command -
vbs = cscript.exe //T:30 //NoLogo scripts\\lib\wrapper.vbs %SCRIPT% %ARGS%

[/settings/external scripts/wrapped scripts]
check_files = check_files.vbs $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$ $ARG7$ $ARG8$ $ARG9$ $ARG10$ $ARG11$ $ARG12$ $ARG13$ $ARG14$
check_nbu_backup = check_nbu_backstat2.ps1 $ARG1$
check_csv = check_csv.ps1
check_csv_freespace = check_csv_freespace.ps1

[/settings/external scripts/alias]
alias_updates = check_csv -warning 80 -critical 75

[/settings/external scripts]
allow arguments = 1
allow nasty characters = 1
allow nasty_meta_chars = 1
timeout = 60
The command looks like this:

Code: Select all

define command {
       command_name                 check_csv_freespace
       command_line                  	$USER1$/check_nrpe -H $HOSTADDRESS$ -c check_csv_freespace -a $ARG1$
}
The service looks like this:

Code: Select all

define service {
	host_name			hyper14
	service_description		check Cluster Shared Volume Freespace
	check_command		check_csv_freespace!ZWEB_CSV01 80 90
	max_check_attempts		5
	check_interval			5
	retry_interval			1
	check_period			24x7
	notification_interval		60
	first_notification_delay	0
	notification_period		standbyuren
	notification_options		n
	notifications_enabled		0
	contacts			       Team 1  
	register			       1
	}	
Restarted the nscp service on the host and ran the check, to get this output:

Code: Select all

C:\Program Files\NSClient++\scripts\check_csv_freespace.ps1 : Cannot process
command because of one or more missing mandatory parameters: csvname warnlevel
critlevel.
At line:1 char:1
+ scripts\\\\check_csv_freespace.ps1 ; exit($lastexitcode)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [check_csv_freespace.ps1],
ParameterBindingException
+ FullyQualifiedErrorId : MissingMandatoryParameter,check_csv_freespace.ps1
As I understand it, somewhere along the line the parameters/arguments get misinterpreted.
Must have been staring at it too long, as I fail to see where..

some technotes:
Host is a Windows 2012 64 bit server with Powershell version 4.0 in a Hyper-V cluster
Nagios Xi server is 2012R2.5
I've been using Xi to create and edit commands.

Any thoughts?

Re: powershell errors

Posted: Fri Jul 11, 2014 12:51 pm
by sreinhardt
I would suggest removing the portion under wrapped scripts, it seems like it is thinking you wish to call that, which does not allow for args, instead of your defined command. You are probably also going to want to modify your command definition just a bit like:

From:

Code: Select all

command[check_csv_freespace]=cmd /c echo .check_csv_freespace.ps1 -n $ARG1$ -w $ARG2$ -c $ARG3$ | powershell.exe -command -
To:

Code: Select all

command[check_csv_freespace]=cmd /c echo scripts\\check_csv_freespace.ps1 -n $ARG1$ -w $ARG2$ -c $ARG3$; exit($lastexitcode) | powershell.exe -command -

Re: powershell errors

Posted: Mon Jul 14, 2014 3:25 am
by MichielvM
I've made the suggested changes. Removing the Wrapped Scripts portion results in error:

Code: Select all

No handler for command: check_csv_freespace
That makes sense, because it defines a variable to be used by the command part. right?
I put that part back and added $ARG1$, to look like this:

Code: Select all

[/settings/external scripts/wrapped scripts]
check_csv_freespace = check_csv_freespace.ps1 $ARG1$
Only to get the "cannot process ..." error back.

Re: powershell errors

Posted: Mon Jul 14, 2014 7:29 am
by MichielvM
Fixed it!

Code: Select all

[/settings/external scripts/wrappings]
bat = scripts\\%SCRIPT% %ARGS%
ps1 = cmd /c echo scripts\\%SCRIPT% %ARGS%; exit($lastexitcode) | powershell.exe -command -
command[check_csv_freespace] = cmd /c echo scripts\\check_csv_freespace.ps1 -n %ARG1% -w %ARG2% -c %ARG3%; exit($lastexitcode) | powershell.exe -command -
vbs = cscript.exe //T:30 //NoLogo scripts\\lib\wrapper.vbs %SCRIPT% %ARGS%

[/settings/external scripts/wrapped scripts]
check_files = check_files.vbs $ARG1$ $ARG2$ $ARG3$ $ARG4$ $ARG5$ $ARG6$ $ARG7$ $ARG8$ $ARG9$ $ARG10$ $ARG11$ $ARG12$ $ARG13$ $ARG14$
check_nbu_backup = check_nbu_backstat2.ps1 $ARG1$
check_csv_freespace = check_csv_freespace.ps1 -n $ARG1$ -w $ARG2$ -c $ARG3$
The only thing is that when one of the disks violates a treshold, Xi shows state OK (green, not red or yellow.)
CRITICAL - ZWEB_CSV01 total: 240GB, used: 81GB (33%), free: 159GB (66%)
When I run the command from the CLI, followed by

Code: Select all

[root@tin-rhn01 ~]# /usr/local/nagios/libexec/check_nrpe -H 10.213.244.213 -c check_csv_freespace -a ZEXC_CSV05 80 90
CRITICAL - ZEXC_CSV05 total: 1300GB, used: 1202GB (92%), free: 98GB (7%) |'ZEXC_CSV05 used_space'=1202Gb;1040;1170;0;1300
[root@tin-rhn01 ~]# echo $?
0
it gives 0
It seems that the used script isn't returning the correct exit codes.

Re: powershell errors

Posted: Mon Jul 14, 2014 10:09 am
by sreinhardt
It exits without the correct state, as it seems the author did not include an exit line, to use the exitcode that he set. I would suggest changing line 53, where there is nothing, to:

Code: Select all

exit $exitcode
Edit: I did send the developer an email asking to have this corrected.

Re: powershell errors

Posted: Tue Jul 15, 2014 2:44 am
by MichielvM
Hi Spenser,

You nailed it! Script works like a charm now!


Thanks
Michiel

Re: powershell errors

Posted: Tue Jul 15, 2014 7:07 am
by MichielvM
If anyone is interested in a variation on the theme: The Techs over here thought it would be nice to have the script punching out Gigabyte values instead of percentages, so I started thinkering with it.
Here's the result so far:

Code: Select all

Param(
    [parameter(Mandatory=$true)]
    [alias("n")]
    $csvname,
    [parameter(Mandatory=$true)]
    [alias("w")]
    $warnlevel,
    [parameter(Mandatory=$true)]
    [alias("c")]
    $critlevel)

$exitcode = 3 # "unknown"

$freespace = Get-WmiObject win32_volume | where-object {$_.Label -eq $csvname} | ForEach-Object {[math]::truncate($_.freespace / 1GB)}
$capacity = Get-WmiObject win32_volume | where-object {$_.Label -eq $csvname} | ForEach-Object {[math]::truncate($_.capacity / 1GB)}
$usedspace = $capacity - $freespace
$warnvalue = [math]::truncate(($capacity - $warnlevel))
$critvalue = [math]::truncate(($capacity - $critlevel))
$used = [math]::truncate(($capacity - $freespace))
$free = [math]::truncate(($capacity - $usedspace))

if ($usedspace -gt $critvalue) {
    $exitcode = 2
    Write-Host "CRITICAL - $csvname total: ${capacity}GB, used: ${usedspace}GB (${used}GB), free: ${freespace}GB (${free}GB) | '$csvname used_space'=${usedspace}Gb;$warnvalue;$critvalue;0;$capacity"
}
elseif ($usedspace -gt $warnvalue) {
    $exitcode = 1
    Write-Host "WARNING - $csvname total: ${capacity}GB, used: ${usedspace}GB (${used}GB), free: ${freespace}GB (${free}GB) | '$csvname used_space'=${usedspace}Gb;$warnvalue;$critvalue;0;$capacity"
}
else {
    $exitcode = 0
    Write-Host "OK - $csvname total: ${capacity}GB, used: ${usedspace}GB (${used}GB), free: ${freespace}GB (${free}GB) | '$csvname used_space'=${usedspace}Gb;$warnvalue;$critvalue;0;$capacity"
}


exit $exitcode
I suppose it could use some trimming here and there. Suggestions welcome.
It works for me!

Re: powershell errors

Posted: Tue Jul 15, 2014 10:32 am
by tmcdonald
If you want to get really fancy you could make it take percentages or GB, user's choice.

Since this seems to be working for you I will be closing the thread now, but people will still be able to see the plugin.