Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6fb67f8615 | |||
| e70ae6f176 | |||
| a77f6d380c | |||
| 6aae2a1dab | |||
| 85ee0e1040 | |||
| c4f09e9ced | |||
| 64710fd4cd | |||
| 1f5e7465a3 | |||
| b290b21e23 | |||
| 65c4267847 | |||
| 462a445235 | |||
| 368e178f93 | |||
| 6905bf266a | |||
| b6dcce4f35 | |||
| e6436fc236 | |||
| c5ce41762e | |||
| 26ca0c095f | |||
| 1eecd67594 | |||
| caf3c2c0ac | |||
| 9af4006097 | |||
| ddf7067d13 | |||
| 505353a8a8 | |||
| 0402d33c71 |
@@ -0,0 +1,4 @@
|
|||||||
|
1. Don't assume. Don't hide confusion. Surface tradeoffs.
|
||||||
|
2. Minimum code that solves the problem. Nothing speculative.
|
||||||
|
3. Touch only what you must. Clean up only your own mess.
|
||||||
|
4. Define success criteria. Loop until verified.
|
||||||
@@ -377,7 +377,7 @@ This project now declares its dependencies in `pyproject.toml`. Instead
|
|||||||
of the old `requirements.txt` flow, install the package into a virtualenv
|
of the old `requirements.txt` flow, install the package into a virtualenv
|
||||||
using `pip`:
|
using `pip`:
|
||||||
|
|
||||||
See `scripts/install.sh` for a way to install.
|
See `scripts/hb_install.sh` for a way to install.
|
||||||
|
|
||||||
Run the daemon (example):
|
Run the daemon (example):
|
||||||
|
|
||||||
@@ -441,6 +441,68 @@ plugins:
|
|||||||
|
|
||||||
All monitoring plugins default to 5-minute (300 second) intervals, but can be customized as needed.
|
All monitoring plugins default to 5-minute (300 second) intervals, but can be customized as needed.
|
||||||
|
|
||||||
|
### hbc_mini — single-file client (no external dependencies)
|
||||||
|
|
||||||
|
`scripts/hbc_mini.py` is a self-contained version of the heartbeat client that requires only Python 3.8+ and no external packages. Copy it to any host and run it directly — no virtualenv, no `pip install`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Basic usage
|
||||||
|
python3 hbc_mini.py your-server.example.com
|
||||||
|
|
||||||
|
# Run as daemon
|
||||||
|
python3 hbc_mini.py -d your-server.example.com
|
||||||
|
|
||||||
|
# Send a boot message
|
||||||
|
python3 hbc_mini.py -b your-server.example.com
|
||||||
|
|
||||||
|
# Send a one-off message
|
||||||
|
python3 hbc_mini.py -m "maintenance starting" your-server.example.com
|
||||||
|
```
|
||||||
|
|
||||||
|
**Config:** `~/.hbc.json` (same keys as `~/.hbc.yaml`, JSON format). Example:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"hb_port": 50003,
|
||||||
|
"interval": 30,
|
||||||
|
"plugins": {
|
||||||
|
"ping_monitor": {
|
||||||
|
"interval": 60,
|
||||||
|
"hosts": ["8.8.8.8", "192.168.1.1"]
|
||||||
|
},
|
||||||
|
"nagios_runner": {
|
||||||
|
"interval": 300,
|
||||||
|
"commands": [
|
||||||
|
{"name": "check_load", "command": "/usr/lib/nagios/plugins/check_load -w 5,4,3 -c 10,8,6"}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Plugin availability:**
|
||||||
|
|
||||||
|
| Plugin | Platform | Data source |
|
||||||
|
|---|---|---|
|
||||||
|
| `os_info` | all | `platform` stdlib |
|
||||||
|
| `ping_monitor` | all | `ping` subprocess |
|
||||||
|
| `nagios_runner` | all (not Windows) | subprocess |
|
||||||
|
| `cpu_monitor` | Linux | `/proc/stat` |
|
||||||
|
| `memory_monitor` | Linux | `/proc/meminfo` |
|
||||||
|
| `disk_monitor` | Linux, macOS, BSD | `df -P` subprocess |
|
||||||
|
| `network_monitor` | Linux | `/proc/net/dev` |
|
||||||
|
|
||||||
|
**What is not available compared to the full `hbc`:**
|
||||||
|
|
||||||
|
- No YAML config (use JSON instead)
|
||||||
|
- No `filesystem_info` plugin
|
||||||
|
- `cpu_monitor` does not report per-core usage or CPU frequency (no psutil)
|
||||||
|
- Plugins cannot be loaded from external `.py` files — all plugins are compiled in
|
||||||
|
|
||||||
|
Everything else — heartbeat protocol, ACK/CMD/UPD handling, `hb_install.sh`-based self-update, daemonize, syslog — is identical to the full client.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 🐞 Debugging in VS Code
|
## 🐞 Debugging in VS Code
|
||||||
|
|
||||||
This repository includes a ready-to-use `.vscode/launch.json` with configurations to run or attach the VS Code debugger to `hbd`.
|
This repository includes a ready-to-use `.vscode/launch.json` with configurations to run or attach the VS Code debugger to `hbd`.
|
||||||
|
|||||||
-234
@@ -1,234 +0,0 @@
|
|||||||
# HBD/HBC Separation Refactoring
|
|
||||||
|
|
||||||
## Overview
|
|
||||||
|
|
||||||
The heartbeat monitoring system has been refactored into a modular package structure with separate client and server components. This allows users to install only what they need and provides clear separation of concerns.
|
|
||||||
|
|
||||||
## New Package Structure
|
|
||||||
|
|
||||||
```
|
|
||||||
hbd/
|
|
||||||
├── __init__.py # Main package (minimal)
|
|
||||||
├── client/ # HBC - System monitoring client
|
|
||||||
│ ├── __init__.py
|
|
||||||
│ ├── main.py # Entry point (was hbc.py)
|
|
||||||
│ ├── config.py # Client-specific configuration
|
|
||||||
│ ├── plugin.py # Plugin framework
|
|
||||||
│ ├── threshold.py # Threshold checking
|
|
||||||
│ └── plugins/ # Monitoring plugins
|
|
||||||
│ ├── cpu_monitor.py
|
|
||||||
│ ├── disk_monitor.py
|
|
||||||
│ ├── memory_monitor.py
|
|
||||||
│ ├── network_monitor.py
|
|
||||||
│ ├── filesystem_info.py
|
|
||||||
│ ├── os_info.py
|
|
||||||
│ └── nagios_runner.py
|
|
||||||
├── server/ # HBD - Heartbeat daemon/server
|
|
||||||
│ ├── __init__.py
|
|
||||||
│ ├── main.py # Server runtime (was server.py)
|
|
||||||
│ ├── cli.py # Command-line interface
|
|
||||||
│ ├── config.py # Server-specific configuration
|
|
||||||
│ ├── http.py # HTTP/REST API
|
|
||||||
│ ├── ws.py # WebSocket server
|
|
||||||
│ ├── udp.py # UDP heartbeat listener
|
|
||||||
│ ├── dns.py # DNS update functionality
|
|
||||||
│ ├── notify.py # Notification handlers
|
|
||||||
│ ├── monitor.py # Host monitoring
|
|
||||||
│ ├── hbdclass.py # Host class definitions
|
|
||||||
│ ├── journal.py # Message journaling
|
|
||||||
│ ├── templates/ # Jinja2 web templates
|
|
||||||
│ └── static/ # Web UI assets
|
|
||||||
└── common/ # Shared utilities
|
|
||||||
├── __init__.py
|
|
||||||
├── proto.py # Protocol encoding/decoding
|
|
||||||
└── utils.py # Common utilities
|
|
||||||
|
|
||||||
## Configuration Files
|
|
||||||
|
|
||||||
### Client Configuration (hbd/client/config.py)
|
|
||||||
|
|
||||||
Client-specific defaults:
|
|
||||||
- `hb_port`: Port where hbd servers listen (default: 50003)
|
|
||||||
- `interval`: Heartbeat interval in seconds (default: 10)
|
|
||||||
- `plugins`: Per-plugin configuration
|
|
||||||
- `thresholds`: Threshold configuration for monitoring
|
|
||||||
|
|
||||||
### Server Configuration (hbd/server/config.py)
|
|
||||||
|
|
||||||
Server-specific defaults:
|
|
||||||
- `hb_port`: Port to listen for heartbeats (default: 50003)
|
|
||||||
- `hbd_port`: HTTP API port (default: 50004)
|
|
||||||
- `ws_port`: WebSocket port (default: 50005)
|
|
||||||
- `logfile`: Log file path
|
|
||||||
- `pushsrv`, `pushover_token`, etc.: Notification settings
|
|
||||||
- `watchhosts`, `dyndnshosts`: Host monitoring
|
|
||||||
- `smtpserver`, etc.: Email settings
|
|
||||||
- `journal_*`: Message journaling settings
|
|
||||||
|
|
||||||
## Installation Options
|
|
||||||
|
|
||||||
### Install Core Only (minimal, PyYAML only)
|
|
||||||
```bash
|
|
||||||
pip install hbd
|
|
||||||
```
|
|
||||||
|
|
||||||
### Install Client Only (for monitoring)
|
|
||||||
```bash
|
|
||||||
pip install hbd[client]
|
|
||||||
# Installs: PyYAML, psutil
|
|
||||||
```
|
|
||||||
|
|
||||||
### Install Server Only (for daemon)
|
|
||||||
```bash
|
|
||||||
pip install hbd[server]
|
|
||||||
# Installs: PyYAML, websockets, mattermostdriver, aiohttp, Jinja2
|
|
||||||
```
|
|
||||||
|
|
||||||
### Install Everything
|
|
||||||
```bash
|
|
||||||
pip install hbd[all]
|
|
||||||
# Installs all dependencies for both client and server
|
|
||||||
```
|
|
||||||
|
|
||||||
### Development Installation
|
|
||||||
```bash
|
|
||||||
pip install -e ".[dev]"
|
|
||||||
# Includes all dependencies plus testing/linting tools
|
|
||||||
```
|
|
||||||
|
|
||||||
## Command-Line Interfaces
|
|
||||||
|
|
||||||
### HBC (Client)
|
|
||||||
```bash
|
|
||||||
hbc [options] host1 [host2 ...]
|
|
||||||
|
|
||||||
# Entry point: hbd.client.main:main
|
|
||||||
# Location: hbd/client/main.py
|
|
||||||
```
|
|
||||||
|
|
||||||
### HBD (Server)
|
|
||||||
```bash
|
|
||||||
hbd [options]
|
|
||||||
|
|
||||||
# Entry point: hbd.server.cli:main
|
|
||||||
# Location: hbd/server/cli.py → hbd/server/main.py
|
|
||||||
```
|
|
||||||
|
|
||||||
## Import Changes
|
|
||||||
|
|
||||||
### Client Code
|
|
||||||
```python
|
|
||||||
# Old imports
|
|
||||||
from .config import load_config
|
|
||||||
from .proto import dicttos, stodict
|
|
||||||
from .plugin import PluginRegistry
|
|
||||||
|
|
||||||
# New imports
|
|
||||||
from .config import load_config # Still in client/
|
|
||||||
from ..common.proto import dicttos # Moved to common/
|
|
||||||
from .plugin import PluginRegistry # Still in client/
|
|
||||||
```
|
|
||||||
|
|
||||||
### Server Code
|
|
||||||
```python
|
|
||||||
# Old imports
|
|
||||||
from .config import load_config
|
|
||||||
from .proto import stodict
|
|
||||||
from .threshold import AlertLevel
|
|
||||||
|
|
||||||
# New imports
|
|
||||||
from .config import load_config # Server-specific config
|
|
||||||
from ..common.proto import stodict # Moved to common/
|
|
||||||
from ..client.threshold import AlertLevel # Client module
|
|
||||||
```
|
|
||||||
|
|
||||||
### Plugin Code
|
|
||||||
```python
|
|
||||||
# Old import
|
|
||||||
from hbd.plugin import MonitorPlugin
|
|
||||||
|
|
||||||
# New import
|
|
||||||
from hbd.client.plugin import MonitorPlugin
|
|
||||||
```
|
|
||||||
|
|
||||||
## Benefits
|
|
||||||
|
|
||||||
1. **Modular Installation**: Install only what you need
|
|
||||||
- Client-only systems don't need web server dependencies
|
|
||||||
- Server-only systems don't need psutil
|
|
||||||
|
|
||||||
2. **Clearer Architecture**: Explicit separation of concerns
|
|
||||||
- Client: System monitoring and data collection
|
|
||||||
- Server: Heartbeat reception, web UI, notifications
|
|
||||||
- Common: Shared protocol and utilities
|
|
||||||
|
|
||||||
3. **Independent Evolution**: Client and server can evolve separately
|
|
||||||
- Different release cycles possible
|
|
||||||
- Clear API boundaries via common/
|
|
||||||
|
|
||||||
4. **Smaller Footprint**: Reduced dependency installation
|
|
||||||
- Client: ~1 dependency (psutil)
|
|
||||||
- Server: ~4 dependencies (websockets, aiohttp, Jinja2, mattermostdriver)
|
|
||||||
|
|
||||||
## Migration Guide
|
|
||||||
|
|
||||||
### For Existing Installations
|
|
||||||
|
|
||||||
1. **Reinstall the package**:
|
|
||||||
```bash
|
|
||||||
pip install -e ".[all]" # For development
|
|
||||||
# or
|
|
||||||
pip install hbd[all] # For production
|
|
||||||
```
|
|
||||||
|
|
||||||
2. **Configuration files remain unchanged**:
|
|
||||||
- Both client and server read from `~/.hb.yaml`
|
|
||||||
- All existing config keys are supported in both configs
|
|
||||||
- Server has additional keys (journal, websocket, email, etc.)
|
|
||||||
- Client has minimal keys (interval, plugins, thresholds)
|
|
||||||
|
|
||||||
3. **Commands remain the same**:
|
|
||||||
- `hbc` command works identically
|
|
||||||
- `hbd` command works identically
|
|
||||||
|
|
||||||
### For New Deployments
|
|
||||||
|
|
||||||
1. **Client-only system** (monitoring host):
|
|
||||||
```bash
|
|
||||||
pip install hbd[client]
|
|
||||||
hbc server1.example.com server2.example.com
|
|
||||||
```
|
|
||||||
|
|
||||||
2. **Server-only system** (monitoring daemon):
|
|
||||||
```bash
|
|
||||||
pip install hbd[server]
|
|
||||||
hbd -c /etc/hbd.yaml -f
|
|
||||||
```
|
|
||||||
|
|
||||||
3. **Combined system** (dev/test):
|
|
||||||
```bash
|
|
||||||
pip install hbd[all]
|
|
||||||
```
|
|
||||||
|
|
||||||
## Testing
|
|
||||||
|
|
||||||
All imports and entry points have been tested and validated:
|
|
||||||
- ✅ Package imports work correctly
|
|
||||||
- ✅ `hbc` command entry point functional
|
|
||||||
- ✅ `hbd` command entry point functional
|
|
||||||
- ✅ Optional dependencies properly configured
|
|
||||||
- ✅ All internal imports updated
|
|
||||||
|
|
||||||
## Files Archived
|
|
||||||
|
|
||||||
The following files were renamed to avoid conflicts:
|
|
||||||
- `hbd/config.py` → `hbd/config.py.old` (split into client/server configs)
|
|
||||||
- `hbd/hbc_old.py` → `hbd/hbc_old.py.bak` (backup file)
|
|
||||||
|
|
||||||
## Next Steps
|
|
||||||
|
|
||||||
1. Test client functionality with a monitoring host
|
|
||||||
2. Test server functionality with web UI and notifications
|
|
||||||
3. Update documentation (README.md) with new structure
|
|
||||||
4. Consider publishing to PyPI with new structure
|
|
||||||
5. Update any deployment scripts/Dockerfiles to use optional dependencies
|
|
||||||
-21
@@ -1,21 +0,0 @@
|
|||||||
Plan the following changes, ask questions to clarify before implementing
|
|
||||||
|
|
||||||
Re-factor the notification system:
|
|
||||||
- use available libraries for pushover, matrix, email and sms notifications.
|
|
||||||
- notifications have a title/subject: alert_type (recover/warning/critical), a body (info from threshold check) and a link to the host plugin metrix page
|
|
||||||
- define a list of notification channels for each user
|
|
||||||
- notifications are dispatched to users that are listed as managers for the host
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
1 - correct
|
|
||||||
2 - for now channels are defined globaly
|
|
||||||
3 - matrix-nio)sounds good, homeserver URL, access token, room ID per channel?
|
|
||||||
4 - use the REST api provided by https://voip.ms/api/v1/rest.php
|
|
||||||
5 - The page does not exist yet, point at the host tab in the /plugins
|
|
||||||
6 - per-channel minimum severity is a good idea, go fo it
|
|
||||||
7 - yes
|
|
||||||
|
|
||||||
1 - use base_url, there might not have been any incoming requests yet
|
|
||||||
2 - use same asyncio loop for matrix-nio
|
|
||||||
3 - for now, just silently do nothing
|
|
||||||
+1
-1
@@ -14,4 +14,4 @@ Install options:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
__all__ = ["__version__"]
|
__all__ = ["__version__"]
|
||||||
__version__ = "5.1.3"
|
__version__ = "5.1.10"
|
||||||
|
|||||||
+34
-32
@@ -14,7 +14,6 @@ import signal
|
|||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
from hashlib import md5
|
|
||||||
from logging.handlers import SysLogHandler
|
from logging.handlers import SysLogHandler
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
@@ -56,6 +55,7 @@ class AsyncConnection:
|
|||||||
|
|
||||||
self.transport: Optional[asyncio.DatagramTransport] = None
|
self.transport: Optional[asyncio.DatagramTransport] = None
|
||||||
self.protocol: Optional[asyncio.DatagramProtocol] = None
|
self.protocol: Optional[asyncio.DatagramProtocol] = None
|
||||||
|
self._dead = False
|
||||||
|
|
||||||
self.logger = logging.getLogger(f"hbc.conn.{addr}")
|
self.logger = logging.getLogger(f"hbc.conn.{addr}")
|
||||||
|
|
||||||
@@ -93,6 +93,9 @@ class AsyncConnection:
|
|||||||
msg: Message dictionary
|
msg: Message dictionary
|
||||||
msg_id: Message ID (HTB, PLG, etc.)
|
msg_id: Message ID (HTB, PLG, etc.)
|
||||||
"""
|
"""
|
||||||
|
if self._dead:
|
||||||
|
return
|
||||||
|
|
||||||
if not self.transport:
|
if not self.transport:
|
||||||
await self.open()
|
await self.open()
|
||||||
|
|
||||||
@@ -167,7 +170,9 @@ class HeartbeatProtocol(asyncio.DatagramProtocol):
|
|||||||
|
|
||||||
def error_received(self, exc):
|
def error_received(self, exc):
|
||||||
"""Handle protocol errors."""
|
"""Handle protocol errors."""
|
||||||
self.logger.error(f"Protocol error: {exc}")
|
self.logger.warning(f"Protocol error on {self.connection.addr}: {exc} — dropping connection")
|
||||||
|
self.connection._dead = True
|
||||||
|
self.connection.close()
|
||||||
|
|
||||||
|
|
||||||
async def handle_command(conn: AsyncConnection, msg: dict):
|
async def handle_command(conn: AsyncConnection, msg: dict):
|
||||||
@@ -204,48 +209,45 @@ async def handle_command(conn: AsyncConnection, msg: dict):
|
|||||||
await conn.sendto(response)
|
await conn.sendto(response)
|
||||||
|
|
||||||
|
|
||||||
async def handle_update(conn: AsyncConnection, msg: dict):
|
async def handle_update(conn: AsyncConnection, _msg: dict): # pyright: ignore[reportUnusedParameter]
|
||||||
"""Handle self-update from server."""
|
"""Handle self-update by running hb_install.sh."""
|
||||||
import codecs
|
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
logger = logging.getLogger("hbc.update")
|
logger = logging.getLogger("hbc.update")
|
||||||
|
|
||||||
try:
|
installer = shutil.which("hb_install.sh")
|
||||||
code = codecs.decode(msg["code"], "base64").decode()
|
if installer is None:
|
||||||
csum = msg["csum"]
|
candidate = Path(sys.argv[0]).parent / "hb_install.sh"
|
||||||
except Exception as e:
|
if candidate.exists():
|
||||||
error = f"Missing code/csum: {e}"
|
installer = str(candidate)
|
||||||
|
|
||||||
|
if installer is None:
|
||||||
|
error = "hb_install.sh not found in PATH or alongside hbc"
|
||||||
logger.error(error)
|
logger.error(error)
|
||||||
await conn.sendto({"service": "update", "msg": error})
|
await conn.sendto({"service": "update", "msg": error})
|
||||||
return
|
return
|
||||||
|
|
||||||
# Verify checksum
|
logger.info(f"Running installer: {installer}")
|
||||||
m = md5()
|
try:
|
||||||
m.update(code.encode())
|
proc = await asyncio.create_subprocess_exec(
|
||||||
if m.hexdigest() != csum:
|
installer, "client",
|
||||||
error = "Checksum mismatch"
|
stdout=asyncio.subprocess.PIPE,
|
||||||
|
stderr=asyncio.subprocess.STDOUT,
|
||||||
|
)
|
||||||
|
out, _ = await asyncio.wait_for(proc.communicate(), timeout=120)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
error = "Installer timed out"
|
||||||
|
logger.error(error)
|
||||||
|
await conn.sendto({"service": "update", "msg": error})
|
||||||
|
return
|
||||||
|
except Exception as e:
|
||||||
|
error = f"Installer failed: {e}"
|
||||||
logger.error(error)
|
logger.error(error)
|
||||||
await conn.sendto({"service": "update", "msg": error})
|
await conn.sendto({"service": "update", "msg": error})
|
||||||
return
|
return
|
||||||
|
|
||||||
# Backup current file
|
if proc.returncode != 0:
|
||||||
fn = sys.argv[0]
|
error = f"Installer exited {proc.returncode}: {out.decode().strip()}"
|
||||||
ofn = f"{fn}.sav"
|
|
||||||
try:
|
|
||||||
shutil.copy2(fn, ofn)
|
|
||||||
except Exception as e:
|
|
||||||
error = f"Backup failed: {e}"
|
|
||||||
logger.error(error)
|
|
||||||
await conn.sendto({"service": "update", "msg": error})
|
|
||||||
return
|
|
||||||
|
|
||||||
# Write new code
|
|
||||||
try:
|
|
||||||
with open(fn, "w") as fh:
|
|
||||||
fh.write(code)
|
|
||||||
except Exception as e:
|
|
||||||
error = f"Write failed: {e}"
|
|
||||||
logger.error(error)
|
logger.error(error)
|
||||||
await conn.sendto({"service": "update", "msg": error})
|
await conn.sendto({"service": "update", "msg": error})
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ class OSInfoPlugin(InfoPlugin):
|
|||||||
"python_version": platform.python_version(),
|
"python_version": platform.python_version(),
|
||||||
"python_implementation": platform.python_implementation(),
|
"python_implementation": platform.python_implementation(),
|
||||||
"hbc_version": hbc_version,
|
"hbc_version": hbc_version,
|
||||||
|
"hbc_type": "full",
|
||||||
}
|
}
|
||||||
|
|
||||||
# Add Linux-specific distribution info
|
# Add Linux-specific distribution info
|
||||||
|
|||||||
+5
-6
@@ -144,17 +144,16 @@ def cmd_notify(args):
|
|||||||
url=f"{base_url}/plugins" if base_url else "",
|
url=f"{base_url}/plugins" if base_url else "",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Bypass min_level for explicit test sends; run async channels directly
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from .notify import _send_matrix_async, _send_sms_voipms_async, _DRIVERS
|
||||||
ch_type = channel_cfg.get("type", "")
|
ch_type = channel_cfg.get("type", "")
|
||||||
print(f"Sending via {args.channel} ({ch_type}): {title} — {args.message}")
|
print(f"Sending via {args.channel} ({ch_type}): {title} — {args.message}")
|
||||||
|
|
||||||
if ch_type in ("matrix", "sms_voipms"):
|
if ch_type == "matrix":
|
||||||
from .notify import _send_matrix_async, _send_sms_voipms_async
|
ok = asyncio.run(_send_matrix_async(channel_cfg, notif))
|
||||||
driver_async = _send_matrix_async if ch_type == "matrix" else _send_sms_voipms_async
|
elif ch_type == "sms_voipms":
|
||||||
ok = asyncio.run(driver_async(channel_cfg, notif))
|
ok = asyncio.run(_send_sms_voipms_async(channel_cfg, notif))
|
||||||
else:
|
else:
|
||||||
from .notify import _DRIVERS
|
|
||||||
driver = _DRIVERS.get(ch_type)
|
driver = _DRIVERS.get(ch_type)
|
||||||
if driver is None:
|
if driver is None:
|
||||||
print(f"Error: unknown channel type '{ch_type}'", file=sys.stderr)
|
print(f"Error: unknown channel type '{ch_type}'", file=sys.stderr)
|
||||||
|
|||||||
+54
-11
@@ -1,7 +1,11 @@
|
|||||||
"""HTTP server implementation using aiohttp and jinja2."""
|
"""HTTP server implementation using aiohttp and jinja2."""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import datetime
|
||||||
import json
|
import json
|
||||||
|
import platform
|
||||||
|
import socket
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import os
|
import os
|
||||||
@@ -111,6 +115,7 @@ async def start(
|
|||||||
This function is intended to be awaited inside the main asyncio event loop.
|
This function is intended to be awaited inside the main asyncio event loop.
|
||||||
"""
|
"""
|
||||||
get_now = get_now or (lambda: time.time())
|
get_now = get_now or (lambda: time.time())
|
||||||
|
_start_epoch = time.time()
|
||||||
|
|
||||||
async def old_index(request):
|
async def old_index(request):
|
||||||
_require_auth_redirect(request)
|
_require_auth_redirect(request)
|
||||||
@@ -210,15 +215,11 @@ async def start(
|
|||||||
return err
|
return err
|
||||||
qa = request.rel_url.query
|
qa = request.rel_url.query
|
||||||
uname = urllib.parse.unquote(qa.get("h", ""))
|
uname = urllib.parse.unquote(qa.get("h", ""))
|
||||||
ucode = qa.get("c")
|
if not uname:
|
||||||
if not ucode or not uname:
|
return web.Response(status=400, text="need h= argument")
|
||||||
return web.Response(status=400, text="need h= and c= arguments")
|
|
||||||
if uname != "All" and uname not in hbdclass.Host.hosts:
|
if uname != "All" and uname not in hbdclass.Host.hosts:
|
||||||
return web.Response(status=400, text=f"h={uname} not found")
|
return web.Response(status=400, text=f"h={uname} not found")
|
||||||
if uname != "All":
|
names = [uname] if uname != "All" else list(hbdclass.Host.hosts)
|
||||||
names = [uname]
|
|
||||||
else:
|
|
||||||
names = [n for n in hbdclass.Host.hosts]
|
|
||||||
out = []
|
out = []
|
||||||
for n in names:
|
for n in names:
|
||||||
host = hbdclass.Host.hosts[n]
|
host = hbdclass.Host.hosts[n]
|
||||||
@@ -227,8 +228,7 @@ async def start(
|
|||||||
continue
|
continue
|
||||||
op_err = None
|
op_err = None
|
||||||
try:
|
try:
|
||||||
r = {"csum": None, "code": ucode}
|
host.cmds.append(("UPD", {}))
|
||||||
host.cmds.append(("UPD", r))
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
op_err = str(e)
|
op_err = str(e)
|
||||||
out.append(f"update started for {n}: {op_err if op_err else 'OK'}")
|
out.append(f"update started for {n}: {op_err if op_err else 'OK'}")
|
||||||
@@ -520,8 +520,8 @@ async def start(
|
|||||||
|
|
||||||
tmpl = env.get_template("plugins.html")
|
tmpl = env.get_template("plugins.html")
|
||||||
body = tmpl.render(
|
body = tmpl.render(
|
||||||
title="Plugin Metrics - Heartbeat",
|
title="Host Overview - Heartbeat",
|
||||||
header="Plugin Metrics",
|
header="Host Overview",
|
||||||
hosts=hosts_with_plugins,
|
hosts=hosts_with_plugins,
|
||||||
current_user=current_user.to_dict() if current_user else None,
|
current_user=current_user.to_dict() if current_user else None,
|
||||||
active_page="plugins",
|
active_page="plugins",
|
||||||
@@ -811,6 +811,48 @@ async def start(
|
|||||||
)
|
)
|
||||||
return web.Response(text=body, content_type="text/html")
|
return web.Response(text=body, content_type="text/html")
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# About page
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
async def about_page(request):
|
||||||
|
"""GET /about — version, runtime, and project information."""
|
||||||
|
current_user, _ = _require_auth_redirect(request)
|
||||||
|
pkg_dir = os.path.dirname(__file__)
|
||||||
|
templates_dir = config.get("templates_dir", os.path.join(pkg_dir, "templates"))
|
||||||
|
env = jinja2.Environment(loader=jinja2.FileSystemLoader(templates_dir))
|
||||||
|
from hbd import __version__ as hbd_version
|
||||||
|
|
||||||
|
uptime_secs = int(time.time() - _start_epoch)
|
||||||
|
days, rem = divmod(uptime_secs, 86400)
|
||||||
|
hours, rem = divmod(rem, 3600)
|
||||||
|
mins, secs = divmod(rem, 60)
|
||||||
|
if days:
|
||||||
|
uptime_str = f"{days}d {hours}h {mins}m"
|
||||||
|
elif hours:
|
||||||
|
uptime_str = f"{hours}h {mins}m {secs}s"
|
||||||
|
else:
|
||||||
|
uptime_str = f"{mins}m {secs}s"
|
||||||
|
|
||||||
|
start_dt = datetime.datetime.fromtimestamp(_start_epoch)
|
||||||
|
start_time_str = start_dt.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
|
tmpl = env.get_template("about.html")
|
||||||
|
body = tmpl.render(
|
||||||
|
title="About - Heartbeat",
|
||||||
|
header="About",
|
||||||
|
hbd_version=hbd_version,
|
||||||
|
python_version=f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} ({platform.python_implementation()})",
|
||||||
|
server_hostname=socket.gethostname(),
|
||||||
|
start_epoch=int(_start_epoch),
|
||||||
|
start_time_str=start_time_str,
|
||||||
|
uptime_str=uptime_str,
|
||||||
|
host_count=len(hbdclass.Host.hosts),
|
||||||
|
current_user=current_user.to_dict() if current_user else None,
|
||||||
|
active_page="about",
|
||||||
|
)
|
||||||
|
return web.Response(text=body, content_type="text/html")
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
# Settings page (admin only)
|
# Settings page (admin only)
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
@@ -864,6 +906,7 @@ async def start(
|
|||||||
web.get("/live", live),
|
web.get("/live", live),
|
||||||
web.get("/plugins", plugins_page),
|
web.get("/plugins", plugins_page),
|
||||||
web.get("/alerts", alerts_page),
|
web.get("/alerts", alerts_page),
|
||||||
|
web.get("/about", about_page),
|
||||||
web.get("/profile", profile_page),
|
web.get("/profile", profile_page),
|
||||||
web.get("/settings", settings_page),
|
web.get("/settings", settings_page),
|
||||||
web.get("/static/{path:.*}", static),
|
web.get("/static/{path:.*}", static),
|
||||||
|
|||||||
@@ -210,7 +210,6 @@ async def _run_async(config, config_path=None):
|
|||||||
ctx = dict(
|
ctx = dict(
|
||||||
config=config,
|
config=config,
|
||||||
hbdclass=hbdclass,
|
hbdclass=hbdclass,
|
||||||
log=eventlog,
|
|
||||||
msg_to_websockets=msg_to_websockets,
|
msg_to_websockets=msg_to_websockets,
|
||||||
msg_journal=msg_journal,
|
msg_journal=msg_journal,
|
||||||
threshold_checker=threshold_checker,
|
threshold_checker=threshold_checker,
|
||||||
@@ -237,7 +236,6 @@ async def _run_async(config, config_path=None):
|
|||||||
restore_ctx = dict(
|
restore_ctx = dict(
|
||||||
config=config,
|
config=config,
|
||||||
hbdclass=hbdclass,
|
hbdclass=hbdclass,
|
||||||
log=eventlog,
|
|
||||||
msg_to_websockets=msg_to_websockets,
|
msg_to_websockets=msg_to_websockets,
|
||||||
threshold_checker=threshold_checker,
|
threshold_checker=threshold_checker,
|
||||||
)
|
)
|
||||||
|
|||||||
+25
-53
@@ -15,7 +15,6 @@ their own ``notification_channels`` list. When no users are configured the
|
|||||||
server runs silently (no notifications sent).
|
server runs silently (no notifications sent).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import asyncio
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
import smtplib
|
import smtplib
|
||||||
@@ -30,13 +29,10 @@ from . import ws as ws_mod
|
|||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
msg_to_websockets = ws_mod.broadcast
|
msg_to_websockets = ws_mod.broadcast
|
||||||
|
|
||||||
# Module-level state set via setup()
|
# Module-level state set via setup()
|
||||||
_config: dict = {}
|
_config: dict = {}
|
||||||
_loop: Optional[asyncio.AbstractEventLoop] = None
|
|
||||||
|
|
||||||
# Tracks which channels fired a WARNING/CRITICAL per host.
|
# Tracks which channels fired a WARNING/CRITICAL per host.
|
||||||
# {host_name: set of channel_names} — used to route RECOVER to the same channels.
|
# {host_name: set of channel_names} — used to route RECOVER to the same channels.
|
||||||
@@ -73,11 +69,9 @@ class Notification:
|
|||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
def setup(cfg: dict, loop: Optional[asyncio.AbstractEventLoop] = None):
|
def setup(cfg: dict, loop: Optional[asyncio.AbstractEventLoop] = None):
|
||||||
"""Initialize notifier from configuration dict and event loop."""
|
"""Initialize notifier from configuration dict."""
|
||||||
global _config, _loop
|
global _config
|
||||||
_config = dict(cfg)
|
_config = dict(cfg)
|
||||||
if loop is not None:
|
|
||||||
_loop = loop
|
|
||||||
|
|
||||||
|
|
||||||
def reload_config(cfg: dict):
|
def reload_config(cfg: dict):
|
||||||
@@ -299,17 +293,6 @@ async def _send_sms_voipms_async(channel_cfg: dict, notif: Notification) -> bool
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def _send_sms_voipms(channel_cfg: dict, notif: Notification) -> bool:
|
|
||||||
"""Dispatch voip.ms SMS send onto the shared event loop."""
|
|
||||||
if _loop is None:
|
|
||||||
logger.warning("sms_voipms: event loop not available")
|
|
||||||
return False
|
|
||||||
future = asyncio.run_coroutine_threadsafe(_send_sms_voipms_async(channel_cfg, notif), _loop)
|
|
||||||
try:
|
|
||||||
return future.result(timeout=15)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error("sms_voipms send timed out or failed: %s", e)
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
async def _send_matrix_async(channel_cfg: dict, notif: Notification) -> bool:
|
async def _send_matrix_async(channel_cfg: dict, notif: Notification) -> bool:
|
||||||
@@ -357,40 +340,23 @@ async def _send_matrix_async(channel_cfg: dict, notif: Notification) -> bool:
|
|||||||
await client.close()
|
await client.close()
|
||||||
|
|
||||||
|
|
||||||
def _send_matrix(channel_cfg: dict, notif: Notification) -> bool:
|
|
||||||
"""Dispatch matrix send onto the shared event loop."""
|
|
||||||
if _loop is None:
|
|
||||||
logger.warning("matrix: event loop not available")
|
|
||||||
return False
|
|
||||||
future = asyncio.run_coroutine_threadsafe(_send_matrix_async(channel_cfg, notif), _loop)
|
|
||||||
try:
|
|
||||||
return future.result(timeout=15)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error("matrix send timed out or failed: %s", e)
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Channel dispatcher
|
# Channel dispatcher (all async — sync drivers run in a thread executor)
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# Sync drivers kept for `hbd notify` CLI usage (asyncio.run wraps them there).
|
||||||
_DRIVERS = {
|
_DRIVERS = {
|
||||||
"pushover": _send_pushover,
|
"pushover": _send_pushover,
|
||||||
"email": _send_email,
|
"email": _send_email,
|
||||||
"mattermost": _send_mattermost,
|
"mattermost": _send_mattermost,
|
||||||
"signal": _send_signal,
|
"signal": _send_signal,
|
||||||
"sms_voipms": _send_sms_voipms,
|
|
||||||
"matrix": _send_matrix,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_TIMEOUT = 15 # seconds per channel send
|
||||||
|
|
||||||
def _dispatch_to_channel(channel_name: str, channel_cfg: dict, notif: Notification) -> bool:
|
|
||||||
"""Send *notif* to a single named channel, honouring min_level.
|
|
||||||
|
|
||||||
RECOVER always bypasses min_level — a recovery is always relevant if the
|
async def _dispatch_to_channel(channel_name: str, channel_cfg: dict, notif: Notification) -> bool:
|
||||||
channel was configured for any alerting (handles the restart-then-recover case
|
"""Send *notif* to a single named channel, honouring min_level."""
|
||||||
where _alerted_channels is empty and we fall through to the normal loop).
|
|
||||||
"""
|
|
||||||
level = notif.level.upper()
|
level = notif.level.upper()
|
||||||
if level != "RECOVER":
|
if level != "RECOVER":
|
||||||
min_level = channel_cfg.get("min_level", "WARNING").upper()
|
min_level = channel_cfg.get("min_level", "WARNING").upper()
|
||||||
@@ -398,14 +364,24 @@ def _dispatch_to_channel(channel_name: str, channel_cfg: dict, notif: Notificati
|
|||||||
logger.debug(
|
logger.debug(
|
||||||
"channel '%s': skipping level %s (min_level=%s)", channel_name, level, min_level
|
"channel '%s': skipping level %s (min_level=%s)", channel_name, level, min_level
|
||||||
)
|
)
|
||||||
return True # not an error — filtered intentionally
|
return True # filtered intentionally
|
||||||
|
|
||||||
ch_type = channel_cfg.get("type", "")
|
ch_type = channel_cfg.get("type", "")
|
||||||
driver = _DRIVERS.get(ch_type)
|
try:
|
||||||
if driver is None:
|
if ch_type == "matrix":
|
||||||
|
return await asyncio.wait_for(_send_matrix_async(channel_cfg, notif), timeout=_TIMEOUT)
|
||||||
|
if ch_type == "sms_voipms":
|
||||||
|
return await asyncio.wait_for(_send_sms_voipms_async(channel_cfg, notif), timeout=_TIMEOUT)
|
||||||
|
sync_driver = _DRIVERS.get(ch_type)
|
||||||
|
if sync_driver is None:
|
||||||
logger.warning("unknown channel type '%s' for channel '%s'", ch_type, channel_name)
|
logger.warning("unknown channel type '%s' for channel '%s'", ch_type, channel_name)
|
||||||
return False
|
return False
|
||||||
return driver(channel_cfg, notif)
|
return await asyncio.wait_for(
|
||||||
|
asyncio.to_thread(sync_driver, channel_cfg, notif), timeout=_TIMEOUT
|
||||||
|
)
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
logger.error("channel '%s' timed out after %ds", channel_name, _TIMEOUT)
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -419,7 +395,7 @@ def _build_url(host_name: str) -> str:
|
|||||||
return f"{base_url}/plugins#{host_name}"
|
return f"{base_url}/plugins#{host_name}"
|
||||||
|
|
||||||
|
|
||||||
def send_notification(host_name: str, notif: Notification) -> dict:
|
async def send_notification(host_name: str, notif: Notification) -> dict:
|
||||||
"""Dispatch *notif* to all managers/owner of *host_name*.
|
"""Dispatch *notif* to all managers/owner of *host_name*.
|
||||||
|
|
||||||
Looks up the host's owner + managers, resolves each user's
|
Looks up the host's owner + managers, resolves each user's
|
||||||
@@ -469,16 +445,12 @@ def send_notification(host_name: str, notif: Notification) -> dict:
|
|||||||
if not channel_cfg:
|
if not channel_cfg:
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
ch_type = channel_cfg.get("type", "")
|
ok = await _dispatch_to_channel(channel_name, channel_cfg, notif)
|
||||||
driver = _DRIVERS.get(ch_type)
|
|
||||||
if driver:
|
|
||||||
ok = driver(channel_cfg, notif)
|
|
||||||
results[channel_name] = ok
|
results[channel_name] = ok
|
||||||
if ok:
|
if ok:
|
||||||
logger.info("recover sent to channel '%s': %s", channel_name, notif.title)
|
logger.info("recover sent to channel '%s': %s", channel_name, notif.title)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("error sending recover to channel '%s': %s", channel_name, e)
|
logger.error("error sending recover to channel '%s': %s", channel_name, e)
|
||||||
# Clear the alerted set once recovery is delivered
|
|
||||||
del _alerted_channels[host_name]
|
del _alerted_channels[host_name]
|
||||||
return results
|
return results
|
||||||
|
|
||||||
@@ -489,14 +461,14 @@ def send_notification(host_name: str, notif: Notification) -> dict:
|
|||||||
continue
|
continue
|
||||||
for channel_name in user.notification_channels:
|
for channel_name in user.notification_channels:
|
||||||
if channel_name in results:
|
if channel_name in results:
|
||||||
continue # already dispatched to this channel this notification
|
continue
|
||||||
channel_cfg = global_channels.get(channel_name)
|
channel_cfg = global_channels.get(channel_name)
|
||||||
if not channel_cfg:
|
if not channel_cfg:
|
||||||
logger.warning("channel '%s' not defined in notification_channels", channel_name)
|
logger.warning("channel '%s' not defined in notification_channels", channel_name)
|
||||||
results[channel_name] = False
|
results[channel_name] = False
|
||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
ok = _dispatch_to_channel(channel_name, channel_cfg, notif)
|
ok = await _dispatch_to_channel(channel_name, channel_cfg, notif)
|
||||||
results[channel_name] = ok
|
results[channel_name] = ok
|
||||||
if ok:
|
if ok:
|
||||||
logger.info("notification sent to channel '%s': %s", channel_name, notif.title)
|
logger.info("notification sent to channel '%s': %s", channel_name, notif.title)
|
||||||
|
|||||||
@@ -0,0 +1,199 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
{% include 'head.html' %}
|
||||||
|
|
||||||
|
<style>
|
||||||
|
html, body { overflow: visible; }
|
||||||
|
|
||||||
|
.container {
|
||||||
|
max-width: 700px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
color: #333;
|
||||||
|
margin-bottom: 4px;
|
||||||
|
font-size: 1.5em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.subtitle {
|
||||||
|
color: #666;
|
||||||
|
margin-bottom: 24px;
|
||||||
|
font-size: 0.9em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section {
|
||||||
|
background: #fff;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 1px 6px rgba(0,0,0,0.1);
|
||||||
|
padding: 20px 24px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section h2 {
|
||||||
|
font-size: 1em;
|
||||||
|
font-weight: 700;
|
||||||
|
color: #333;
|
||||||
|
margin: 0 0 16px;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
border-bottom: 1px solid #eee;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.info-row {
|
||||||
|
display: flex;
|
||||||
|
align-items: baseline;
|
||||||
|
padding: 8px 0;
|
||||||
|
border-bottom: 1px solid #f5f5f5;
|
||||||
|
font-size: 0.9em;
|
||||||
|
}
|
||||||
|
.info-row:last-child { border-bottom: none; }
|
||||||
|
|
||||||
|
.info-label {
|
||||||
|
width: 160px;
|
||||||
|
flex-shrink: 0;
|
||||||
|
color: #666;
|
||||||
|
font-size: 0.88em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.info-value {
|
||||||
|
color: #222;
|
||||||
|
word-break: break-all;
|
||||||
|
}
|
||||||
|
|
||||||
|
.info-value a {
|
||||||
|
color: #0066cc;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
.info-value a:hover { text-decoration: underline; }
|
||||||
|
|
||||||
|
.version-badge {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 3px 12px;
|
||||||
|
background: #e8f0fe;
|
||||||
|
color: #1a73e8;
|
||||||
|
border-radius: 12px;
|
||||||
|
font-size: 0.85em;
|
||||||
|
font-weight: 600;
|
||||||
|
font-family: monospace;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hb-logo {
|
||||||
|
font-size: 2.5em;
|
||||||
|
font-weight: 700;
|
||||||
|
color: #0066cc;
|
||||||
|
letter-spacing: -1px;
|
||||||
|
margin-bottom: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hb-tagline {
|
||||||
|
color: #555;
|
||||||
|
font-size: 0.95em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logo-section {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 20px;
|
||||||
|
padding: 8px 0 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.logo-text { flex: 1; }
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
{% include 'nav.html' %}
|
||||||
|
|
||||||
|
<div class="container">
|
||||||
|
<h1>{{ header }}</h1>
|
||||||
|
<p class="subtitle">Heartbeat monitoring system</p>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
<div class="logo-section">
|
||||||
|
<div class="logo-text">
|
||||||
|
<div class="hb-logo">Heartbeat</div>
|
||||||
|
<div class="hb-tagline">Lightweight host monitoring over UDP</div>
|
||||||
|
</div>
|
||||||
|
<span class="version-badge">v{{ hbd_version }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
<h2>Version</h2>
|
||||||
|
<div class="info-row">
|
||||||
|
<span class="info-label">Server version</span>
|
||||||
|
<span class="info-value">{{ hbd_version }}</span>
|
||||||
|
</div>
|
||||||
|
<div class="info-row">
|
||||||
|
<span class="info-label">Python</span>
|
||||||
|
<span class="info-value">{{ python_version }}</span>
|
||||||
|
</div>
|
||||||
|
<div class="info-row">
|
||||||
|
<span class="info-label">License</span>
|
||||||
|
<span class="info-value">MIT</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
<h2>Runtime</h2>
|
||||||
|
<div class="info-row">
|
||||||
|
<span class="info-label">Host</span>
|
||||||
|
<span class="info-value">{{ server_hostname }}</span>
|
||||||
|
</div>
|
||||||
|
<div class="info-row">
|
||||||
|
<span class="info-label">Started</span>
|
||||||
|
<span class="info-value">{{ start_time_str }}</span>
|
||||||
|
</div>
|
||||||
|
<div class="info-row">
|
||||||
|
<span class="info-label">Uptime</span>
|
||||||
|
<span class="info-value" id="uptime-value">{{ uptime_str }}</span>
|
||||||
|
</div>
|
||||||
|
<div class="info-row">
|
||||||
|
<span class="info-label">Hosts monitored</span>
|
||||||
|
<span class="info-value">{{ host_count }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="section">
|
||||||
|
<h2>Contact & Source</h2>
|
||||||
|
<div class="info-row">
|
||||||
|
<span class="info-label">Author</span>
|
||||||
|
<span class="info-value">Andreas Wrede</span>
|
||||||
|
</div>
|
||||||
|
<div class="info-row">
|
||||||
|
<span class="info-label">Email</span>
|
||||||
|
<span class="info-value"><a href="mailto:aew@wrede.ca">aew@wrede.ca</a></span>
|
||||||
|
</div>
|
||||||
|
<div class="info-row">
|
||||||
|
<span class="info-label">Repository</span>
|
||||||
|
<span class="info-value"><a href="https://git.wrede.ca/andreas/heartbeat" target="_blank" rel="noopener">git.wrede.ca/andreas/heartbeat</a></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
(function() {
|
||||||
|
var startEpoch = {{ start_epoch }};
|
||||||
|
var el = document.getElementById('uptime-value');
|
||||||
|
if (!el) return;
|
||||||
|
function fmt(s) {
|
||||||
|
var d = Math.floor(s / 86400);
|
||||||
|
var h = Math.floor((s % 86400) / 3600);
|
||||||
|
var m = Math.floor((s % 3600) / 60);
|
||||||
|
var sec = s % 60;
|
||||||
|
if (d > 0) return d + 'd ' + h + 'h ' + m + 'm';
|
||||||
|
if (h > 0) return h + 'h ' + m + 'm ' + sec + 's';
|
||||||
|
return m + 'm ' + sec + 's';
|
||||||
|
}
|
||||||
|
function tick() {
|
||||||
|
var up = Math.floor(Date.now() / 1000 - startEpoch);
|
||||||
|
el.textContent = fmt(up);
|
||||||
|
}
|
||||||
|
tick();
|
||||||
|
setInterval(tick, 1000);
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
h1 { color: #333; margin-bottom: 10px; font-size: 1.5em; }
|
h1 { color: #333; margin-bottom: 5px; margin-top: 15px; font-size: 1.5em; }
|
||||||
|
|
||||||
.subtitle {
|
.subtitle {
|
||||||
color: #666;
|
color: #666;
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
body {
|
body {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
|
padding-top: 60px;
|
||||||
background: #f5f5f5;
|
background: #f5f5f5;
|
||||||
}
|
}
|
||||||
h1 { font-size: 1.5em; color: #333; margin: 0 0 5px; }
|
h1 { font-size: 1.5em; color: #333; margin: 0 0 5px; }
|
||||||
@@ -23,11 +24,14 @@
|
|||||||
|
|
||||||
/* Navigation bar — shared across all pages */
|
/* Navigation bar — shared across all pages */
|
||||||
.nav {
|
.nav {
|
||||||
|
position: fixed;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
z-index: 200;
|
||||||
background: #fff;
|
background: #fff;
|
||||||
padding: 6px 12px;
|
padding: 6px 12px;
|
||||||
margin-bottom: 10px;
|
|
||||||
box-shadow: 0 2px 4px rgba(0,0,0,.1);
|
box-shadow: 0 2px 4px rgba(0,0,0,.1);
|
||||||
border-radius: 4px;
|
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
|
|||||||
@@ -45,6 +45,7 @@
|
|||||||
h1 {
|
h1 {
|
||||||
color: #333;
|
color: #333;
|
||||||
margin-bottom: 5px;
|
margin-bottom: 5px;
|
||||||
|
margin-top: 15px;
|
||||||
font-size: 1.5em;
|
font-size: 1.5em;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,12 @@
|
|||||||
</button>
|
</button>
|
||||||
<div class="nav-links" id="nav-links">
|
<div class="nav-links" id="nav-links">
|
||||||
<a href="/live"{% if active_page == "live" %} class="active"{% endif %}>Live Dashboard</a>
|
<a href="/live"{% if active_page == "live" %} class="active"{% endif %}>Live Dashboard</a>
|
||||||
<a href="/plugins"{% if active_page == "plugins" %} class="active"{% endif %}>Plugin Metrics</a>
|
<a href="/plugins"{% if active_page == "plugins" %} class="active"{% endif %}>Host Overview</a>
|
||||||
<a href="/alerts"{% if active_page == "alerts" %} class="active"{% endif %}>Alerts</a>
|
<a href="/alerts"{% if active_page == "alerts" %} class="active"{% endif %}>Alerts</a>
|
||||||
{% if current_user and current_user.admin %}
|
{% if current_user and current_user.admin %}
|
||||||
<a href="/settings"{% if active_page == "settings" %} class="active"{% endif %}>Settings</a>
|
<a href="/settings"{% if active_page == "settings" %} class="active"{% endif %}>Settings</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
<a href="/about"{% if active_page == "about" %} class="active"{% endif %}>About</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="nav-clock" title="Click for full-screen clock">
|
<div class="nav-clock" title="Click for full-screen clock">
|
||||||
<canvas id="swiss-clock" width="44" height="44"></canvas>
|
<canvas id="swiss-clock" width="44" height="44"></canvas>
|
||||||
|
|||||||
+884
-855
File diff suppressed because it is too large
Load Diff
@@ -9,7 +9,7 @@
|
|||||||
max-width: 960px;
|
max-width: 960px;
|
||||||
}
|
}
|
||||||
|
|
||||||
h1 { color: #333; margin-bottom: 4px; font-size: 1.5em; }
|
h1 { color: #333; margin-bottom: 5px; margin-top: 15px; font-size: 1.5em; }
|
||||||
.subtitle { color: #666; margin-bottom: 24px; font-size: 0.9em; }
|
.subtitle { color: #666; margin-bottom: 24px; font-size: 0.9em; }
|
||||||
|
|
||||||
/* ---- Sidebar + content layout ---- */
|
/* ---- Sidebar + content layout ---- */
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
width: 180px;
|
width: 180px;
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
position: sticky;
|
position: sticky;
|
||||||
top: 20px;
|
top: 60px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.sidebar-nav a {
|
.sidebar-nav a {
|
||||||
|
|||||||
+4
-11
@@ -987,18 +987,14 @@ class ThresholdChecker:
|
|||||||
value: Any,
|
value: Any,
|
||||||
):
|
):
|
||||||
"""Send notification and log to journal/eventlog."""
|
"""Send notification and log to journal/eventlog."""
|
||||||
try:
|
asyncio.get_event_loop().create_task(notify_mod.send_notification(
|
||||||
notify_mod.send_notification(
|
|
||||||
host_name,
|
host_name,
|
||||||
notify_mod.Notification(
|
notify_mod.Notification(
|
||||||
title=f"[{lvl}] {host_name}",
|
title=f"[{lvl}] {host_name}",
|
||||||
body=message,
|
body=message,
|
||||||
level=lvl,
|
level=lvl,
|
||||||
),
|
),
|
||||||
)
|
))
|
||||||
logger.info("Notification sent: %s", message)
|
|
||||||
except Exception as e:
|
|
||||||
logger.error("Failed to send notification: %s", e)
|
|
||||||
|
|
||||||
# Log to journal
|
# Log to journal
|
||||||
if self.journal is not None:
|
if self.journal is not None:
|
||||||
@@ -1195,20 +1191,17 @@ class ThresholdChecker:
|
|||||||
else:
|
else:
|
||||||
message = f"REMINDER ({alert_state.level.name}): {host_name} - {metric_path} = {value} (ongoing for {int(now - alert_state.since)}s)"
|
message = f"REMINDER ({alert_state.level.name}): {host_name} - {metric_path} = {value} (ongoing for {int(now - alert_state.since)}s)"
|
||||||
|
|
||||||
try:
|
asyncio.get_event_loop().create_task(notify_mod.send_notification(
|
||||||
notify_mod.send_notification(
|
|
||||||
host_name,
|
host_name,
|
||||||
notify_mod.Notification(
|
notify_mod.Notification(
|
||||||
title=f"[REMINDER/{alert_state.level.name}] {host_name}",
|
title=f"[REMINDER/{alert_state.level.name}] {host_name}",
|
||||||
body=message,
|
body=message,
|
||||||
level=alert_state.level.name,
|
level=alert_state.level.name,
|
||||||
),
|
),
|
||||||
)
|
))
|
||||||
alert_state.last_notification = now
|
alert_state.last_notification = now
|
||||||
alert_state.notification_count += 1
|
alert_state.notification_count += 1
|
||||||
logger.info("Re-notification sent: %s", message)
|
logger.info("Re-notification sent: %s", message)
|
||||||
except Exception as e:
|
|
||||||
logger.error("Failed to send re-notification: %s", e)
|
|
||||||
|
|
||||||
def get_active_alerts(self, alert_states: Dict[str, AlertState]) -> list:
|
def get_active_alerts(self, alert_states: Dict[str, AlertState]) -> list:
|
||||||
"""
|
"""
|
||||||
|
|||||||
+12
-15
@@ -211,10 +211,10 @@ def _make_timer_callbacks(uname, host, ctx):
|
|||||||
connection.newstate(connection.__class__.OVERDUE, now, cfg.get("grace", 2))
|
connection.newstate(connection.__class__.OVERDUE, now, cfg.get("grace", 2))
|
||||||
msg = f"{connection.afam} overdue"
|
msg = f"{connection.afam} overdue"
|
||||||
eventlog(uname, "CRITICAL", msg)
|
eventlog(uname, "CRITICAL", msg)
|
||||||
notify_mod.send_notification(
|
asyncio.create_task(notify_mod.send_notification(
|
||||||
uname,
|
uname,
|
||||||
notify_mod.Notification(title=f"[CRITICAL] {uname}", body=msg, level="CRITICAL"),
|
notify_mod.Notification(title=f"[CRITICAL] {uname}", body=msg, level="CRITICAL"),
|
||||||
)
|
))
|
||||||
# Track in alert_states so the Alerts Dashboard shows this
|
# Track in alert_states so the Alerts Dashboard shows this
|
||||||
_set_connectivity_alert(host, connection.afam, "CRITICAL")
|
_set_connectivity_alert(host, connection.afam, "CRITICAL")
|
||||||
if threshold_checker:
|
if threshold_checker:
|
||||||
@@ -315,7 +315,6 @@ def handle_datagram(msg: dict, addr, transport, ctx: dict):
|
|||||||
|
|
||||||
cfg = ctx.get("config", {})
|
cfg = ctx.get("config", {})
|
||||||
hbdcls = ctx.get("hbdclass")
|
hbdcls = ctx.get("hbdclass")
|
||||||
log = ctx.get("log")
|
|
||||||
msg_to_websockets = ctx.get("msg_to_websockets")
|
msg_to_websockets = ctx.get("msg_to_websockets")
|
||||||
DEBUG = ctx.get("DEBUG", 0)
|
DEBUG = ctx.get("DEBUG", 0)
|
||||||
verbose = ctx.get("verbose", False)
|
verbose = ctx.get("verbose", False)
|
||||||
@@ -408,10 +407,10 @@ def handle_datagram(msg: dict, addr, transport, ctx: dict):
|
|||||||
|
|
||||||
if res:
|
if res:
|
||||||
eventlog(uname, "WARNING", res)
|
eventlog(uname, "WARNING", res)
|
||||||
notify_mod.send_notification(
|
asyncio.create_task(notify_mod.send_notification(
|
||||||
uname,
|
uname,
|
||||||
notify_mod.Notification(title=f"[WARNING] {uname}", body=res, level="WARNING"),
|
notify_mod.Notification(title=f"[WARNING] {uname}", body=res, level="WARNING"),
|
||||||
)
|
))
|
||||||
|
|
||||||
interval = int(msg.get("interval", 0) or 0)
|
interval = int(msg.get("interval", 0) or 0)
|
||||||
shutdown = msg.get("shutdown", 0)
|
shutdown = msg.get("shutdown", 0)
|
||||||
@@ -421,10 +420,10 @@ def handle_datagram(msg: dict, addr, transport, ctx: dict):
|
|||||||
|
|
||||||
if boot:
|
if boot:
|
||||||
eventlog(uname, "INFO", "booted")
|
eventlog(uname, "INFO", "booted")
|
||||||
notify_mod.send_notification(
|
asyncio.create_task(notify_mod.send_notification(
|
||||||
uname,
|
uname,
|
||||||
notify_mod.Notification(title=f"[INFO] {uname}", body=f"{host.name} booted", level="INFO"),
|
notify_mod.Notification(title=f"[INFO] {uname}", body=f"{host.name} booted", level="INFO"),
|
||||||
)
|
))
|
||||||
if message:
|
if message:
|
||||||
eventlog(uname, "INFO", "msg: %s" % message, service=service)
|
eventlog(uname, "INFO", "msg: %s" % message, service=service)
|
||||||
|
|
||||||
@@ -441,10 +440,10 @@ def handle_datagram(msg: dict, addr, transport, ctx: dict):
|
|||||||
else:
|
else:
|
||||||
m = "%s back after being %s for %s" % (conn.afam, lasts, dur(d))
|
m = "%s back after being %s for %s" % (conn.afam, lasts, dur(d))
|
||||||
eventlog(uname, "RECOVER", m)
|
eventlog(uname, "RECOVER", m)
|
||||||
notify_mod.send_notification(
|
asyncio.create_task(notify_mod.send_notification(
|
||||||
uname,
|
uname,
|
||||||
notify_mod.Notification(title=f"[RECOVER] {uname}", body=m, level="RECOVER"),
|
notify_mod.Notification(title=f"[RECOVER] {uname}", body=m, level="RECOVER"),
|
||||||
)
|
))
|
||||||
|
|
||||||
if boot or newh:
|
if boot or newh:
|
||||||
host.upcount = host.doesack
|
host.upcount = host.doesack
|
||||||
@@ -454,10 +453,10 @@ def handle_datagram(msg: dict, addr, transport, ctx: dict):
|
|||||||
if shutdown:
|
if shutdown:
|
||||||
m = "%s shutdown" % conn.afam
|
m = "%s shutdown" % conn.afam
|
||||||
eventlog(uname, "INFO", m)
|
eventlog(uname, "INFO", m)
|
||||||
notify_mod.send_notification(
|
asyncio.create_task(notify_mod.send_notification(
|
||||||
uname,
|
uname,
|
||||||
notify_mod.Notification(title=f"[INFO] {uname}", body=m, level="INFO"),
|
notify_mod.Notification(title=f"[INFO] {uname}", body=m, level="INFO"),
|
||||||
)
|
))
|
||||||
conn.newstate(hbdcls.Connection.DOWN, now)
|
conn.newstate(hbdcls.Connection.DOWN, now)
|
||||||
_set_connectivity_alert(host, conn.afam, "CRITICAL")
|
_set_connectivity_alert(host, conn.afam, "CRITICAL")
|
||||||
|
|
||||||
@@ -491,12 +490,10 @@ def handle_datagram(msg: dict, addr, transport, ctx: dict):
|
|||||||
op, rmsg = host.cmds[0]
|
op, rmsg = host.cmds[0]
|
||||||
if op == "CMD":
|
if op == "CMD":
|
||||||
del host.cmds[0]
|
del host.cmds[0]
|
||||||
if log:
|
eventlog(uname, "INFO", "command sent")
|
||||||
log(uname, "command sent")
|
|
||||||
elif op == "UPD":
|
elif op == "UPD":
|
||||||
del host.cmds[0]
|
del host.cmds[0]
|
||||||
if log:
|
eventlog(uname, "INFO", "update initiated")
|
||||||
log(uname, "update initiated")
|
|
||||||
opkt = dicttos(op, rmsg)
|
opkt = dicttos(op, rmsg)
|
||||||
try:
|
try:
|
||||||
transport.sendto(opkt, addr)
|
transport.sendto(opkt, addr)
|
||||||
|
|||||||
+7
-1
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "hbd"
|
name = "hbd"
|
||||||
version = "5.1.3"
|
version = "5.1.10"
|
||||||
description = "Heartbeat monitoring system — client (hbc) and server (hbd)"
|
description = "Heartbeat monitoring system — client (hbc) and server (hbd)"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.11"
|
requires-python = ">=3.11"
|
||||||
@@ -34,6 +34,9 @@ server = [
|
|||||||
"matrix-nio>=0.24",
|
"matrix-nio>=0.24",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# Minimal client — hbc_mini only, no external dependencies
|
||||||
|
mini = []
|
||||||
|
|
||||||
# Install both client and server
|
# Install both client and server
|
||||||
all = [
|
all = [
|
||||||
"hbd[client,server]",
|
"hbd[client,server]",
|
||||||
@@ -54,6 +57,9 @@ dev = [
|
|||||||
hbd = "hbd.server.cli:main"
|
hbd = "hbd.server.cli:main"
|
||||||
hbc = "hbd.client.main:main"
|
hbc = "hbd.client.main:main"
|
||||||
|
|
||||||
|
[tool.setuptools]
|
||||||
|
script-files = ["scripts/hb_install.sh", "scripts/hbc_mini.py"]
|
||||||
|
|
||||||
[tool.setuptools.packages.find]
|
[tool.setuptools.packages.find]
|
||||||
where = ["."]
|
where = ["."]
|
||||||
include = ["hbd*"]
|
include = ["hbd*"]
|
||||||
|
|||||||
@@ -4,12 +4,14 @@ set -e
|
|||||||
uv version --bump patch
|
uv version --bump patch
|
||||||
VER=$(uv version --short)
|
VER=$(uv version --short)
|
||||||
sed -i".bak" "s/__version__ = \"[0-9.]*\"\(.*\)$/__version__ = \"$VER\"\1/" hbd/__init__.py
|
sed -i".bak" "s/__version__ = \"[0-9.]*\"\(.*\)$/__version__ = \"$VER\"\1/" hbd/__init__.py
|
||||||
|
sed -i".bak" "s/__version__ = \"[0-9.]*\"\(.*\)$/__version__ = \"$VER\"\1/" scripts/hbc_mini.py
|
||||||
|
|
||||||
# commit pyproject.toml
|
# commit pyproject.toml
|
||||||
git commit -m "version $VER" pyproject.toml hbd/__init__.py
|
git commit -m "version $VER" pyproject.toml hbd/__init__.py scripts/hbc_mini.py
|
||||||
git push
|
git push
|
||||||
# tag version
|
# tag version
|
||||||
git tag -a v$VER -m "Version $VER"
|
git tag -a v$VER -m "Version $VER"
|
||||||
git push --tags
|
git push --tags
|
||||||
|
|
||||||
rm hbd/__init__.py.bak
|
rm hbd/__init__.py.bak
|
||||||
|
rm scripts/hbc_mini.py.bak
|
||||||
|
|||||||
Executable
+132
@@ -0,0 +1,132 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Helper script to install the heartbeat tools. By default, it will only
|
||||||
|
# install the heartbeat client, hbc. The server is installed when the arg 'server' is passed
|
||||||
|
# to the script. The script will install the heartbeat tools in a python
|
||||||
|
# virtual environment in ~/venvs/hbd. The hbd and hbc commands will be
|
||||||
|
# installed from the wheel and symlinked to ~/bin/hbd and ~/bin/hbc,
|
||||||
|
# respectively. If the virtual environment already exists, it will be
|
||||||
|
# reused. The script will also remove any existing symlinks for hbd and hbc
|
||||||
|
# in ~/bin before creating new ones.
|
||||||
|
|
||||||
|
set -e
|
||||||
|
what=$1
|
||||||
|
on_ha=0
|
||||||
|
where=""
|
||||||
|
venv=""
|
||||||
|
prog=$(realpath $0)
|
||||||
|
[ "$2" = "HA" ] && on_ha=1
|
||||||
|
[ -z "$what" ] && what="client"
|
||||||
|
|
||||||
|
if [ -d /homeassistant ]; then
|
||||||
|
echo "HA, running \"docker exec homeassistant $prog $@\""
|
||||||
|
docker exec homeassistant $prog $@ HA
|
||||||
|
rc=$?
|
||||||
|
if [ $rc -ne 0 ]; then
|
||||||
|
echo "Failed to install heartbeat in HA, please check the logs for more details"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $on_ha -eq 1 ]; then
|
||||||
|
echo "Installing under docker on Home Assistant OS, using /config/bin for executables and /config/venvs for virtual environments "
|
||||||
|
where="/config/bin"
|
||||||
|
venv="/config/venvs"
|
||||||
|
else
|
||||||
|
if [ ! -d $HOME/.local/bin ] && [ ! -d $HOME/bin ]; then
|
||||||
|
echo "No suitable bin directory found in PATH, please add either $HOME/.local/bin or $HOME/bin to your PATH"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
for where in $HOME/bin $HOME/.local/bin notset ; do
|
||||||
|
if echo ":$PATH:" | grep -q ":$where:" ; then
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ "$where" = "notset" ]; then
|
||||||
|
echo "No suitable bin directory found in PATH, please add either $HOME/.local/bin or $HOME/bin to your PATH"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [ "$what" = "mini" ]; then
|
||||||
|
venv=""
|
||||||
|
else
|
||||||
|
venv="$HOME/venvs"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Installing heartbeat $what"
|
||||||
|
|
||||||
|
if [ "$venv" != "" ] && [ ! -d $venv/hbd ]; then
|
||||||
|
set +e
|
||||||
|
python3 -m pip --version > /dev/null 2>&1
|
||||||
|
rc=$?
|
||||||
|
set -e
|
||||||
|
arg=""
|
||||||
|
if [ $rc -ne 0 ]; then
|
||||||
|
# some systems do not have pip installed by default, so we need to fetch get-pip.py and install pip
|
||||||
|
echo "pip is not installed, fetching get-pip.py and installing pip"
|
||||||
|
arg="--without-pip"
|
||||||
|
fi
|
||||||
|
mkdir -p $venv
|
||||||
|
have_venv=$(python3 -c "import venv" &> /dev/null && echo "Installed" || echo "Not Installed")
|
||||||
|
if [ "$have_venv" = "Not Installed" ]; then
|
||||||
|
echo "python venv module not found, installing virtualenv"
|
||||||
|
python3 -m pip install --user virtualenv
|
||||||
|
python3 -m virtualenv $venv/hbd --system-site-packages $arg
|
||||||
|
else
|
||||||
|
python3 -m venv $venv/hbd --system-site-packages $arg
|
||||||
|
fi
|
||||||
|
. $venv/hbd/bin/activate
|
||||||
|
if [ -n "$arg" ]; then
|
||||||
|
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py
|
||||||
|
fi
|
||||||
|
deactivate
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$venv" ]; then
|
||||||
|
echo "Installing heartbeat $what globally"
|
||||||
|
else
|
||||||
|
echo "Installing heartbeat $what in virtual environment $venv/hbd"
|
||||||
|
. $venv/hbd/bin/activate
|
||||||
|
fi
|
||||||
|
if [ "$what" = "mini" ]; then
|
||||||
|
echo "Installing hbc mini, which has no external dependencies and is meant for quick setup and testing. For the full client with all features, please run this script with the 'client' argument."
|
||||||
|
curl -s -o $where/hbc_mini https://git.wrede.ca/andreas/heartbeat/raw/branch/master/scripts/hbc_mini.py
|
||||||
|
chmod +x $where/hbc_mini
|
||||||
|
else
|
||||||
|
echo "Installing heartbeat $what, which includes the full client with all features. If you want to install the minimal client with no external dependencies, please run this script with the 'mini' argument."
|
||||||
|
python3 -mpip install --upgrade --index-url https://git.wrede.ca/api/packages/andreas/pypi/simple/ --extra-index-url https://pypi.org/simple hbd[$what]
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$what" = "server" ]; then
|
||||||
|
rm -f $where/hbd
|
||||||
|
ln -sf $(which hbd) $where/hbd
|
||||||
|
echo "hbd installed, you can run it with \"$where/hbd\" or \"hbd\" if $where is in your PATH"
|
||||||
|
elif [ "$what" = "client" ]; then
|
||||||
|
hbc_path=$(which hbc)
|
||||||
|
if [ -z "$hbc_path" ]; then
|
||||||
|
echo "hbc not found in PATH, installation failed"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
if [ "$hbc_path" != "$where/hbc" ]; then
|
||||||
|
rm -f $where/hbc
|
||||||
|
ln -sf $(which hbc) $where/hbc
|
||||||
|
fi
|
||||||
|
if [ "$prog" != "$where/hb_install.sh" ]; then
|
||||||
|
cp "$prog" $where/hb_install.sh
|
||||||
|
chmod +x $where/hb_install.sh
|
||||||
|
fi
|
||||||
|
if [ $on_ha -eq 1 ]; then
|
||||||
|
echo "restarting hbc "
|
||||||
|
job=$(grep run_hbc configuration.yaml | sed 's/run_hbc://')
|
||||||
|
$job
|
||||||
|
else
|
||||||
|
echo "hbc installed, you can run it with \"$where/hbc\" or \"hbc\" if $where is in your PATH"
|
||||||
|
fi
|
||||||
|
elif [ "$what" = "mini" ]; then
|
||||||
|
hbc_path=$(which hbc_mini)
|
||||||
|
if [ "$hbc_path" != "$where/hbc_mini" ]; then
|
||||||
|
ln -sf $hbc_path $where/hbc_mini
|
||||||
|
fi
|
||||||
|
echo "hbc mini installed, you can run it with \"$where/hbc_mini\" or \"hbc_mini\" if $where is in your PATH"
|
||||||
|
fi
|
||||||
Executable
+1147
File diff suppressed because it is too large
Load Diff
@@ -1,88 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
# install the heartbeat client, hbc. The server is installed when the arg 'server' is passed
|
|
||||||
# install the heartbeat client, hbc. The server is installed when the arg 'server' is passed
|
|
||||||
# to the script. The script will install the heartbeat tools in a python
|
|
||||||
# virtual environment in ~/venvs/hbd. The hbd and hbc commands will be
|
|
||||||
# installed from the wheel and symlinked to ~/bin/hbd and ~/bin/hbc,
|
|
||||||
# respectively. If the virtual environment already exists, it will be
|
|
||||||
# reused. The script will also remove any existing symlinks for hbd and hbc
|
|
||||||
# in ~/bin before creating new ones.
|
|
||||||
|
|
||||||
|
|
||||||
# hbd/hbc from wheel and create symlinks for hbd and hbc in ~/bin
|
|
||||||
|
|
||||||
set -e
|
|
||||||
what=$1
|
|
||||||
on_ha=0
|
|
||||||
[ -z "$what" ] && what="client"
|
|
||||||
|
|
||||||
if [ -d /homeassistant ]; then
|
|
||||||
echo "cannot install in HA, run \"docker exec -it homeassistant $0 $@\""
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
if [ -d /config ]; then
|
|
||||||
echo "Installing on HA"
|
|
||||||
where="/config/bin"
|
|
||||||
venv="/config/venvs"
|
|
||||||
on_ha=1
|
|
||||||
else
|
|
||||||
if [ ! -d $HOME/.local/bin ] && [ ! -d $HOME/bin ]; then
|
|
||||||
echo "No suitable bin directory found in PATH, please add either $HOME/.local/bin or $HOME/bin to your PATH"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
for where in $HOME/bin $HOME/.local/bin notset ; do
|
|
||||||
if echo ":$PATH:" | grep -q ":$where:" ; then
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
if [ "$where" = "notset" ]; then
|
|
||||||
echo "No suitable bin directory found in PATH, please add either $HOME/.local/bin or $HOME/bin to your PATH"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
venv="$HOME/venvs"
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "Installing heartbeat $what"
|
|
||||||
|
|
||||||
if [ ! -d $venv/hbd ]; then
|
|
||||||
python3 -m pip --version > /dev/null 2>&1
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
# truenas does not have pip installed by default, so we need to fetch get-pip.py and install pip
|
|
||||||
echo "pip is not installed, fetching get-pip.py and installing pip"
|
|
||||||
arg="--without-pip"
|
|
||||||
fi
|
|
||||||
mkdir -p $venv
|
|
||||||
have_venv=$(python3 -c "import venv" &> /dev/null && echo "Installed" || echo "Not Installed")
|
|
||||||
if [ "$have_venv" = "Not Installed" ]; then
|
|
||||||
echo "python venv module not found, installing virtualenv"
|
|
||||||
python3 -m pip install --user virtualenv
|
|
||||||
python3 -m virtualenv $venv/hbd --system-site-packages $arg
|
|
||||||
else
|
|
||||||
python3 -m venv $venv/hbd --system-site-packages $arg
|
|
||||||
fi
|
|
||||||
. $venv/hbd/bin/activate
|
|
||||||
if [ -n "$arg" ]; then
|
|
||||||
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py
|
|
||||||
fi
|
|
||||||
deactivate
|
|
||||||
fi
|
|
||||||
|
|
||||||
. $venv/hbd/bin/activate
|
|
||||||
python3 -mpip install --upgrade --index-url https://git.wrede.ca/api/packages/andreas/pypi/simple/ --extra-index-url https://pypi.org/simple hbd[$what]
|
|
||||||
|
|
||||||
if [ "$what" = "server" ]; then
|
|
||||||
rm -f $where/hbd
|
|
||||||
ln -sf $(which hbd) $where/hbd
|
|
||||||
echo "hbd installed, you can run it with \"$where/hbd\" or \"hbd\" if $where is in your PATH"
|
|
||||||
else
|
|
||||||
rm -f $where/hbc
|
|
||||||
ln -sf $(which hbc) $where/hbc
|
|
||||||
if [ $on_ha -eq 1 ]; then
|
|
||||||
echo "restarting hbc "
|
|
||||||
job=$(grep run_hbc configuration.yaml | sed 's/run_hbc://')
|
|
||||||
$job
|
|
||||||
else
|
|
||||||
echo "hbc installed, you can run it with \"$where/hbc\" or \"hbc\" if $where is in your PATH"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
Reference in New Issue
Block a user