From 256e1c24308529646f1c662016edc434696b6a77 Mon Sep 17 00:00:00 2001 From: PTah Date: Tue, 14 Apr 2026 07:59:52 +1000 Subject: [PATCH] =?UTF-8?q?feat(heartbeat):=20=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=BA=D0=B0=20=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80=D0=B0=20?= =?UTF-8?q?=D1=81=20=D0=BE=D1=81=D0=BD=D0=BE=D0=B2=D0=BD=D1=8B=D0=BC=20IPv?= =?UTF-8?q?4=20=D0=B2=20=D1=81=D0=BA=D0=BE=D0=B1=D0=BA=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ssh-monitor | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/ssh-monitor b/ssh-monitor index 14a6ed3..8b67fb6 100644 --- a/ssh-monitor +++ b/ssh-monitor @@ -1249,6 +1249,28 @@ check_daily_report() { # Heartbeat # ============================================ +# Для уведомлений: «hostname (IPv4)»; без доступного IPv4 — только hostname. +server_display_name_with_ip() { + local hn ip + hn=$(hostname 2>/dev/null | tr -d '\r\n') + [ -z "$hn" ] && hn="(unknown)" + ip="" + if command -v ip >/dev/null 2>&1; then + ip=$(ip -4 route get 1.1.1.1 2>/dev/null | awk '/\bsrc\b/ {for (i = 1; i < NF; i++) if ($i == "src") { print $(i + 1); exit }}') + [ -z "$ip" ] && ip=$(ip -4 route get 8.8.8.8 2>/dev/null | awk '/\bsrc\b/ {for (i = 1; i < NF; i++) if ($i == "src") { print $(i + 1); exit }}') + fi + if [ -z "$ip" ] && command -v hostname >/dev/null 2>&1; then + if hostname -I >/dev/null 2>&1; then + ip=$(hostname -I 2>/dev/null | tr ' ' '\n' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' | grep -Ev '^(127\.|0\.0\.0\.0)$' | head -n1) + fi + fi + if [ -n "$ip" ]; then + printf '%s (%s)' "$hn" "$ip" + else + printf '%s' "$hn" + fi +} + send_heartbeat() { local timestamp timestamp=$(date '+%s') @@ -1256,7 +1278,7 @@ send_heartbeat() { timestamp_human=$(date '+%d.%m.%Y %H:%M:%S') local message=" ❤️ Heartbeat - скрипт мониторинга работает "$'\n' - message="${message}🖥️ Сервер: $(hostname)"$'\n' + message="${message}🖥️ Сервер: $(server_display_name_with_ip)"$'\n' message="${message}🕐 Время: ${timestamp_human}" notify_send "$message"