From 37f6971e34f5bbd471f1e8e0a8b917d188e9e273 Mon Sep 17 00:00:00 2001 From: Christian Elberfeld <elberfeld@web.de> Date: Wed, 10 May 2023 09:59:40 +0200 Subject: [PATCH] icinga matrix notification --- site.yml | 2 + webserver/docker_icinga/tasks/main.yml | 1 + webserver/docker_icinga/templates/Dockerfile | 6 + .../etc/icinga/conf.d/commands2.conf | 147 +++++++++++++++++- .../etc/icinga/conf.d/notifications.conf | 30 ++++ .../etc/icinga/conf.d/templates.conf | 35 +++++ 6 files changed, 220 insertions(+), 1 deletion(-) diff --git a/site.yml b/site.yml index 58b6b656..b05735b3 100644 --- a/site.yml +++ b/site.yml @@ -190,6 +190,8 @@ domain: icinga.warpzone.ms, api_port: 5665, mysql_port: 33306, + matrix_notification_domain: "matrix.warpzone.ms", + matrix_notification_room: "!iYefxbySFEfFQfUGEK:matrix.warpzone.ms" } - { role: webserver/docker_hackmd, tags: hackmd, diff --git a/webserver/docker_icinga/tasks/main.yml b/webserver/docker_icinga/tasks/main.yml index 284f8a7c..c9714182 100644 --- a/webserver/docker_icinga/tasks/main.yml +++ b/webserver/docker_icinga/tasks/main.yml @@ -8,6 +8,7 @@ - { path: "{{ basedir }}/icinga_api_pass", length: 8 } - { path: "{{ basedir }}/mysql_admin_pass", length: 12 } - { path: "{{ basedir }}/mysql_user_pass", length: 12 } + - { path: "{{ basedir }}/matrix_notification_access_token", length: -1 } - name: pakete installieren diff --git a/webserver/docker_icinga/templates/Dockerfile b/webserver/docker_icinga/templates/Dockerfile index 7211fcf6..1c804d96 100644 --- a/webserver/docker_icinga/templates/Dockerfile +++ b/webserver/docker_icinga/templates/Dockerfile @@ -48,3 +48,9 @@ RUN cd /opt/ \ && git clone https://github.com/elberfeld/check_metric_value.git \ && cd /opt/check_metric_value/ \ && git checkout b94d3c3e78497a05e3b4520d33421f37e4d77985 + +# icinga2-matrix_notification - commit from 15.04.2012 +RUN cd /opt/ \ + && git clone https://github.com/linuxmail/icinga2-matrix_notification \ + && cd /opt/icinga2-matrix_notification/ \ + && git checkout 99d2174a3b00e9a88648fe58bcd975368f69837d 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 7ff9ab0d..dd72f3f4 100644 --- a/webserver/docker_icinga/templates/etc/icinga/conf.d/commands2.conf +++ b/webserver/docker_icinga/templates/etc/icinga/conf.d/commands2.conf @@ -1,4 +1,8 @@ +/** + * Check MQTT values + */ + object CheckCommand "check_mqtt" { import "plugin-check-command" @@ -33,6 +37,10 @@ object CheckCommand "check_mqtt" { } } +/** + * Check for Mail Blacklisting + */ + object CheckCommand "check_mail_blacklist" { import "plugin-check-command" @@ -45,6 +53,9 @@ object CheckCommand "check_mail_blacklist" { } } +/** + * Check for Prometheus values + */ object CheckCommand "check_metric_value" { import "plugin-check-command" @@ -62,4 +73,138 @@ object CheckCommand "check_metric_value" { "-w" = "$metric_warn$" "-c" = "$metric_crit$" } -} \ No newline at end of file +} + +/** + * Matrix Notification + */ + +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$" +} + + +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$" +} 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 e69de29b..bab5c7bf 100644 --- a/webserver/docker_icinga/templates/etc/icinga/conf.d/notifications.conf +++ b/webserver/docker_icinga/templates/etc/icinga/conf.d/notifications.conf @@ -0,0 +1,30 @@ + +// 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 host problems" to Host { + import "matrix-host-notification" + + 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 +} + +apply Notification "Matrix service problems" to Service { + import "matrix-service-notification" + + 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 services + 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 5655e3f2..913364bb 100644 --- a/webserver/docker_icinga/templates/etc/icinga/conf.d/templates.conf +++ b/webserver/docker_icinga/templates/etc/icinga/conf.d/templates.conf @@ -81,3 +81,38 @@ template Notification "mail-service-notification" { period = "24x7" } + +/** + * Provides default settings for Matrix.org service notifications. + */ + +template Notification "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 + period = "24x7" +} + +template Notification "matrix-service-notification" { + command = "matrix-service-notification" + + states = [ OK, Warning, Critical, Unknown ] + types = [ Problem, Acknowledgement, Recovery, Custom, + FlappingStart, FlappingEnd, + DowntimeStart, DowntimeEnd, DowntimeRemoved ] + + vars += { + // notification_icingaweb2url = "https://{{ domain }}/icingaweb2" + notification_logtosyslog = false + } + // interval = 0s + period = "24x7" +} -- GitLab