Truth be told. I haven't done anything new to fix the problem, but its become rare enough that I haven't tried anything new (looks like the last time the ethernet went down was six months ago). Since I failed to previously, I've attached the code for my userscript to this post. I modified another script (author Marc Gutt, but I don't remember what the script was originally). The script checks every hour if the Ethernet is down, and if it is down, it restarts the server. It also saves the syslog anytime the ethernet goes down.
#!/bin/bash
# #####################################
# Name: NIC bandaid
# Description: Script to restart the server if the ethernet adapter goes down. Also saves the syslog file for reference.
# Author: *** - modified from script by Marc Gutt
# #####################################
# #####################################
# Settings
# #####################################
# get most recent syslog file
syslog_file=$(ls -t /var/log/syslog{.[0-9],} 2>/dev/null | head -n 1)
# The error message to look for in the syslog
words="eth0: PCIe link lost, device now detached"
# store line number of last found error in this file
log_file="/tmp/syslog-notify-last-error-line-number.log"
# store the time & date of a shutdown on a shared folder which can be viewed later for reference.
log_file_shutdown="/mnt/user/appdata/_nic_bandaid/nic_restarts.log"
log_file_syslog="/mnt/user/appdata/_nic_bandaid/syslog_"
# log what is being done
verbose=false
# #####################################
# Script
# #####################################
# make script race condition safe
if [[ -d "/tmp/${0//\//_}" ]] || ! mkdir "/tmp/${0//\//_}"; then echo "Script is already running!" && exit 1; fi; trap 'rmdir "/tmp/${0//\//_}"' EXIT;
# check user settings
[[ $verbose == 0 ]] || [[ $verbose == false ]] && unset verbose
# obtain line number of last check
if [[ -f "$log_file" ]]; then
line_number_start=$(cat "$log_file")
# syslog has been truncated
if [[ $line_number_start -gt $(grep -c ^ "$syslog_file") ]]; then
[[ $verbose ]] && echo "Monitoring of a new syslog file begins"
line_number_start=0
fi
# store last line number on first execution
else
line_number_start=$(grep -c ^ "$syslog_file")
line_number_start=$((line_number_start-100))
[[ $verbose ]] && echo "Monitoring syslog starts from line $line_number_start"
echo "$line_number_start" > "$log_file"
fi
# parse logs
EOL=$'\n'
errors=""
while read -r line; do
# remember last line
last_line="$line"
# combine multiple error messages
errors="$errors$EOL$line"
done < <(tail -n +"$((line_number_start+1))" "$syslog_file" | grep -iP "($words)")
# create notification for new errors
if [[ $errors ]]; then
# remember line number of last error
line_number_start=$(grep -nFx "$last_line" "$syslog_file" | cut -f 1 -d ":")
[[ $verbose ]] && echo "Monitoring syslog continues from line $line_number_start"
echo "$line_number_start" > "$log_file"
datetime=$(date)
datetime_safe=$(date +%F_%H-%M-%S)
# send notification
/usr/local/emhttp/webGui/scripts/notify -i "alert" -s "NIC down" -d "eth0 appears lost as of $datetime! Prepping to restart the server!"
# copy the syslog to the set destinateion & write to the log -fiel
log_file_syslog_name="$log_file_syslog$datetime_safe"
cp /var/log/syslog "$log_file_syslog_name"
echo "System to be restarted as NIC was found down on: $datetime. See $log_file_syslog_name"
echo "System to be restarted as NIC was found down on: $datetime. See $log_file_syslog_name" >> "$log_file_shutdown"
powerdown -r
exit
else
# store last line number if no error has been found
line_number_start=$(grep -c ^ "$syslog_file")
[[ $verbose ]] && echo "Monitoring syslog continues from last line $line_number_start"
echo "$line_number_start" > "$log_file"
datetime=$(date)
echo "NIC operational on: $datetime."
echo "NIC operational on: $datetime." >> "$log_file_shutdown"
fi
# create notificaton if log exceeds usage of 90%
log_size=$(df | grep -oP "[0-9]+(?=% /var/log)")
if [[ ! -f /tmp/syslog-notify.size ]] && [[ $log_size -gt 90 ]]; then
touch /tmp/syslog-notify.size
/usr/local/emhttp/webGui/scripts/notify -i "alert" -s "log utilizes more than 90%!" -d "$(du -h /var/log/* | sort -h | tail)"
elif [[ -f /tmp/syslog-notify.size ]]; then
rm /tmp/syslog-notify.size
fi