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.0.tar.gz (24.6 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.0-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: hosts_monitoring-0.2.0.tar.gz
  • Upload date:
  • Size: 24.6 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.0.tar.gz
Algorithm Hash digest
SHA256 39ab6389377d22c3dc1b401e12e85a56c731be570f00b5fe62d9dd820cda2a68
MD5 2e343d1876967fca32f047590084381d
BLAKE2b-256 0ebabb493ad2d6e6f7292d45321e84ae7a8278b314d13c7fc19465a0f984905f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hosts_monitoring-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 57a3be11708d56a827105743d7161714ffb96c229c7706f2c2f8e8c449c03924
MD5 bbdd0903d0c292b3c8187fd7a6c2a67f
BLAKE2b-256 3e562b1d73b8898faf2a21468a3560c34eff88f5af1ea4118e6ed576fcd42274

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