From 321b34ed42affdbc0edc28fac0047c89bec45971 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Thu, 1 Aug 2024 13:05:45 +0200 Subject: [PATCH 1/2] add cpu job --- src/register_jobs.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/register_jobs.py b/src/register_jobs.py index 8563bdb..eb5110c 100644 --- a/src/register_jobs.py +++ b/src/register_jobs.py @@ -1,9 +1,10 @@ import logging import os +import psutil import docker from dotenv import load_dotenv -from src.core.utility import exec_sh, time_in_seconds +from src.core.utility import exec_sh, time_in_seconds from src.core.jobs import Jobs from src.core.metrics import Metrics from src.core.telegrambot import TelegramBot @@ -28,6 +29,9 @@ ) bot.informants.append( lambda: f"Running Containers: {metrics.running_container_count}") +bot.informants.append( + lambda: f"Current CPU usage: {psutil.cpu_percent() * 100:.2f}%" +) @jobs.register(time_in_seconds(hours=1)) @@ -70,3 +74,18 @@ async def update_packages(): async def resend_status_message(): await bot.stop() await bot.start() + +@jobs.register(time_in_seconds(seconds=30), run_initially=True) +# JOB: check CPU usage +async def check_cpu_usage(cpu_high=[False]): + cpu_usage = psutil.cpu_percent() + if cpu_usage > 0.8: + if not cpu_high[0]: + cpu_high[0] = True + return + decision = bot.decide("CPU has been over 80%% for at least 1 minute", ["ignore", "reboot", "shutdown"]) + match decision: + case 2: + os.system("shutdown now -h") + case 3: + os.system("reboot") From d0b5eb7b7aeb443f6f532dae4ac218a125ed2ed3 Mon Sep 17 00:00:00 2001 From: Tch1b0 Date: Thu, 1 Aug 2024 13:06:44 +0200 Subject: [PATCH 2/2] add requirement psutil --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 5d477e0..8dd24fe 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ docker~=6.0.0 python-telegram-bot~=20.0a2 -python-dotenv~=0.20.0 \ No newline at end of file +python-dotenv~=0.20.0 +psutil~=6.0.0 \ No newline at end of file