Home Directory Addons Scheduled Downtime Schedule Downtime via cron

Search Exchange

Search All Sites

Nagios Live Webinars

Let our experts show you how Nagios can help your organization.

Contact Us

Phone: 1-888-NAGIOS-1
Email: sales@nagios.com

Login

Remember Me

Directory Tree

Schedule Downtime via cron

Rating
5 votes
Favoured:
0
Hits
120862
Files:
FileDescription
schedule_host_downtime.txtHost Script
schedule_svc_downtime.txtService Script
Nagios CSP

Meet The New Nagios Core Services Platform

Built on over 25 years of monitoring experience, the Nagios Core Services Platform provides insightful monitoring dashboards, time-saving monitoring wizards, and unmatched ease of use. Use it for free indefinitely.

Monitoring Made Magically Better

  • Nagios Core on Overdrive
  • Powerful Monitoring Dashboards
  • Time-Saving Configuration Wizards
  • Open Source Powered Monitoring On Steroids
  • And So Much More!
A couple of scripts that could be run from cron to schedule downtime automatically.
Reviews (2)
First, thanks for both scripts, helped out a lot!

One thing, I couldn't schedule host downtime using your script and found out that it has to to with a missing trigger_id in the commandline call... at least for 3.4.1

$trigger_id=0
cmdline="[$datetime] SCHEDULE_HOST_DOWNTIME;$hostname;$start_time;$end_time;$fixed;$trigger_id;$duration;$comment_author;$comment_data"
this fix will accept a $9 parameter for all_services or for all_hosts, so you need to run only one script from crontab

#!/bin/bash

#
# Write a command to the Nagios command file to cause
# it to schedule service downtime.
#
# Author: Sam Tilders sam@jovianprojects.com.au
# Bugfix and adding syntax for 3.2.x nagios version
# Frank Gruenwald f.gruenwald@spsg.de
#
# 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 or service desc before passing them to
# the command pipe. Nagios seems to ignore the command if it doesn't match.
# Write a command to the Nagios command file to cause
# it to schedule host downtime

# Example:
# A cron entry to schedule downtime for the nntp service every day at 0400 while
# other cron jobs (locate database update) slow the machine down.
# 30 0 * * * schedule_svc_downtime news NNTP "`date --iso-8601` 04:00:00" "`date --iso-8601` 05:00:00" 1 3600 auto "while cron does things"
# 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 or "
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 " 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 9 ]; then
echo "$0: Too few parameters"
usage
exit 1
fi
if [ $# -gt 9 ]; then
echo "$0: Too many parameters"
usage
exit 1
fi
hostname=$1
servicedesc=$2
raw_start=$3
raw_end=$4
fixed=$5
duration=$6
comment_author=$7
# command is comment with ; replaced for space
comment_data=${8/;/ }



echocmd="/bin/echo"

CommandFile="/opt/nagios/var/rw/nagios.cmd"

# get the current date/time in seconds since UNIX epoch
datetimes=`date +%s`
datetime="[$datetimes]"


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

# create the command line to add to the command file
#cmdline="$datetime SCHEDULE_SVC_DOWNTIME;$hostname;$servicedesc;$start_time;$end_time;$fixed;$duration;7200;$comment_author;$comment_data"
case "$9" in
all_services)
cmdline="$datetime SCHEDULE_HOST_SVC_DOWNTIME;$hostname;$start_time;$end_time;$fixed;$duration;7200;$comment_author;$comment_data"
;;
all_hosts)
cmdline="$datetime SCHEDULE_HOST_DOWNTIME;$hostname;$start_time;$end_time;$fixed;$duration;7200;$comment_author;$comment_data"
;;
esac
# append the command to the end of the command file

echo $cmdline >> $CommandFile