diff --git a/inventory/group_vars/all.yml b/inventory/group_vars/all.yml index 864fa30..85a65e2 100644 --- a/inventory/group_vars/all.yml +++ b/inventory/group_vars/all.yml @@ -138,6 +138,14 @@ secret_nagios_admin_pass: !vault | 34616338636630633539353335336631313361656633333539323130626132356263653436343363 3930323538613137370a373861376566376631356564623665313662636562626234643862343863 61326232633266633262613931303631396163326266386363366639366639613938 +secret_nagios_matrix_token: !vault | + $ANSIBLE_VAULT;1.1;AES256 + 66366665666437643765366533646666386162393038653262333461376566333366363332643135 + 6233376362633566303939623832636366333330393238370a323766366164393733383736633435 + 37633137626634643530653665613166633439376333633663633561313864396465623036653063 + 6433376138386531380a383762393137613738643538343438633730313135613730613139393536 + 35666133666262383862663637623738643836383633653864626231623034613662646563623936 + 3763356331333561383833386162616664376335333139376363 # For Netbox secret_netbox_user_pass: !vault | diff --git a/playbooks/prod_web.yml b/playbooks/prod_web.yml index ca95d17..5b3e7d0 100755 --- a/playbooks/prod_web.yml +++ b/playbooks/prod_web.yml @@ -140,9 +140,15 @@ # TODO: Replace this with Naemon(?) - role: nagios vars: + nagios_matrix_server: "https://matrix.desu.ltd" + nagios_matrix_room: "!QJpSrEHPWSAHwdUMgy:desu.ltd" + nagios_matrix_token: "{{ secret_nagios_matrix_token }}" nagios_data_dir: /data/nagios nagios_admin_pass: "{{ secret_nagios_admin_pass }}" nagios_contacts: + - name: matrix + host_notification_commands: notify-host-by-matrix + service_notification_commands: notify-service-by-matrix - name: salt host_notification_commands: notify-host-by-email service_notification_commands: notify-service-by-email @@ -155,6 +161,10 @@ command: "$USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$" - name: check_by_ssh command: "$USER1$/check_by_ssh -H $HOSTADDRESS$ -F /opt/nagios/etc/ssh_config -t 30 -q -i /opt/nagios/etc/id_ed25519 -l nagios-checker -C \"$ARG1$\"" + - name: notify-host-by-matrix + command: "/usr/bin/printf \"%b\" \"$NOTIFICATIONTYPE$ - $HOSTNAME$ is $HOSTSTATE$\\nAddress: $HOSTADDRESS$\\nInfo: $HOSTOUTPUT$\\nDate/Time: $LONGDATETIME$\" | /opt/Custom-Nagios-Plugins/notify-by-matrix" + - name: notify-service-by-matrix + command: "/usr/bin/printf \"%b\" \"$NOTIFICATIONTYPE$ - Service $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$\\nInfo: $SERVICEOUTPUT$\\nDate/Time: $LONGDATETIME$\" | /opt/Custom-Nagios-Plugins/notify-by-matrix" nagios_services: # Agentless checks - name: HTTP diff --git a/roles/nagios/tasks/main.yml b/roles/nagios/tasks/main.yml index b86edb5..e6dba6c 100644 --- a/roles/nagios/tasks/main.yml +++ b/roles/nagios/tasks/main.yml @@ -27,6 +27,11 @@ - /dev/null:/opt/nagios/bin/nsca - /dev/null:/opt/nagios/bin/send_nsca tags: [ docker, nagios ] +- name: template out scripts for nagios + template: src="{{ item }}" dest="{{ nagios_data_dir }}/plugins/{{ item }}" owner=root group=root mode=0755 + with_items: + - notify-by-matrix + tags: [ nagios, template, plugins ] - name: template out config for nagios template: src=nagios-ansible.cfg.j2 dest="{{ nagios_data_dir }}/etc/objects/ansible.cfg" owner=root group=root mode=0644 tags: [ nagios, template ] diff --git a/roles/nagios/templates/notify-by-matrix b/roles/nagios/templates/notify-by-matrix new file mode 100644 index 0000000..86323fa --- /dev/null +++ b/roles/nagios/templates/notify-by-matrix @@ -0,0 +1,34 @@ +#! /bin/sh +# +# notify-by-matrix +# Copyright (C) 2021 Vintage Salt +# +# Distributed under terms of the MIT license. +# + +set -e + +# Set our Matrix-related vars here +MX_TOKEN="{{ nagios_matrix_token }}" +MX_SERVER="{{ nagios_matrix_server }}" +MX_ROOM="{{ nagios_matrix_room }}" + +# Get a TXN to prefix this particular message with +MX_TXN="$(date "+%s")$(( RANDOM % 9999 ))" + +# Read a message from STDIN +# NOTE: This is dangerous and stupid and unsanitized +read message +while read line; do + message="${message}\n${line}" +done + +# Push it to the channel +curl -X PUT \ + --header 'Content-Type: application/json' \ + --header 'Accept: application/json' \ + -d "{ + \"msgtype\": \"m.text\", + \"body\": \"$message\" + }" \ + "$MX_SERVER/_matrix/client/unstable/rooms/$MX_ROOM/send/m.room.message/$MX_TXN?access_token=$MX_TOKEN"