From a06ee4c65cf4c76da9f530fb1b71820294db37b5 Mon Sep 17 00:00:00 2001 From: Zsolt Ero Date: Sun, 9 Jun 2024 22:27:10 +0200 Subject: [PATCH] telegram --- config/.env.sample | 13 +++++++++++-- scripts/loadbalancer/loadbalancer.py | 15 +++++++++------ .../loadbalancer/loadbalancer_lib/telegram_.py | 14 ++++++++++++++ scripts/loadbalancer/telegram.py | 12 ------------ ssh_lib/tasks.py | 8 ++++---- 5 files changed, 38 insertions(+), 24 deletions(-) create mode 100644 scripts/loadbalancer/loadbalancer_lib/telegram_.py delete mode 100644 scripts/loadbalancer/telegram.py diff --git a/config/.env.sample b/config/.env.sample index f6f4c49..28344a7 100644 --- a/config/.env.sample +++ b/config/.env.sample @@ -2,7 +2,7 @@ SSH_PASSWD= # Direct subdomain, using Let's Encrypt certificates -DOMAIN_LE=le.openfreemap.org +DOMAIN_LE= # Let's Encrypt account email LE_EMAIL= @@ -17,5 +17,14 @@ SKIP_PLANET=false # --- Let's Encrypt DNS related variables, not needed for self-hosting -DOMAIN_LEDNS = direct.openfreemap.org +DOMAIN_LEDNS=direct.openfreemap.org +# --- host list + +HTTP_HOST_LIST= + + +# --- Load Balancer script + +TELEGRAM_BOT_TOKEN= +TELEGRAM_BOT_CHAT_ID= \ No newline at end of file diff --git a/scripts/loadbalancer/loadbalancer.py b/scripts/loadbalancer/loadbalancer.py index 6a40241..fb17b0c 100755 --- a/scripts/loadbalancer/loadbalancer.py +++ b/scripts/loadbalancer/loadbalancer.py @@ -5,6 +5,7 @@ import json import click import requests from loadbalancer_lib.curl import pycurl_get, pycurl_status +from loadbalancer_lib.telegram_ import telegram_send_message AREAS = ['planet', 'monaco'] @@ -25,7 +26,7 @@ def run(): with open('/data/ofm/config/loadbalancer.json') as fp: c = json.load(fp) - print(c) + # print(c) try: results_by_ip = {} @@ -37,12 +38,14 @@ def run(): for host_ip, host_ok in results_by_ip.items(): if not host_ok: - print(f'{host_ip} ERROR') - # TODO send message + message = f'ERROR with host: {host_ip}' + print(message) + telegram_send_message(message, c['telegram_bot_token'], c['telegram_chat_id']) except Exception as e: - print(e) - # TODO send message + message = f'ERROR with loadbalancer: {e}' + print(message) + telegram_send_message(message, c['telegram_bot_token'], c['telegram_chat_id']) def run_area(c, area): @@ -52,7 +55,7 @@ def run_area(c, area): results = dict() - for host_ip in c['load_balance_host_list']: + for host_ip in c['http_host_list']: try: check_host(c['domain_ledns'], host_ip, area, deployed_version) results[host_ip] = True diff --git a/scripts/loadbalancer/loadbalancer_lib/telegram_.py b/scripts/loadbalancer/loadbalancer_lib/telegram_.py new file mode 100644 index 0000000..ab1161d --- /dev/null +++ b/scripts/loadbalancer/loadbalancer_lib/telegram_.py @@ -0,0 +1,14 @@ +import requests + + +def telegram_send_message(message, bot_token, chat_id): + url = f'https://api.telegram.org/bot{bot_token}/sendMessage' + + payload = {'chat_id': chat_id, 'text': message} + + response = requests.post(url, data=payload) + + if response.status_code == 200: + print('Message sent successfully!') + else: + print('Failed to send message:', response.text) diff --git a/scripts/loadbalancer/telegram.py b/scripts/loadbalancer/telegram.py deleted file mode 100644 index 21063d6..0000000 --- a/scripts/loadbalancer/telegram.py +++ /dev/null @@ -1,12 +0,0 @@ -from telegram import Bot - - -bot = Bot(token='YOUR_BOT_TOKEN') - -# Replace 'CHAT_ID' with the chat ID you want to send the message to -chat_id = 'CHAT_ID' - -# The message you want to send -message = 'Hello, this is a test message from my bot!' - -bot.send_message(chat_id=chat_id, text=message) diff --git a/ssh_lib/tasks.py b/ssh_lib/tasks.py index 7c117c9..b7bdaab 100644 --- a/ssh_lib/tasks.py +++ b/ssh_lib/tasks.py @@ -223,14 +223,14 @@ def setup_ledns_writer(c): def setup_loadbalancer(c): domain_ledns = dotenv_val('DOMAIN_LEDNS').lower() - load_balance_host_list = [ - h.strip() for h in dotenv_val('LOAD_BALANCE_HOST_LIST').split(',') if h.strip() - ] + http_host_list = [h.strip() for h in dotenv_val('HTTP_HOST_LIST').split(',') if h.strip()] assert (CONFIG_DIR / 'cloudflare.ini').exists() config = { 'domain_ledns': domain_ledns, - 'load_balance_host_list': load_balance_host_list, + 'http_host_list': http_host_list, + 'telegram_bot_token': dotenv_val('TELEGRAM_BOT_TOKEN'), + 'telegram_bot_chat_id': dotenv_val('TELEGRAM_BOT_CHAT_ID'), } config_str = json.dumps(config, indent=2, ensure_ascii=False)