Page 1 of 1

New Script to check jobs returning Blank from Nagios Check

Posted: Fri Jan 03, 2020 3:50 pm
by kwhogster
Trying to write a script so that Nagios can check My Veeam Backup Copy Jobs

My command line

Code: Select all

/usr/lib/nagios/plugins/check_nrpe -t 50 -H TGCS008 -c  checkveeamcopy -a 'Exchange Backup Copy Job'
My Nsclient.ini

Code: Select all

; check veeam copy jobs
checkveeamcopy = cmd /c echo scripts/powershell/check_veeam_backups.ps1 "$ARG1$" "$ARG2$"; exit $LastExitCode | powershell.exe -command -

When I run it from my command line on the Nagios server I get a blank return.

root@nagios017:/usr/local/nagios/etc/objects/windowsservers# /usr/lib/nagios/plugins/check_nrpe -t 50 -H TGCS008 -c checkveeamcopy -a 'Exchange Backup Copy Job'


root@nagios017:/usr/local/nagios/etc/objects/windowsservers#


My powershell script

Code: Select all

$name = $args[0]

if ((Get-PSSnapin -Name VeeamPSSNapin -ErrorAction SilentlyContinue) -eq $null)

{

    Add-PSSnapin VeeamPSSNapin

}

$JobNames = Get-VBRJob -Name $name


    foreach ($JobName in $JobNames) {

    $Job = Get-VBRJob -name $JobName.Name

    $LastSession = $Job.FindLastSession()

    $Name = $Job.Name

    $Status = $LastSession.State

    $Progress = "$($LastSession.BaseProgress)%"

    $results += New-Object PSObject -Property @{JobName = $Name; Status = $Status; Progress = $Progress;}
    $results
    
} 
When I run the powerhell script manually

PS C:\program files\nsclient++\scripts\powershell> .\check_veeam_backups.ps1 'Exchange Backup Copy Job'

Status Progress JobName
------ -------- -------
Idle 100% Exchange Backup Copy Job


Any ideas


Thank you

Tom

Re: New Script to check jobs returning Blank from Nagios Che

Posted: Fri Jan 03, 2020 4:55 pm
by cdienger
Try moving $results outside of the foreach loop and adding an exit code:

Code: Select all

    $name = $args[0]

    if ((Get-PSSnapin -Name VeeamPSSNapin -ErrorAction SilentlyContinue) -eq $null)

    {

        Add-PSSnapin VeeamPSSNapin

    }

	$results = ""
    $JobNames = Get-VBRJob -Name $name


        foreach ($JobName in $JobNames) {

        $Job = Get-VBRJob -name $JobName.Name

        $LastSession = $Job.FindLastSession()

        $Name = $Job.Name

        $Status = $LastSession.State

        $Progress = "$($LastSession.BaseProgress)%"

        $results += New-Object PSObject -Property @{JobName = $Name; Status = $Status; Progress = $Progress;}
       
    } 
	
	$results
	exit 0

Re: New Script to check jobs returning Blank from Nagios Che

Posted: Fri Jan 03, 2020 5:06 pm
by kwhogster

Code: Select all

$name = $args[0]

if ((Get-PSSnapin -Name VeeamPSSNapin -ErrorAction SilentlyContinue) -eq $null)

{

    Add-PSSnapin VeeamPSSNapin

}

$JobNames = Get-VBRJob -Name $name


    foreach ($JobName in $JobNames) {

    $Job = Get-VBRJob -name $JobName.Name

    $LastSession = $Job.FindLastSession()

    $Name = $Job.Name

    $Status = $LastSession.State

    $Progress = "$($LastSession.BaseProgress)%"

    $results += New-Object PSObject -Property @{JobName = $Name; Status = $Status; Progress = $Progress;}
    
    
} 

$results
exit 0

Same results from the Nagios server command line just returns blanks
From powershell works fine.

Re: New Script to check jobs returning Blank from Nagios Che

Posted: Mon Jan 06, 2020 11:58 am
by cdienger
The line:

Code: Select all

$results = ""
is missing in your updated script. Make sure it is added outside of and above the scope of the foreach loop.

Re: New Script to check jobs returning Blank from Nagios Che

Posted: Mon Jan 06, 2020 2:29 pm
by kwhogster
root@tgcs017:/usr/local/nagios/etc/objects/windowsservers# /usr/lib/nagios/plugi
No output available from command (checkveeamcopy).


Now I get this

Re: New Script to check jobs returning Blank from Nagios Che

Posted: Mon Jan 06, 2020 3:47 pm
by tgriep
I just want to verify that if you run the script manually, is the first line a blank line?

If so, then the powershell script needs to be written to not do that as the first line is sent back to Nagios as a blank, it will show as a blank line in the GUI.

Re: New Script to check jobs returning Blank from Nagios Che

Posted: Tue Jan 07, 2020 2:39 pm
by kwhogster
Guys I got this working now.

changed the "$results = """ to this write-host $Name $Status $Progress $LastResult

I would like to post this script so that other can use this as a plugin.

Can you tell me how I can do that?

Thank you

Tom

Re: New Script to check jobs returning Blank from Nagios Che

Posted: Tue Jan 07, 2020 3:10 pm
by tgriep
The Nagios Exchange site is where you would post the Powershell script.
https://exchange.nagios.org/

Create a login, upload and document the plugin and after it is reviewed, it should be enabled for others to download.

Re: New Script to check jobs returning Blank from Nagios Che

Posted: Tue Jan 07, 2020 4:21 pm
by kwhogster
Thank you

I will document it and post

You can lock this one as resolved.

Thanks

Tom

Re: New Script to check jobs returning Blank from Nagios Che

Posted: Tue Jan 07, 2020 4:48 pm
by tgriep
Your Welcome.

Closing the post.