This commit is contained in:
Iris De Santis 2024-08-05 14:18:15 +02:00
parent d336019209
commit 2c325e9cdb
13 changed files with 166 additions and 0 deletions

11
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,11 @@
{
"files.exclude": {
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/CVS": true,
"**/.DS_Store": true,
"**/Thumbs.db": true
},
"hide-files.files": []
}

0
src/checks/__init__.py Normal file
View file

View file

@ -0,0 +1,11 @@
import psutil
from logger import log_message
def check_battery():
"""Check the battery status."""
battery = psutil.sensors_battery()
if battery:
if battery.percent < 20 and not battery.power_plugged:
log_message("running out of battery,,, (less than 20%) ")
elif battery.percent == 100 and battery.power_plugged:
log_message("Battery is fully charged!!")

View file

@ -0,0 +1,10 @@
from os import environ
import subprocess
from logger import log_message
def check_boot_errors():
"""Check for boot errors using dmesg."""
result = subprocess.run(["dmesg"], capture_output=True, text=True)
errors = result.stdout.lower().count("error")
if errors > 30:
log_message("Last boot has had more than 30 errors,, :( (according to dmesg)" + environ.get("BOT_OWNER") + " please check the logs!")

View file

@ -0,0 +1,26 @@
from os import environ
import psutil
from datetime import datetime
from logger import log_message
cpu_overload_start = None
def check_cpu_usage():
"""Check CPU usage and log if overloaded for more than 2 minutes."""
global cpu_overload_start
cpu_usage = psutil.cpu_percent(interval=1)
if cpu_usage > 90:
if cpu_overload_start is None:
cpu_overload_start = datetime.now()
elif (datetime.now() - cpu_overload_start).total_seconds() > 120:
process_info = get_top_cpu_process()
log_message(f"CPU overloaded by {process_info} for more than 2 minutes,," + environ.get("BOT_OWNER") + " please do something!")
cpu_overload_start = None # Reset
else:
cpu_overload_start = None # Reset if not overloaded
def get_top_cpu_process():
"""Get the process using the most CPU."""
processes = [(p.info['name'], p.info['cpu_percent']) for p in psutil.process_iter(['name', 'cpu_percent'])]
top_process = max(processes, key=lambda x: x[1], default=("Unknown", 0))
return f"{top_process[0]} ({top_process[1]}%)"

View file

@ -0,0 +1,22 @@
import subprocess
from logger import log_message
def check_gpu_usage():
"""Check AMD GPU usage using rocm-smi or radeontop if available."""
try:
result = subprocess.run(["rocm-smi", "--showuse"], capture_output=True, text=True)
for line in result.stdout.split('\n'):
if "GPU use" in line:
usage = int(line.split()[-1].strip('%'))
if usage > 90:
log_message("GPU overloaded")
except FileNotFoundError:
try:
result = subprocess.run(["radeontop", "-d", "-", "-l", "1"], capture_output=True, text=True)
for line in result.stdout.split('\n'):
if "gpu" in line:
usage = int(line.split()[1].strip('%'))
if usage > 90:
log_message("GPU overloaded")
except FileNotFoundError:
log_message("AMD GPU monitoring tool not found")

View file

@ -0,0 +1,10 @@
import subprocess
from logger import log_message
def log_package_updates():
"""Log the number of package updates."""
result = subprocess.run(["dnf", "history", "list", "updates", "--reverse"], capture_output=True, text=True)
updates = result.stdout.split('\n')[1:-1]
if updates:
num_updates = len(updates)
log_message(f"Updated {num_updates} packages")

View file

@ -0,0 +1,20 @@
import os
from datetime import datetime
from logger import log_message
suspend_start = None
last_suspend_time = None
def log_suspension():
"""Log time spent in suspension."""
global suspend_start, last_suspend_time
if os.path.exists('/var/log/pm-suspend.log'):
suspend_log_time = os.path.getmtime('/var/log/pm-suspend.log')
if last_suspend_time is None or suspend_log_time > last_suspend_time:
if suspend_start is None:
suspend_start = datetime.fromtimestamp(suspend_log_time)
else:
suspend_duration = (datetime.fromtimestamp(suspend_log_time) - suspend_start).total_seconds() / 60
log_message(f"Spent {suspend_duration:.2f} minutes in suspension")
suspend_start = None
last_suspend_time = suspend_log_time

13
src/logger.py Normal file
View file

@ -0,0 +1,13 @@
from datetime import datetime
def log_message(message):
"""Logs a message with a timestamp."""
with open("system_health_log.txt", "a") as log_file:
log_file.write(f"{datetime.now()}: {message}\n")
print(message)
post_to_misskey(message)
def post_to_misskey(message):
"""Posts a message to Misskey."""
# Placeholder for Misskey posting functionality
pass

31
src/main.py Normal file
View file

@ -0,0 +1,31 @@
from os import environ
import time
from logger import log_message, post_to_misskey
from src.messages.quirky_messages import add_quirky_messages
from checks.check_battery import check_battery
from checks.check_boot_errors import check_boot_errors
from checks.check_cpu_usage import check_cpu_usage
from checks.check_gpu_usage import check_gpu_usage
from checks.log_package_updates import log_package_updates
from checks.log_suspension import log_suspension
def main():
log_message("Starting system health monitoring,, ")
try:
while True:
check_boot_errors()
check_battery()
check_cpu_usage()
check_gpu_usage()
log_suspension()
log_package_updates()
add_quirky_messages()
log_message("System health check complete,, ")
# Optionally, post to Misskey
post_to_misskey("Performed system health check.")
time.sleep(300) # Wait for 5 minutes before next check
except KeyboardInterrupt:
log_message("Monitoring stopped by " + environ.get("BOT_OWNER") + " ,,")
if __name__ == "__main__":
main()

0
src/messages/__init__.py Normal file
View file

View file

@ -0,0 +1,12 @@
from datetime import datetime
from logger import log_message
def add_quirky_messages():
"""Add some quirky messages."""
quirky_messages = [
"Feeling a bit slow today,, need some coffee?",
"Why do programmers prefer dark mode? Because light attracts bugs!",
"If you see me lagging, just blame it on a solar flare!",
]
message = quirky_messages[datetime.now().second % len(quirky_messages)]
log_message(message)

0
src/system_checks.py Normal file
View file