mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 23:08:06 +02:00
Refactor systemctl services and categories due to alarm bugs
This commit restructures systemctl service definitions and category mappings. Motivation: Alarm-related bugs revealed inconsistencies in service and role handling. Preparation step: lays the groundwork for fixing the alarm issues by aligning categories, roles, and service templates.
This commit is contained in:
24
roles/sys-ctl-alm-telegram/README.md
Normal file
24
roles/sys-ctl-alm-telegram/README.md
Normal file
@@ -0,0 +1,24 @@
|
||||
# Automated Telegram Alerts for Service Failures
|
||||
|
||||
## Description
|
||||
|
||||
This role installs and configures the necessary components for sending notifications via systemd when a service fails. It sets up the `sys-ctl-alm-telegram` service and configures parameters and customizable templates for sending messages through [Telegram](https://telegram.org).
|
||||
|
||||
## Overview
|
||||
|
||||
Optimized for real-time alerts, this role is a key component of the overall [`sys-ctl-alm-compose` suite](../). It ensures that, upon failure of a critical service, a Telegram message is automatically sent to notify administrators and enable prompt troubleshooting.
|
||||
|
||||
## Purpose
|
||||
|
||||
The primary purpose of this role is to provide a robust solution for automated Telegram notifications in a systemd environment. By integrating with Telegram’s Bot API and using customizable message templates, it delivers clear and timely alerts about service failures, thereby enhancing system observability and reliability.
|
||||
|
||||
## Features
|
||||
|
||||
- **Service Installation & Configuration:** Installs and configures necessary components (including the `curl` package).
|
||||
- **Customizable Templates:** Supports tailored Telegram message templates for service failure notifications.
|
||||
- **Secure Notifications:** Leverages systemd to trigger alerts automatically when services fail.
|
||||
- **Suite Integration:** Part of the [`sys-ctl-alm-compose` suite](../) which includes related roles such as [sys-ctl-alm-email](../sys-ctl-alm-email/README.md) and others.
|
||||
|
||||
## Other Resources
|
||||
|
||||
This role was developed as part of a conversation with OpenAI's ChatGPT and can be found [here](https://chat.openai.com/share/96e4ca12-0888-41c0-9cfc-29c0180f0dba).
|
2
roles/sys-ctl-alm-telegram/defaults/main.yml
Normal file
2
roles/sys-ctl-alm-telegram/defaults/main.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
telegram_bot_token: '' # The token of your telegram bot
|
||||
telegram_chat_id: '' # The id of your telegram chat
|
4
roles/sys-ctl-alm-telegram/handlers/main.yml
Normal file
4
roles/sys-ctl-alm-telegram/handlers/main.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
- name: "restart sys-ctl-alm-telegram service"
|
||||
systemd:
|
||||
name: sys-ctl-alm-telegram{{ SYS_SERVICE_SUFFIX }}
|
||||
daemon_reload: yes
|
23
roles/sys-ctl-alm-telegram/meta/main.yml
Normal file
23
roles/sys-ctl-alm-telegram/meta/main.yml
Normal file
@@ -0,0 +1,23 @@
|
||||
galaxy_info:
|
||||
author: "Kevin Veen-Birkenbach"
|
||||
description: "Installs and configures components for sending Telegram notifications through systemd. This role is part of the sys-ctl-alm-compose suite, providing automated alerts when services fail."
|
||||
license: "Infinito.Nexus NonCommercial License"
|
||||
license_url: "https://s.infinito.nexus/license"
|
||||
company: |
|
||||
Kevin Veen-Birkenbach
|
||||
Consulting & Coaching Solutions
|
||||
https://www.veen.world
|
||||
min_ansible_version: "2.9"
|
||||
platforms:
|
||||
- name: Archlinux
|
||||
versions:
|
||||
- rolling
|
||||
galaxy_tags:
|
||||
- telegram
|
||||
- systemd
|
||||
- notifications
|
||||
- automation
|
||||
- curl
|
||||
repository: "https://s.infinito.nexus/code"
|
||||
issue_tracker_url: "https://s.infinito.nexus/issues"
|
||||
documentation: "https://docs.infinito.nexus"
|
37
roles/sys-ctl-alm-telegram/tasks/01_core.yml
Normal file
37
roles/sys-ctl-alm-telegram/tasks/01_core.yml
Normal file
@@ -0,0 +1,37 @@
|
||||
- name: Include dependency 'sys-rst-daemon'
|
||||
include_role:
|
||||
name: sys-rst-daemon
|
||||
when: run_once_sys_rst_daemon is not defined
|
||||
|
||||
- name: Fail if Telegram bot credentials are not set
|
||||
assert:
|
||||
that:
|
||||
- telegram_bot_token != ""
|
||||
- telegram_chat_id != ""
|
||||
fail_msg: |
|
||||
Telegram configuration is incomplete!
|
||||
Please provide non‑empty values for:
|
||||
- telegram_bot_token # Your Telegram bot’s API token
|
||||
- telegram_chat_id # The Telegram chat ID to send messages to
|
||||
|
||||
- name: install curl
|
||||
community.general.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 sys-ctl-alm-telegram.sh
|
||||
template:
|
||||
src: sys-ctl-alm-telegram.sh.j2
|
||||
dest: "{{ systemd_telegram_script }}"
|
||||
|
||||
- name: configure sys-ctl-alm-telegram{{ SYS_SERVICE_SUFFIX }}
|
||||
template:
|
||||
src: sys-ctl-alm-telegram@.service.j2
|
||||
dest: "/etc/systemd/system/sys-ctl-alm-telegram.infinito@.service"
|
||||
notify: "restart sys-ctl-alm-telegram service"
|
4
roles/sys-ctl-alm-telegram/tasks/main.yml
Normal file
4
roles/sys-ctl-alm-telegram/tasks/main.yml
Normal file
@@ -0,0 +1,4 @@
|
||||
- block:
|
||||
- include_tasks: 01_core.yml
|
||||
- include_tasks: utils/run_once.yml
|
||||
when: run_once_sys_ctl_alm_telegram is not defined
|
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
# determine host name: try hostname command, otherwise use $HOSTNAME
|
||||
if command -v hostname &>/dev/null; then
|
||||
host=$(hostname)
|
||||
else
|
||||
host="$HOSTNAME"
|
||||
fi
|
||||
|
||||
# send the Telegram message
|
||||
/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 on ${host} 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/sys-ctl-alm-telegram/vars/main.yml
Normal file
2
roles/sys-ctl-alm-telegram/vars/main.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
systemd_telegram_folder: /opt/ansible-roles/sys-ctl-alm-telegram/
|
||||
systemd_telegram_script: '{{systemd_telegram_folder}}sys-ctl-alm-telegram.sh'
|
Reference in New Issue
Block a user