Implemented telegram notifications

This commit is contained in:
Kevin Veen-Birkenbach 2023-05-28 22:17:33 +02:00
parent 196f1e759d
commit 25e3d3c31c
47 changed files with 232 additions and 55 deletions

View File

@ -2,7 +2,7 @@
Description=Backup to USB when mounted to {{ backup_to_usb_mount }}
Wants={{systemctl_mount_service_name}}
After={{systemctl_mount_service_name}}
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
Requires=backups-cleanup.service
After=backups-cleanup.service

View File

@ -1,3 +1,3 @@
dependencies:
- server_native-python-pip
- independent_systemd-email
- systemd_notifier

View File

@ -1,6 +1,6 @@
[Unit]
Description=delete old backups
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -1,6 +0,0 @@
# server_native-systemd-mail
# see
- https://wiki.archlinux.org/index.php/Systemd/Timers#MAILTO
- https://serverfault.com/questions/876233/how-to-send-an-email-if-a-systemd-service-is-restarted/876254
- https://serverfault.com/questions/694818/get-notification-when-systemd-monitored-service-enters-failed-state

View File

@ -1,4 +0,0 @@
- name: "restart systemd-email service"
systemd:
name: systemd-email.service
daemon_reload: yes

View File

@ -1,18 +0,0 @@
- name: install smtp-forwarder
pacman: name=smtp-forwarder state=present
- name: configure msmtprc.conf.j2
template: src=msmtprc.conf.j2 dest=/root/.msmtprc
- name: "create {{systemd_email_folder}}"
file:
path: "{{systemd_email_folder}}"
state: directory
mode: 0755
- name: configure systemd-email.sh
template: src=systemd-email.sh.j2 dest={{systemd_email_folder}}systemd-email.sh
- name: configure systemd-email.service
template: src=systemd-email@.service.j2 dest=/etc/systemd/system/systemd-email@.service
notify: restart systemd-email service

View File

@ -1 +0,0 @@
systemd_email_folder: "{{path_administrator_scripts}}systemd_email/"

View File

@ -7,5 +7,6 @@ galaxy_info:
- name: Linux
versions:
- all
dependencies:
- pc_system-aur-helper
dependencies:
- pc_system-aur-helper
- systemd_notifier

View File

@ -1,5 +1,6 @@
[Unit]
Description=Keyboard Color Service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -1,6 +1,6 @@
- name: pull swapfile creating script
git:
repo: "git@github.com:kevinveenbirkenbach/create-linux-swapfile.git"
repo: "https://github.com/kevinveenbirkenbach/create-linux-swapfile.git"
dest: "$HOME/Repositories/github.com/kevinveenbirkenbach/create-linux-swapfile"
update: yes
become: false

View File

@ -1,3 +1,3 @@
dependencies:
- server_native-docker-reverse-proxy
- independent_systemd-email
- systemd_notifier

View File

@ -1,6 +1,6 @@
[Unit]
Description=Let's Encrypt Mailu Deploy
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -1,4 +1,4 @@
dependencies:
- server_native-git
- independent_systemd-email
- systemd_notifier
- independent_backups-cleanup-timer

View File

@ -1,6 +1,6 @@
[Unit]
Description=pull remote backups
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -1,2 +1,2 @@
dependencies:
- independent_systemd-email
- systemd_notifier

View File

@ -1,6 +1,6 @@
[Unit]
Description=Check btrfs status
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -1,3 +1,3 @@
dependencies:
- server_native-nginx
- independent_systemd-email
- systemd_notifier

View File

@ -1,6 +1,6 @@
[Unit]
Description=Let's Encrypt renewal
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -1,2 +1,2 @@
dependencies:
- independent_systemd-email
- systemd_notifier

View File

@ -1,6 +1,6 @@
[Unit]
Description=checking disc space
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -1,6 +1,6 @@
[Unit]
Description=restart unhealthy docker containers
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -1,2 +1,2 @@
dependencies:
- independent_systemd-email
- systemd_notifier

View File

@ -1,6 +1,6 @@
[Unit]
Description=Checking docker health
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -1,4 +1,4 @@
dependencies:
- server_native-git
- server_native-backups-provider
- independent_systemd-email
- systemd_notifier

View File

@ -1,6 +1,6 @@
[Unit]
Description=docker volume backup
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -1,2 +1,2 @@
dependencies:
- independent_systemd-email
- systemd_notifier

View File

@ -1,6 +1,6 @@
[Unit]
Description=freeing disc space
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -1,2 +1,2 @@
dependencies:
- independent_systemd-email
- systemd_notifier

View File

@ -1,6 +1,6 @@
[Unit]
Description=checking journalctl health
OnFailure=systemd-email@%n.service
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot

View File

@ -0,0 +1,37 @@
# systemd_notifier Ansible Role
Author: Kevin Veen-Birkenbach (kevin@veen.world)
## Description
This Ansible role installs a systemd service that sends notifications via both `systemd_notifier_telegram` and `systemd_notifier_email` when any service fails.
Features include:
- Configuration of a systemd service for notification.
- Dependency management for the `systemd_notifier_telegram` and `systemd_notifier_email` roles.
This role was created as part of a conversation with OpenAI's ChatGPT and can be found [here](https://chat.openai.com/share/96e4ca12-0888-41c0-9cfc-29c0180f0dba).
## License
This Ansible role is licensed under the AGPL v3 License. See the LICENSE file for the full license text.
## Role Dependencies
This role has the following dependencies:
- `systemd_notifier_telegram`
- `systemd_notifier_email`
## Features
- Installs a systemd service for sending notifications.
- Integrates with both Telegram and Email for notification delivery.
## Contact Information
For any questions or feedback, please contact the author:
Author: Kevin Veen-Birkenbach
Email: kevin@veen.world

View File

@ -0,0 +1,4 @@
- name: "restart systemd-notifier service"
systemd:
name: systemd-notifier.service
daemon_reload: yes

View File

@ -0,0 +1,4 @@
---
dependencies:
- role: systemd_notifier_telegram
- role: systemd_notifier_email

View File

@ -0,0 +1,6 @@
---
- name: configure systemd-notifier@.service
template:
src: systemd-notifier@.service.j2
dest: "/etc/systemd/system/systemd-notifier@.service"
notify: "restart systemd-notifier service"

View File

@ -0,0 +1,8 @@
[Unit]
Description=Notifier for %i
[Service]
Type=oneshot
ExecStart=/usr/bin/systemctl start systemd_notifier_telegram@%i.service systemd_notifier_email@%i.service
User=root
Group=systemd-journal

View File

@ -0,0 +1,44 @@
# systemd_notifier_email Ansible Role
Author: Kevin Veen-Birkenbach (kevin@veen.world)
## Description
This Ansible role installs the necessary components for sending email notifications through systemd when any service fails. It configures the `systemd_notifier_email` service and handles the setup of email parameters and templates.
Features include:
- Installation and configuration of an email sending service.
- Customizable email templates for service failure notifications.
This role is part of the `systemd_notifier` suite, which provides a comprehensive solution for service failure notifications in a systemd environment.
This role was created as part of a conversation with OpenAI's ChatGPT and can be found [here](https://chat.openai.com/share/96e4ca12-0888-41c0-9cfc-29c0180f0dba).
## Requirements
This role has the following requirements:
- Access to an SMTP server for sending email notifications.
- Availability of the `msmtp` package on the target system.
## Role Variables
The following variables can be customized in the role's `vars/main.yml` file:
- `systemd_notifier_email_folder`: The path to the folder where email-related scripts and configurations will be stored.
## Usage
To use this role, include it as a dependency in your playbook or role that requires email notifications. Ensure that the `systemd_notifier_email` role is correctly referenced and configured.
## License
This Ansible role is licensed under the AGPL v3 License. See the LICENSE file for the full license text.
## Contact Information
For any questions or feedback, please contact the author:
Author: Kevin Veen-Birkenbach
Email: kevin@veen.world

View File

@ -0,0 +1,4 @@
- name: "restart systemd_notifier_email service"
systemd:
name: systemd_notifier_email.service
daemon_reload: yes

View File

