Friday, January 19, 2024

Monitor Unix server without any monitoring tool | Linux server monitoring from command mode | Script for server monitoring in performance testing

#!/bin/bash

# Specify the time duration for which you want to collect metrics
start_time="10:00:00"
end_time="11:00:00"

# Interval in seconds
interval=30

# Output file
output_file="metrics.txt"

# Loop to collect metrics every 30 seconds
current_time=$start_time
while [[ "$current_time" < "$end_time" ]]; do
    sar -u -r -d -n DEV -s "$current_time" -e "$current_time" >> "$output_file"
    current_time=$(date -d "$current_time + $interval seconds" +%H:%M:%S)
    sleep $interval
done

Save this script to a file, make it executable (chmod +x script_name.sh), and then run it. It will collect metrics using the sar command every 30 seconds within the specified time range and append the output to the metrics.txt file.

After the script runs, you can open the metrics.txt file in Excel or another tool for analysis. Keep in mind that running this script for an hour will generate a large amount of data, so ensure you have enough disk space for the output file.

No comments: