Matt Shields <m...@shields.tv> writes:

> An API or send_ncsa might work. Do you have an example?

We have implemented only the downtime part because our machines are
always on. It is just to catch reboots. Ah, and it is written for
RHEL. 

There are two scripts. One for init and the other for /usr/local/bin.
This can be made a lot simpler...

 
Regards,
Wolfgang



----------- first file for /etc/init.d/icinga_downtime --------
#!/bin/bash
#
# chkconfig: 35 99 01
# description: Setzt downtime beim stoppen und loescht dise beim starten.
#

# Source function library.
 . /etc/init.d/functions

start() {
  /usr/local/bin/icinga_downtime -D
  touch /var/lock/subsys/icinga_downtime
 
}

stop() {
  CLIENT=$(hostname -f)
  if [ "x$CLIENT" != "x" ]; then
    /usr/local/bin/icinga_downtime -C reboot -A $CLIENT
  else
    /usr/local/bin/icinga_downtime -C reboot
  fi
  rm -rf /var/lock/subsys/icinga_downtime
}

case "$1" in

        start)
         start
        ;;

        stop)
         stop
        ;;

        *) echo $"Usage: $0 {start|stop}"
           exit 1
esac
exit 0




--------------- second file for /usr/local/bin --------------

#!/bin/bash

#icinga host
ICINGA_HOST="u1du103.lksdom21.lks.local"
#local host
LOCAL_HOST="$(hostname -f)"

#default downtime is 4 hours
DOWNTIME_LENGTH=14400

#debug
DEBUG=0

# Hilfe Ausgabe über das Script
function usage {
 echo "Usage: $SCRIPTNAME [-H Host] [-S Service] [-A Author] [-l Length] [-C 
Comment] [-D] [-h]" >&2
 exit $1
}

######################################################################
# Einlesen der Optionen
while getopts ':A:C:H:l:S:dDh' OPTION ; do
 case $OPTION in
 h) usage 0
 ;;
 A) AUTHOR="$OPTARG"
 ;;
 H) HOSTS+=("$OPTARG")
 ;;
 S) SERVICE="$OPTARG"
 ;;
 d|D) DEL=1
 ;;
 C) COMMENT="$OPTARG"
 ;;
 l) LENGTH="$OPTARG"
 ;;
 \?) echo "Unbekannte Option \"-$OPTARG\"." >&2
 usage 1
 ;;
 :) echo "Option \"-$OPTARG\" benötigt ein Argument." >&2
 usage 1
 ;;
 *) echo "Dies kann eigentlich gar nicht passiert sein..." >&2
 usage 1
 ;;
 esac
done

if [ ! $HOSTS ]; then
    HOSTS=$LOCAL_HOST
fi
if [ ! $COMMENT ]; then
    COMMENT=icinga_downtime_script
fi

for HOST in ${HOSTS[@]}; do
  epochTime=$(date +%s)
  COMMAND="[$epochTime]"
  if [ -n "$SERVICE" ];then
    COMMAND="$COMMAND SCHEDULE_SVC_DOWNTIME;$HOST;$SERVICE"
  else
    if [ -n "$DEL" ]; then
      COMMAND="$COMMAND DEL_DOWNTIME_BY_HOST_NAME;$HOST"
    else
      COMMAND="$COMMAND SCHEDULE_HOST_SVC_DOWNTIME;$HOST"
    fi
  fi

  let periode=$epochTime+300;
  if [ ! $LENGTH ]; then
        let LENGTH=$DOWNTIME_LENGTH;
  fi
  if [ -n "$AUTHOR" ]; then
    [[ $DEBUG -eq 1 ]] && echo "remote with author set"
    user=$AUTHOR
  elif [ -n "$SSH_CLIENT" ]; then
    [[ $DEBUG -eq 1 ]] && echo "remote, set $SSH_CLIENT as author"
    user=$(echo $SSH_CLIENT|awk '{print $1}')
  else
    [[ $DEBUG -eq 1 ]] && echo "local, set IP from first interface as author"
    user=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' -m1| grep -Eo 
'([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
  fi

  if [ -z "$DEL" ]; then
    COMMAND=$COMMAND";$epochTime;$periode;0;0;$LENGTH;$user;$COMMENT"
  fi

  if [ "$LOCAL_HOST" != "$ICINGA_HOST" ];then
    [[ $DEBUG -eq 1 ]] && echo "nsca"
    [[ $DEBUG -eq 1 ]] && echo "$COMMAND"
    echo -e $COMMAND | /usr/sbin/send_nsca -C
  else
    [[ $DEBUG -eq 1 ]] && echo "write to file"
    [[ $DEBUG -eq 1 ]] && echo "$COMMAND"
    echo "$COMMAND" > /usr/local/icinga/var/rw/icinga.cmd
  fi
done
_______________________________________________
icinga-users mailing list
icinga-users@lists.icinga.org
https://lists.icinga.org/mailman/listinfo/icinga-users

Reply via email to