@ -0,0 +1,18 @@
- name: install smtp-forwarder
pacman: name=smtp-forwarder state=present
- name: configure msmtprc.conf.j2
template: src=msmtprc.conf.j2 dest=/root/.msmtprc
- name: "create {{systemd_notifier_email_folder}}"
file:
path: "{{systemd_notifier_email_folder}}"
state: directory
mode: 0755
- name: configure systemd_notifier_email.sh
template: src=systemd_notifier_email.sh.j2 dest={{systemd_notifier_email_folder}}systemd_notifier_email.sh
- name: configure systemd_notifier_email.service
template: src=systemd_notifier_email@.service.j2 dest=/etc/systemd/system/systemd_notifier_email@.service
notify: restart systemd_notifier_email service

View File

@ -3,6 +3,6 @@ Description=status email for %i to user
[Service]
Type=oneshot
ExecStart=/bin/bash {{systemd_email_folder}}systemd-email.sh %i
ExecStart=/bin/bash {{systemd_notifier_email_folder}}systemd_notifier_email.sh %i
User=root
Group=systemd-journal

View File

@ -0,0 +1 @@
systemd_notifier_email_folder: "{{path_administrator_scripts}}systemd_notifier_email/"

View File

@ -0,0 +1,41 @@
## Description
This Ansible role installs the necessary components for sending Telegram notifications through systemd when any service fails. It configures the `systemd_notifier_telegram` service and handles the setup of Telegram bot parameters and notification templates.
Features include:
- Installation and configuration of a Telegram notifier service.
- Customizable templates for Telegram messages sent on service failure.
This role is part of the `systemd_notifier` suite, which provides a comprehensive solution for service failure notifications in a systemd environment.
## Requirements
This role has the following requirements:
- Access to a Telegram bot token for sending messages.
- Availability of the `curl` package on the target system.
## Role Variables
The following variables can be customized in the role's `vars/main.yml` file:
- `systemd_notifier_telegram_folder`: The path to the folder where Telegram-related scripts and configurations will be stored.
- `systemd_notifier_telegram_script`: The full path to the systemd-telegram.sh script.
This role was created as part of a conversation with OpenAI's ChatGPT and can be found [here](https://chat.openai.com/share/96e4ca12-0888-41c0-9cfc-29c0180f0dba).
## Usage
To use this role, include it as a dependency in your playbook or role that requires Telegram notifications. Ensure that the `systemd_notifier_telegram` role is correctly referenced and configured.
## License
This Ansible role is licensed under the AGPL v3 License. See the LICENSE file for the full license text.
## Contact Information
For any questions or feedback, please contact the author:
Author: Kevin Veen-Birkenbach
Email: kevin@veen.world

View File

@ -0,0 +1,4 @@
- name: "restart systemd_notifier_telegram service"
systemd:
name: systemd_notifier_telegram.service
daemon_reload: yes

View File

@ -0,0 +1,21 @@
- name: install curl
pacman:
name: curl
state: present
- name: Create a directory with a subdirectory
ansible.builtin.file:
path: "{{systemd_telegram_folder}}"
state: directory
mode: '0755'
- name: configure systemd_notifier_telegram.sh
template:
src: systemd_notifier_telegram.sh.j2
dest: "{{ systemd_telegram_script }}"
- name: configure systemd_notifier_telegram.service
template:
src: systemd_notifier_telegram@.service.j2
dest: "/etc/systemd/system/systemd_notifier_telegram@.service"
notify: "restart systemd_notifier_telegram service"

View File

@ -0,0 +1,2 @@
#!/bin/bash
/usr/bin/curl -s -X POST https://api.telegram.org/bot{{ telegram_bot_token }}/sendMessage -d chat_id={{ telegram_chat_id }} -d text="service $1 failed"

View File

@ -0,0 +1,8 @@
[Unit]
Description=status Telegram message for %i to user
[Service]
Type=oneshot
ExecStart=/bin/bash {{ systemd_telegram_script }} %i
User=root
Group=systemd-journal

View File

@ -0,0 +1,2 @@
systemd_telegram_folder: "/opt/ansible-roles/systemd_notifier_telegram/"
systemd_telegram_script: "{{systemd_telegram_folder}}systemd_notifier_telegram.sh"