Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Systemd-resolved 100% CPU usage #76

Open
FalsePhilosopher opened this issue Nov 8, 2024 · 0 comments
Open

Systemd-resolved 100% CPU usage #76

FalsePhilosopher opened this issue Nov 8, 2024 · 0 comments

Comments

@FalsePhilosopher
Copy link

FalsePhilosopher commented Nov 8, 2024

Ubuntu ARM port running on a RPI 5, when the connection gets spotty systemd-resolved goes nuts and network connectivity drops out. On X86 linux I don't see that behavior, but on the RPI I can hear the fan going nuts once a day. I tried every bit of piactl connect and sudo systemctl restart systemd-networkd or restart systemd-resolved to solve the issue and get the VPN to reconnect. I eventually settled on a cron job script to detect high systemd-resolved usage and reboot.
sudo touch /usr/local/bin/monitor_systemd_resolved.sh
sudo nano /usr/local/bin/monitor_systemd_resolved.sh

#!/bin/bash

# CPU usage threshold for systemd-resolved (90%)
THRESHOLD=90
COUNTER_FILE="/tmp/systemd_resolved_counter.txt"

# Log file
LOG_FILE="/var/log/systemd_resolved_monitor.log"

# Function to clear the log file weekly on Sunday
clear_log_weekly() {
    local day_of_week
    day_of_week=$(date +%u)  # Get the day of the week (1=Monday, 7=Sunday)
    
    if [[ "$day_of_week" -eq 7 ]]; then
        echo "$(date): Clearing log file for weekly maintenance." > "$LOG_FILE"
    fi
}

# Read or initialize the counter
if [[ -f "$COUNTER_FILE" ]]; then
    COUNTER=$(<"$COUNTER_FILE")
else
    COUNTER=0
fi

# Clear log if today is Sunday
clear_log_weekly

# Check CPU usage of systemd-resolved
USAGE=$(ps -C systemd-resolved -o %cpu= | awk '{print int($1)}')

# Increment or reset the counter based on usage
if [[ "$USAGE" -ge "$THRESHOLD" ]]; then
    ((COUNTER++))
    echo "$(date): CPU usage at $USAGE%. Counter incremented to $COUNTER." >> "$LOG_FILE"
else
    COUNTER=0
    echo "$(date): CPU usage at $USAGE%. Counter reset to 0." >> "$LOG_FILE"
fi

# Save the updated counter
echo "$COUNTER" > "$COUNTER_FILE"

# If counter reaches 3 (approx. 15 minutes of high usage), reboot
if [[ "$COUNTER" -ge 3 ]]; then
    echo "$(date): High CPU usage detected for systemd-resolved. Rebooting..." >> "$LOG_FILE"
    rm "$COUNTER_FILE"  # Reset the counter on reboot
    reboot
fi

crontab -e
*/5 * * * * /usr/local/bin/monitor_systemd_resolved.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant