'Python - Fetch system data from remote host with psutil and paramiko
I'm currently working on a simple monitoring project.
I want to have a dedicated monitoring server that checks several remote instances for cpu, memory and disk usage.
I've got two scripts running so far. One script on the monitoring server to connect to remote host and there execute another script to fetch system data. For alarm purpose I'd like the monitoring server to receive the fetched data from remote hosts, evaluate the data and send an email if certain limits are exceeded.
I can connect to the remote host and also execute a python script there (via paramiko lib), which then collects the data (via psutil)
How do I get this data back to the monitoring server?
I was thinking of writing it into a textfile, sending it over scp to the monitoring server, import it into the python evaluation script and then delete the textfile or setting up a database, write into the database from remote host and then retreive data on monitoring host for evaluation purpose.
Both options don't seem like best practice to me, but I'm lacking of alternatives. Can anyone give me some advice?
Here's the code snippets:
script runs on monitoring server to connect to remote host:
import paramiko
def connect_host():
host = "192.168.1.100"
port = 22
username = "pi"
password = "password"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)
stdout = ssh.exec_command('python3 /home/pi/Documents/data_remote_check.py')[1]
connect_host()
Script runs on remote host to collect data:
import psutil, socket
def check_system():
# Fetch data
hostname = socket.gethostname()
cpu_usage = psutil.cpu_percent(interval=1)
memory_usage = psutil.virtual_memory()
disk_usage = psutil.disk_usage('/')
# Process memory data
memory_total = memory_usage.total/1024**3
memory_free = memory_usage.free/1024**3
memory_used = memory_usage.used/1024**3
memory_used_percent = memory_usage.percent
# Process disk data
disk_total = disk_usage.total/1024**3
disk_free = disk_usage.free/1024**3
disk_used = disk_usage.used/1024**3
disk_used_percent = disk_usage.percent
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
