Start a conversation

A script which monitors the network interfaces of a proxmox host, if a network connection goes up or down a mail should be send

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 update
apt install -y mailutils
systemctl daemon-reload
systemctl enable --now interface-monitor
systemctl status interface-monitor


TEST

ip link set enp2s0f1np1 down && sleep 5 && ip link set enp2s0f1np1 up


Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Philippe Van Impe

  2. Posted
  3. Updated

Comments