You will need to do these scripts on all ProxMox nodes.
Replace your@email.com with your email address:
cat >/usr/local/bin/interface-monitor.sh <<'EOF'
#!/bin/bash
EMAIL_TO="your@email.com"; HOSTNAME=$(hostname); STATE_DIR="/run/interface-monitor"; mkdir -p "$STATE_DIR"; send_alert(){ echo -e "Host: $HOSTNAME\nInterface: $1\nStatus: $2\nTime: $(date)\n" | mail -s "[Proxmox] Interface $1 $2 on $HOSTNAME" "$EMAIL_TO"; }; ip monitor link | while read -r line; do [[ "$line" =~ ^[0-9]+:\ ([^:]+): ]] || continue; IFACE=${BASH_REMATCH[1]}; IFACE=${IFACE%@*}; [[ "$IFACE" == "lo" ]] && continue; [[ ! "$IFACE" =~ ^(eno|ens|enp|eth|bond) ]] && continue; if [[ "$line" == *"state UP"* ]]; then NEWSTATE="UP"; elif [[ "$line" == *"state DOWN"* ]]; then NEWSTATE="DOWN"; else continue; fi; LASTFILE="$STATE_DIR/$IFACE"; LASTSTATE=$(cat "$LASTFILE" 2>/dev/null || echo UNKNOWN); if [[ "$LASTSTATE" != "$NEWSTATE" ]]; then echo "$NEWSTATE" > "$LASTFILE"; send_alert "$IFACE" "$NEWSTATE"; fi; done
EOF && chmod +x /usr/local/bin/interface-monitor.sh
cat >/etc/systemd/system/interface-monitor.service <<'EOF'
[Unit]
Description=Network Interface Monitor
After=network.target
[Service]
ExecStart=/usr/local/bin/interface-monitor.sh
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload && systemctl enable --now interface-monitor.service
``
apt install -y mailutils
systemctl enable --now interface-monitor
TEST
ip link set enp2s0f1np1 down && sleep 5 && ip link set enp2s0f1np1 up
Philippe Van Impe
Comments