Automating Nagios Restart

Support requests, bug reports, questions etc.
Locked
JoePrecious

Automating Nagios Restart

Post by JoePrecious » Thu Sep 10, 2009 21:56

Hi

I've got nconf installed and running and really like it. However, the lack of ability to restart Nagios with the newly generated config is a bit of of a downside.

I know I can run the deploy_local.sh script, and even use crontab to run it regularly, but it would be really useful to be able to do this from the web interface, especially while doing initial setup and testing. I've have a look at the deploy option in nconf.php but I think I'm correct in the fact that this is for automatically deploying the config to other Nagios servers.

Has anyone come up with a simple solution to restar Nagios with the new config once its been generated? This would be a really useful function and make nconf a very powerful tool.

Thanks in advance.

Joe

melber
beginner
beginner
Posts: 11
Joined: Tue Jun 30, 2009 16:22

Re: Automating Nagios Restart

Post by melber » Fri Sep 11, 2009 08:04

hi,

actually you just need a little shell script to pipe the correct command in the commmand file. if nagios is configured right apache should be able to write to the command file.

script:

Code: Select all

#!/bin/bash
# This is a sample shell script showing how you can submit the RESTART_PROGRAM command
# to Nagios.  Adjust variables to fit your environment as necessary.

now=`date +%s`
commandfile='/usr/local/nagios/var/rw/nagios.cmd'

/bin/printf "[%lu] RESTART_PROGRAM\n" $now > $commandfile
if you want to restart a remote nagios is gets more difficult you would either need a inetd xinetd listener that takes the command and pipes it to the command file (dangerouse to do) or you give a user ssh access with a key and configure sudo to allow that user to restart nagios or execute the above script on the remote host.

JoePrecious

Re: Automating Nagios Restart

Post by JoePrecious » Fri Sep 11, 2009 08:11

Thanks for the response.

I was just thinking about this more and realised that the deploy_local.sh script actually untar's the new config and puts it in the new location, then restarts Nagios so the restart is just one part. I guess I'm looking for something that replicates both these process from the web interface.

Joe

melber
beginner
beginner
Posts: 11
Joined: Tue Jun 30, 2009 16:22

Re: Automating Nagios Restart

Post by melber » Fri Sep 11, 2009 08:33

okay a little hack to get this in the webinterface:

create the file include/ajax/exec_reload_nagios.php with this content:

Code: Select all

<?php
    require_once 'config/main.php';
//    require_once 'include/head.php';
    $status = "OK";

    // check if "temp" dir is writable
    if(!is_writable(NCONFDIR."/temp/")){
        echo "<br><div id=attention>Could not write to 'temp' folder. Cannot generate config.</div>";
        $status = "error";
        exit;
    }


    // check if generate_config script is executable
    if(!is_executable(NCONFDIR."ADD-ONS/deploy_local.sh")){
        echo "<br><div id=attention>Could not execute generate_config script. <br>The file '".NCONFDIR."ADD-ONS/deploy_local.sh' is not executable.</div>";
        $status = "error";
        exit;
    }


    // check if existing "output/NagiosConfig.tgz" is writable
    if(file_exists(NCONFDIR."/output/NagiosConfig.tgz" and !is_writable(NCONFDIR."/output/NagiosConfig.tgz"))){
        echo "<br><div id=attention>Cannot rename ".NCONFDIR."/output/NagiosConfig.tgz. Access denied.</div>";
        $status = "error";
        exit;
    }

    // check if static config folder(s) are readable
    foreach ($STATIC_CONFIG as $static_folder){
        if(!is_readable($static_folder)){
            echo "<br><div id=attention>Could not access static config folder '".$static_folder."'.";
            echo "<br>Check your \$STATIC_CONFIG array in 'config/nconf.php'.</div>";
            $status = "error";
            exit;
        }
    }


    // Log to history
    history_add("general", "config", "reloaded");
?>

<table border=0>
    <tr><td>
        <b>Reloading Nagios:</b><pre><?php system(NCONFDIR."ADD-ONS/deploy_local.sh") ?></pre><br>


    </td></tr>
