You are here

Downtime CLI script

1 post / 0 new
gpburdell93's picture
Downtime CLI script

I don't know how many other people have been frustrated by the unclear documentation on setting downtimes via the CLI, but in case anyone is suffering silently, here's my bash script which sets and cancels downtimes from the host level.

The first frustration is with the restapi documentation, which states that one of the attributes required for creating a downtime is duration, and that either duration or end_time is required.  Please note:  there is no duration attribute!  There is also no end_time attribute!  There is only endtime, and that endtime can either be a specific time or a duration.

The second frustration is with the natural language processing.  It's based on, but not all of the natural language phrases appear to be supported in Opsview.  I've noted a few successful examples (for English) in the script below.  Using epoch times works just fine.

The restapi was one of the big selling points for Opsview with my company.  I hope this will help speed other Opsview users along.

# Set a Opsview downtime for current host (default is for 2 hours)
# Wget, used for portability (Solaris) does not support HTTP DELETE,
# so cancelling a downtime is possible only on RHEL and zLinux
# Tested on Solaris 10, RHEL5, RHEL6, zLinux
# Chris Riggins
# 20150526
if [ "$1" == '-h' ]; then
  echo "USAGE: [-h] [-c]"
  echo "   -h:  prints this help message"
  echo "   -c:  cancels all downtimes for this host (NOT SUPPORTED on Solaris)"
  echo "   With no option, will require username, password, a comment, and a duration"
  echo "   Successful run returns SUCCESS confirmation to screen"
  exit 0
echo -n "Username: "
read user
echo -n "Password: "
read -s passwd
# Get authentication token from Opsview
token=$(wget --no-check-certificate --header="Content-type: application/json" --post-data '{"username":"'$user'","password":"'$passwd'"}' -O - -q ${opsurl}/login | cut -d: -f2 | sed -e 's/}//' -e 's/\"//g')
if [ "$1" == '-c' ]; then
# Cancel all downtimes for current host
  curl -s -o /dev/null --insecure --header "Content-type: application/json" --header "X-Opsview-Username: $user" --header "X-Opsview-token: $token" -X "DELETE" "${opsurl}/downtime/?host=$host" && echo "SUCCESS:  Cancelled all downtimes for $host"
if [ -z "$1" ]; then
  echo -n "Downtime comment: "
  read comment
  if [ -z "$comment" ]; then
    echo "Comment is required!"
    exit 1
  echo "Duration (eg. for 10 mins, +3h, 6pm today, next Tuesday at 5pm)"
  echo -n "[default = for 2 hours]: "
  read duration
  if [ -z "$duration" ]; then
    duration="for 2 hours"
  encduration=$(echo $duration | sed -e 's/+/%2B/g')
# Set downtime for current host
  wget --no-check-certificate --header="X-Opsview-Username: $user" --header="X-Opsview-token: $token"  -O /dev/null -q --post-data "" ${opsurl}/downtime/?"host="$host"&starttime=now&actual_start_time=now&endtime=""${encduration}""&author="$user"&comment=""${comment}" && echo "SUCCESS:  Set downtime: ""${duration}"" for $host"
exit 0