Add arguments

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
User avatar
ruffsense
Posts: 140
Joined: Thu Apr 11, 2013 12:40 am

Add arguments

Post by ruffsense »

I have created a script for nagios but now i want to change the $folder and $file $MaxAgeMinutes into $args[0] - $args[1] - $args[2].

So i can pass arguments in my nsclient.ini

test = cmd /c echo scripts\test.ps1 "$ARG1$" "$ARG2$" "$ARG3$"; exit($lastexitcode) | powershell.exe -command -

Code: Select all

$folder = 'c:\temp\'
$file = 'CGO.csv'
$MaxAgeMinutes = 1440

# Process Limit
$Limit = (Get-Date).AddMinutes((-1 * $MaxAgeMinutes))

$path = "$folder\$file"
if (Test-Path $path)
{
	if ((Get-Item $Path).LastWriteTime -lt $limit)
	{
		echo "WARNING status - $file is older than 24 hours."
		exit 1 #returns warning status
	}
	else
	{
		echo "OK status – file is OK"
		exit 0 #Return OK status
	}
}
else
{
	echo "CRITICAL status – file $file doesn’t exist"
	exit 2 #returns critical status
}
Last edited by dwhitfield on Mon Nov 28, 2016 5:21 pm, edited 1 time in total.
Reason: marking with green check mark
I don't insult, I diagnose.
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: Add arguments

Post by mcapra »

Using args in powershell:
http://stackoverflow.com/questions/1232 ... powershell

Proof of concept code:

Code: Select all

Write-Host "here's arg 0: $($args[0])"
Write-Host "here's arg 1: $($args[1])"
Write-Host "here's arg 2: $($args[2])"

Code: Select all

PS C:\> ./test.ps1 one two three
here's arg 0: one
here's arg 1: two
here's arg 2: three
Former Nagios employee
https://www.mcapra.com/
User avatar
ruffsense
Posts: 140
Joined: Thu Apr 11, 2013 12:40 am

Re: Add arguments

Post by ruffsense »

thnx.

If any one is looking for a check_file powershell script with arguments that checks if a file excist or is older then 24ours. Here you go.

Code: Select all

function check_file()
{
$folder = "$($args[0])"
$file = "$($args[1])"
$MaxAgeMinutes = 1440

# Process Limit
$Limit = (Get-Date).AddMinutes((-1 * $MaxAgeMinutes))

$path = "$folder\$file"
if (Test-Path $path)
{
   if ((Get-Item $Path).LastWriteTime -lt $limit)
   {
      echo "WARNING status - $file is older than 24 hours."
      exit 1 #returns warning status
   }
   else
   {
      echo "OK status – file is OK"
      exit 0 #Return OK status
   }
}
else
{
   echo "CRITICAL status – file $file doesn’t exist"
   exit 2 #returns critical status
}
}
I don't insult, I diagnose.
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

Re: Add arguments

Post by dwhitfield »

Thanks so much for posting!

It looks like your issue is resolved. Is that correct? Is it ok to lock the thread?
User avatar
ruffsense
Posts: 140
Joined: Thu Apr 11, 2013 12:40 am

Re: Add arguments

Post by ruffsense »

you can lock it.:)
I don't insult, I diagnose.
Locked