How to Continuously Monitor Linux Server Resources During Load Tests with Python
Real‑time monitoring of CPU, memory, disk I/O and network bandwidth during performance testing helps identify bottlenecks, prevent crashes, collect actionable data, ensure accurate results, and meet SLA requirements, and a Python script can automate data collection and storage in MySQL.
Why Real‑Time Server Monitoring Matters
During performance testing, continuously tracking CPU, memory, disk I/O and network bandwidth helps you locate bottlenecks, prevent crashes, collect data for analysis, ensure accurate test results, and meet SLA requirements.
Identify bottlenecks : By observing resource usage you can pinpoint which component limits performance.
Avoid crashes : Early detection of resource exhaustion lets you add capacity, optimise code, or adjust configurations before the system fails.
Data for analysis : Collected metrics reveal usage trends, inform scaling decisions, and evaluate system elasticity under different loads.
Accurate results : Ignoring server constraints can skew response‑time measurements; monitoring guarantees that test outcomes reflect true application behaviour.
SLA compliance : Monitoring ensures that the system stays within the service‑level agreements required for high‑availability applications.
Python Script for Real‑Time Metrics and MySQL Storage
The script below uses paramiko to SSH into one or more Linux hosts, runs standard commands to fetch CPU, memory and disk I/O usage, and writes the values into a MySQL table named server_resource_data. Adjust the servers list and db_config to match your environment.
import paramiko
import pymysql
def ssh_to_server(server, username, password):
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(server, username=username, password=password)
return client
def get_cpu_usage(client):
stdin, stdout, stderr = client.exec_command(
"top -bn1 | grep 'Cpu(s)' | \
sed 's/.*, *\\([0-9.]*\\)%* id.*/\\1/' | awk '{print 100 - $1}'")
return float(stdout.read().decode().strip())
def get_memory_usage(client):
stdin, stdout, stderr = client.exec_command(
"free -m | awk 'NR==2{printf \"%.2f%\", $3*100/$2 }'")
return float(stdout.read().decode().strip())
def get_disk_io_usage(client):
stdin, stdout, stderr = client.exec_command(
"iostat -dx 1 1 | awk '/^x/{print $(NF-2)}' | head -n 1")
return float(stdout.read().decode().strip())
if __name__ == "__main__":
servers = [
{"address": "server1.example.com", "username": "user1", "password": "pass1"},
{"address": "server2.example.com", "username": "user2", "password": "pass2"},
# add more servers as needed
]
db_config = {
"host": "localhost",
"port": 3306,
"user": "your_database_user",
"password": "your_database_password",
"database": "your_database_name",
}
connection = pymysql.connect(**db_config)
try:
with connection.cursor() as cursor:
for server_info in servers:
server = server_info["address"]
username = server_info["username"]
password = server_info["password"]
client = ssh_to_server(server, username, password)
cpu_usage = get_cpu_usage(client)
memory_usage = get_memory_usage(client)
disk_io_usage = get_disk_io_usage(client)
insert_query = f"INSERT INTO server_resource_data (server, cpu_usage, memory_usage, disk_io_usage) VALUES ('{server}', {cpu_usage}, {memory_usage}, {disk_io_usage})"
cursor.execute(insert_query)
print(f"Server: {server}")
print(f"CPU usage: {cpu_usage}%")
print(f"Memory usage: {memory_usage}%")
print(f"Disk IO usage: {disk_io_usage}%")
client.close()
connection.commit()
finally:
connection.close()Run the script before or during a load test to capture live metrics, store them for later analysis, and react quickly if any resource approaches its limit.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
