#!/bin/sh

# Log file
OUTPUT="/4server/data/log/cpu_idle.log"

# Sampling interval in seconds
INTERVAL=60

while true; do
    DATA=""  # buffer to store idle samples

    # Current date for the measurement period
    DATE=$(date "+%Y-%m-%d")

    echo "Starting measurement for $DATE"

    while [ "$(date +%H:%M)" != "23:45" ]; do
        # Get idle CPU percentage
        IDLE=$(mpstat 1 1 | awk '/Average/ {print $12}')

        # Append to buffer
        DATA="$DATA$IDLE\n"

        sleep $INTERVAL
    done

    # Write all data to log file with date
    # Only one line per day: Date + space-separated idle samples
    echo -n "$DATE " >> "$OUTPUT"
    echo -e "$DATA" | tr '\n' ' ' >> "$OUTPUT"
    echo >> "$OUTPUT"  # newline at the end
    echo "Measurement for $DATE written to $OUTPUT"

    # Wait until 00:15 to start next day
    while [ "$(date +%H:%M)" != "00:15" ]; do
        sleep 30
    done
done

