mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2024-11-22 20:51:07 +01:00
Implemented telegram notifications
This commit is contained in:
parent
196f1e759d
commit
25e3d3c31c
@ -2,7 +2,7 @@
|
|||||||
Description=Backup to USB when mounted to {{ backup_to_usb_mount }}
|
Description=Backup to USB when mounted to {{ backup_to_usb_mount }}
|
||||||
Wants={{systemctl_mount_service_name}}
|
Wants={{systemctl_mount_service_name}}
|
||||||
After={{systemctl_mount_service_name}}
|
After={{systemctl_mount_service_name}}
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
Requires=backups-cleanup.service
|
Requires=backups-cleanup.service
|
||||||
After=backups-cleanup.service
|
After=backups-cleanup.service
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
- server_native-python-pip
|
- server_native-python-pip
|
||||||
- independent_systemd-email
|
- systemd_notifier
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=delete old backups
|
Description=delete old backups
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
@ -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
|
|
@ -1,4 +0,0 @@
|
|||||||
- name: "restart systemd-email service"
|
|
||||||
systemd:
|
|
||||||
name: systemd-email.service
|
|
||||||
daemon_reload: yes
|
|
@ -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
|
|
@ -1 +0,0 @@
|
|||||||
systemd_email_folder: "{{path_administrator_scripts}}systemd_email/"
|
|
@ -9,3 +9,4 @@ galaxy_info:
|
|||||||
- all
|
- all
|
||||||
dependencies:
|
dependencies:
|
||||||
- pc_system-aur-helper
|
- pc_system-aur-helper
|
||||||
|
- systemd_notifier
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=Keyboard Color Service
|
Description=Keyboard Color Service
|
||||||
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
- name: pull swapfile creating script
|
- name: pull swapfile creating script
|
||||||
git:
|
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"
|
dest: "$HOME/Repositories/github.com/kevinveenbirkenbach/create-linux-swapfile"
|
||||||
update: yes
|
update: yes
|
||||||
become: false
|
become: false
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
- server_native-docker-reverse-proxy
|
- server_native-docker-reverse-proxy
|
||||||
- independent_systemd-email
|
- systemd_notifier
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=Let's Encrypt Mailu Deploy
|
Description=Let's Encrypt Mailu Deploy
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
- server_native-git
|
- server_native-git
|
||||||
- independent_systemd-email
|
- systemd_notifier
|
||||||
- independent_backups-cleanup-timer
|
- independent_backups-cleanup-timer
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=pull remote backups
|
Description=pull remote backups
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
- independent_systemd-email
|
- systemd_notifier
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=Check btrfs status
|
Description=Check btrfs status
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
- server_native-nginx
|
- server_native-nginx
|
||||||
- independent_systemd-email
|
- systemd_notifier
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=Let's Encrypt renewal
|
Description=Let's Encrypt renewal
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
- independent_systemd-email
|
- systemd_notifier
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=checking disc space
|
Description=checking disc space
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=restart unhealthy docker containers
|
Description=restart unhealthy docker containers
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
- independent_systemd-email
|
- systemd_notifier
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=Checking docker health
|
Description=Checking docker health
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
- server_native-git
|
- server_native-git
|
||||||
- server_native-backups-provider
|
- server_native-backups-provider
|
||||||
- independent_systemd-email
|
- systemd_notifier
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=docker volume backup
|
Description=docker volume backup
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
- independent_systemd-email
|
- systemd_notifier
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=freeing disc space
|
Description=freeing disc space
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
dependencies:
|
dependencies:
|
||||||
- independent_systemd-email
|
- systemd_notifier
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[Unit]
|
[Unit]
|
||||||
Description=checking journalctl health
|
Description=checking journalctl health
|
||||||
OnFailure=systemd-email@%n.service
|
OnFailure=systemd-notifier@%n.service
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
|
37
roles/systemd_notifier/README.md
Normal file
37
roles/systemd_notifier/README.md
Normal 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
|
4
roles/systemd_notifier/handlers/main.yml
Normal file
4
roles/systemd_notifier/handlers/main.yml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
- name: "restart systemd-notifier service"
|
||||||
|
systemd:
|
||||||
|
name: systemd-notifier.service
|
||||||
|
daemon_reload: yes
|
4
roles/systemd_notifier/meta/main.yml
Normal file
4
roles/systemd_notifier/meta/main.yml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
---
|
||||||
|
dependencies:
|
||||||
|
- role: systemd_notifier_telegram
|
||||||
|
- role: systemd_notifier_email
|
6
roles/systemd_notifier/tasks/main.yml
Normal file
6
roles/systemd_notifier/tasks/main.yml
Normal 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"
|
@ -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
|
44
roles/systemd_notifier_email/README.md
Normal file
44
roles/systemd_notifier_email/README.md
Normal 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
|
4
roles/systemd_notifier_email/handlers/main.yml
Normal file
4
roles/systemd_notifier_email/handlers/main.yml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
- name: "restart systemd_notifier_email service"
|
||||||
|
systemd:
|
||||||
|
name: systemd_notifier_email.service
|
||||||
|
daemon_reload: yes
|
18
roles/systemd_notifier_email/tasks/main.yml
Normal file
18
roles/systemd_notifier_email/tasks/main.yml
Normal 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
|
@ -3,6 +3,6 @@ Description=status email for %i to user
|
|||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
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
|
User=root
|
||||||
Group=systemd-journal
|
Group=systemd-journal
|
1
roles/systemd_notifier_email/vars/main.yml
Normal file
1
roles/systemd_notifier_email/vars/main.yml
Normal file
@ -0,0 +1 @@
|
|||||||
|
systemd_notifier_email_folder: "{{path_administrator_scripts}}systemd_notifier_email/"
|
41
roles/systemd_notifier_telegram/README.md
Normal file
41
roles/systemd_notifier_telegram/README.md
Normal 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
|
4
roles/systemd_notifier_telegram/handlers/main.yml
Normal file
4
roles/systemd_notifier_telegram/handlers/main.yml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
- name: "restart systemd_notifier_telegram service"
|
||||||
|
systemd:
|
||||||
|
name: systemd_notifier_telegram.service
|
||||||
|
daemon_reload: yes
|
21
roles/systemd_notifier_telegram/tasks/main.yml
Normal file
21
roles/systemd_notifier_telegram/tasks/main.yml
Normal 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"
|
@ -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"
|
@ -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
|
2
roles/systemd_notifier_telegram/vars/main.yml
Normal file
2
roles/systemd_notifier_telegram/vars/main.yml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
systemd_telegram_folder: "/opt/ansible-roles/systemd_notifier_telegram/"
|
||||||
|
systemd_telegram_script: "{{systemd_telegram_folder}}systemd_notifier_telegram.sh"
|
Loading…
Reference in New Issue
Block a user