Friday, September 14, 2012

Schedule Nagios Downtime From Powershell

After finally getting some time to setup Nagios at my work, we have some real availability monitoring! However in a small and somewhat neglected environment alerting can be a double edged sword. While we have learned some things, we are having intermittent issues with out mail server services. We also are getting alert bombed due to some automated reboots on our network. Now there are several options for solving this that exist in the Nagios community (I.E. this). The simple and workable solution we found was to tie the downtime scheduling right into the Powershell script. Here is the script in case it can help anyone else out!

[Reflection.Assembly]::LoadWithPartialName("System.Web") > $null
$url = "http://yourdomain/nagios/cgi-bin/cmd.cgi";
$ajax = new-object -com msxml2.xmlhttp;
$ajax.open("POST",$url,$false, "httpAuthUsername",
    "httpAuthPassword");
$ajax.setRequestHeader("Content-Type",
    "application/x-www-form-urlencoded");
$startTime = "{0:MM-dd-yyyy} 12:30:00" -f (Get-Date);
$endTime = "{0:MM-dd-yyyy} 13:30:00" -f (Get-Date);
$postStrMain = "cmd_typ=55&cmd_mod=2&"+
    "com_author=httpAuthUsername&"+ 
    "com_data=Automated Downtime Scheduled&"+
    "trigger=0&"+ 
    "start_time="+
    [System.Web.HttpUtility]::UrlEncode($startTime)+"&"+
    "end_time="+
    [System.Web.HttpUtility]::UrlEncode($endTime) +"&"+
    "fixed=1&hours=0&minutes=0&childoptions=1";
$hostName = "yourHostName"
$str = "host="+[System.Web.HttpUtility]::UrlEncode($hostName)+
    "&"+$postStrMain;
$ajax.setRequestHeader("Content-Length", $str.length);
$ajax.send($str);

The important parts to replace with your specific info are:

  • yourdomain
  • httpAuthUsername
  • httpAuthPassword
  • yourHostName

3 comments:

  1. this is really great stuff, I'm wondering how to receive if command was executed successfully? and which other commands exist and what parameters they have? Thanks a lot!

    ReplyDelete
  2. this is really great stuff, I'm wondering how to receive if command was executed successfully? and which other commands exist and what parameters they have? Thanks a lot!

    ReplyDelete
  3. Hi This is great, is there a way to cancel downtime from powershell as well?

    ReplyDelete