-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote_monitor.py
More file actions
66 lines (57 loc) · 2.28 KB
/
remote_monitor.py
File metadata and controls
66 lines (57 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import paramiko
import logging
import logstash
import time
from dotenv import load_dotenv
import os
# Load environment variables
load_dotenv()
LOGSTASH_HOST = os.getenv("LOGSTASH_HOST")
LOGSTASH_PORT = int(os.getenv("LOGSTASH_PORT"))
PRIVATE_KEY_PATH = os.getenv("PRIVATE_KEY_PATH")
# Read multiple hosts from environment variables
HOSTS = os.getenv("REMOTE_HOSTS").split(",")
USERNAME = os.getenv("USERNAME")
# Configure logging to push log to logstash
logger = logging.getLogger("RemoteSystemMonitor")
logger.setLevel(logging.INFO)
logstash_handler = logstash.TCPLogstashHandler(LOGSTASH_HOST, LOGSTASH_PORT, version=1)
logger.addHandler(logstash_handler)
def execute_remote_command(host, command):
"""Execute a command on the remote server via SSH using RSA authentication."""
try:
key = paramiko.RSAKey(filename=PRIVATE_KEY_PATH)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(host, 22, USERNAME, pkey=key)
stdin, stdout, stderr = client.exec_command(command)
output = stdout.read().decode().strip()
client.close()
return output
except Exception as e:
logger.error(f"Error executing remote command on {host}: {e}")
return None
def get_remote_metrics(host):
"""Retrieve CPU, memory, and disk usage from the remote server."""
metrics = {
"cpu_usage": execute_remote_command(host, "top -bn1 | grep 'Cpu(s)' | awk '{print $2}'"),
"memory_usage": execute_remote_command(host, "free | grep Mem | awk '{print $3/$2 * 100.0}'"),
"disk_usage": execute_remote_command(host, "df -h / | awk 'NR==2 {print $5}'")
}
return metrics
def log_remote_metrics():
"""Log system metrics from multiple remote servers and send logs to Logstash."""
while True:
for host in HOSTS:
metrics = get_remote_metrics(host)
if metrics:
logger.info({
"message": f"Metrics from {host}",
"host": host,
"cpu_usage": metrics["cpu_usage"],
"memory_usage": metrics["memory_usage"],
"disk_usage": metrics["disk_usage"]
})
time.sleep(5)
if __name__ == "__main__":
log_remote_metrics()