Last active
October 30, 2022 12:10
-
-
Save arsalanses/aece4b47c88e2c6287244789bfbba1e9 to your computer and use it in GitHub Desktop.
Telegram Bot for checking docker status and alert if a container is not up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import subprocess | |
GROUP_ID = '' | |
BOT_TOKEN = '' | |
BASE_URL = f'https://api.telegram.org/bot{BOT_TOKEN}/sendMessage?chat_id={GROUP_ID}&text=' | |
docker_ps = "echo -n <password> | sudo docker ps --format '{{.Status}}' | awk '{print $1}'" | |
docker_stats = "sudo docker stats --all --no-stream --format 'table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}'" | |
docker_ps_output = subprocess.check_output(docker_ps, shell=True) | |
docker_stats_output = subprocess.check_output(docker_stats, shell=True) | |
docker_ps_output = docker_ps_output.decode("utf-8").split('\n') | |
docker_ps_output = docker_ps_output[:-1] | |
docker_ps_output = [True if item == "Up" else False for item in docker_ps_output] | |
if all(docker_ps_output): | |
requests.get(f'{BASE_URL}System is up and running enjoy!&disable_notification=true').json() | |
else: | |
requests.get(f'{BASE_URL}{docker_stats_output}').json() | |
# python -m venv venv | |
# source venv/bin/activate | |
# pip install requests | |
# 0 */1 * * * bash -c "cd /home/telegram-bot && source venv/bin/activate && echo <password> | sudo -S python3 docker-bot.py && deactivate" | |
# crontab -e | |
# https://crontab.guru/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment