Hosts Monitoring
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:
Fork the repository
Create a feature branch
Add tests for new functionality
Ensure tests pass: pytest
Submit a pull request
Release files for hosts-monitoring 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| hosts_monitoring-0.2.2.tar.gz | 25.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| hosts_monitoring-0.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 48.0 kB
Release files / hosts_monitoring-0.2.2.tar.gz
| Download URL | hosts_monitoring-0.2.2.tar.gz |
|---|---|
| Size | 25.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e9aa305e3f1d7c419222131a95fced617a7197390ebf149739ab11f4e94abf9e
|
|
BLAKE2b-256 checksum How to use checksums |
279928719e533e447b2fa5c34df0e7df83a0b5b1843b5c0908be5978c417444d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / hosts_monitoring-0.2.2-py3-none-any.whl
| Download URL | hosts_monitoring-0.2.2-py3-none-any.whl |
|---|---|
| Size | 22.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e8f47ebc4b8e2ef7211ce7b96c3e4bff96b2d7c64197095f34ce12050eba7708
|
|
BLAKE2b-256 checksum How to use checksums |
125f8e6f8844afc2fa57b24bdd8982182a5f933b8f9d1038178cb3b77842d7b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|