<commit_before>#!/bin/bash
## /etc/icinga2/scripts/host-by-mail.sh / 20160616
## <NAME> <<EMAIL>>
## Last updated 20170310
## Tested 2.6.2-1~ppa1~xenial1 // https://www.unixe.de/icinga2-director-notifications/

## Probably you'll have to change at least two of them to fit your needs
HOSTNAME="`hostname`"
MAILFROM="Icinga 2 Monitoring <<EMAIL>>"
MONITORING_URL="https://www.example.com/icingaweb2"

function Usage() {
cat << EOF

host-by-mail notification script for Icinga 2 by spillerm <<EMAIL>>
- Create a command (type: Notification Plugin Command) using this script as 'Command'
  => Take care of the command arguments!
- Then create notification templates using this command
- And now create notification objects and assign them as you want to
Have fun!

EOF
}

while getopts a:b:c:d:hl:o:r:s:t: opt
do
  case "$opt" in
    a) HOSTADDRESS=$OPTARG ;;
    b) NAUTHOR=$OPTARG ;;
    c) NCOMMENT=$OPTARG ;;
    d) DATE=$OPTARG ;;
    h) Usage
       exit 1 ;;
    l) HOSTDN=$OPTARG ;;
    o) HOSTOUTPUT=$OPTARG ;;
    r) RECIPIENT=$OPTARG ;;
    s) HOSTSTATE=$OPTARG ;;
    t) NTYPE=$OPTARG ;;
    ?) echo "ERROR: invalid option" >&2
       exit 1 ;;
  esac
done

shift $((OPTIND - 1))

NOTIFICATION_MESSAGE=`cat << EOF
***** Icinga 2 Host Monitoring on $HOSTNAME *****

==> $HOSTDN is $HOSTSTATE! <==

When?    $DATE
Host?    $HOSTDN ($HOSTADDRESS)
Info?    $HOSTOUTPUT

Comment by $NAUTHOR: $NCOMMENT

Get live status:
$MONITORING_URL/monitoring/host/show?host=$HOSTDN

EOF
`

SUBJECT="[$NTYPE] Host $HOSTDN is $HOSTSTATE!"

/usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" \
| mail -a "From: $MAILFROM" -s "$SUBJECT" $RECIPIENT


##------------------------------------------------------------
## object NotificationCommand "Host Alarm" {
##     import "plugin-notification-command"
##     command = [ "/etc/icinga2/scripts/host-by-mail.sh" ]
##     arguments += {
##         "-a" = "$address$"
##         "-b" = "$notification.author$"
##         "-c" = "$notification.comment$"
##         "-d" = "$icinga.short_date_time$"
##         "-l" = "$host.name$"
##         "-o" = "$host.output$"
##         "-r" = "$user.email$"
##         "-s" = "$host.state$"
##         "-t" = "$notification.type$"
##     }
## }
## 
## template Notification "Generic Host Alarm" {
##   command = "Host Alarm"
##   interval = 0s
##   states = [ Down, Up ]
##   types = [ Recovery, Acknowledgement, Custom, Problem ]
## }
##
## apply Notification "Notify my team about host" to Host {
##   import "Generic Host Alarm"
## 
##   period = "always"
##   user_groups = [ "the_people" ]
##   interval = 4h
## 
##   assign where true
##   ignore where "External Hosts" in host.templates
## }<commit_msg>Backward compatibility and some enhancements.<commit_after>#!/bin/bash
## /etc/icinga2/scripts/host-by-mail.sh / 20160616
## <NAME> <<EMAIL>>
## Last updated 20170316
## Tested 2.6.2-1~ppa1~xenial1 // https://www.unixe.de/icinga2-director-notifications/

PROG="`basename $0`"
HOSTNAME="`hostname`"