</table>



<script type="text/javascript">



</script>

then add this line to include/menu/menu_user.php :

Code: Select all

         # Generate Nagios config link
         array_push($user_menu_end, array("nav_links" => "Generate Nagios config::generate_config.php", "friendly_name" => "", "grouping" => ""));
         array_push($user_menu_end, array("nav_links" => "Deploy Config::reload_nagios.php", "friendly_name" => "", "grouping" => ""));
the generate config line should be allready there.

create the file reload_nagios.php in your nconf root directory with the content:

Code: Select all

<?php
    require_once 'config/main.php';
    require_once 'include/head.php';
?>

<table><tr><td height=20 colspan=4></tr>
       <tr><td width=15></td>
           <td><img src="img/working.gif"></td>
           <td width=15></td>
           <td><br><h2>Reloading Nagios. <br>Please stand by...</h2></td>
</tr></table>


<?php

# Load exec script with AJAX, so content will change when script is finished
echo js_prepare("ajax_loadContent('maincontent','call_ajax.php?ajax_file=exec_reload_nagios.php');");


mysql_close($dbh);
require_once 'include/foot.php';

?>
and finaly you still need to set the permissions to execute all this in include/access_rules.php you need to add:

Code: Select all

    array_push($page_access_sub, "reload_nagios.php");
take either the user rules or the admin rule but in my opinion no one else than an admin should restart nagios.


hope this is what you where thinking about.

User avatar
agargiulo
NConf developer
NConf developer
Posts: 725
Joined: Fri Mar 06, 2009 17:50
Location: Zurich, Switzerland
Contact:

Re: Automating Nagios Restart

Post by agargiulo » Fri Sep 11, 2009 10:16

We solved this by adding a cronjob on each Nagios server that runs every minute and looks for Nagios config files newer than a minute. If it finds new files, it restarts the daemon. That way you don't have to worry about executing something remotely, or about apache not having "root" rights...

melber
beginner
beginner
Posts: 11
Joined: Tue Jun 30, 2009 16:22

Re: Automating Nagios Restart

Post by melber » Fri Sep 11, 2009 10:47

agargiulo wrote:We solved this by adding a cronjob on each Nagios server that runs every minute and looks for Nagios config files newer than a minute. If it finds new files, it restarts the daemon. That way you don't have to worry about executing something remotely, or about apache not having "root" rights...
yes it can be done like this but my intention when i wrote those things are that if i create a new config and i change on just one server one service all files will get updated and in conclusion all servers will do a reload or restart. I would never give apache root rights too dangerouse. but i would give a script sudo access to a command running it as root.

I don't know maybe i am wrong with my thoughts but if not nescessary i wouldn't want to restart nagios if i can't say 100% that really something has changed. If you read through the script I posted here somewhere you should see that I am usually not just going with whats newest i go with do the md5sums match. if they don't match upload it if they match don't do anything. out of my experience this is the best way. actually i am at the moment thinking of writing for our systems a network listener that takes an encrypted hash from one server and if it is right restarts that particulary server.

In the end it is preference of how it is done and as usual in unix there are a thousand ways to get it done.

JoePrecious

Re: Automating Nagios Restart

Post by JoePrecious » Fri Sep 11, 2009 15:51

That's exactly what I was looking for, but unfortunately having a couple of problems.

I've got the script working (I had to add a couple of forward slashes before the ADD-ONS in the first script) and it appears to run fine. It deploys the new config from the archive and says its restarting Nagios, but it doesn't actually restart. I've checked the permissions on the Nagios binary and the init script and they appear to be correct.

I can't see any errors in either the Apache logs or syslog so I'm a bit confused as to why Nagios is not restarting.

Any ideas?

melber
beginner
beginner
Posts: 11
Joined: Tue Jun 30, 2009 16:22

Re: Automating Nagios Restart

Post by melber » Fri Sep 11, 2009 19:07

check if the commandfile is the same as in your nagios.cfg and that nconf is running on the same host.

Locked