Rocket.Chat notification help

Hello again,

I want to send info notification to a rocket.chat channel.

I’ve followed the official guide, but I don’t get it to work.
I use the icinga2 Director module to create the commands, one thing is diffrent in my configuration - I don’t know how to create the env = { ... variable as seen in github.

My command configuration:

zones.d/director-global/commands.conf

zones.d/director-global/commands.conf
object NotificationCommand "rocketchat" {
    import "plugin-notification-command"
    command = [ "/etc/icinga2/scripts/notify_rocketchat" ]
    arguments += {
        "-host.action_url" = "$rocketchat_host_action_url$"
        "-host.display_name" = "$host.display_name$"
        "-host.name" = "$host.name$"
        "-host.output" = "$host.output$"
        "-host.state" = "$host.state$"
        "-icinga.timet" = "$icinga.timet$"
        "-service.action_url" = "$rocketchat_service_action_url$"
        "-service.display_name" = "$service.display_name$"
        "-service.name" = "$service.name$"
        "-service.output" = "$service.output$"
        "-service.state" = "$service.state$"
    }
    vars.rocketchat_host_action_url = "$host.action_url$"
    vars.rocketchat_service_action_url = "$service.action_url$"
}

My notification template looks like this:

zones.d/s060w44/notification_templates.conf

template Notification "rocketchat-notifiy-template" {
    command = "rocketchat"
    states = [ Critical, OK ]
    types = [ Acknowledgement, Problem, Recovery ]
    users = [ "rocket.chat" ]
}

… and both notification commands look like this:

zones.d/s060w44/notification_apply.conf

apply Notification "host rocketchat" to Host {
    import "rocketchat-notifiy-template"

    assign where match("*", host.name)
    states = [ Down, Up ]
    types = [ Acknowledgement, Custom, Problem, Recovery ]
    users = [ "rocket.chat" ]
    vars.rocketchat_host_action_url = "https://URL/hooks/6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    vars.rocketchat_service_action_url = "https://URL/hooks/6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

zones.d/s060w44/notification_apply.conf

zones.d/s060w44/notification_apply.conf
apply Notification "service rocketchat" to Service {
    import "rocketchat-notifiy-template"

    assign where match("*", host.name)
    states = [ Critical, OK ]
    types = [ Acknowledgement, Problem, Recovery ]
    users = [ "rocket.chat" ]
    vars.rocketchat_host_action_url = "https://URL/hooks/6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    vars.rocketchat_service_action_url = "https://URL/hooks/6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

}

Thanks for any help!

change the command in commands.conf to:
/usr/bin/env ROCKETCHAT_WEBHOOK_URL="https://test/hook" /etc/icinga2/scripts/notify_rocketchat

I tested it out with:
/usr/bin/env ROCKETCHAT_WEBHOOK_URL="https://test/hook" printenv

Hello, how/where do I do this in icinga2 director?

Thanks

first picture:

replace

/etc/icinga2/scripts/notify_rocketchat

with:

/usr/bin/env ROCKETCHAT_WEBHOOK_URL="https://test/hook" /etc/icinga2/scripts/notify_rocketchat

Hello, I’ve past the string (/usr/bin/env ROCKETCHAT_WEBHOOK_URL=“https://url” /etc/icinga2/scripts/notify_rocketchat) in the command field.

zones.d/director-global/commands.conf
object NotificationCommand "rocketchat" {
    import "plugin-notification-command"
    command = [
        "/usr/bin/env",
        "ROCKETCHAT_WEBHOOK_URL=\"https://url\"",
        "/etc/icinga2/scripts/notify_rocketchat"
    ]
    arguments += {
        "-host.action_url" = "$rocketchat_host_action_url$"
        "-host.display_name" = "$host.display_name$"
        "-host.name" = "$host.name$"
        "-host.output" = "$host.output$"
        "-host.state" = "$host.state$"
        "-icinga.timet" = "$icinga.timet$"
        "-service.action_url" = "$rocketchat_service_action_url$"
        "-service.display_name" = "$service.display_name$"
        "-service.name" = "$service.name$"
        "-service.output" = "$service.output$"
        "-service.state" = "$service.state$"
    }
    vars.rocketchat_host_action_url = "$host.action_url$"
    vars.rocketchat_service_action_url = "$service.action_url$"
}

I don’t think the director acknowledge that /usr/bin/env ROCKETCHAT_WEBHOOK_URL= is 1 command because it sets a comma. Or did I do something wrong?

Thanks

looks fine to me,

my test command renderd to

object CheckCommand "envtest" {
    import "plugin-check-command"
    command = [
        "/usr/bin/env",
        "ROCKETCHAT_WEBHOOK_URL=\"https://test/hook\"",
        "printenv"
    ]
}

and produced the following output:

PIDFILE=/run/icinga2/icinga2.pid
PWD=/
LANG=en_US.UTF-8
INVOCATION_ID=148ff0e1959d42f281ddf932ead25000
SHLVL=0
ICINGA2_ERROR_LOG=/var/log/icinga2/error.log
JOURNAL_STREAM=9:34313
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
LC_NUMERIC=C
ROCKETCHAT_WEBHOOK_URL="https://test/hook"

Okay, right now I get no messages, how can I test run it like you did?

try to run the go command manually (from shell) first, so we can exclude any configuration issues on the rocketchat side

Did you mean this?

root@Server:/etc/icinga2/scripts# /etc/icinga2/scripts/notify_rocketchat
$ROCKETCHAT_WEBHOOK_URL missing
root@Server:/etc/icinga2/scripts#

yes but your command is incomplete:

  1. read the documentation on notify_rocketchat
  2. set the environmentvar of the url via export
  3. prepare the shell command
  4. run it and see if that works and connects to your rocketchat instance

Hello, I think I do someting wrong with setting a variable as shown in the documentation.

root@s060w44:~# /etc/icinga2/scripts/notify_rocketchat env={"ROCKETCHAT_WEBHOOK_URL" = "https://URL"}
$ROCKETCHAT_WEBHOOK_URL missing
root@s060w44:~# /etc/icinga2/scripts/notify_rocketchat vars.rocketchat_webhook_url="https://URL"
$ROCKETCHAT_WEBHOOK_URL missing
root@s060w44:~#

But I can run a curl command to the rocket.chat webhook, so connection should not be a problem.

please look up who to set an environment variable in bash.

ROCKETCHAT_WEBHOOK_URL is an environment variable

https://www.google.com/search?q=set+environment+var+in+bash&oq=set+environment+var+in+bash

1 Like

Hello, communication works:

/etc/icinga2/scripts/notify_rocketchat -host.name hostname -host.state down -host.name hostname -service.name ping -service.state up

Result:
image

well thats great,
please try if that works and replace the webhook url:

/usr/bin/env ROCKETCHAT_WEBHOOK_URL="https://test/hook" /etc/icinga2/scripts/notify_rocketchat -host.name hostname -host.state down -host.name hostname -service.name ping -service.state up

Yes, this works aswell!

if you look up a service or a host in icingaweb, what do you see under the section Notifications?

image

looks great so the service sends notifications

look into the icigna2.log and search for notification

Hello, this is the output.

[2022-07-28 10:59:37 +0000] debug/HttpUtility: Request body: '{"author":"Username","comment":"test","force":false,"host":"test"}'
....
[2022-07-28 10:59:37 +0000] information/Notification: Completed sending 'Custom' notification 'test!host rocketchat' for checkable 'test' and user 'rocket.chat' using command 'rocketchat_alt'.
[2022-07-28 10:59:37 +0000] notice/Process: PID 1242560 ('/usr/bin/env' 'ROCKETCHAT_WEBHOOK_URL="https://URL/hooks/"' '/etc/icinga2/scripts/notify_rocketchat' '-host.action_url' 'https://URL/hooks/' '-host.display_name' 'test' '-host.name' 'test' '-host.output' 'PING CRITICAL - Packet loss = 100%' '-host.state' 'DOWN' '-icinga.timet' '1659005977' '-service.action_url' 'https://URL/hooks/') terminated with exit code 2
[2022-07-28 10:59:37 +0000] warning/PluginNotificationTask: Notification command for object 'test' (PID: 1242560, arguments: '/usr/bin/env' 'ROCKETCHAT_WEBHOOK_URL="https://URL/hooks/"' '/etc/icinga2/scripts/notify_rocketchat' '-host.action_url' 'https://URL/hooks/' '-host.display_name' 'test' '-host.name' 'test' '-host.output' 'PING CRITICAL - Packet loss = 100%' '-host.state' 'DOWN' '-icinga.timet' '1659005977' '-service.action_url' 'https://URL/hooks/') terminated with exit code 2, output: -service.* is given, missing some of: -host.name, -service.name, -service.state

your host notification is not working because the service action url is set

remove from Notification "host rocketchat" to Host

vars.rocketchat_service_action_url = "https://URL/hooks/6xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"