scheduled downtime through cron v. 1.2

Arno Lehmann al at its-lehmann.de
Wed Mar 16 10:57:36 CET 2005


Hi.

Andrew Judge wrote:

> 
> 
> Need to schedule downtime for a host and it is driving me nuts.  What is 
> the format for the downtime.log file?  Mine looks like so:
> 
> [1110962381] 
> HOST_DOWNTIME;2;lnxboxen;1110962368;1110969568;1;7200;theman;test downtime
> 
> so how do I get 1110962381, 1110962368;1110969568; ?  I assume it is 
> derived from date and concatenated?  Help much appreciated.

It is not. It's unix time - seconds since 1970. You find more 
information in nagios' manual - the section "External Commands" is a 
good starting point.

The following script might be helpful:
> #!/bin/sh
> 
> #
> # Write a command to the Nagios command file to cause
> # it to schedule host downtime.
> #
> # Author: Sam Tilders sam at jovianprojects.com.au
 > # Modified by Arno Lehmann al at its-lehmann 2005
> #
> # Based on the example event handler scripts and cmd.c/.cgi
> #
> # Requires:
> #  GNU Date (for date formatting options)
> #  Bash > 2.x (for inline variable regex substitution on the comment_data)
> 
> # Notes:
> # 1) In order for Nagios to process any commands that
> #    are written to the command file, you must enable
> #    the check_external_commands option in the main
> #    configuration file.
> 
> # Caveats:
> #  Using "date" to validate the command line date format doesn't always pick
> # up all mistakes in the date format. Sometimes "date" seems to invent something
> # that might have been what you intented.
> #  Makes no attempt to verify the hostname before passing it to
> # the command pipe. Nagios seems to ignore the command if it doesn't match.
> 
> # Example:
> # A cron entry to schedule downtime for the mailserver on the first
> # day of the month from 0400 - 0500:
> # 30 0 1 * * schedule_host_downtime mailserver "`date --iso-8601` 04:00:00" "`date --iso-8601` 05:00:00" 1 3600 auto "scheduled maintenance"
> # There are utilities such as "shellsupport" that will do date manipulation allowing shell scripts
> # to wrap around this command to schedule for days in advance instead of same date.
> 
> usage()
> {
>         echo "Usage: $0 <hostname> <start time> <end time> <fixed> <child> <duration> <user> <comment>"
>         echo "   Times must be in the form \"CCYY-MM-DD HH:mm:ss\" and will probably need to be quoted."
>         echo "   fixed is either 1 or 0 and indicates that the time period is fixed and duration should be ignored"
>         echo "   child should notifications for childs be turned off"
>         echo "   length of the downtime in seconds, only used if not fixed downtime"
>         echo "   user who is requesting the downtime"
>         echo "   comment to place on the downtime, probably will need to be quoted."
> }
> 
> if [ $# -lt 8 ]; then
>         echo "$0: Too few parameters"
>         usage
>         exit 1
> fi
> if [ $# -gt 8 ]; then
>         echo "$0: Too many parameters"
>         usage
>         exit 1
> fi
> 
> hostname=$1
> raw_start=$2
> raw_end=$3
> fixed=$4
> child=$5
> duration=$6
> comment_author=$7
> # command is comment with ; replaced for space
> comment_data=${8/;/ }
> 
> CommandFile="/usr/local/nagios/var/rw/nagios.cmd"
> 
> echocmd="ssh -l nagios elf "
> 
> # get the current date/time in seconds since UNIX epoch
> datetime=`date +%s`
> 
> 
> start_time=`date -d "$raw_start" +%s`
> if [ $? != 0 ]; then
>         echo "Bad format for start time."
>         usage
>         exit 1
> fi
> end_time=`date -d "$raw_end" +%s`
> if [ $? != 0 ]; then
>         echo "Bad format for end time."
>         usage
>         exit 1
> fi
> if [ $fixed != 1 -a $fixed != 0 ]; then
>         echo "Fixed must be 1 or 0"
>         usage
>         exit 1
> fi
> 
> if [ $child != 1 -a $child != 0 ]; then
>         echo "Child must be 1 or 0"
>         usage
>         exit 1
> fi
> 
> # create the command line to add to the command file
> cmdline="[$datetime] SCHEDULE_HOST_DOWNTIME;$hostname;$start_time;$end_time;$fixed;$child;$duration;$comment_author;$comment_data"
> 
> # append the command to the end of the command file
> $echocmd "echo \"$cmdline\" >>$CommandFile"

Note that I'm not sure about the correct order of $child and $fixed - 
this works for me, and I didn't look up any further information in the 
source.

This is called sched_downtime

And using it like this:
sched_downtime goblin "`date -d "now + 5 min" "+%Y-%m-%d %H:%M:%S"`" 
"`date "-d tomorrow 8:12"`" 1 0 0 bacula "goblin turns off until 
tomorrow morning..."
shutdown -h -t 120 +6 &

Gives the result you expect.

Of course: No warranties, GPL applies, etc. pp. The usual stuff.

Arno

> Best regards,
> 
> Andrew Judge
> 

-- 
IT-Service Lehmann                    al at its-lehmann.de
Arno Lehmann                  http://www.its-lehmann.de


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
Nagios-users mailing list
Nagios-users at 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





More information about the Users mailing list