2. February 2017

PowerShell Version: >1
Modules: none
As I am focused on a new check_mk implementation there might be a lot of PowerShell/check_mk around here for a bit 😉 This time I wrote something to monitor the result of a scheduled task the oldschool way. With PowerShell >4 you can use the Get-ScheduledTask function as shown here but as I want this to run even on PowerShell v1 I used the old schtasks /query command line function:
#enter task name with path in scheduler
$name = "TEST"
#gather information
$scheduled_task = schtasks /query /TN $name /v /fo LIST
# EN - "Last Result" ; DE - "Letztes Ergebnis"
$scheduled_task_result = $scheduled_task | Select-String -Pattern "Last Result"
#fix name for check_mk
if ($name -like "*\*")
{
$name = $name -replace '.*\\',''
}
#task errorstate 1
if ($scheduled_task_result -like "*1*")
{
echo `<`<`<local`>`>`>
echo "2 $name $name=1 Scheduled Task not successful!"
}
#task successful
elseif ($scheduled_task_result -like "*0*")
{
echo `<`<`<local`>`>`>
echo "0 $name $name=0 Scheduled Task successful!"
}
#task errorstate $?
else
{
echo `<`<`<local`>`>`>
echo "1 $name $name=2 Status unclear!"
}
24. January 2017

PowerShell Version: >4
Modules: none

I just wrote a custom local check for check_mk to get the Citrix Licensing Usage checked. All you need to do is fill in the $license_server and maybe customize $percent_warning and $percent_critical. The check reads the licensing WMI and gives you OK / WARNING / CRIT in check_mk for the corresponding variables. It will also show the actual used licenses as performance data:
# reset arrays
$inusecount = @()
$count = @()
# globals
$license_server = "YOUR_LICENSE_SERVER"
$percent_warning = "90"
$percent_critical = "95"
# script
$citrix_licenses = Get-WmiObject -class “Citrix_GT_License_Pool” -namespace “ROOT\CitrixLicensing” -ComputerName $license_server| select Count, InUseCount
foreach ($citrix_license in $citrix_licenses)
{
if ($($citrix_license.inusecount) -ne "0")
{
$inusecount += $($citrix_license.inusecount)
$count += $($citrix_license.Count)
}
}
# summing up variables for multiple licenses
$inusecount_sum = $(($inusecount | Measure-Object -Sum).Sum)
$count_sum = $(($count | Measure-Object -Sum).Sum)
# calculation and echo
$PercentageNum = [math]::round(($inusecount_sum/$count_sum)*100,0)
if ($PercentageNum -lt $percent_warning)
{
echo `<`<`<local`>`>`>
echo "0 CitrixLicensing used_licenses=$inusecount_sum $PercentageNum percent of licenses in use ($inusecount_sum of $count_sum)"
}
elseif ($PercentageNum -gt $percent_critical)
{
echo `<`<`<local`>`>`>
echo "2 CitrixLicensing used_licenses=$inusecount_sum $PercentageNum percent of licenses in use ($inusecount_sum of $count_sum)"
}
else
{
echo `<`<`<local`>`>`>
echo "1 CitrixLicensing used_licenses=$inusecount_sum $PercentageNum percent of licenses in use ($inusecount_sum of $count_sum)"
}
4. December 2015

PowerShell Version: >2
Modules: none
If you use check_mk to monitor your environment you are able to create simple local host checks using PowerShell.
Your check_mk agent should be installed on the target host in a specific location, such as C:\Program Files (x86)\check_mk
After the agent installation you should have a local folder (C:\Program Files (x86)\check_mk\local) in which you can put a simple PowerShell Get-Content script to return whatever you have scripted in a check_mk readable format:
Get-Content -path "C:\Program Files (x86)\check_mk\tmp\xxxxx.log"
To achieve the check_mk readable format make sure to print your scripted output into something like this:
<<<local>>>
0 CHECK_NAME – OK – CHECK_OUTPUT|PERF_DATA_DESCRIPTION=CHECK_OUTPUT(INT)
I created a short example with a folder count:
$outfile = "C:\Program Files (x86)\check_mk\tmp\xxxxx.log"
#check folder count on c
$c_folder_count = (Get-Childitem c:\ | measure).count
#check_mk output
echo `<`<`<local`>`>`> | Out-File $outfile
echo "0 FOLDER-COUNT-C - OK - We have $c_folder_count folders on drive C|folders_on_c=$c_folder_count" | Out-File $outfile -Append
The output to file C:\Program Files (x86)\check_mk\tmp\xxxx.log is:
<<<local>>>
0 FOLDER-COUNT-C – OK – We have 40 folders on drive C|folders_on_c=40
You are now able to do a short cmk -II hostname && cmk -R on your check_mk server and the new check should appear on your host.
The next step would be different states for different results:
<<<local>>>
0 CHECK_NAME – OK – CHECK_OUTPUT|PERF_DATA_DESCRIPTION=CHECK_OUTPUT(INT)
<<<local>>>
1 CHECK_NAME – WARNING – CHECK_OUTPUT|PERF_DATA_DESCRIPTION=CHECK_OUTPUT(INT)
<<<local>>>
2 CHECK_NAME – CRITICAL – CHECK_OUTPUT|PERF_DATA_DESCRIPTION=CHECK_OUTPUT(INT)
With a simple if we could generate different outputs for different cases:
$outfile = "C:\Program Files (x86)\check_mk\tmp\xxxxx.log"
#check folder count on c
$c_folder_count = (Get-Childitem c:\ | measure).count
#check_mk output
echo `<`<`<local`>`>`> | Out-File $outfile
if ($c_folder_count -lt 30)
{
#if count is below 100 we are fine
echo "0 FOLDER-COUNT-C - OK - We have $c_folder_count folders on drive C|folders_on_c=$c_folder_count" | Out-File $outfile -Append
}
else
{
#if the count is over 100 we get create a warning
echo "1 FOLDER-COUNT-C - WARNING - We have $c_folder_count folders on drive C|folders_on_c=$c_folder_count" | Out-File $outfile -Append
}
It is important to give only the second Out-File an Append, so that the output will be overwritten every time the script is running.
You could now create a local task, running the script once a week/day/hour to generate the output for check_mk.