Cloudkick customed plugins for windows

Cloudkick which is part of RackSpace provides Agent based monitoring for external servers and cloud servers. Other than monitoring computer resources such as CPU, memory, HDD utilization. It is able to cater for customed check which is known as customed plugin. In other words, Cloudkick offers limitless way of administrator to monitor their IT assets as host level which is limited to the operating systems shell scripting support as well as limited to the creativity of the scripters.

Based on the instruction from Cloudkick, the process seems to be simple. So a working sample should looks like this, which is conviniently named as user_plugin.bat (version 0.1) :
@echo off
query user|findstr /v "USERNAME SESSIONNAME"|find /c /v "" > result_user.tmp
set /p NUM_USERS=< result_user.tmp
del result_user.tmp
if %NUM_USERS% LSS 10 ( echo status ok there are only %NUM_USERS% logged in ) else ( goto :moreten )
goto :submitmetric
:moreten
if %NUM_USERS% LSS 15 ( echo status warn there are %NUM_USERS% logged in ) else ( echo status err uh oh, there are %NUM_USERS% logged in! )
goto :submitmetric
:submitmetric
echo metric users int %NUM_USERS%

Unfortunately, user_plugin.bat does not works as expected. To make things worse there is no logs. Based on Cloudkick support, they have advised to forcing the Agent in the backend server to run in command line. Sample of the command is “Cloudkick Agent.exe –noservice –loglevel all” . The error acquired was :
Tue Jul 19 14:12:05 2011 INF: check=local_exec token=abcde.xyz.12345678 payload
_len=54 payload={"type": ["local_exec"], "check": ["user_plugin.bat"]}
Tue Jul 19 14:12:05 2011 DBG: helen select
Tue Jul 19 14:12:05 2011 DBG: 2 checks active
Tue Jul 19 14:12:05 2011 DBG: task done
Tue Jul 19 14:12:05 2011 DBG: in the lane for check_local_exec
Tue Jul 19 14:12:05 2011 ERR: err from custom plugin: exit code: 255 output: The
handle could not be opened
during redirection of handle 0.
10 was unexpected at this time.

After another round of discussion with Cloudkick support, I’ve got confirmation from Cloudkick support that there is a flaw of Cloudkick Agent 0.9.18 to handle the I/O redirection in Windows environment. Exploring the solution to create Cloudkick customed plugin for windows. The plan is to have windows batch script to wrap around the Powershell script. And it works.

The Powershell script, named user_plugin.ps1 :
query user|findstr /v "USERNAME SESSIONNAME" > match.tmp
$NUM_USERS = (get-content .match.tmp | Measure-Object)
if ($NUM_USERS.Count -le 10 )
{
write-host "status ok there are only" $NUM_USERS.Count "logged in"
}
elseif  ($NUM_USERS.Count -le 15 )
{
write-host "status warn there are" $NUM_USERS.Count "logged in"
}
else
{
write-host "status err uh oh, there are" $NUM_USERS.Count "logged in!"
}
write-host "metric users int" $NUM_USERS.Count

Windows batch script wrapper user_plugin.bat :
@echo off
powershell -executionpolicy unrestricted -file "%~dp0user_plugin.ps1

Lessons learned :
1. logs is definately your friend , use run Cloudkick Agent in command prompt using the following parameters “–noservice –loglevel all” .
2. Cloudkick Agent unable to receive parse batch sripts that has multiple I/O redirections or piping.
3. Make sure that the policy of Powershell in your server is allowing unrestricted policy when running the powershell.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.