05-03-26

Network Ping Monitor with Audio Alert

This Lua script for Rebind hardware monitors network ping and alerts you with audio when latency spikes. Configure the ping target, threshold, and alert volume using the built-in UI.

rebindlua scriptnetwork monitorpinglatencyaudio alertscript

Question

Network ping monitor with audio alert on spike

Rebind AI

Network Ping Monitor with Audio Alert

luau
-- rebind: min_sdk=0.1.0
-- rebind: name=Network Ping Monitor
-- rebind: permission=exec

local cfg = UI.Schema({
    enabled = UI.Toggle(true, { label = "Enable Monitor" }),
    host = UI.Text("8.8.8.8", { label = "Ping Target", maxLength = 255 }),
    interval = UI.Slider(1000, { min = 500, max = 5000, suffix = "ms", label = "Check Interval" }),
    threshold = UI.Slider(100, { min = 10, max = 500, suffix = "ms", label = "Spike Threshold" }),
    alert_volume = UI.Slider(0.7, { min = 0, max = 1, label = "Alert Volume" }),
})

local lastPing = nil
local monitorTimer = nil
local lastAlertTime = 0
local ALERT_COOLDOWN = 2000 -- minimum ms between alerts

local function isWindows()
    local result = System.Exec("ver")
    return result.exit == 0
end

local function getPing()
    local cmd
    if isWindows() then
        cmd = string.format("ping -n 1 -w 1000 %s", cfg.host)
    else
        cmd = string.format("ping -c 1 -W 1000 %s", cfg.host)
    end

    local result = System.Exec(cmd, { timeout = 3000 })

    if result.exit ~= 0 then
        return nil
    end

    -- Extract ping time (milliseconds)
    local pattern = isWindows() and "time=(%d+)ms" or "time=([%d.]+) ms"
    local match = Regex.Find(result.stdout, pattern)

    if match and match.captures[1] then
        return tonumber(match.captures[1])
    end

    return nil
end

local function triggerAlert(ping)
    local now = System.Time()
    if now - lastAlertTime < ALERT_COOLDOWN then
        return
    end

    Audio.Play("alert.wav", { volume = cfg.alert_volume })
    UI.Notify(string.format("Ping spike! %.0fms (threshold: %.0fms)", ping, cfg.threshold), "warning")
    Log.Warn(string.format("Ping spike detected: %.0fms", ping))

    lastAlertTime = now
end

function OnStart()
    if not File.Exists("alert.wav") then
        Log.Warn("alert.wav not found in script directory - audio alerts disabled")
    end

    monitorTimer = Timer.Every(cfg.interval, function()
        if not cfg.enabled then
            return
        end

        Run(function()
            local ping = getPing()

            if ping then
                Log.Debug(string.format("Ping: %.0fms", ping))

                if lastPing and ping > lastPing + cfg.threshold then
                    triggerAlert(ping)
                end

                lastPing = ping
            else
                Log.Debug("Ping failed or timeout")
            end
        end)
    end)

    Log.Info("Ping monitor started - target: " .. cfg.host)
end

function OnStop()
    if monitorTimer then
        monitorTimer:Cancel()
    end
    Audio.StopAll()
end

function OnBlur()
    Audio.StopAll()
end

How It Works

This script continuously pings a network host and triggers an audio alert when latency spikes above your configured threshold.

Key features:

  • Cross-platform: Uses native ping commands (Windows via ping -n, Linux/Mac via ping -c)
  • Async monitoring: Runs ping checks in coroutines so they don't block input processing
  • Spike detection: Compares current ping to the last recorded ping; alerts only when the spike exceeds your threshold
  • Alert cooldown: Prevents alert spam — waits 2 seconds between alerts even if spikes occur rapidly
  • UI controls: Adjust target host, check interval, spike threshold, and alert volume without editing code
  • Configurable timeout: Each ping waits up to 1 second for a response

Config options:

  • Ping Target: IP or hostname (default: Google DNS at 8.8.8.8)
  • Check Interval: How often to ping (500ms–5000ms)
  • Spike Threshold: Alert when current ping exceeds last ping by this amount
  • Alert Volume: Volume for audio alert (0–1)

Audio setup:

Place an alert.wav file in your script directory. If missing, the monitor still works but logs a warning and disables audio. Use any short alert sound (beep, chime, etc.).

Typical usage:

  • Gaming: Monitor your ISP connection and alert on latency spikes
  • Streaming: Detect network issues that could cause buffering
  • Work-from-home: Track connection stability during important calls

Learn more: