init
This commit is contained in:
59
monitor_logic.py
Normal file
59
monitor_logic.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import paramiko
|
||||
import requests
|
||||
import time
|
||||
import re
|
||||
|
||||
def get_ssh_data(host, port, user, password):
|
||||
try:
|
||||
ssh = paramiko.SSHClient()
|
||||
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||||
ssh.connect(host, port=port, username=user, password=password, timeout=10)
|
||||
|
||||
# CPU Usage
|
||||
stdin, stdout, stderr = ssh.exec_command("top -bn1 | grep 'Cpu(s)' | sed 's/.*, *\\([0-9.]*\\)%* id.*/\\1/' | awk '{print 100 - $1}'")
|
||||
cpu_usage = stdout.read().decode().strip()
|
||||
|
||||
# CPU Temp
|
||||
# For Ubuntu Server
|
||||
stdin, stdout, stderr = ssh.exec_command("cat /sys/class/thermal/thermal_zone0/temp 2>/dev/null || vcgencmd measure_temp 2>/dev/null")
|
||||
temp_raw = stdout.read().decode().strip()
|
||||
if "temp=" in temp_raw:
|
||||
cpu_temp = temp_raw.replace("temp=", "").replace("'C", "")
|
||||
elif temp_raw:
|
||||
cpu_temp = str(float(temp_raw) / 1000)
|
||||
else:
|
||||
cpu_temp = "N/A"
|
||||
|
||||
# RAM Status
|
||||
stdin, stdout, stderr = ssh.exec_command("free -m | awk 'NR==2{printf \"%.2f\", $3*100/$2 }'")
|
||||
ram_usage = stdout.read().decode().strip()
|
||||
|
||||
# Disk Space
|
||||
stdin, stdout, stderr = ssh.exec_command("df -h / | awk 'NR==2{print $5}' | sed 's/%//'")
|
||||
disk_usage = stdout.read().decode().strip()
|
||||
|
||||
ssh.close()
|
||||
return {
|
||||
"status": "online",
|
||||
"cpu": cpu_usage,
|
||||
"temp": cpu_temp,
|
||||
"ram": ram_usage,
|
||||
"disk": disk_usage
|
||||
}
|
||||
except Exception as e:
|
||||
return {
|
||||
"status": "offline",
|
||||
"error": str(e)
|
||||
}
|
||||
|
||||
def check_web_service(url):
|
||||
try:
|
||||
start_time = time.time()
|
||||
response = requests.get(url, timeout=5)
|
||||
latency = int((time.time() - start_time) * 1000)
|
||||
if response.status_code == 200:
|
||||
return {"status": "online", "latency": f"{latency}ms"}
|
||||
else:
|
||||
return {"status": "error", "code": response.status_code}
|
||||
except Exception:
|
||||
return {"status": "offline"}
|
||||
Reference in New Issue
Block a user