LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Nagios Restart Windows Service on Demand (https://www.linuxquestions.org/questions/linux-server-73/nagios-restart-windows-service-on-demand-896050/)

nomolos 08-07-2011 11:46 AM

Nagios Restart Windows Service on Demand
 
I've recently implemented Nagios for our organization and have successfully implemented it accordingly.

We'd like to further enhance Nagios functionality to be able to restart the windows service on demand. Basically, if we make a judgement call that the service needs to be restarted, we want to do so via Nagios instead of remote desktop into the server.

The existing Nagios Event Handler is able to restart the service on failure. This is not feasible for us as the service does not fail, but the processing behind it is actually failing. This is something Nagios is not able to monitor.

Hope I can get some input through you guys. Thanks in advance.

norbert74 08-08-2011 01:38 AM

If you want to restart the windows service manually you could use an action_url in the nagios service which points to a cgi.
This cgi executes the nagios plugin check_nrpe to contact nsclient++ on the windows server.
nsclient++ executes a local script (VB, Java, power shell,...) which restarts the service.

1. Create a link in nagios for the corresponding service or host with action_url that points to a cgi:
Code:

define service{
        ...
        action_url        /path/restartxyz
}

2. The cgi executes the nagios plugin check_nrpe to access nsclient++ on the windows server and passes a command name for this operation:
Code:

...
/usr/local/nagios/libexec/check_nrpe -H 192.168.1.123 -p 5666 -c restartxyz
...

3. nsclient++ on the windows server must be configured in NSC.ini to execute a local script which restarts the service.

nomolos 08-08-2011 02:40 AM

Hi Norbert,

Thank you for your feedback.

Do you happen to know where I can get more resources pertaining this? Or in fact the best guide.

Cheers!

norbert74 08-08-2011 04:44 AM

Wmic from ZenOSS can only query status info.
You need nsclient++ for operations like restarting services.
Nagios object definitions
nsclient++ with check_nrpe

nomolos 08-08-2011 01:26 PM

Hi Norbert,

I've attempted the instructions that you gave. When i clicked on the URL it directed me to http://NAGIOSIP/scripts/test.bat (my input in action_url is scripts/test.bat).

I've also configured my NSC.ini to recognize test.bat using the command nrpe_test (nrpe_test=scripts/test.bat).

Can you advise me where am I doing it wrong.

Additional Note:
- NRPE has been tested and is functional
- My configuration for Nagios is via NagiosQL

Thank you in advance.

norbert74 08-09-2011 04:00 AM

Hi nomolos,

NSC.ini does not recognize test.bat. There is one step missing: NSC.ini must recognize the command
which you pass when you execute the check_nrpe command (what comes after -c, see below).

Please make sure first that the communication between check_nrpe (nagios server) and nsclient++ (windows server) works.
Open a shell on the nagios server and execute a command, something similar to this:
Code:

/usr/local/nagios/libexec/check_nrpe -H [IP windows server] -p [port nsclient++] -c restartxyz
This command executed on the nagios machine would trigger nsclient++ on the windows machine to execute the external script restartxyz.

It works only if NSC.ini is configured to recognize restartxyz. It must contain a line like this:
Code:

[External Scripts]
...
restartxyz=java restartservice xyz

Wether it is java, VB, power shell,... does not matter. You need an external script which does the operation you need.
In this case it is a java class called restartservice which needs one parameter.
(Of course you must make sure that the windows service for nslient++
is actually running and that it has enough permissions to execute the desired operation.)

If you can't get this running I would recommend to work through this page:
http://www.nsclient.org/nscp/wiki/gu...ios/check_proc

As soon as you can restart the service from the nagios server with the check_nrpe command from the shell you can
integrate it into the nagios web interface. Write a cgi which executes the check_nrpe command and use the url
in the action_url directive.

nomolos 08-09-2011 04:08 AM

Hi Norbert,

Thanks for sticking up with my hassle.

I've configured Nagios to work with nrpe. The test that you mention was successful. The area that I fail to implement is the cgi writing. I'm not sure how to implement the command to the web interface. I tried scouting for leads on web sites but it's just a little tricky for my understanding.

Can you provide me a basic CGI sample to call the command. Thank you in advance.

norbert74 08-10-2011 01:46 AM

The most simple way I can think of is like this:

Add the action_url in the desired service definition in the Nagios configuration
Code:

define service{
        ...
        action_url                      /nagios/cgi-bin/restartxyz.php
        }

Write the script /usr/local/nagios/sbin/restartxyz.php
Code:

<?php
$output = shell_exec('/usr/local/nagios/libexec/check_nrpe -H 192.168.1.123 -p 5666 -c restartxyz');
echo "<pre>$output</pre>";
?>

Set permissions
Code:

chmod 775 /usr/local/nagios/sbin/restartxyz.php
chown nagios.nagios /usr/local/nagios/sbin/restartxyz.php

Reload Nagios

There are probably more secure ways of doing this but you get the idea and it should basically work.

nomolos 08-12-2011 03:37 AM

Hi Norbert,

That was fantastic. Thank you very much for your help. I'm sure many people would find this page helpful.

-SoLoMoN-

norbert74 08-13-2011 03:01 AM

Yes, the external scripts feature of nsclient++ is really great.
With it you can do just everything with a windows box from Nagios.
I like this very much too :-)


All times are GMT -5. The time now is 11:50 PM.