Certainly! Adding a random fluctuation to the CPU utilization target can make the artificial load appear more natural and less detectable. This can be achieved by modifying the script to dynamically adjust the target CPU utilization within a specified range around the base target (25%).
Here's how you can adapt the script to include a specified percentage of additional random CPU fluctuation:
Step 1: Modify the Script to Include Random Fluctuation
Update the /usr/local/bin/dynamic_cpu_load.sh script with the following changes:
#!/bin/bash
# --- Configuration ---
BASE_TARGET_MIN_TOTAL_CPU_UTIL=25 # Base target minimum total CPU utilization (percentage)
FLUCTUATION_PERCENTAGE=10 # Percentage of fluctuation around the base target
CHECK_INTERVAL_SECONDS=10 # How often to check and adjust load (seconds)
NICE_LEVEL=19 # Lowest CPU scheduling priority
# --- Logic ---
# Function to get current overall CPU utilization (100 - idle percentage)
get_current_cpu_utilization() {
# Get idle percentage from vmstat.
# vmstat first line is average since boot, second line is current sample.
# We take the second sample after a 1-second interval.
# $15 is the 'id' (idle) column in vmstat output.
IDLE_CPU=$(vmstat 1 2 | tail -1 | awk '{print $15}')
CURRENT_UTILIZATION=$(echo "100 - $IDLE_CPU" | bc)
echo "$CURRENT_UTILIZATION"
}
# Function to generate a random fluctuation within the specified percentage
generate_random_fluctuation() {
local fluctuation_percentage=$1
local base_target=$2
local fluctuation=$(echo "$fluctuation_percentage * 0.01" | bc)
local random_value=$(echo "$RANDOM % 100" | bc)
local fluctuation_amount=$(echo "$fluctuation * $base_target * ($random_value / 100)" | bc)
local adjusted_target=$(echo "$base_target + $fluctuation_amount" | bc)
echo "$adjusted_target"
}
echo "Dynamic CPU Load Manager started."
echo "Base target minimum CPU: ${BASE_TARGET_MIN_TOTAL_CPU_UTIL}%"
echo "Fluctuation percentage: ${FLUCTUATION_PERCENTAGE}%"
echo "Check interval: ${CHECK_INTERVAL_SECONDS}s"
echo "Nice level: ${NICE_LEVEL}"
# Ensure no orphaned stress-ng processes from previous runs if script was killed abruptly
pkill -f "stress-ng --cpu 0 --cpu-load"
while true; do
CURRENT_CPU_UTIL=$(get_current_cpu_utilization)
CURRENT_CPU_UTIL_INT=${CURRENT_CPU_UTIL%.*} # Convert to integer for bash comparison
echo "$(date): Current CPU Util: ${CURRENT_CPU_UTIL}%"
# Generate a random fluctuation around the base target
TARGET_MIN_TOTAL_CPU_UTIL=$(generate_random_fluctuation "$FLUCTUATION_PERCENTAGE" "$BASE_TARGET_MIN_TOTAL_CPU_UTIL")
# Check if we need to add artificial load
if (( CURRENT_CPU_UTIL_INT < TARGET_MIN_TOTAL_CPU_UTIL )); then
# Calculate the potential load stress-ng should attempt to generate.
LOAD_TO_GENERATE=$TARGET_MIN_TOTAL_CPU_UTIL
echo "$(date): Below target. Current: ${CURRENT_CPU_UTIL}%. Adding load (aiming for ${LOAD_TO_GENERATE}% on workers) for ${CHECK_INTERVAL_SECONDS}s."
# Run stress-ng at the lowest priority.
nice -n "$NICE_LEVEL" stress-ng --cpu 0 --cpu-load "$LOAD_TO_GENERATE" --timeout "${CHECK_INTERVAL_SECONDS}s" --quiet &
else
echo "$(date): At or above target. Current: ${CURRENT_CPU_UTIL}%. No artificial load needed."
fi
# Wait for the next check interval.
sleep "$CHECK_INTERVAL_SECONDS"
done
Explanation of Changes:
FLUCTUATION_PERCENTAGE: This variable defines the percentage of fluctuation around the base target CPU utilization.
generate_random_fluctuation function: This function calculates a random fluctuation within the specified percentage of the base target.
- It calculates the fluctuation amount as a percentage of the base target.
- It generates a random value between 0 and 100 to determine the direction and magnitude of the fluctuation.
- It adjusts the target CPU utilization by adding the fluctuation amount.
TARGET_MIN_TOTAL_CPU_UTIL: This variable is now dynamically adjusted using the generate_random_fluctuation function.
How the Fluctuation Works:
- If
FLUCTUATION_PERCENTAGE is 10%, the target CPU utilization will fluctuate between 22.5% and 27.5% (25% ± 10% of 25%).
- The
generate_random_fluctuation function ensures that the target CPU utilization is randomly adjusted within this range in each iteration.
Why This Helps:
- By adding random fluctuation, the CPU utilization will not be consistently at 25%, making it less obvious that the load is artificially generated.
- The fluctuation makes the CPU utilization appear more natural and less predictable.
Step 2: Reload and Restart the Systemd Service
After updating the script, reload the systemd daemon and restart the service to apply the changes:
sudo systemctl daemon-reload
sudo systemctl restart dynamic-cpu-loader.service
Step 3: Monitor and Verify
- Check service status:
sudo systemctl status dynamic-cpu-loader.service
- Tail the logs:
tail -f /var/log/dynamic_cpu_load.log
tail -f /var/log/dynamic_cpu_load.error.log
- Observe CPU utilization using
top or htop. You should see:
- When overall CPU usage is low,
stress-ng processes will appear (running with nice 19).
- The total CPU utilization should fluctuate around the base target (25%) within the specified fluctuation range (e.g., 22.5% to 27.5%).
- If you manually start a CPU-intensive task, the overall CPU usage will rise. In the next check interval, the script should detect this, and
stress-ng might not be launched, or its contribution will be minimal because it yields to the higher-priority task.
Summary:
By adding a random fluctuation to the target CPU utilization, you make the artificial load appear more natural and less detectable. This approach helps ensure that your VM meets the OCI requirement while making the CPU utilization data less indicative of artificial load generation.