Page 1 of 2

NRPE - Powershell check - historical data

Posted: Mon Jul 13, 2015 11:06 am
by mhixson2
Hello,

Background:
I recently set up a check that runs a simple Powershell script that returns how many Citrix licenses we have in use in our environment. This is on a Windows host using NSClient++ and NRPE.

Powershell Script:

Code: Select all

Add-PSSnapin *citrix*
$licenseServer = "[citrix license server FQDN]"
$countLicenseFree = Get-WmiObject -Class "Citrix_GT_License_Pool" -Namespace "ROOT\CitrixLicensing" -ComputerName $licenseServer | Measure-Object -Property PooledAvailable -Sum

$returnStateOK = 0
$returnStateWarning = 1
$returnStateCritical = 2
$returnStateUnknown = 3

if ($countLicenseFree.sum -gt 300) {
    Write-Host "We're good:"$countLicenseFree.sum
	exit $returnStateOK
}

if ($countLicenseFree.sum -lt 300) {
	Write-Host "Keep an eye on this:"$countLicenseFree.sum
	exit $returnStateWarning
}

if ($countLicenseFree.sum -lt 100) {
	Write-Host "We're going down captain!"$countLicenseFree.sum
	exit $returnStateCritical
}

Write-Host "UNKNOWN script state"
exit $returnStateUnknown
nsclient.ini config:
[/settings/external scripts/scripts]
citrix_licenses = cmd /c echo scripts\citrix_licenses.ps1; exit($lastexitcode) | powershell.exe -command -

Question:
How do I set up this check to record historical data and possibly show a performance graph? Is one related to the other?

Thanks

Re: NRPE - Powershell check - historical data

Posted: Mon Jul 13, 2015 1:24 pm
by jdalrymple
Yes, you need to get perfdata out of it.

At the end of each of your status outputs add:

"|licenses=$countLicneseFree.sum;300;100"

https://nagios-plugins.org/doc/guidelines.html#AEN200

Re: NRPE - Powershell check - historical data

Posted: Mon Jul 13, 2015 2:50 pm
by mhixson2
jdalrymple wrote:Yes, you need to get perfdata out of it.

At the end of each of your status outputs add:

"|licenses=$countLicneseFree.sum;300;100"

https://nagios-plugins.org/doc/guidelines.html#AEN200
Great, thanks. I'm almost there.

1. Please confirm my syntax is correct in the updated script below. I had to play around with the variables a bit to actually get the value to show up in what was passed to Nagios. It did not like the $countLicenseFree.sum.
2. Should the recent data show up in the graph immediately? The performance graph is still reading zero across the board. It's been about 15 minutes.
3. What are all the extra items in the attached screenshot other than my defined citrix_licenses? Can I filter those out?

Thanks!

Code: Select all

Add-PSSnapin *citrix*
$licenseServer = "[citrix license server FQDN]"
$countLicenseFree = Get-WmiObject -Class "Citrix_GT_License_Pool" -Namespace "ROOT\CitrixLicensing" -ComputerName $licenseServer | Measure-Object -Property PooledAvailable -Sum
$sumLicenseFree = $countLicenseFree.sum

$returnStateOK = 0 
$returnStateWarning = 1
$returnStateCritical = 2
$returnStateUnknown = 3

if ($countLicenseFree.sum -gt 300) {
    Write-Host "We're good:"$countLicenseFree.sum"|citrix_licenses=$sumLicenseFree;300;100"
	exit $returnStateOK
}

if ($countLicenseFree.sum -lt 300) {
	Write-Host "Keep an eye on this:"$countLicenseFree.sum"|citrix_licenses=$sumLicenseFree;300;100"
	exit $returnStateWarning
}

if ($countLicenseFree.sum -lt 100) {
	Write-Host "We're going down captain!"$countLicenseFree.sum"|citrix_licenses=$sumLicenseFree;300;100"
	exit $returnStateCritical
}

Write-Host "UNKNOWN script state"
exit $returnStateUnknown

Re: NRPE - Powershell check - historical data

Posted: Mon Jul 13, 2015 3:02 pm
by jdalrymple
mhixson2 wrote:1. Please confirm my syntax is correct in the updated script below. I had to play around with the variables a bit to actually get the value to show up in what was passed to Nagios. It did not like the $countLicenseFree.sum.
2. Should the recent data show up in the graph immediately? The performance graph is still reading zero across the board. It's been about 15 minutes.
Naah, something is haywire. Why aren't you using "$countLicenseFree.sum" as I indicated? I assume this is from multiple "groups" of Citrix licenses that were installed and that's why you're using the sum operator? You'll want that in the perfdata also, I assume leaving it out is why you have 17 series. You can also omit the inner pair of quotes for clarity's sake.
mhixson2 wrote:3. What are all the extra items in the attached screenshot other than my defined citrix_licenses? Can I filter those out?
Can you show us the raw output of perfdata, it's on the advanced tab.

Re: NRPE - Powershell check - historical data

Posted: Mon Jul 13, 2015 3:49 pm
by mhixson2
jdalrymple wrote:Naah, something is haywire. Why aren't you using "$countLicenseFree.sum" as I indicated?
The Write-Host doesn't like the $countLicenseFree.sum variable. Here is a test in powershell of the Write-Host alone. I assume if this doesn't come out clean in powershell, Nagios won't be able to do anything with it.

Code: Select all

Write-Host "We're good:"$countLicenseFree.sum"|citrix_licenses=$countLicenseFree.sum;300;100"
We're good:  |citrix_licenses=.sum;300;100
Same test, now with the $sumLicenseFree variable (which = $countLicenseFree.sum as you can see in my script). The returned value is an accurate sum of all licenses.

Code: Select all

Write-Host "We're good:"$countLicenseFree.sum"|citrix_licenses=$sumLicenseFree;300;100"
We're good: 454 |citrix_licenses=454;300;100
jdalrymple wrote:I assume this is from multiple "groups" of Citrix licenses that were installed and that's why you're using the sum operator? You'll want that in the perfdata also, I assume leaving it out is why you have 17 series.
Correct, this is representing the total of several individual licenses. An accurate sum is returned by both $countLicenseFree.sum and $sumLicenseFree since essentially they're the same thing.
jdalrymple wrote:You can also omit the inner pair of quotes for clarity's sake.
If I omit the inner quotes, this happens

Code: Select all

Write-Host "We're good:$countLicenseFree.sum|citrix_licenses=$countLicenseFree.sum;300;100"
We're good:Microsoft.PowerShell.Commands.GenericMeasureInfo.sum|citrix_licenses=Microsoft.PowerShell.Commands.GenericMeasureInfo.sum;300;100
jdalrymple wrote:Can you show us the raw output of perfdata, it's on the advanced tab.
Sure. We've covered things in a few different states, so here's a screenshot from each.
$countLicenseFree.sum variable with inner quotes
sum-withinner.png
$countLicenseFree.sum variable without inner quotes
sum-noinner.png
$sumLicenseFree variable with inner quotes.
sum-inner-customvariable.png
Thanks

Re: NRPE - Powershell check - historical data

Posted: Mon Jul 13, 2015 4:06 pm
by jdalrymple
Weird, where is the 0 value coming from in those screenshots? The output you posted that looks like this:

Code: Select all

We're good: 454 |citrix_licenses=454;300;100
Is what you need. If you have your script configured to return data like that does it not show up as that in the Nagios interface?

Since they've dumped data into the XML and RRDs in a broken state you may need to either adjust those files to get it to display properly at this point. You need clean perfdata first though.

Re: NRPE - Powershell check - historical data

Posted: Tue Jul 14, 2015 8:15 am
by mhixson2
jdalrymple wrote:Weird, where is the 0 value coming from in those screenshots? The output you posted that looks like this:

Code: Select all

We're good: 454 |citrix_licenses=454;300;100
Is what you need. If you have your script configured to return data like that does it not show up as that in the Nagios interface?
Right. The same configuration that returns We're good: 454 |citrix_licenses=454;300;100 returns a zero value for perfdata as shown in that screenshot. That is the configuration where I had to create a new variable in my powershell script for $sumLicenseFree = $countLicenseFree.sum.
jdalrymple wrote:Since they've dumped data into the XML and RRDs in a broken state you may need to either adjust those files to get it to display properly at this point. You need clean perfdata first though.
Sorry, I don't follow this. I understand we need to get the perfdata recognized by Nagios, but what files are you referring to?

Re: NRPE - Powershell check - historical data

Posted: Tue Jul 14, 2015 9:08 am
by jdalrymple
mhixson2 wrote:Right. The same configuration that returns We're good: 454 |citrix_licenses=454;300;100 returns a zero value for perfdata as shown in that screenshot.
So - when you get good data you're running it as the Nagios user from the command line? That's the proper way to test.
mhixson2 wrote:Sorry, I don't follow this. I understand we need to get the perfdata recognized by Nagios, but what files are you referring to?
Ignore it for now.

Re: NRPE - Powershell check - historical data

Posted: Tue Jul 14, 2015 10:55 am
by mhixson2
jdalrymple wrote:So - when you get good data you're running it as the Nagios user from the command line? That's the proper way to test.
Ok, that makes sense. Up until now I have just been running commands with my admin user. I'll use nagios from here on out.

So... I found an issue with the powershell script I actually had running on the server that did not exist in my dev script on my local machine. I was defining $sumLicenseFree = $countLicenseFree.sum before I was defining $countLicenseFree.sum. Doh!

Now, with that fixed, in the service details in Nagios it's displaying performance data: Performance Data: 'citrix_licenses'=472;300;100.

What's next? Nothing has displayed in the graphs yet, and the 16 extra items in the graph are still showing up:
perfdata-update.png

Re: NRPE - Powershell check - historical data

Posted: Tue Jul 14, 2015 11:01 am
by jdalrymple
The easiest solution to fix the broken files is to delete and recreate the service. It will save you a lot of pain.

Do that and your perfdata should start working properly.