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.1.tar.gz (24.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.1-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hosts_monitoring-0.2.1.tar.gz
  • Upload date:
  • Size: 24.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.1.tar.gz
Algorithm Hash digest
SHA256 359ead7674573e52603fb548f7ee09f169df1fabf61b910ee2098c6a55661d8a
MD5 8741aaeb031a04e8951ab5e6bfcbc826
BLAKE2b-256 eac933fdd144db97c00d0c6f8f2c1614d1185fb8f9a1e1c5936be1171ab3cae8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hosts_monitoring-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4a5bf09fd01c183678f332c023d84e9407829cf72997cf7e04b57b69e0088dea
MD5 6a5a7f0474ab9721b9057cfa27271cec
BLAKE2b-256 0ad524b097ea08b4e6c4501bee4d16cc6892cfae0e9de8733866e90df65b369b

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