How devusb/pingshutdown works
Purpose and Deployment
pingshutdown is a Go daemon that protects a host by monitoring reachability of a remote target and shutting the local machine down after sustained ping failure. It is designed for systemd or NixOS deployment, with optional Pushover alerting and a local status/control HTTP interface.
Entry point: cmd/pingshutdown/main.go. System service assets: init/pingshutdown.service, init/pingshutdown.env. Nix packaging/module integration: flake.nix, module.nix, package.nix.
Process Bootstrap and Configuration
The daemon reads environment-driven operational settings: ping target, failure delay, dry-run mode, notification credentials/enablement, and HTTP status port. Configuration is intentionally supplied outside the binary through a systemd EnvironmentFile or NixOS service settings.
Primary bootstrap/control wiring resides in cmd/pingshutdown/main.go; deployment supplies values through init/pingshutdown.env and module.nix.
Reachability Monitoring and Shutdown Decision
The main loop repeatedly probes the configured remote host. A reachable target represents normal operation. Consecutive or sustained failure transitions the service into a pending-shutdown state rather than immediately invoking system shutdown.
The monitoring orchestration is centered in cmd/pingshutdown/main.go. The countdown abstraction is in pkg/countdown/countdown.go, separating timeout/state handling from probe and shutdown side effects.
Countdown State Machine and Safety Controls
pkg/countdown/countdown.go owns the delayed-action lifecycle: starting a configured shutdown delay after failure, exposing whether it is active, and allowing cancellation/lockout when connectivity returns or an operator disables shutdown. This is the core safety boundary between transient network loss and destructive host shutdown.
The HTTP handlers in cmd/pingshutdown/handlers.go expose this state and provide the operator-facing lockout/control path. Dry-run configuration preserves monitoring, countdown, status, and notification behavior while suppressing the actual shutdown side effect.
HTTP Status and Operator Boundary
The daemon serves a small local web interface on the configured status port. It reports timer state and enables temporary disabling of shutdown functionality, supporting maintenance windows or manual intervention.
HTTP endpoint implementation is in cmd/pingshutdown/handlers.go, with server setup and shared runtime state assembled in cmd/pingshutdown/main.go. This interface is a control boundary: it changes shutdown eligibility but does not replace reachability monitoring.
Notifications and External Services
Optional Pushover notifications are emitted when a pending shutdown begins. The Pushover client integration is isolated in pkg/pushover/pushover.go; application token and recipient user key arrive through environment configuration rather than source-controlled settings.
External dependencies are the monitored host’s ICMP/ping responsiveness, the local OS shutdown mechanism, optional Pushover APIs, and systemd/NixOS for service lifecycle management.
Runtime and Packaging Boundaries
The runtime is a Go executable built from cmd/pingshutdown. It expects host-level privileges/capabilities appropriate for invoking shutdown and is intended to be supervised by systemd via init/pingshutdown.service.
Nix consumers use the flake/module path (flake.nix, module.nix) to install the package and declare the service. The application remains a host daemon, not a general web application: HTTP is operational status/control only.
Main entry points: main.go Daemon bootstrap · pingshutdown.service systemd service · module.nix NixOS module · handlers.go Status and control handlers
Components 14
Go daemon
- Daemon bootstrap · Assembles configuration, monitoring, HTTP server, and shutdown behavior.
cmd/pingshutdown/main.go - Status and control handlers · Reports countdown state and lets operators disable shutdown.
cmd/pingshutdown/handlers.go - Countdown state machine · Delays shutdown and supports cancellation or lockout.
pkg/countdown/countdown.go - Pushover client
pkg/pushover/pushover.go - pingshutdown executable
cmd/pingshutdown
Deployment
- systemd service
init/pingshutdown.service - Service environment · Supplies target, delays, dry-run, HTTP, and notification settings.
init/pingshutdown.env - Nix flake
flake.nix - NixOS module
module.nix - Nix package
package.nix
Host and external services
- Monitored remote host
- Local operator
- Local OS shutdown
- Pushover API
Connections 18
- Nix package → pingshutdown executable: builds
- Nix flake → Nix package: exposes
- Nix flake → NixOS module: exports
- NixOS module → systemd service: declares
- NixOS module → Service environment: supplies settings
- systemd service → pingshutdown executable: runs
- systemd service → Service environment: loads
- pingshutdown executable → Daemon bootstrap: starts
- Service environment → Daemon bootstrap: configures
- Daemon bootstrap → Monitored remote host: repeatedly pings
- Daemon bootstrap → Countdown state machine: starts or cancels on reachability
- Daemon bootstrap → Status and control handlers: serves shared state
- Local operator → Status and control handlers: views status / sets lockout
- Status and control handlers → Countdown state machine: reads and controls
- Daemon bootstrap → Pushover client: notifies on pending shutdown
- Pushover client → Pushover API: sends alert
- Countdown state machine → Daemon bootstrap: signals delay expiry
- Daemon bootstrap → Local OS shutdown: invokes shutdown unless dry-run