#!/bin/bash # idle_measurement.sh # Usage: ./idle_measurement.sh output.csv OUTPUT_FILE="$1" if [[ -z "$OUTPUT_FILE" ]]; then echo "Usage: $0 output_file.csv" exit 1 fi echo "Sample,CPU_idle_percent,Mem_available_MB" > "$OUTPUT_FILE" echo "Starting idle measurement for 60 samples (1 per second)" echo "" CPU_TOTAL=0 MEM_TOTAL=0 SAMPLES=60 for i in $(seq 1 $SAMPLES); do # mpstat waits 1 second and returns average for that interval CPU_IDLE=$(mpstat 1 1 | awk '/Average/ && $NF ~ /[0-9.]+/ {print $NF}') MEM_AVAILABLE=$(free -m | awk '/^Mem:/ {print $7}') CPU_TOTAL=$(echo "$CPU_TOTAL + $CPU_IDLE" | bc) MEM_TOTAL=$(echo "$MEM_TOTAL + $MEM_AVAILABLE" | bc) echo "$i,$CPU_IDLE,$MEM_AVAILABLE" >> "$OUTPUT_FILE" printf "Sample %2d: CPU idle = %5.1f%% | Available memory = %6d MB\n" "$i" "$CPU_IDLE" "$MEM_AVAILABLE" done CPU_AVG=$(echo "scale=2; $CPU_TOTAL / $SAMPLES" | bc) MEM_AVG=$(echo "scale=2; $MEM_TOTAL / $SAMPLES" | bc) echo "" echo "Results saved to: $OUTPUT_FILE" echo "Average CPU idle: $CPU_AVG%" echo "Average Free Memory: $MEM_AVG MB"