Re: [Nagios-users] C++ Plugins

2009-04-27 Thread Michael Medin

On 2009-04-24 12:05, Christopher McAtackney wrote:

2009/4/21 Michael Medin :
  

Hello,

All the "built in" plugins are avalible as source so it should be pretty
simple to figure out.
The idea is anyways that you fill the buffer passed to the function from the
"core".
(this will BTW change in the next major release)




Thanks for the explanation Michael - this should be very helpful.
  


Ahh...

Sorry, when you mentioned C++ I assumed you were writing extensions.
External scripts can be used "as normal programs" without any specific 
NSClient++ code at all.
Then just follow the regular "writing extensions" guideline for nagios 
and you should be fine.


The difference between external script and extensions are that 
extensions are running "inside" NSClient and thus can do "more" things 
but all in all it is not that much difference so there is no real 
benefit (apart from maybe the forking and such).


// Michael Medin

I'm curious though, what are the guidelines around writing a C++
program to be executed as an external script? I've written a simple
program for checking if the driver name supplied as an arg is loaded
in the system, but I'm not using any of the above "glue" to do so, I
just make the checks and printf the output and return the result code.
Is this sufficient? Here's the code;

#include 
#include 
#include 
#include 
#include 

#define ARRAY_SIZE 1024

int main(int argc, char* argv[])
{
LPVOID drivers[ARRAY_SIZE];
DWORD cbNeeded;
int cDrivers, i;

if(EnumDeviceDrivers(drivers, sizeof(drivers), &cbNeeded) && cbNeeded
< sizeof(drivers))
{
TCHAR szDriver[ARRAY_SIZE];

cDrivers = cbNeeded / sizeof(drivers[0]);;

for (i=0; i < cDrivers; i++ )
{
if(GetDeviceDriverBaseName(drivers[i], szDriver, 
sizeof(szDriver) /
sizeof(szDriver[0])))
{
if(argv[1] != NULL && _tcscmp ( szDriver, 
_T(argv[1])) == 0)
{
printf("Driver is loaded");
return 0; // OK
}
}
}

printf("Driver is not loaded!");
return 2; // CRITICAL
}
else
{
printf("EnumDeviceDrivers failed; array size needed is %d\n",
cbNeeded / sizeof(LPVOID));
return 3; // UNKNOWN
}
}


It's based off the code here;
http://msdn.microsoft.com/en-us/library/ms682619(VS.85).aspx

I just drop the compiled program into "ExternalScripts" and add this to nsc.ini;

[External Scripts]
check_driver = ExternalScripts\Check_Driver.exe 

Is this an appropriate way to carry-out this check? It works perfectly
fine so far, but I'm just curious to see if this is a proper way of
doing things?

Cheers,
Chris

  


--
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensign option that enables unlimited
royalty-free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects___
Nagios-users mailing list
Nagios-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting 
any issue. 
::: Messages without supporting info will risk being sent to /dev/null

Re: [Nagios-users] C++ Plugins

2009-04-24 Thread Christopher McAtackney
2009/4/21 Michael Medin :
> Hello,
>
> All the "built in" plugins are avalible as source so it should be pretty
> simple to figure out.
> The idea is anyways that you fill the buffer passed to the function from the
> "core".
> (this will BTW change in the next major release)


Thanks for the explanation Michael - this should be very helpful.

I'm curious though, what are the guidelines around writing a C++
program to be executed as an external script? I've written a simple
program for checking if the driver name supplied as an arg is loaded
in the system, but I'm not using any of the above "glue" to do so, I
just make the checks and printf the output and return the result code.
Is this sufficient? Here's the code;

#include 
#include 
#include 
#include 
#include 

#define ARRAY_SIZE 1024

int main(int argc, char* argv[])
{
LPVOID drivers[ARRAY_SIZE];
DWORD cbNeeded;
int cDrivers, i;

if(EnumDeviceDrivers(drivers, sizeof(drivers), &cbNeeded) && cbNeeded
< sizeof(drivers))
{
TCHAR szDriver[ARRAY_SIZE];

cDrivers = cbNeeded / sizeof(drivers[0]);;

for (i=0; i < cDrivers; i++ )
{
if(GetDeviceDriverBaseName(drivers[i], szDriver, 
sizeof(szDriver) /
sizeof(szDriver[0])))
{
if(argv[1] != NULL && _tcscmp ( szDriver, 
_T(argv[1])) == 0)
{
printf("Driver is loaded");
return 0; // OK
}
}
}

printf("Driver is not loaded!");
return 2; // CRITICAL
}
else
{
printf("EnumDeviceDrivers failed; array size needed is %d\n",
cbNeeded / sizeof(LPVOID));
return 3; // UNKNOWN
}
}


It's based off the code here;
http://msdn.microsoft.com/en-us/library/ms682619(VS.85).aspx

I just drop the compiled program into "ExternalScripts" and add this to nsc.ini;

[External Scripts]
check_driver = ExternalScripts\Check_Driver.exe 

Is this an appropriate way to carry-out this check? It works perfectly
fine so far, but I'm just curious to see if this is a proper way of
doing things?

Cheers,
Chris

--
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensign option that enables unlimited
royalty-free distribution of the report engine for externally facing 
server and web deployment.
http://p.sf.net/sfu/businessobjects
___
Nagios-users mailing list
Nagios-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting 
any issue. 
::: Messages without supporting info will risk being sent to /dev/null


Re: [Nagios-users] C++ Plugins

2009-04-21 Thread Michael Medin
Hello,

All the "built in" plugins are avalible as source so it should be pretty 
simple to figure out.
The idea is anyways that you fill the buffer passed to the function from 
the "core".
(this will BTW change in the next major release)

Anyways...
If you are using the "glue" (around the C api) I have written have a 
look at for instance this:
http://nsclient.org/nscp/browser/branches/stable/modules/CheckExternalScripts/CheckExternalScripts.cpp

If not then have a look inside the "glue file":
http://nsclient.org/nscp/browser/branches/stable/include/NSCHelper.cpp

But essential if you are not using the glue:

NSCAPI::nagiosReturn wrapReturnString(char *buffer, unsigned int bufLen, 
std::wstring str, NSCAPI::nagiosReturn defaultReturnCode /* = 
NSCAPI::success */) {
if (str.length() >= bufLen)
return NSCAPI::returnInvalidBufferLen;
strncpy(buffer, str.c_str(), bufLen);
return defaultReturnCode;
}
extern NSCAPI::nagiosReturn NSHandleCommand(const TCHAR* IN_cmd, const 
unsigned int IN_argsLen, TCHAR **IN_args, TCHAR *OUT_retBufMessage, 
unsigned int IN_retBufMessageLen, TCHAR *OUT_retBufPerf, unsigned int 
IN_retBufPerfLen)
{
try {
std::wstring message = _T("my message");
std::wstring perf = _T("my message");
NSCAPI::nagiosReturn ret = NSCAPI::returnOK;
   
// Wrap strings
ret = wrapReturnString(OUT_retBufMessage, IN_retBufMessageLen, 
message, ret);
if (ret == NSCAPI::returnInvalidBufferLen) {
//NSC_LOG_CRITICAL(_T("error in: wrapHandleCommand(...)"));
return NSCAPI::returnIgnored;
}
ret = wrapReturnString(OUT_retBufPerf, IN_retBufPerfLen, perf, ret);
if (ret == NSCAPI::returnInvalidBufferLen) {
//NSC_LOG_CRITICAL(_T("error in: wrapHandleCommand(...)"));
return NSCAPI::returnIgnored;
}
} catch (...) {
//NSC_LOG_CRITICAL(_T("Unknown exception in: 
wrapHandleCommand(...)"));
return NSCAPI::returnIgnored;
}
}

In general I would recommend using the glue as it will be simpler to 
upgrade then...

// Michael Medin


Christopher McAtackney skrev:
> Hi all,
>
> I'm trying to develop a plug-in for NSClient++ written in C++, but I'm
> unsure of how to return the status code and the check result string
> (e.g. 2 and "Service is Critical").
>
> Are there any examples of C++ plugins online anywhere that I can use
> as a template?
>
> Cheers,
> Chris
>
> --
> Stay on top of everything new and different, both inside and 
> around Java (TM) technology - register by April 22, and save
> $200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
> 300 plus technical and hands-on sessions. Register today. 
> Use priority code J9JMT32. http://p.sf.net/sfu/p
> ___
> Nagios-users mailing list
> Nagios-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/nagios-users
> ::: Please include Nagios version, plugin version (-v) and OS when reporting 
> any issue. 
> ::: Messages without supporting info will risk being sent to /dev/null
>
>   

--
Stay on top of everything new and different, both inside and 
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today. 
Use priority code J9JMT32. http://p.sf.net/sfu/p
___
Nagios-users mailing list
Nagios-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting 
any issue. 
::: Messages without supporting info will risk being sent to /dev/null


Re: [Nagios-users] C++ Plugins

2009-04-21 Thread Christopher McAtackney
> Chris,
>
> if you do a search on http://nagiosexchange.org for "c++" you will
> find various plugins, utilities and addons written using C++.  I guess
> most or at least some of them will include the source code.  I don't
> program any version of C myself so can't check for sure which might be
> a good example for you.
>
> Cheers,
>
> Jim

Thanks Jim, I found some examples there and was able to base my plugin off them.

Cheers,
Chris

--
Stay on top of everything new and different, both inside and 
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today. 
Use priority code J9JMT32. http://p.sf.net/sfu/p
___
Nagios-users mailing list
Nagios-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting 
any issue. 
::: Messages without supporting info will risk being sent to /dev/null


Re: [Nagios-users] C++ Plugins

2009-04-21 Thread Jim Avery
2009/4/21 Christopher McAtackney :
> Hi all,
>
> I'm trying to develop a plug-in for NSClient++ written in C++, but I'm
> unsure of how to return the status code and the check result string
> (e.g. 2 and "Service is Critical").
>
> Are there any examples of C++ plugins online anywhere that I can use
> as a template?
>
> Cheers,
> Chris

Chris,

if you do a search on http://nagiosexchange.org for "c++" you will
find various plugins, utilities and addons written using C++.  I guess
most or at least some of them will include the source code.  I don't
program any version of C myself so can't check for sure which might be
a good example for you.

Cheers,

Jim

--
Stay on top of everything new and different, both inside and 
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today. 
Use priority code J9JMT32. http://p.sf.net/sfu/p
___
Nagios-users mailing list
Nagios-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting 
any issue. 
::: Messages without supporting info will risk being sent to /dev/null


[Nagios-users] C++ Plugins

2009-04-21 Thread Christopher McAtackney
Hi all,

I'm trying to develop a plug-in for NSClient++ written in C++, but I'm
unsure of how to return the status code and the check result string
(e.g. 2 and "Service is Critical").

Are there any examples of C++ plugins online anywhere that I can use
as a template?

Cheers,
Chris

--
Stay on top of everything new and different, both inside and 
around Java (TM) technology - register by April 22, and save
$200 on the JavaOne (SM) conference, June 2-5, 2009, San Francisco.
300 plus technical and hands-on sessions. Register today. 
Use priority code J9JMT32. http://p.sf.net/sfu/p
___
Nagios-users mailing list
Nagios-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting 
any issue. 
::: Messages without supporting info will risk being sent to /dev/null