diff --git a/chia-blockchain/defaults/main.yml b/chia-blockchain/defaults/main.yml index 60c075e..636fd9c 100644 --- a/chia-blockchain/defaults/main.yml +++ b/chia-blockchain/defaults/main.yml @@ -260,6 +260,9 @@ chia_add_healthcheck_service: true chia_healthcheck_port: 9950 add_monit_config: true monit_start_now: true +chia_alerts_receiver_token: "" +chia_monit_alert_on_restart: false +chia_monit_alert_webhook_url: "" # How many times the port check needs to fail before monit will restart the service chia_monit_failure_threshold: 3 diff --git a/chia-blockchain/tasks/main.yml b/chia-blockchain/tasks/main.yml index 6510c2b..6b2bf50 100644 --- a/chia-blockchain/tasks/main.yml +++ b/chia-blockchain/tasks/main.yml @@ -255,6 +255,15 @@ when: add_monit_config notify: restart-monit +- name: Add a notify script that send a webhook to Keybase if monit restarts the chia service + become: true + ansible.builtin.template: + src: monit_notify.sh.j2 + dest: /etc/monit/monit-notify.sh + mode: "0644" + when: add_monit_config + notify: restart-monit + - name: Start Monit Service become: true ansible.builtin.service: diff --git a/chia-blockchain/templates/monit_config.j2 b/chia-blockchain/templates/monit_config.j2 index d097f40..4e14406 100644 --- a/chia-blockchain/templates/monit_config.j2 +++ b/chia-blockchain/templates/monit_config.j2 @@ -8,5 +8,7 @@ check host localhost with address 127.0.0.1 {% if "timelord" in chia_enabled_services %} if failed port {{ chia_healthcheck_port }} protocol http request "/timelord" status = 200 for 1 cycles then restart {% endif %} -{% endif %} if 5 restarts within {{ 5 * chia_monit_failure_threshold }} cycles then timeout +{% if chia_monit_alert_on_restart %} + if 1 restarts within {{ chia_monit_failure_threshold }} cycles then exec "/etc/monit/monit-notify.sh" +{% endif %} diff --git a/chia-blockchain/templates/monit_notify.sh.j2 b/chia-blockchain/templates/monit_notify.sh.j2 new file mode 100644 index 0000000..18cc386 --- /dev/null +++ b/chia-blockchain/templates/monit_notify.sh.j2 @@ -0,0 +1,36 @@ +#!/bin/bash + +# Webhook URL where the alert will be sent +WEBHOOK_URL="{{ chia_monit_alert_webhook_url }}" +HOSTNAME=$(/bin/hostname) +BEARER_TOKEN="{{ chia_alerts_receiver_token }}" + +PAYLOAD=$( + cat <