function Usage() {
cat << EOF

host-by-mail notification script for Icinga 2 by spillerm <<EMAIL>>
=> See https://docs.icinga.com/icinga2/latest/doc/module/icinga2/chapter/monitoring-basics

The following are mandatory:
  -a HOSTADDRESS (\$adress$)
  -d DATE (\$icinga.short_date_time$)
  -l HOSTALIAS (\$host.name$)
  -o HOSTOUTPUT (\$host.output$)
  -r USEREMAIL (\$user.email$)
  -s HOSTSTATE (\$host.state$)
  -t NOTIFICATIONTYPE (\$notification.type$)

And these are optional:
  -b NOTIFICATIONAUTHORNAME (\$notification.author$)
  -c NOTIFICATIONCOMMENT (\$notification.comment$)
  -i HAS_ICINGAWEB2 (\$icingaweb2url$, Default: unset)
  -f FROM (\$notification_mailfrom$, Default: "Icinga 2 Monitoring <icinga@$HOSTNAME>")
  -v (\$notification_sendtosyslog$, Default: false)

EOF

exit 1;
}

while getopts a:b:c:d:f:hi:l:o:r:s:t:v: opt
do
  case "$opt" in
    a) HOSTADDRESS=$OPTARG ;;
    b) NOTIFICATIONAUTHORNAME=$OPTARG ;;
    c) NOTIFICATIONCOMMENT=$OPTARG ;;
    d) DATE=$OPTARG ;;
    f) MAILFROM=$OPTARG ;;
    h) Usage ;;
    i) HAS_ICINGAWEB2=$OPTARG ;;
    l) HOSTALIAS=$OPTARG ;;
    o) HOSTOUTPUT=$OPTARG ;;
    r) USEREMAIL=$OPTARG ;;
    s) HOSTSTATE=$OPTARG ;;
    t) NOTIFICATIONTYPE=$OPTARG ;;
    v) VERBOSE=$OPTARG ;;
   \?) echo "ERROR: Invalid option -$OPTARG" >&2
       Usage ;;
    :) echo "Missing option argument for -$OPTARG" >&2
       Usage ;;
    *) echo "Unimplemented option: -$OPTARG" >&2
       Usage ;;
  esac
done

shift $((OPTIND - 1))

## Default sender address
if [ ! -n "$MAILFROM" ] ; then
  MAILFROM="Icinga 2 Monitoring <<EMAIL>@$HOSTNAME>"
fi

NOTIFICATION_MESSAGE=`cat << EOF
***** Icinga 2 Host Monitoring on $HOSTNAME *****

==> $HOSTALIAS is $HOSTSTATE! <==

When?    $DATE
Host?    $HOSTALIAS ($HOSTADDRESS)
Info?    $HOSTOUTPUT
EOF
`

## Are there any comments? Put them into the message!
if [ -n "$NOTIFICATIONCOMMENT" ] ; then
  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE

Comment by $NOTIFICATIONAUTHORNAME:
  $NOTIFICATIONCOMMENT"
fi

## Are we using Icinga Web 2? Put the URL into the message!
if [ -n "$HAS_ICINGAWEB2" ] ; then
  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE

Get live status:
  $HAS_ICINGAWEB2/monitoring/host/show?host=$HOSTALIAS"
fi

## Build the message's subject
SUBJECT="[$NOTIFICATIONTYPE] Host $HOSTALIAS is $HOSTSTATE!"

## Are we verbose? Then put a message to syslog...
if [ "$VERBOSE" == "true" ] ; then
  logger "$PROG sends $SUBJECT => $USEREMAIL"
fi

## And finally, send the message using mail command
/usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" \
| mail -a "From: $MAILFROM" -s "$SUBJECT" $USEREMAIL 
{'fraction_non_alphanumeric': 0.17225910640588551, 'fraction_numerical': 0.013816615826305402, 'mean_word_length': 3.4879227053140096, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 27, 'https://': 4, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4987974', 'n_tokens_mistral': 2092, 'n_tokens_neox': 1985, 'n_words': 507}