Custom Service Var in Email Notification

Hello

I have a Single Service where we have created a custom variable - u_ResolverTeam and have populated it on specific Server

service.vars.u_ResolverQueue

But I do not know in the mail-service-notification.sh how do I push it under which OPT

while getopts 4:6:b:c:d:e:f:hi:l:n:o:r:s:t:u:v: opt
do
  case "$opt" in
    4) HOSTADDRESS=$OPTARG ;;
    6) HOSTADDRESS6=$OPTARG ;;
    b) NOTIFICATIONAUTHORNAME=$OPTARG ;;
    c) NOTIFICATIONCOMMENT=$OPTARG ;;
    d) LONGDATETIME=$OPTARG ;; # required
    e) SERVICENAME=$OPTARG ;; # required
    f) MAILFROM=$OPTARG ;;
    h) Usage ;;
    i) ICINGAWEB2URL=$OPTARG ;;
    l) HOSTNAME=$OPTARG ;; # required
    n) HOSTDISPLAYNAME=$OPTARG ;; # required
    o) SERVICEOUTPUT=$OPTARG ;; # required
    r) USEREMAIL=$OPTARG ;; # required
    s) SERVICESTATE=$OPTARG ;; # required
    t) NOTIFICATIONTYPE=$OPTARG ;; # required
    u) SERVICEDISPLAYNAME=$OPTARG ;; # required
    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

Can you please share a small example on how do I pass the my resolver queue in these OPTARGs - little confused are these 4:6:b:c … are hard coded somewhere? Where does it know to link -e = ($service.name$). The one above is more of help display right?
I want to write something like -w = ($service.vars.u_ResolverQueue$)

Hi,

the link between the OPTARG definition and for example the the $service.name$ is made in the NotificationCommand. You can find an example in the conf.d folder in the commands.conf file.

Best regards
Michael

Thank You for the guidance

I am little worried making this change as this will affect the production. But this is what I did

"-u" = {
  required = true
  value = "$notification_servicedisplayname$"
}
"-v" = "$notification_logtosyslog$"
**"-w" = "$notification_serviceresolverqueue$"**
  }

  vars += {
notification_address = "$address$"
notification_address6 = "$address6$"
notification_author = "$notification.author$"
notification_comment = "$notification.comment$"
notification_type = "$notification.type$"
notification_date = "$icinga.long_date_time$"
notification_hostname = "$host.name$"
notification_hostdisplayname = "$host.display_name$"
notification_servicename = "$service.name$"
notification_serviceoutput = "$service.output$"
notification_servicestate = "$service.state$"
notification_useremail = "$user.email$"
notification_servicedisplayname = "$service.display_name$"
**notification_serviceresolverqueue = "$service.vars.u_ResolverQueue$"**
  }
}

Now I just add w: in the email notification script and we are done? Is this OK?

Hello

Thank You - I was using Director. So i made the following change in the custom defination

object NotificationCommand "mail-service-notification-custom" {
    import "plugin-notification-command"
    command = [
        "/etc/icinga2/scripts/mail-service-notification-custom.sh"
    ]
    timeout = 1m
    arguments += {
        "-4" = "$notification_address$"
        "-6" = "$notification_address6$"
        "-b" = "$notification_author$"
        "-c" = "$notification_comment$"
        "-d" = {
            required = true
            value = "$notification_date$"
        }
        "-e" = {
            required = true
            value = "$notification_servicename$"
        }
        "-f" = {
            description = "Set from address. Requires GNU mailutils (Debian/Ubuntu) or mailx (RHEL/SUSE)"
            value = "$notification_from$"
        }
        "-i" = "$notification_icingaweb2url$"
        "-l" = {
            required = true
            value = "$notification_hostname$"
        }
        "-n" = {
            required = true
            value = "$notification_hostdisplayname$"
        }
        "-o" = {
            required = true
            value = "$notification_serviceoutput$"
        }
        "-r" = {
            required = true
            value = "$notification_useremail$"
        }
        "-s" = {
            required = true
            value = "$notification_servicestate$"
        }
        "-t" = {
            required = true
            value = "$notification_type$"
        }
        "-u" = {
            required = true
            value = "$notification_servicedisplayname$"
        }
        "-v" = "$notification_logtosyslog$"
        "-w" = "$notification_resolver$"
    }
    vars.notification_address = "$address$"
    vars.notification_address6 = "$address6$"
    vars.notification_author = "$notification.author$"
    vars.notification_comment = "$notification.comment$"
    vars.notification_date = "$icinga.long_date_time$"
    vars.notification_hostdisplayname = "$host.display_name$"
    vars.notification_hostname = "$host.name$"
    vars.notification_resolver = "$service.vars.u_ResolverQueue$"
    vars.notification_servicedisplayname = "$service.display_name$"
    vars.notification_servicename = "$service.name$"
    vars.notification_serviceoutput = "$service.output$"
    vars.notification_servicestate = "$service.state$"
    vars.notification_type = "$notification.type$"
    vars.notification_useremail = "$user.email$"

}

I have introduced a -w (for resolverQueue) and trying to map it to service.vars.u_ResolverQueue

then tried to use it in the script

while getopts 4:6:b:c:d:e:f:hi:l:n:o:r:s:t:u:v:w: opt
do
...
...
v) VERBOSE=$OPTARG ;;
w) RESOLVER=$OPTARG ;;

But somehow I am not getting the variable. Is service.vars.u_ResolverQueue the right thing to map ? I don’t see any issue

Hi,

in the script you have to use the variable RESOLVER to actually get it printed in the notification mail.

For example:

# vim /etc/icinga2/scripts/mail-service-notification.sh

[...]
## Build the notification message
NOTIFICATION_MESSAGE=`cat << EOF
***** Service Monitoring on $ICINGA2HOST *****

$SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!

Info:    $SERVICEOUTPUT

When:        $LONGDATETIME
Service:     $SERVICENAME
Host:        $HOSTNAME
Resolver:    $RESOLVER
EOF
`
[...]

This will add the resolver to the message body.

Best regards
Michael

Yes - did that

## Check whether Resolver Queue was specified
if [ -n "$RESOLVER" ] ; then
  NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
ResolverQueue: $RESOLVER"

but looks like it is blank even though the value is being overridden on specific server for the service.

I actually tried to print all the information when the service fires on specific server

echo "$@" >/tmp/test1-out

Found even the -w is not printing ideally -w as blank should have printed

-4 53.XXX.XX.XXX -6 -b -c -d 2020-11-30 20:23:50 -0800 -e XXXX-P_WS_HttpUrl -l XXXXXXXXX005.XXX.intra.net -n XXXXXXXXX005.XXX.intra.net -o HTTP WARNING: HTTP/1.1 404 Not Found - https://XXXXXXXXX005.XXX.intra.net:8443/Shop/index.jsp2 - 1244 bytes in 0.278 second response time -r radioactive@domain.com -s WARNING -t PROBLEM -u XXXX-P_WS_HttpUrl

As you see the output -4 -6 -b -c -d etc even some of the variables are blank - but for some reason -w is not shown. Means somehow it is not even there

service.vars.u_ResolverQueue is correct way to capture it?

EDIT
With support was troubleshooting the issue - found the following if the variable is populated in Service it works perfectly ok
What we are doing is we are overriding the service on the particular server - and we are not able to get the value

zones.d/DTNA_CORE_NETWORK/hosts.conf

object Host "XXXXXXXXX005.XXX.intra.net" {
    import "XXXX-tmplHost-AG-LZ"
    import "XXXX-tmplHost-HttpUrl"

    display_name = "XXXXXXXXX005.XXX.intra.net"
    address = "53.XXX.XX.XXX"
    groups = [ "XXXX_GroupHost_HttpUrl_CALL", "XXXX_Linux_All" ]
    vars["_override_servicevars"] += {
        "XXXX-P_WS_HttpUrl" = {
            http_port = "8443"
            http_ssl = "true"
            http_uri = "/Shop/index.jsp2"
            http_vhost = "XXXXXXXXX005.XXX.intra.net"
            u_ResolverTeam = "XXXX-ICINGA-TEAM"
        }
    }
    vars.u_Environment = "Prod"
    vars.u_OperatingSystem = "Linux"
}

I changed to

host.vars[_override_servicevars].u_ResolverQueue - but still not able to capture it when the service fires on the host

Question: How do we capture override vars for a service on a particular server?

EDIT
Never Mind this is correct way of capturing
service.vars.u_ResolverQueue