# devusb/pingshutdown architecture

> Architecture diagram of the GitHub repository devusb/pingshutdown, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/devusb/pingshutdown
- Repository: https://github.com/devusb/pingshutdown

## Overview

#### 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: [`cmd/pingshutdown/main.go`](https://github.com/devusb/pingshutdown/blob/main/cmd/pingshutdown/main.go) (Daemon bootstrap), [`init/pingshutdown.service`](https://github.com/devusb/pingshutdown/blob/main/init/pingshutdown.service) (systemd service), [`module.nix`](https://github.com/devusb/pingshutdown/blob/main/module.nix) (NixOS module), [`cmd/pingshutdown/handlers.go`](https://github.com/devusb/pingshutdown/blob/main/cmd/pingshutdown/handlers.go) (Status and control handlers)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Go daemon"]
  node_main["Daemon bootstrap<br/>Go entrypoint<br/>[main.go]"]
  node_handlers["Status and control handlers<br/>HTTP handlers<br/>[handlers.go]"]
  node_countdown{{"Countdown state machine<br/>safety control<br/>[countdown.go]"}}
  node_pushover["Pushover client<br/>notification integration<br/>[pushover.go]"]
  node_binary["pingshutdown executable<br/>Go daemon"]
end

subgraph group_deployment["Deployment"]
  node_service["systemd service<br/>service unit"]
  node_env["Service environment<br/>EnvironmentFile<br/>[pingshutdown.env]"]
  node_flake["Nix flake<br/>Nix integration<br/>[flake.nix]"]
  node_module["NixOS module<br/>NixOS service module<br/>[module.nix]"]
  node_package["Nix package<br/>[package.nix]"]
end

subgraph group_external["Host and external services"]
  node_target(("Monitored remote host<br/>ICMP target"))
  node_operator(("Local operator<br/>HTTP client"))
  node_os{{"Local OS shutdown<br/>host shutdown mechanism"}}
  node_pushover_api(("Pushover API<br/>external notification API"))
end

node_package -->|"builds"| node_binary
node_flake -->|"exposes"| node_package
node_flake -->|"exports"| node_module
node_module -->|"declares"| node_service
node_module -->|"supplies settings"| node_env
node_service -->|"runs"| node_binary
node_service -->|"loads"| node_env
node_binary -->|"starts"| node_main
node_env -->|"configures"| node_main
node_main -->|"repeatedly pings"| node_target
node_main -->|"starts or cancels on reachability"| node_countdown
node_main -->|"serves shared state"| node_handlers
node_operator -->|"views status / sets lockout"| node_handlers
node_handlers -->|"reads and controls"| node_countdown
node_main -->|"notifies on pending shutdown"| node_pushover
node_pushover -->|"sends alert"| node_pushover_api
node_countdown -->|"signals delay expiry"| node_main
node_main -->|"invokes shutdown unless dry-run"| node_os

click node_main "https://github.com/devusb/pingshutdown/blob/main/cmd/pingshutdown/main.go"
click node_handlers "https://github.com/devusb/pingshutdown/blob/main/cmd/pingshutdown/handlers.go"
click node_countdown "https://github.com/devusb/pingshutdown/blob/main/pkg/countdown/countdown.go"
click node_pushover "https://github.com/devusb/pingshutdown/blob/main/pkg/pushover/pushover.go"
click node_binary "https://github.com/devusb/pingshutdown/tree/main/cmd/pingshutdown"
click node_service "https://github.com/devusb/pingshutdown/blob/main/init/pingshutdown.service"
click node_env "https://github.com/devusb/pingshutdown/blob/main/init/pingshutdown.env"
click node_flake "https://github.com/devusb/pingshutdown/blob/main/flake.nix"
click node_module "https://github.com/devusb/pingshutdown/blob/main/module.nix"
click node_package "https://github.com/devusb/pingshutdown/blob/main/package.nix"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_handlers,node_countdown,node_pushover,node_binary toneBlue
class node_service,node_env,node_flake,node_module,node_package toneAmber
class node_target,node_operator,node_os,node_pushover_api toneMint
```

## Components

### Go daemon

- [Daemon bootstrap](https://github.com/devusb/pingshutdown/blob/main/cmd/pingshutdown/main.go): Go entrypoint · Assembles configuration, monitoring, HTTP server, and shutdown behavior. · `cmd/pingshutdown/main.go`
- [Status and control handlers](https://github.com/devusb/pingshutdown/blob/main/cmd/pingshutdown/handlers.go): HTTP handlers · Reports countdown state and lets operators disable shutdown. · `cmd/pingshutdown/handlers.go`
- [Countdown state machine](https://github.com/devusb/pingshutdown/blob/main/pkg/countdown/countdown.go): safety control · Delays shutdown and supports cancellation or lockout. · `pkg/countdown/countdown.go`
- [Pushover client](https://github.com/devusb/pingshutdown/blob/main/pkg/pushover/pushover.go): notification integration · `pkg/pushover/pushover.go`
- [pingshutdown executable](https://github.com/devusb/pingshutdown/tree/main/cmd/pingshutdown): Go daemon · `cmd/pingshutdown`

### Deployment

- [systemd service](https://github.com/devusb/pingshutdown/blob/main/init/pingshutdown.service): service unit · `init/pingshutdown.service`
- [Service environment](https://github.com/devusb/pingshutdown/blob/main/init/pingshutdown.env): EnvironmentFile · Supplies target, delays, dry-run, HTTP, and notification settings. · `init/pingshutdown.env`
- [Nix flake](https://github.com/devusb/pingshutdown/blob/main/flake.nix): Nix integration · `flake.nix`
- [NixOS module](https://github.com/devusb/pingshutdown/blob/main/module.nix): NixOS service module · `module.nix`
- [Nix package](https://github.com/devusb/pingshutdown/blob/main/package.nix): Nix package definition · `package.nix`

### Host and external services

- **Monitored remote host**: ICMP target
- **Local operator**: HTTP client
- **Local OS shutdown**: host shutdown mechanism
- **Pushover API**: external notification API

## Connections

- 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
