Skip to main content

Monitor LAN hosts via ping/arp, export Prometheus metrics

Project description

Monitor LAN hosts via ping/ARP and export metrics to Prometheus.

Features

  • Monitors hosts from /etc/hosts file

  • Checks reachability via ping (falls back to ARP)

  • Exports Prometheus metrics for Node Exporter textfile collector

  • Environment-based configuration

  • Proper error handling and logging

  • Cross-platform support (Linux, macOS, Windows)

  • Python 3.9+ with type hints

Installation

From PyPI:

pip install hosts-monitoring

From source:

git clone https://github.com/blasebast/hosts_monitoring
cd hosts_monitoring
pip install -e .

Quick Start

Basic usage:

# Copy configuration template
cp .env.example .env

# Edit configuration
nano .env

# Run monitoring
hosts-monitoring

Monitor single host:

hosts-monitoring -hostname 192.168.1.1

Docker usage:

docker-compose up -d

Configuration

Configure via environment variables in .env file:

  • HOSTS_FILE - Path to hosts file (default: /etc/hosts)

  • OUTPUT_DIR - Output directory for metrics (default: /var/lib/node_exporter/textfile_collector)

  • OUTPUT_FILE_BASE - Output filename (default: node_network_hosts_up.prom)

  • PING_TIMEOUT - Ping timeout in seconds (default: 1)

  • LOG_DIR - Log directory (default: /tmp)

  • ARP_CMD - Path to arp command (default: /usr/sbin/arp)

  • LOG_LEVEL - Logging level (default: INFO)

Docker

Using docker-compose (recommended):

docker-compose up -d

Using Docker image directly:

docker build -t hosts-monitoring .
docker run --rm \
  -v /etc/hosts:/etc/hosts:ro \
  -v /var/lib/node_exporter/textfile_collector:/metrics \
  -e HOSTS_FILE=/etc/hosts \
  -e OUTPUT_DIR=/metrics \
  hosts-monitoring

Integration with Prometheus

Add to prometheus.yml:

scrape_configs:
  - job_name: 'node'
    static_configs:
      - targets: ['localhost:9100']

The node_network_hosts_up metric will be collected via Node Exporter’s textfile collector.

Metrics

Metric name: node_network_hosts_up

Labels: - hostname: Hostname from /etc/hosts

Values: - 1: Host is reachable (ping or ARP success) - 0: Host is unreachable

Example output:

node_network_hosts_up{hostname="router"} 1
node_network_hosts_up{hostname="server"} 1
node_network_hosts_up{hostname="offline-host"} 0

Running as Cron Job

Add to crontab to run every minute:

* * * * * /usr/local/bin/hosts-monitoring

Or with custom configuration:

* * * * * source /etc/hosts-monitoring/.env && hosts-monitoring

Development

Setup development environment:

pip install -e ".[dev]"
pytest

Run tests:

pytest tests/
pytest --cov=hmon tests/

Troubleshooting

“Output directory doesn’t exist” - Ensure output directory exists and is writable - Usually /var/lib/node_exporter/textfile_collector for Node Exporter

“Hosts file not found” - Check HOSTS_FILE environment variable - Default is /etc/hosts on Linux/macOS - Windows: typically C:\Windows\System32\drivers\etc\hosts

“ARP command not found” - ARP checking is optional and will be skipped if unavailable - Ping will still be used for reachability checks

“Permission denied” writing metrics - Container/process needs write permissions to output directory - In docker-compose, metrics directory is volume-mounted - For manual setup: chmod 777 /var/lib/node_exporter/textfile_collector

Performance

  • Default ping timeout: 1 second (configurable via PING_TIMEOUT)

  • Falls back to ARP if ping fails

  • Metrics written atomically (temp file + rename)

  • Suitable for cron execution every 1-5 minutes

License

GNU General Public License v3.0 - See LICENSE file

Contributing

Contributions welcome! Please:

  1. Fork the repository

  2. Create a feature branch

  3. Add tests for new functionality

  4. Ensure tests pass: pytest

  5. Submit a pull request

Author

Sebastian Blasiak - https://github.com/blasebast

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hosts_monitoring-0.2.2.tar.gz (25.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hosts_monitoring-0.2.2-py3-none-any.whl (22.1 kB view details)

Uploaded Python 3

File details

Details for the file hosts_monitoring-0.2.2.tar.gz.

File metadata

  • Download URL: hosts_monitoring-0.2.2.tar.gz
  • Upload date:
  • Size: 25.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hosts_monitoring-0.2.2.tar.gz
Algorithm Hash digest
SHA256 e9aa305e3f1d7c419222131a95fced617a7197390ebf149739ab11f4e94abf9e
MD5 ae4950f2e61744004e7121155611c2f8
BLAKE2b-256 279928719e533e447b2fa5c34df0e7df83a0b5b1843b5c0908be5978c417444d

See more details on using hashes here.

File details

Details for the file hosts_monitoring-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for hosts_monitoring-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e8f47ebc4b8e2ef7211ce7b96c3e4bff96b2d7c64197095f34ce12050eba7708
MD5 dd870442083c78a3cfdb94aaf753301b
BLAKE2b-256 125f8e6f8844afc2fa57b24bdd8982182a5f933b8f9d1038178cb3b77842d7b1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page