From 61a26c778cf92779807ffb6f82eefcc25669f8cd Mon Sep 17 00:00:00 2001 From: Christian Elberfeld <elberfeld@web.de> Date: Tue, 16 May 2023 19:51:36 +0200 Subject: [PATCH] icinga matrix notification --- site.yml | 4 +- webserver/docker_icinga/templates/Dockerfile | 21 ++- .../etc/icinga/conf.d/commands2.conf | 166 +++++------------- .../etc/icinga/conf.d/notifications.conf | 36 ++-- .../etc/icinga/conf.d/templates.conf | 34 ++-- .../etc/icinga/conf.d/users_groups.conf | 15 ++ 6 files changed, 98 insertions(+), 178 deletions(-) diff --git a/site.yml b/site.yml index b05735b3..10363b65 100644 --- a/site.yml +++ b/site.yml @@ -189,9 +189,7 @@ basedir: /srv/icinga, domain: icinga.warpzone.ms, api_port: 5665, - mysql_port: 33306, - matrix_notification_domain: "matrix.warpzone.ms", - matrix_notification_room: "!iYefxbySFEfFQfUGEK:matrix.warpzone.ms" + mysql_port: 33306 } - { role: webserver/docker_hackmd, tags: hackmd, diff --git a/webserver/docker_icinga/templates/Dockerfile b/webserver/docker_icinga/templates/Dockerfile index 1c804d96..0480e561 100644 --- a/webserver/docker_icinga/templates/Dockerfile +++ b/webserver/docker_icinga/templates/Dockerfile @@ -13,9 +13,10 @@ RUN apt-get update \ libnet-dns-perl \ libnet-ip-perl \ perl \ - python3-requests \ python3 \ - python3-paho-mqtt \ + python3-pip \ + python3-requests \ + virtualenv \ && apt-get autoremove -y \ && apt-get clean \ && rm -rf /tmp/* /var/lib/apt/lists/* /var/cache/debconf/*-old @@ -49,8 +50,16 @@ RUN cd /opt/ \ && cd /opt/check_metric_value/ \ && git checkout b94d3c3e78497a05e3b4520d33421f37e4d77985 -# icinga2-matrix_notification - commit from 15.04.2012 +# icinga2-matrix-bot - commit from 10.06.2018 +# create a symlink to emulate virtualenv +# patch matrix-service-notification.sh RUN cd /opt/ \ - && git clone https://github.com/linuxmail/icinga2-matrix_notification \ - && cd /opt/icinga2-matrix_notification/ \ - && git checkout 99d2174a3b00e9a88648fe58bcd975368f69837d + && git clone https://github.com/symptog/icinga2-matrix-bot.git \ + && cd /opt/icinga2-matrix-bot/ \ + && git checkout 86d03ed8884950eb2b883a9a94ecf93460b8e0bb \ + && pip install -r requirements.txt \ + && mkdir /opt/icinga2-matrix-bot/.venv \ + && mkdir /opt/icinga2-matrix-bot/.venv/bin \ + && ln -s /usr/bin/python3 /opt/icinga2-matrix-bot/.venv/bin/python \ + && sed -i 's/matrix-host-state/matrix-service-state/' icinga2/matrix-service-notification.sh + \ No newline at end of file diff --git a/webserver/docker_icinga/templates/etc/icinga/conf.d/commands2.conf b/webserver/docker_icinga/templates/etc/icinga/conf.d/commands2.conf index dd72f3f4..d1c3cbe0 100644 --- a/webserver/docker_icinga/templates/etc/icinga/conf.d/commands2.conf +++ b/webserver/docker_icinga/templates/etc/icinga/conf.d/commands2.conf @@ -80,131 +80,49 @@ object CheckCommand "check_metric_value" { */ object NotificationCommand "matrix-host-notification" { - import "plugin-notification-command" - command = [ "/opt/icinga2-matrix_notification/scripts/matrix-host-notification.sh" ] - arguments += { - "-4" = "$notification_address$" - "-6" = "$notification_address6$" - "-b" = "$notification_author$" - "-c" = "$notification_comment$" - "-d" = { - required = true - value = "$notification_date$" - } - "-i" = "$notification_icingaweb2url$" - "-l" = { - required = true - value = "$notification_hostname$" - } - "-m" = { - required = true - value = "$notification_matrix_room_id$" - } - "-n" = { - required = true - value = "$notification_hostdisplayname$" - } - "-o" = { - required = true - value = "$notification_hostoutput$" - } - "-s" = { - required = true - value = "$notification_hoststate$" - } - "-t" = { - required = true - value = "$notification_type$" - } - "-x" = { - required = true - value = "$notification_matrix_server$" - } - "-y" = { - required = true - value = "$notification_matrix_token$" - } - } - 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_hostoutput = "$host.output$" - vars.notification_hoststate = "$host.state$" - vars.notification_type = "$notification.type$" + import "plugin-notification-command" + + command = [ "/opt/icinga2-matrix-bot/icinga2/matrix-host-notification.sh" ] + + env = { + "NOTIFICATIONTYPE" = "$notification.type$" + "HOSTALIAS" = "$host.display_name$", + "HOSTADDRESS" = "$address$", + "HOSTNAME" = "$host.name$", + "HOSTSTATE" = "$host.state$", + "LONGDATETIME" = "$icinga.long_date_time$", + "HOSTOUTPUT" = "$host.output$", + "NOTIFICATIONAUTHORNAME" = "$notification.author$", + "NOTIFICATIONCOMMENT" = "$notification.comment$", + "HOSTDISPLAYNAME" = "$host.display_name$", + "ICINGA_WEBURL" = "$notification_icingaweb2url$", + "MATRIX_SERVER" = "$user.vars.matrix.server$", + "MATRIX_TOKEN" = "$user.vars.matrix.token$", + "MATRIX_CHANNEL" = "$user.vars.matrix.channel$", + } } - object NotificationCommand "matrix-service-notification" { - import "plugin-notification-command" - command = [ "/opt/icinga2-matrix_notification/scripts/matrix-service-notification.sh" ] - arguments += { - "-4" = { - required = true - value = "$notification_address$" - } - "-6" = "$notification_address6$" - "-b" = "$notification_author$" - "-c" = "$notification_comment$" - "-d" = { - required = true - value = "$notification_date$" - } - "-e" = { - required = true - value = "$notification_servicename$" - } - "-i" = "$notification_icingaweb2url$" - "-l" = { - required = true - value = "$notification_hostname$" - } - "-m" = { - required = true - value = "$notification_matrix_room_id$" - } - "-n" = { - required = true - value = "$notification_hostdisplayname$" - } - "-o" = { - required = true - value = "$notification_serviceoutput$" - } - "-s" = { - required = true - value = "$notification_servicestate$" - } - "-t" = { - required = true - value = "$notification_type$" - } - "-u" = { - required = true - value = "$notification_servicedisplayname$" - } - "-x" = { - required = true - value = "$notification_matrix_server$" - } - "-y" = { - required = true - value = "$notification_matrix_token$" - } - } - 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_servicedisplayname = "$service.display_name$" - vars.notification_serviceoutput = "$service.output$" - vars.notification_servicestate = "$service.state$" - vars.notification_type = "$notification.type$" - vars.notification_servicename = "$service.name$" + import "plugin-notification-command" + + command = [ "/opt/icinga2-matrix-bot/icinga2/matrix-service-notification.sh" ] + + env = { + "NOTIFICATIONTYPE" = "$notification.type$" + "SERVICEDESC" = "$service.name$" + "HOSTALIAS" = "$host.display_name$", + "HOSTNAME" = "$host.name$", + "HOSTADDRESS" = "$address$", + "SERVICESTATE" = "$service.state$", + "LONGDATETIME" = "$icinga.long_date_time$", + "SERVICEOUTPUT" = "$service.output$", + "NOTIFICATIONAUTHORNAME" = "$notification.author$", + "NOTIFICATIONCOMMENT" = "$notification.comment$", + "HOSTDISPLAYNAME" = "$host.display_name$", + "SERVICEDISPLAYNAME" = "$service.display_name$", + "ICINGA_WEBURL" = "$notification_icingaweb2url$", + "MATRIX_SERVER" = "$user.vars.matrix.server$", + "MATRIX_TOKEN" = "$user.vars.matrix.token$", + "MATRIX_CHANNEL" = "$user.vars.matrix.channel$", + } } diff --git a/webserver/docker_icinga/templates/etc/icinga/conf.d/notifications.conf b/webserver/docker_icinga/templates/etc/icinga/conf.d/notifications.conf index 31194cae..41cc0ad0 100644 --- a/webserver/docker_icinga/templates/etc/icinga/conf.d/notifications.conf +++ b/webserver/docker_icinga/templates/etc/icinga/conf.d/notifications.conf @@ -1,34 +1,22 @@ -// https://github.com/linuxmail/icinga2-matrix_notification -/** - * Example Matrix.org apply rules. - * The "!<id>:matrix.org" needs to be replaced with the room ID - * for example "!SDFfskjfdszhdaslasdkjhdasd:matrix.org". - * Also a Matrix access token is required too. - */ +apply Notification "matrix" to Host { + import "matrix-host-notification" -apply Notification "Matrix host problems" to Host { - import "matrix-host-notification" + users = [ "matrix" ] - user_groups = [ "icingaadmins" ] + interval = 4h - vars.notification_matrix_server = "https://{{ matrix_notification_domain }}" - vars.notification_matrix_room_id = "{{ matrix_notification_room }}" - vars.notification_matrix_token = "{{ matrix_notification_access_token }}" - - # Assign to all hosts - assign where host.address + assign where host.address } -apply Notification "Matrix service problems" to Service { - import "matrix-service-notification" - user_groups = [ "icingaadmins" ] +apply Notification "matrix" to Service { + import "matrix-service-notification" + + users = [ "matrix" ] - vars.notification_matrix_server = "https://{{ matrix_notification_domain }}" - vars.notification_matrix_room_id = "{{ matrix_notification_room }}" - vars.notification_matrix_token = "{{ matrix_notification_access_token }}" + interval = 4h - # Assign to all services - assign where service.name + assign where service.name } + diff --git a/webserver/docker_icinga/templates/etc/icinga/conf.d/templates.conf b/webserver/docker_icinga/templates/etc/icinga/conf.d/templates.conf index 913364bb..4cc6d9e0 100644 --- a/webserver/docker_icinga/templates/etc/icinga/conf.d/templates.conf +++ b/webserver/docker_icinga/templates/etc/icinga/conf.d/templates.conf @@ -87,32 +87,24 @@ template Notification "mail-service-notification" { */ template Notification "matrix-host-notification" { - command = "matrix-host-notification" + command = "matrix-host-notification" - states = [ Up, Down ] - types = [ Problem, Acknowledgement, Recovery, Custom, - FlappingStart, FlappingEnd, - DowntimeStart, DowntimeEnd, DowntimeRemoved ] - vars += { - // notification_icingaweb2url = "https://{{ domain }}/icingaweb2" - notification_logtosyslog = false - } - // interval = 0s + states = [ Up, Down ] + types = [ Problem, Acknowledgement, Custom, FlappingStart, FlappingEnd ] period = "24x7" + vars += { + notification_icingaweb2url = "https://{{ domain }}/icingaweb2" + } } template Notification "matrix-service-notification" { - command = "matrix-service-notification" - - states = [ OK, Warning, Critical, Unknown ] - types = [ Problem, Acknowledgement, Recovery, Custom, - FlappingStart, FlappingEnd, - DowntimeStart, DowntimeEnd, DowntimeRemoved ] + command = "matrix-service-notification" - vars += { - // notification_icingaweb2url = "https://{{ domain }}/icingaweb2" - notification_logtosyslog = false - } - // interval = 0s + states = [ OK, Critical, Unknown ] + types = [ Problem, Acknowledgement, Custom, FlappingStart, FlappingEnd ] period = "24x7" + vars += { + notification_icingaweb2url = "https://{{ domain }}/icingaweb2" + } } + diff --git a/webserver/docker_icinga/templates/etc/icinga/conf.d/users_groups.conf b/webserver/docker_icinga/templates/etc/icinga/conf.d/users_groups.conf index bd6aee12..39b5f5a2 100644 --- a/webserver/docker_icinga/templates/etc/icinga/conf.d/users_groups.conf +++ b/webserver/docker_icinga/templates/etc/icinga/conf.d/users_groups.conf @@ -3,3 +3,18 @@ object UserGroup "icingaadmins" { display_name = "Icinga 2 Admin Group" } +object User "matrix" { + import "generic-user" + + display_name = "Matrix User" + vars.matrix = { + token = "{{ matrix_notification_access_token }}" + channel = "!iYefxbySFEfFQfUGEK:matrix.warpzone.ms" + server = "https://matrix.warpzone.ms" + } +} + +object UserGroup "matrix" { + display_name = "Matrix Group" + assign where user.vars.matrix +} -- GitLab