Env variables are being taken in count?

I’m trying to use a telegram notification script I found here (GitHub - TeraIT-at/icinga-telegram-bot: Telegram Bot for Icinga2)
For the notification definition it uses env variables and it’s defined as follows:

object NotificationCommand "telegram-host-notification" {
  import "plugin-notification-command"

  command = [ SysconfDir + "/icinga2/scripts/telegram-host-notification.sh" ]

  env = {
    NOTIFICATIONTYPE = "$notification.type$"
    HOSTNAME = "$host.name$"
    HOSTALIAS = "$host.display_name$"
    HOSTADDRESS = "$address$"
    HOSTSTATE = "$host.state$"
    LONGDATETIME = "$icinga.long_date_time$"
    HOSTOUTPUT = "$host.output$"
    NOTIFICATIONAUTHORNAME = "$notification.author$"
    NOTIFICATIONCOMMENT = "$notification.comment$"
    HOSTDISPLAYNAME = "$host.display_name$"
    TELEGRAM_BOT_TOKEN = "here_i_put_my_token"
    TELEGRAM_CHAT_ID = "$user.vars.telegram_chat_id$"

    // optional
    ICINGAWEB2_URL = "https://myicingaurl/icingaweb2"
  }
}

When icinga calls the script it does nothing (and I don’t know how to debug). But I’m guessing it’s because the env variables are not being set, because when on director I open the definition it’s just like this:

object NotificationCommand "telegram-host-notification" {
    import "plugin-notification-command"
    command = [ "/etc/icinga2/scripts/telegram-host-notification.sh" ]
    timeout = 1m
}

How do I enable icinga to use env variables? How to test if those are being set?

Thanks in advance

  • Version used (icinga2 --version)
    version: r2.13.6-1

  • Operating System and version
    Platform: Debian GNU/Linux
    Platform version: 11 (bullseye)
    Kernel: Linux
    Kernel version: 5.10.0-19-amd64
    Architecture: x86_64

  • Enabled features (icinga2 feature list)
    Disabled features: command compatlog debuglog elasticsearch gelf graphite influxdb influxdb2 livestatus opentsdb perfdata statusdata syslog
    Enabled features: api checker icingadb mainlog notification

Do you want to manage this in the classical DSL or in the Director?
What is the active configuration? You can check with: $ icinga2 object list

Hello Dominik

Want to use Director (I’m so new to icinga that I guess DSL would just complicate things for me).
Thanks for the $ icinga2 object list command.
When running it, the check-command does contain the env variables:

Object 'telegram-service-notification' of type 'NotificationCommand':
  % declared in '/etc/icinga2/conf.d/telegram_commands.conf', lines 26:1-26:58
  * __name = "telegram-service-notification"
  * arguments = null
  * command = [ "/etc/icinga2/scripts/telegram-service-notification.sh" ]
    % = modified in '/etc/icinga2/conf.d/telegram_commands.conf', lines 29:3-29:80
  * env
    % = modified in '/etc/icinga2/conf.d/telegram_commands.conf', lines 31:3-49:3
    * HOSTADDRESS = "$address$"
    * HOSTALIAS = "$host.display_name$"
    * HOSTDISPLAYNAME = "$host.display_name$"
    * HOSTNAME = "$host.name$"
    * ICINGAWEB2_URL = "https://myicingadomain.tld/icingaweb2"
    * LONGDATETIME = "$icinga.long_date_time$"
    * NOTIFICATIONAUTHORNAME = "$notification.author$"
    * NOTIFICATIONCOMMENT = "$notification.comment$"
    * NOTIFICATIONTYPE = "$notification.type$"
    * SERVICEDESC = "$service.name$"
    * SERVICEDISPLAYNAME = "$service.display_name$"
    * SERVICEOUTPUT = "$service.output$"
    * SERVICESTATE = "$service.state$"
    * TELEGRAM_BOT_TOKEN = "token_goes_here"
    * TELEGRAM_CHAT_ID = "$user.vars.telegram_chat_id$"
  * execute
    % = modified in 'methods-itl.conf', lines 23:3-23:30
    % = modified in 'methods-itl.conf', lines 23:3-23:30
    * arguments = [ "notification", "user", "cr", "itype", "author", "comment", "resolvedMacros", "useResolvedMacros" ]
    * deprecated = false
    * name = "Internal#PluginNotification"
    * side_effect_free = false
    * type = "Function"
  * name = "telegram-service-notification"
  * package = "_etc"
  * source_location
    * first_column = 1
    * first_line = 26
    * last_column = 58
    * last_line = 26
    * path = "/etc/icinga2/conf.d/telegram_commands.conf"
  * templates = [ "telegram-service-notification", "plugin-notification-command", "plugin-notification-command" ]
    % = modified in '/etc/icinga2/conf.d/telegram_commands.conf', lines 26:1-26:58
    % = modified in 'methods-itl.conf', lines 22:2-22:122
    % = modified in 'methods-itl.conf', lines 22:2-22:122
  * timeout = 60
  * type = "NotificationCommand"
  * vars = null
  * zone = ""

Is there a way I can debug what happens when Icinga runs the .sh script?

Sure, have a look at Troubleshooting - Icinga 2 - be careful to restart icinga every time you enable and disable debug log or it will not work also don’t forget to disable it or your disk can fill up quickly!
Maybe temporarily replace telegram-service-notification.sh with:

/bin/bash
touch /tmp/debug_notification.log # create log
chmod 600 /tmp/debug_notification.log # secure it
env > /tmp/debug_notification.log # get the env vars
echo $@ >> /temp/debug_notification.log # get all attributes

to dump all information that was given to it.

1 Like

Thanks Dominik!

Got it working after some debugging :slight_smile:

1 Like