Add arguments

This forum is intended for the discussion of Nagios plugin development. Feature requests, patches, bug fixes, and all types of development-related discussions are welcome!

NOTE: The SourceForge.net nagiosplug-devel mailing list has been deprecated in favor of this forum in order to expedite support and provide additional features not available on the old mailing list.

Add arguments

Postby ruffsense » Fri Nov 25, 2016 3:12 pm

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
ruffsense
 
Posts: 129
Joined: Thu Apr 11, 2013 12:40 am

Re: Add arguments

Postby mcapra » Mon Nov 28, 2016 2:32 pm

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
http://www.mcapra.com/
User avatar
mcapra
 
Posts: 2828
Joined: Thu May 05, 2016 3:54 pm

Re: Add arguments

Postby ruffsense » Mon Nov 28, 2016 3:45 pm

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.
User avatar
ruffsense
 
Posts: 129
Joined: Thu Apr 11, 2013 12:40 am

Re: Add arguments

Postby dwhitfield » Mon Nov 28, 2016 4:31 pm

Thanks so much for posting!

It looks like your issue is resolved. Is that correct? Is it ok to lock the thread?
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
dwhitfield
The Doctor
 
Posts: 3859
Joined: Wed Sep 21, 2016 10:29 am
Location: Nagios Enterprises, LLC

Re: Add arguments

Postby ruffsense » Mon Nov 28, 2016 5:20 pm

you can lock it.:)
I don't insult, I diagnose.
User avatar
ruffsense
 
Posts: 129
Joined: Thu Apr 11, 2013 12:40 am


Return to Nagios Plugin Development

Who is online

Users browsing this forum: No registered users and 2 guests