Compare commits
25 Commits
9bd0c16a90
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d5713ed36 | ||
|
|
cc26726ddf | ||
|
|
8cb33dbc90 | ||
|
|
50dcfcdc83 | ||
|
|
761b91b031 | ||
|
|
0a37b04506 | ||
|
|
a5a683d1de | ||
|
|
e0b8f8650b | ||
|
|
3a7b5a0f9a | ||
|
|
999a595b9c | ||
|
|
e6f705486d | ||
|
|
66734923cb | ||
|
|
48d2d7f83d | ||
|
|
444bda7263 | ||
|
|
99fc1a28ad | ||
|
|
2075cd2901 | ||
|
|
f4ec33fe53 | ||
|
|
3080cb1e87 | ||
|
|
89fb5bbf7d | ||
|
|
6c6bc0d57f | ||
|
|
3dbd60ac27 | ||
|
|
5664105111 | ||
|
|
b87c61ea99 | ||
|
|
98ecc61624 | ||
|
|
9184de0a1d |
59
.coolify
59
.coolify
@@ -1,59 +0,0 @@
|
|||||||
# Nerd Monitor - Coolify Configuration
|
|
||||||
# This file documents the Coolify setup for Nerd Monitor
|
|
||||||
|
|
||||||
# Build Pack: Dockerfile (Custom)
|
|
||||||
# The project includes a Dockerfile optimized for Coolify
|
|
||||||
|
|
||||||
# Build Configuration:
|
|
||||||
# - Base Image: golang:1.23-alpine (builder), alpine:latest (runtime)
|
|
||||||
# - Build Context: . (repository root)
|
|
||||||
# - Dockerfile: ./Dockerfile
|
|
||||||
|
|
||||||
# Ports
|
|
||||||
# - Internal: 8080
|
|
||||||
# - External: 8080 (configurable in Coolify UI)
|
|
||||||
|
|
||||||
# Environment Variables (set in Coolify):
|
|
||||||
# ADDR=0.0.0.0
|
|
||||||
# PORT=8080
|
|
||||||
# USERNAME=admin (change in production)
|
|
||||||
# PASSWORD=admin (change in production)
|
|
||||||
|
|
||||||
# Health Check: Enabled
|
|
||||||
# - Endpoint: http://localhost:8080/login
|
|
||||||
# - Interval: 30s
|
|
||||||
# - Timeout: 3s
|
|
||||||
# - Retries: 3
|
|
||||||
# - Start Period: 5s
|
|
||||||
|
|
||||||
# Auto-Deploy on Push:
|
|
||||||
# 1. Connect repository to Coolify (Gitea)
|
|
||||||
# 2. Set branch to: master
|
|
||||||
# 3. Enable auto-deploy on push
|
|
||||||
# 4. Coolify will automatically build on every push to master
|
|
||||||
|
|
||||||
# Build Optimization:
|
|
||||||
# - Multi-stage build reduces final image size
|
|
||||||
# - Alpine-based for minimal footprint (~40MB final image)
|
|
||||||
# - Non-root user for security
|
|
||||||
# - Strip binaries (-w -s flags) to reduce size
|
|
||||||
|
|
||||||
# CI/CD Pipeline Flow:
|
|
||||||
# Push to master → Gitea webhook → Coolify detects
|
|
||||||
# → Coolify builds Dockerfile → New image created
|
|
||||||
# → Coolify deploys container → Service available
|
|
||||||
|
|
||||||
# Related Files:
|
|
||||||
# - Dockerfile: Server container definition
|
|
||||||
# - Dockerfile.agent: Agent container definition
|
|
||||||
# - docker-compose.yml: Local development setup
|
|
||||||
# - .dockerignore: Files excluded from Docker build context
|
|
||||||
|
|
||||||
# For local testing before pushing:
|
|
||||||
# docker-compose up -d
|
|
||||||
|
|
||||||
# For production deployment via Coolify:
|
|
||||||
# 1. Set USERNAME and PASSWORD in Coolify environment variables
|
|
||||||
# 2. Configure domain/SSL if needed
|
|
||||||
# 3. Set resource limits (CPU, Memory)
|
|
||||||
# 4. Configure auto-scaling if needed
|
|
||||||
@@ -1,54 +1,39 @@
|
|||||||
# Git
|
# Git
|
||||||
.git
|
.git
|
||||||
.gitignore
|
.gitignore
|
||||||
.gitmodules
|
.gitattributes
|
||||||
|
|
||||||
# Build artifacts
|
# Development
|
||||||
bin/
|
.vscode
|
||||||
dist/
|
.idea
|
||||||
*.exe
|
|
||||||
*.dll
|
|
||||||
*.so
|
|
||||||
*.dylib
|
|
||||||
|
|
||||||
# Generated code
|
|
||||||
views/*_templ.go
|
|
||||||
|
|
||||||
# IDE
|
|
||||||
.vscode/
|
|
||||||
.idea/
|
|
||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
*~
|
*~
|
||||||
.DS_Store
|
.DS_Store
|
||||||
*.iml
|
|
||||||
|
|
||||||
# Go
|
# Build artifacts
|
||||||
|
bin/
|
||||||
|
dist/
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
|
*.so
|
||||||
|
|
||||||
|
# Dependencies
|
||||||
vendor/
|
vendor/
|
||||||
.env
|
|
||||||
.env.local
|
|
||||||
*.test
|
|
||||||
|
|
||||||
# OS
|
|
||||||
Thumbs.db
|
|
||||||
.DS_Store
|
|
||||||
*.log
|
|
||||||
|
|
||||||
# Docker
|
|
||||||
Dockerfile
|
|
||||||
docker-compose.yml
|
|
||||||
.dockerignore
|
|
||||||
|
|
||||||
# CI/CD
|
|
||||||
.github/
|
|
||||||
.gitlab-ci.yml
|
|
||||||
.circleci/
|
|
||||||
|
|
||||||
# Node (if added in future)
|
|
||||||
node_modules/
|
|
||||||
package-lock.json
|
|
||||||
yarn.lock
|
|
||||||
|
|
||||||
# Documentation
|
# Documentation
|
||||||
*.md
|
*.md
|
||||||
CHANGELOG
|
RELEASE.md
|
||||||
|
README.md
|
||||||
|
QUICKSTART.md
|
||||||
|
AGENTS.md
|
||||||
|
|
||||||
|
# Other
|
||||||
|
docker-compose.yml
|
||||||
|
Dockerfile
|
||||||
|
Dockerfile.server
|
||||||
|
Dockerfile.agent
|
||||||
|
.dockerignore
|
||||||
|
.github
|
||||||
|
.gitea
|
||||||
|
scripts/
|
||||||
|
|||||||
157
.gitea/workflows/release.yml
Normal file
157
.gitea/workflows/release.yml
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
name: Build and Release
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- master
|
||||||
|
tags:
|
||||||
|
- 'v*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
run: |
|
||||||
|
cd $GITHUB_WORKSPACE
|
||||||
|
git init
|
||||||
|
git remote add origin ${{ github.server_url }}/${{ github.repository }}.git
|
||||||
|
|
||||||
|
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
|
||||||
|
# For tags, fetch the specific tag and checkout the commit it points to
|
||||||
|
git fetch origin ${{ github.ref }}:refs/tags/${{ github.ref_name }}
|
||||||
|
git checkout refs/tags/${{ github.ref_name }}
|
||||||
|
else
|
||||||
|
# For branches, fetch and checkout with tracking
|
||||||
|
git fetch origin ${{ github.ref_name }}:refs/remotes/origin/${{ github.ref_name }}
|
||||||
|
git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }}
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Set up Go
|
||||||
|
run: |
|
||||||
|
wget https://go.dev/dl/go1.24.4.linux-amd64.tar.gz
|
||||||
|
tar -C /usr/local -xzf go1.24.4.linux-amd64.tar.gz
|
||||||
|
export PATH=$PATH:/usr/local/go/bin
|
||||||
|
go version
|
||||||
|
|
||||||
|
- name: Generate version
|
||||||
|
id: version
|
||||||
|
run: |
|
||||||
|
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
|
||||||
|
VERSION=${{ github.ref_name }}
|
||||||
|
else
|
||||||
|
VERSION=dev-${{ github.sha }}
|
||||||
|
fi
|
||||||
|
echo "version=${VERSION}" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Build all binaries
|
||||||
|
run: |
|
||||||
|
export PATH=$PATH:/usr/local/go/bin
|
||||||
|
mkdir -p bin
|
||||||
|
|
||||||
|
# Generate templ first
|
||||||
|
go run github.com/a-h/templ/cmd/templ@latest generate
|
||||||
|
|
||||||
|
# Linux AMD64
|
||||||
|
echo "Building Linux AMD64..."
|
||||||
|
GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-server-linux-amd64 ./cmd/server
|
||||||
|
GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-agent-linux-amd64 ./cmd/agent
|
||||||
|
|
||||||
|
# Linux ARM64
|
||||||
|
echo "Building Linux ARM64..."
|
||||||
|
GOOS=linux GOARCH=arm64 go build -ldflags="-w -s" -o bin/nerd-monitor-server-linux-arm64 ./cmd/server
|
||||||
|
GOOS=linux GOARCH=arm64 go build -ldflags="-w -s" -o bin/nerd-monitor-agent-linux-arm64 ./cmd/agent
|
||||||
|
|
||||||
|
# macOS AMD64
|
||||||
|
echo "Building macOS AMD64..."
|
||||||
|
GOOS=darwin GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-server-darwin-amd64 ./cmd/server
|
||||||
|
GOOS=darwin GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-agent-darwin-amd64 ./cmd/agent
|
||||||
|
|
||||||
|
# macOS ARM64
|
||||||
|
echo "Building macOS ARM64..."
|
||||||
|
GOOS=darwin GOARCH=arm64 go build -ldflags="-w -s" -o bin/nerd-monitor-server-darwin-arm64 ./cmd/server
|
||||||
|
GOOS=darwin GOARCH=arm64 go build -ldflags="-w -s" -o bin/nerd-monitor-agent-darwin-arm64 ./cmd/agent
|
||||||
|
|
||||||
|
# Windows AMD64
|
||||||
|
echo "Building Windows AMD64..."
|
||||||
|
GOOS=windows GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-server-windows-amd64.exe ./cmd/server
|
||||||
|
GOOS=windows GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-agent-windows-amd64.exe ./cmd/agent
|
||||||
|
|
||||||
|
echo "Build complete! Files:"
|
||||||
|
ls -lh bin/
|
||||||
|
|
||||||
|
- name: Create checksums
|
||||||
|
run: |
|
||||||
|
cd bin
|
||||||
|
sha256sum * > SHA256SUMS
|
||||||
|
cd ..
|
||||||
|
echo "Checksums:"
|
||||||
|
cat bin/SHA256SUMS
|
||||||
|
|
||||||
|
- name: Create Release and Upload
|
||||||
|
if: startsWith(github.ref, 'refs/tags/')
|
||||||
|
run: |
|
||||||
|
export GITEA_TOKEN="${{ secrets.GITEA_TOKEN }}"
|
||||||
|
export GITEA_URL="${{ github.server_url }}"
|
||||||
|
export REPO_OWNER="${{ github.repository_owner }}"
|
||||||
|
export REPO_NAME="${{ github.repository }}"
|
||||||
|
export REPO_NAME=${REPO_NAME#*/}
|
||||||
|
|
||||||
|
TAG=${{ github.ref_name }}
|
||||||
|
|
||||||
|
echo "Creating release for tag: $TAG"
|
||||||
|
echo "Repository: $REPO_OWNER/$REPO_NAME"
|
||||||
|
|
||||||
|
# Create release using Gitea API with wget
|
||||||
|
echo "Creating new release..."
|
||||||
|
|
||||||
|
# Create JSON payload in a temp file
|
||||||
|
cat > /tmp/release.json << 'PAYLOAD'
|
||||||
|
{"tag_name":"TAG_PLACEHOLDER","name":"Release TAG_PLACEHOLDER","draft":false,"prerelease":false}
|
||||||
|
PAYLOAD
|
||||||
|
sed -i "s/TAG_PLACEHOLDER/$TAG/g" /tmp/release.json
|
||||||
|
|
||||||
|
RESPONSE=$(wget --post-file=/tmp/release.json \
|
||||||
|
--header="Authorization: token $GITEA_TOKEN" \
|
||||||
|
--header="Content-Type: application/json" \
|
||||||
|
-O - -q \
|
||||||
|
"$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases")
|
||||||
|
|
||||||
|
# Extract release ID using grep
|
||||||
|
RELEASE_ID=$(echo "$RESPONSE" | grep -o '"id":[0-9]*' | head -1 | grep -o '[0-9]*')
|
||||||
|
|
||||||
|
if [ -z "$RELEASE_ID" ]; then
|
||||||
|
echo "Failed to create release. Response:"
|
||||||
|
echo "$RESPONSE"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Created release ID: $RELEASE_ID"
|
||||||
|
|
||||||
|
# Upload all binaries
|
||||||
|
echo "Uploading release artifacts..."
|
||||||
|
for file in bin/*; do
|
||||||
|
if [ -f "$file" ]; then
|
||||||
|
filename=$(basename "$file")
|
||||||
|
echo " Uploading: $filename"
|
||||||
|
|
||||||
|
# Upload binary file to Gitea API
|
||||||
|
UPLOAD_RESPONSE=$(wget --post-file="$file" \
|
||||||
|
--header="Authorization: token $GITEA_TOKEN" \
|
||||||
|
--header="Content-Type: application/octet-stream" \
|
||||||
|
-O - -q \
|
||||||
|
"$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases/$RELEASE_ID/assets?name=$filename" 2>&1)
|
||||||
|
|
||||||
|
if echo "$UPLOAD_RESPONSE" | grep -q '"id"'; then
|
||||||
|
echo " ✓ $filename uploaded"
|
||||||
|
else
|
||||||
|
echo " ✗ Failed to upload $filename"
|
||||||
|
echo "Response: $UPLOAD_RESPONSE"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Release completed!"
|
||||||
|
echo "View at: $GITEA_URL/$REPO_OWNER/$REPO_NAME/releases/tag/$TAG"
|
||||||
7
.gitignore
vendored
7
.gitignore
vendored
@@ -32,7 +32,6 @@ Thumbs.db
|
|||||||
config.local.yaml
|
config.local.yaml
|
||||||
.env.*.local
|
.env.*.local
|
||||||
|
|
||||||
# Docker volumes/containers (local development)
|
# Temporary build files
|
||||||
# Note: Keep Dockerfile, Dockerfile.agent, docker-compose.yml, and .dockerignore in git
|
agent
|
||||||
docker-volumes/
|
server
|
||||||
*.tar.gz
|
|
||||||
|
|||||||
50
AGENTS.md
50
AGENTS.md
@@ -1,42 +1,20 @@
|
|||||||
# Nerd Monitor - Agent Guidelines
|
# Nerd Monitor - Agent Guidelines
|
||||||
|
|
||||||
## Build & Test Commands
|
## Build & Test Commands
|
||||||
|
- **Generate templates**: `make templ`
|
||||||
|
- **Build**: `make build` (server + agent) or `make build-server`/`make build-agent`
|
||||||
|
- **Cross-platform**: `make build-all`
|
||||||
|
- **Clean**: `make clean`
|
||||||
|
- **Test all**: `go test ./...`
|
||||||
|
- **Test single**: `go test -run TestName ./package`
|
||||||
|
- **Format**: `gofmt -w ./cmd ./internal ./views`
|
||||||
|
|
||||||
```bash
|
Note: Go 1.23+ project using Templ templating. Run `make templ` before building.
|
||||||
# Generate Templ templates (required before building)
|
|
||||||
make templ
|
|
||||||
|
|
||||||
# Build server and agent for current OS
|
|
||||||
make build
|
|
||||||
|
|
||||||
# Build individual components
|
|
||||||
make build-server
|
|
||||||
make build-agent
|
|
||||||
|
|
||||||
# Build for all platforms (Linux, macOS, Windows)
|
|
||||||
make build-all
|
|
||||||
|
|
||||||
# Clean build artifacts
|
|
||||||
make clean
|
|
||||||
```
|
|
||||||
|
|
||||||
Note: This Go 1.21 project uses Templ for templating. Always run `make templ` before building.
|
|
||||||
|
|
||||||
## Code Style Guidelines
|
## Code Style Guidelines
|
||||||
|
- **Imports**: stdlib → third-party → internal (alphabetical within groups)
|
||||||
**Imports**: Standard library first, then third-party packages, then internal modules. Organize alphabetically within each group.
|
- **Formatting**: `gofmt` standards, explicit exported struct comments
|
||||||
|
- **Naming**: CamelCase exports, lowercase unexported, single-letter receivers (e.g., `(h *Handler)`)
|
||||||
**Formatting & Types**: Use `gofmt` standards. Explicit struct field comments for exported types. Prefer `error` return values (no panic for recoverable errors).
|
- **Error Handling**: Explicit checks, `http.Error()` for HTTP, contextual wrapping
|
||||||
|
- **Concurrency**: `sync.RWMutex` for shared state, defer lock releases, small critical sections
|
||||||
**Naming**: CamelCase for exported symbols, lowercase for unexported. Receiver names as single letters (e.g., `(m *Manager)`). Constants use ALL_CAPS_SNAKE_CASE.
|
- **HTTP**: Chi router, `func (h *Handler) Method(w http.ResponseWriter, r *http.Request)`, set Content-Type, validate early
|
||||||
|
|
||||||
**Error Handling**: Define sentinel errors in dedicated files (e.g., `auth/errors.go`). Check errors explicitly; use `http.Error(w, "message", status)` for HTTP responses. Wrap errors with context when needed.
|
|
||||||
|
|
||||||
**Project Structure**:
|
|
||||||
- `cmd/`: Executable entry points (server, agent)
|
|
||||||
- `internal/`: Packages (api, auth, stats, store, ui)
|
|
||||||
- `views/`: Templ templates (*.templ files)
|
|
||||||
|
|
||||||
**Concurrency**: Use `sync.RWMutex` for shared state. Always defer lock releases. Keep critical sections small.
|
|
||||||
|
|
||||||
**HTTP**: Use Chi router. Handlers follow `func (h *Handler) Name(w http.ResponseWriter, r *http.Request)`. Set Content-Type headers explicitly. Validate query parameters early and return 400/404 appropriately.
|
|
||||||
|
|||||||
365
DOCKER.md
365
DOCKER.md
@@ -1,365 +0,0 @@
|
|||||||
# Docker & Coolify Deployment Guide
|
|
||||||
|
|
||||||
## Overview
|
|
||||||
|
|
||||||
Nerd Monitor is optimized for containerized deployment using Docker and Coolify CI/CD. This guide covers setup with Coolify, local development with Docker Compose, and deployment options.
|
|
||||||
|
|
||||||
## Quick Start with Coolify
|
|
||||||
|
|
||||||
### Prerequisites
|
|
||||||
- Gitea repository configured
|
|
||||||
- Coolify instance running
|
|
||||||
- Docker/container runtime available
|
|
||||||
|
|
||||||
### Setup Steps
|
|
||||||
|
|
||||||
1. **Connect Repository to Coolify**
|
|
||||||
- In Coolify, add new application
|
|
||||||
- Select "Git Repository"
|
|
||||||
- Choose your Gitea instance
|
|
||||||
- Select `nerd-monitor` repository
|
|
||||||
|
|
||||||
2. **Configure Build Settings**
|
|
||||||
- **Build Pack**: Dockerfile
|
|
||||||
- **Dockerfile Path**: `./Dockerfile`
|
|
||||||
- **Build Context**: `.` (root)
|
|
||||||
- **Docker Image**: `nerd-monitor-server:latest`
|
|
||||||
|
|
||||||
3. **Set Environment Variables**
|
|
||||||
In Coolify environment configuration:
|
|
||||||
```
|
|
||||||
ADDR=0.0.0.0
|
|
||||||
PORT=8080
|
|
||||||
USERNAME=admin
|
|
||||||
PASSWORD=secure_password_here
|
|
||||||
```
|
|
||||||
|
|
||||||
4. **Enable Auto-Deploy**
|
|
||||||
- Set **Watch** to: `master`
|
|
||||||
- Enable **Auto-deploy on push**
|
|
||||||
- Coolify will automatically build and deploy on every push to master
|
|
||||||
|
|
||||||
5. **Configure Ports**
|
|
||||||
- **Container Port**: 8080
|
|
||||||
- **Public Port**: 8080 (or as needed)
|
|
||||||
- **Protocol**: HTTP/HTTPS
|
|
||||||
|
|
||||||
6. **Set Resource Limits** (optional)
|
|
||||||
- CPU: 500m - 1000m
|
|
||||||
- Memory: 256MB - 512MB
|
|
||||||
|
|
||||||
## Docker Files Included
|
|
||||||
|
|
||||||
### Dockerfile
|
|
||||||
- **Purpose**: Multi-stage build for the server
|
|
||||||
- **Base Image**: Alpine (lightweight)
|
|
||||||
- **Final Size**: ~40MB
|
|
||||||
- **User**: Non-root (nerdmonitor)
|
|
||||||
- **Health Check**: Enabled
|
|
||||||
|
|
||||||
### Dockerfile.agent
|
|
||||||
- **Purpose**: Container for agent deployment
|
|
||||||
- **Use Case**: Running agents in containerized environments
|
|
||||||
- **Base Image**: Alpine
|
|
||||||
- **Size**: ~15MB
|
|
||||||
|
|
||||||
### docker-compose.yml
|
|
||||||
- **Purpose**: Local development and testing
|
|
||||||
- **Services**: Server + Demo Agent
|
|
||||||
- **Network**: Bridged (isolated)
|
|
||||||
- **Volume**: Logs persistence
|
|
||||||
|
|
||||||
### .dockerignore
|
|
||||||
- **Purpose**: Optimize build context
|
|
||||||
- **Excludes**: Git files, IDE configs, documentation
|
|
||||||
- **Benefit**: Faster builds, smaller context
|
|
||||||
|
|
||||||
## Local Development
|
|
||||||
|
|
||||||
### Using Docker Compose
|
|
||||||
|
|
||||||
Start the full stack locally:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# Build and start all services
|
|
||||||
docker-compose up -d
|
|
||||||
|
|
||||||
# View logs
|
|
||||||
docker-compose logs -f server
|
|
||||||
|
|
||||||
# Stop all services
|
|
||||||
docker-compose down
|
|
||||||
|
|
||||||
# Remove volumes (clean slate)
|
|
||||||
docker-compose down -v
|
|
||||||
```
|
|
||||||
|
|
||||||
Access dashboard:
|
|
||||||
- URL: http://localhost:8080
|
|
||||||
- Username: admin
|
|
||||||
- Password: admin
|
|
||||||
|
|
||||||
### Manual Docker Commands
|
|
||||||
|
|
||||||
Build server image:
|
|
||||||
```bash
|
|
||||||
docker build -t nerd-monitor-server:latest -f Dockerfile .
|
|
||||||
```
|
|
||||||
|
|
||||||
Run server container:
|
|
||||||
```bash
|
|
||||||
docker run -d \
|
|
||||||
--name nerd-monitor-server \
|
|
||||||
-p 8080:8080 \
|
|
||||||
-e USERNAME=admin \
|
|
||||||
-e PASSWORD=admin \
|
|
||||||
nerd-monitor-server:latest
|
|
||||||
```
|
|
||||||
|
|
||||||
Build agent image:
|
|
||||||
```bash
|
|
||||||
docker build -t nerd-monitor-agent:latest -f Dockerfile.agent .
|
|
||||||
```
|
|
||||||
|
|
||||||
Run agent container:
|
|
||||||
```bash
|
|
||||||
docker run -d \
|
|
||||||
--name nerd-monitor-agent \
|
|
||||||
-e SERVER=server-ip:8080 \
|
|
||||||
nerd-monitor-agent:latest
|
|
||||||
```
|
|
||||||
|
|
||||||
## Coolify Deployment Flow
|
|
||||||
|
|
||||||
```
|
|
||||||
┌─────────────────┐
|
|
||||||
│ Push to master │
|
|
||||||
│ (Git commit) │
|
|
||||||
└────────┬────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌─────────────────────┐
|
|
||||||
│ Gitea Webhook │
|
|
||||||
│ notifies Coolify │
|
|
||||||
└────────┬────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌─────────────────────┐
|
|
||||||
│ Coolify receives │
|
|
||||||
│ build trigger │
|
|
||||||
└────────┬────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌─────────────────────┐
|
|
||||||
│ Coolify clones │
|
|
||||||
│ repository │
|
|
||||||
└────────┬────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌─────────────────────┐
|
|
||||||
│ Docker build │
|
|
||||||
│ executes Dockerfile│
|
|
||||||
└────────┬────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌─────────────────────┐
|
|
||||||
│ Multi-stage build: │
|
|
||||||
│ 1. Build binaries │
|
|
||||||
│ 2. Create runtime │
|
|
||||||
└────────┬────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌─────────────────────┐
|
|
||||||
│ Image pushed to │
|
|
||||||
│ registry (local) │
|
|
||||||
└────────┬────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌─────────────────────┐
|
|
||||||
│ Coolify stops old │
|
|
||||||
│ container │
|
|
||||||
└────────┬────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌─────────────────────┐
|
|
||||||
│ Coolify starts new │
|
|
||||||
│ container │
|
|
||||||
└────────┬────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌─────────────────────┐
|
|
||||||
│ Health check │
|
|
||||||
│ verifies service │
|
|
||||||
└────────┬────────────┘
|
|
||||||
│
|
|
||||||
▼
|
|
||||||
┌─────────────────────┐
|
|
||||||
│ ✅ Deployment │
|
|
||||||
│ Complete │
|
|
||||||
└─────────────────────┘
|
|
||||||
```
|
|
||||||
|
|
||||||
## Image Specifications
|
|
||||||
|
|
||||||
### Server Image
|
|
||||||
- **Name**: nerd-monitor-server
|
|
||||||
- **Tag**: latest (or version-based)
|
|
||||||
- **Size**: ~40MB
|
|
||||||
- **Base**: alpine:latest
|
|
||||||
- **User**: nerdmonitor (uid: 1000)
|
|
||||||
- **Port**: 8080
|
|
||||||
- **Health Check**: Yes (30s interval)
|
|
||||||
|
|
||||||
### Agent Image
|
|
||||||
- **Name**: nerd-monitor-agent
|
|
||||||
- **Tag**: latest
|
|
||||||
- **Size**: ~15MB
|
|
||||||
- **Base**: alpine:latest
|
|
||||||
- **User**: nerdmonitor (uid: 1000)
|
|
||||||
- **Stateless**: Yes (no persistent storage)
|
|
||||||
|
|
||||||
## Environment Variables
|
|
||||||
|
|
||||||
### Server
|
|
||||||
```
|
|
||||||
ADDR=0.0.0.0 # Server bind address
|
|
||||||
PORT=8080 # Server port
|
|
||||||
USERNAME=admin # Admin username
|
|
||||||
PASSWORD=admin # Admin password (change!)
|
|
||||||
```
|
|
||||||
|
|
||||||
### Agent
|
|
||||||
```
|
|
||||||
SERVER=localhost:8080 # Server address:port
|
|
||||||
INTERVAL=15s # Reporting interval
|
|
||||||
```
|
|
||||||
|
|
||||||
## Build Optimization
|
|
||||||
|
|
||||||
The Dockerfile uses several optimization techniques:
|
|
||||||
|
|
||||||
1. **Multi-stage Build**
|
|
||||||
- Builder stage: Includes compiler toolchain
|
|
||||||
- Runtime stage: Only includes runtime dependencies
|
|
||||||
- Result: Minimal final image size
|
|
||||||
|
|
||||||
2. **Alpine Linux**
|
|
||||||
- Lightweight base image (5-10MB)
|
|
||||||
- Includes essentials only
|
|
||||||
- Fast pull and startup
|
|
||||||
|
|
||||||
3. **Binary Optimization**
|
|
||||||
- `-w -s` flags strip debug symbols
|
|
||||||
- Reduces binary size by ~30%
|
|
||||||
- No runtime impact
|
|
||||||
|
|
||||||
4. **Non-root User**
|
|
||||||
- Improved security
|
|
||||||
- Prevents privilege escalation
|
|
||||||
- Best practice for containers
|
|
||||||
|
|
||||||
5. **Health Checks**
|
|
||||||
- Automatic service monitoring
|
|
||||||
- Coolify/Kubernetes aware
|
|
||||||
- Allows orchestration decisions
|
|
||||||
|
|
||||||
## Production Checklist
|
|
||||||
|
|
||||||
- [ ] Change USERNAME and PASSWORD in Coolify
|
|
||||||
- [ ] Enable HTTPS/SSL certificate
|
|
||||||
- [ ] Configure resource limits (CPU/Memory)
|
|
||||||
- [ ] Set up logging aggregation
|
|
||||||
- [ ] Configure backup strategy (if needed)
|
|
||||||
- [ ] Enable monitoring/alerts
|
|
||||||
- [ ] Test rollback procedure
|
|
||||||
- [ ] Document deployment process
|
|
||||||
- [ ] Set up redundancy if needed
|
|
||||||
|
|
||||||
## Troubleshooting
|
|
||||||
|
|
||||||
### Build Fails
|
|
||||||
- Check Dockerfile syntax
|
|
||||||
- Verify build context includes all files
|
|
||||||
- Review build logs in Coolify
|
|
||||||
- Ensure .dockerignore isn't excluding source files
|
|
||||||
|
|
||||||
### Container Won't Start
|
|
||||||
- Check environment variables are set
|
|
||||||
- Verify port isn't already in use
|
|
||||||
- Check Docker logs: `docker logs container-name`
|
|
||||||
- Verify health check endpoint is accessible
|
|
||||||
|
|
||||||
### Deployment Shows Unhealthy
|
|
||||||
- Check network connectivity
|
|
||||||
- Verify PORT environment variable matches exposed port
|
|
||||||
- Review health check command
|
|
||||||
- Check application logs
|
|
||||||
|
|
||||||
### Image Size Too Large
|
|
||||||
- Remove unnecessary files from .dockerignore
|
|
||||||
- Use `docker image inspect` to analyze layers
|
|
||||||
- Consider multi-stage build optimization
|
|
||||||
|
|
||||||
## Performance Tips
|
|
||||||
|
|
||||||
1. **Caching**
|
|
||||||
- Copy go.mod/go.sum first (cached layer)
|
|
||||||
- Copy source code second
|
|
||||||
- Docker reuses layers if unchanged
|
|
||||||
|
|
||||||
2. **Build Speed**
|
|
||||||
- Use `docker buildx` for faster builds
|
|
||||||
- Enable BuildKit: `DOCKER_BUILDKIT=1`
|
|
||||||
- Parallelize stages if possible
|
|
||||||
|
|
||||||
3. **Runtime Performance**
|
|
||||||
- Alpine is lightweight but bare-bones
|
|
||||||
- Add tools only as needed
|
|
||||||
- Monitor resource usage
|
|
||||||
|
|
||||||
## Advanced Configuration
|
|
||||||
|
|
||||||
### Private Container Registry
|
|
||||||
If using private Docker registry:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# In Coolify, set registry credentials
|
|
||||||
docker login private-registry.example.com
|
|
||||||
|
|
||||||
# In docker-compose, use:
|
|
||||||
image: private-registry.example.com/nerd-monitor-server:latest
|
|
||||||
```
|
|
||||||
|
|
||||||
### Multi-architecture Builds
|
|
||||||
Build for multiple architectures:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
docker buildx build \
|
|
||||||
--platform linux/amd64,linux/arm64 \
|
|
||||||
-t nerd-monitor-server:latest \
|
|
||||||
-f Dockerfile .
|
|
||||||
```
|
|
||||||
|
|
||||||
### Custom Base Images
|
|
||||||
Modify Dockerfile FROM directive:
|
|
||||||
|
|
||||||
```dockerfile
|
|
||||||
# Use different base image if needed
|
|
||||||
FROM golang:1.23-bookworm # Debian-based
|
|
||||||
FROM golang:1.23-bullseye # Another option
|
|
||||||
```
|
|
||||||
|
|
||||||
## Support & Documentation
|
|
||||||
|
|
||||||
- Review Dockerfile comments for details
|
|
||||||
- Check Coolify documentation for advanced features
|
|
||||||
- See README.md for general project info
|
|
||||||
- Review AGENTS.md for development guidelines
|
|
||||||
|
|
||||||
## Next Steps
|
|
||||||
|
|
||||||
1. Commit Docker files: `git add Dockerfile*`
|
|
||||||
2. Push to master: `git push origin master`
|
|
||||||
3. Coolify will automatically detect and build
|
|
||||||
4. Monitor build progress in Coolify UI
|
|
||||||
5. Access deployed application once ready
|
|
||||||
158
DOCKER_COMPOSE.md
Normal file
158
DOCKER_COMPOSE.md
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
# Docker Compose Quick Reference
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
### Full Stack (Server + Agent)
|
||||||
|
```bash
|
||||||
|
docker-compose up
|
||||||
|
```
|
||||||
|
Access dashboard: http://localhost:8080
|
||||||
|
Login: admin / admin
|
||||||
|
|
||||||
|
### Server Only
|
||||||
|
```bash
|
||||||
|
docker-compose up server
|
||||||
|
```
|
||||||
|
|
||||||
|
### Agent Only
|
||||||
|
```bash
|
||||||
|
SERVER=your-server:8080 docker-compose up agent
|
||||||
|
```
|
||||||
|
|
||||||
|
## Common Commands
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Start services in background
|
||||||
|
docker-compose up -d
|
||||||
|
|
||||||
|
# Stop all services
|
||||||
|
docker-compose down
|
||||||
|
|
||||||
|
# View logs
|
||||||
|
docker-compose logs -f
|
||||||
|
|
||||||
|
# View logs for specific service
|
||||||
|
docker-compose logs -f server
|
||||||
|
docker-compose logs -f agent
|
||||||
|
|
||||||
|
# Restart services
|
||||||
|
docker-compose restart
|
||||||
|
|
||||||
|
# Remove volumes/data
|
||||||
|
docker-compose down -v
|
||||||
|
|
||||||
|
# Rebuild images
|
||||||
|
docker-compose build --no-cache
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
### Change Server Credentials
|
||||||
|
Edit `docker-compose.yml`:
|
||||||
|
```yaml
|
||||||
|
environment:
|
||||||
|
USERNAME: "myuser"
|
||||||
|
PASSWORD: "mysecurepassword"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Change Agent Reporting Interval
|
||||||
|
```yaml
|
||||||
|
environment:
|
||||||
|
INTERVAL: "30s" # Report every 30 seconds instead of 15
|
||||||
|
```
|
||||||
|
|
||||||
|
### Set Custom Agent ID
|
||||||
|
```yaml
|
||||||
|
environment:
|
||||||
|
AGENT_ID: "my-machine"
|
||||||
|
```
|
||||||
|
|
||||||
|
### Run Multiple Agents
|
||||||
|
```bash
|
||||||
|
# Terminal 1: Start server
|
||||||
|
docker-compose up server
|
||||||
|
|
||||||
|
# Terminal 2: Run agent 1
|
||||||
|
docker-compose run --name agent1 -e AGENT_ID=machine1 agent
|
||||||
|
|
||||||
|
# Terminal 3: Run agent 2
|
||||||
|
docker-compose run --name agent2 -e AGENT_ID=machine2 agent
|
||||||
|
```
|
||||||
|
|
||||||
|
## Docker Commands
|
||||||
|
|
||||||
|
### View Running Containers
|
||||||
|
```bash
|
||||||
|
docker-compose ps
|
||||||
|
```
|
||||||
|
|
||||||
|
### Execute Commands in Container
|
||||||
|
```bash
|
||||||
|
# Connect to server
|
||||||
|
docker-compose exec server sh
|
||||||
|
|
||||||
|
# View server config
|
||||||
|
docker-compose exec server ps aux
|
||||||
|
```
|
||||||
|
|
||||||
|
### View Resource Usage
|
||||||
|
```bash
|
||||||
|
docker stats
|
||||||
|
```
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Agent can't connect to server
|
||||||
|
```bash
|
||||||
|
# Check if server is running and healthy
|
||||||
|
docker-compose ps
|
||||||
|
|
||||||
|
# Check server logs
|
||||||
|
docker-compose logs server
|
||||||
|
|
||||||
|
# Check if containers are on same network
|
||||||
|
docker network inspect nerd-monitor
|
||||||
|
```
|
||||||
|
|
||||||
|
### Clear Everything and Start Fresh
|
||||||
|
```bash
|
||||||
|
docker-compose down -v
|
||||||
|
docker-compose build --no-cache
|
||||||
|
docker-compose up
|
||||||
|
```
|
||||||
|
|
||||||
|
### Port Already in Use
|
||||||
|
If port 8080 is already in use, edit `docker-compose.yml`:
|
||||||
|
```yaml
|
||||||
|
ports:
|
||||||
|
- "8090:8080" # Maps host port 8090 to container port 8080
|
||||||
|
```
|
||||||
|
|
||||||
|
## Environment Variables Reference
|
||||||
|
|
||||||
|
### Server
|
||||||
|
| Variable | Default | Description |
|
||||||
|
|----------|---------|-------------|
|
||||||
|
| ADDR | 0.0.0.0 | Bind address |
|
||||||
|
| PORT | 8080 | Server port |
|
||||||
|
| USERNAME | admin | Admin username |
|
||||||
|
| PASSWORD | admin | Admin password |
|
||||||
|
|
||||||
|
### Agent
|
||||||
|
| Variable | Default | Description |
|
||||||
|
| SERVER | server:8080 | Server address |
|
||||||
|
| INTERVAL | 15s | Reporting interval |
|
||||||
|
| AGENT_ID | (auto) | Agent identifier |
|
||||||
|
|
||||||
|
## Production Tips
|
||||||
|
|
||||||
|
1. **Change credentials**: Update USERNAME and PASSWORD in docker-compose.yml
|
||||||
|
2. **Use external volumes**: Add volume mounts for data persistence
|
||||||
|
3. **Set resource limits**: Uncomment resource limits in docker-compose.yml
|
||||||
|
4. **Enable restart policies**: Already set to `unless-stopped`
|
||||||
|
5. **Use environment files**: Create `.env` file for sensitive data:
|
||||||
|
```bash
|
||||||
|
USERNAME=myuser
|
||||||
|
PASSWORD=mysecurepass
|
||||||
|
```
|
||||||
|
Then in docker-compose.yml: `env_file: .env`
|
||||||
60
Dockerfile
60
Dockerfile
@@ -1,60 +0,0 @@
|
|||||||
# Multi-stage Dockerfile for Nerd Monitor
|
|
||||||
# Optimized for Coolify CI/CD pipeline
|
|
||||||
# Build stage
|
|
||||||
FROM golang:1.23-alpine AS builder
|
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
# Install build dependencies
|
|
||||||
RUN apk add --no-cache git make
|
|
||||||
|
|
||||||
# Copy go mod files
|
|
||||||
COPY go.mod go.sum ./
|
|
||||||
|
|
||||||
# Download dependencies
|
|
||||||
RUN go mod download
|
|
||||||
|
|
||||||
# Copy source code
|
|
||||||
COPY . .
|
|
||||||
|
|
||||||
# Generate Templ templates
|
|
||||||
RUN go run github.com/a-h/templ/cmd/templ@latest generate
|
|
||||||
|
|
||||||
# Build the server
|
|
||||||
RUN CGO_ENABLED=1 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-server ./cmd/server
|
|
||||||
|
|
||||||
# Runtime stage
|
|
||||||
FROM alpine:latest
|
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
# Install runtime dependencies
|
|
||||||
RUN apk add --no-cache ca-certificates tzdata
|
|
||||||
|
|
||||||
# Copy binary from builder
|
|
||||||
COPY --from=builder /app/bin/nerd-monitor-server /app/nerd-monitor-server
|
|
||||||
|
|
||||||
# Create non-root user for security
|
|
||||||
RUN addgroup -g 1000 nerdmonitor && adduser -D -u 1000 -G nerdmonitor nerdmonitor
|
|
||||||
|
|
||||||
# Change ownership
|
|
||||||
RUN chown -R nerdmonitor:nerdmonitor /app
|
|
||||||
|
|
||||||
# Switch to non-root user
|
|
||||||
USER nerdmonitor
|
|
||||||
|
|
||||||
# Expose port
|
|
||||||
EXPOSE 8080
|
|
||||||
|
|
||||||
# Health check
|
|
||||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
|
||||||
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/login || exit 1
|
|
||||||
|
|
||||||
# Default environment variables
|
|
||||||
ENV ADDR=0.0.0.0
|
|
||||||
ENV PORT=8080
|
|
||||||
ENV USERNAME=admin
|
|
||||||
ENV PASSWORD=admin
|
|
||||||
|
|
||||||
# Run the application
|
|
||||||
CMD ["/app/nerd-monitor-server", "-addr", "0.0.0.0", "-port", "8080", "-username", "admin", "-password", "admin"]
|
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
# Dockerfile for Nerd Monitor Agent
|
# Multi-stage build for nerd-monitor agent
|
||||||
# Multi-stage build optimized for minimal size
|
FROM golang:1.24.4-alpine AS builder
|
||||||
|
|
||||||
FROM golang:1.23-alpine AS builder
|
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
@@ -17,33 +15,31 @@ RUN go mod download
|
|||||||
# Copy source code
|
# Copy source code
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
# Build the agent
|
# Build the agent binary (no templ needed for agent)
|
||||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o bin/nerd-monitor-agent ./cmd/agent
|
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o nerd-monitor-agent ./cmd/agent
|
||||||
|
|
||||||
# Runtime stage
|
# Runtime stage
|
||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Install runtime dependencies (minimal)
|
COPY --from=builder /app/nerd-monitor-agent .
|
||||||
RUN apk add --no-cache ca-certificates tzdata
|
|
||||||
|
|
||||||
# Copy binary from builder
|
# Create entrypoint script BEFORE switching users
|
||||||
COPY --from=builder /app/bin/nerd-monitor-agent /app/nerd-monitor-agent
|
RUN echo '#!/bin/sh' > /app/entrypoint.sh && \
|
||||||
|
echo 'SERVER=${SERVER:-localhost:8080}' >> /app/entrypoint.sh && \
|
||||||
|
echo 'INTERVAL=${INTERVAL:-15s}' >> /app/entrypoint.sh && \
|
||||||
|
echo 'AGENT_ID=${AGENT_ID:-}' >> /app/entrypoint.sh && \
|
||||||
|
echo 'if [ -z "$AGENT_ID" ]; then' >> /app/entrypoint.sh && \
|
||||||
|
echo ' exec ./nerd-monitor-agent --server "$SERVER" --interval "$INTERVAL"' >> /app/entrypoint.sh && \
|
||||||
|
echo 'else' >> /app/entrypoint.sh && \
|
||||||
|
echo ' exec ./nerd-monitor-agent --server "$SERVER" --interval "$INTERVAL" --id "$AGENT_ID"' >> /app/entrypoint.sh && \
|
||||||
|
echo 'fi' >> /app/entrypoint.sh && \
|
||||||
|
chmod +x /app/entrypoint.sh
|
||||||
|
|
||||||
# Create non-root user for security
|
# Create non-root user
|
||||||
RUN addgroup -g 1000 nerdmonitor && adduser -D -u 1000 -G nerdmonitor nerdmonitor
|
RUN addgroup -g 1000 appgroup && adduser -D -u 1000 -G appgroup appuser
|
||||||
|
USER appuser
|
||||||
# Change ownership
|
|
||||||
RUN chown -R nerdmonitor:nerdmonitor /app
|
|
||||||
|
|
||||||
# Switch to non-root user
|
|
||||||
USER nerdmonitor
|
|
||||||
|
|
||||||
# Default environment variables
|
|
||||||
ENV SERVER=localhost:8080
|
|
||||||
ENV INTERVAL=15s
|
|
||||||
|
|
||||||
# Run the agent
|
# Run the agent
|
||||||
ENTRYPOINT ["/app/nerd-monitor-agent"]
|
ENTRYPOINT ["/app/entrypoint.sh"]
|
||||||
CMD ["--server", "${SERVER}", "--interval", "${INTERVAL}"]
|
|
||||||
|
|||||||
56
Dockerfile.server
Normal file
56
Dockerfile.server
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
# Multi-stage build for nerd-monitor server
|
||||||
|
FROM golang:1.24.4-alpine AS builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Install build dependencies
|
||||||
|
RUN apk add --no-cache git make
|
||||||
|
|
||||||
|
# Copy go mod files
|
||||||
|
COPY go.mod go.sum ./
|
||||||
|
|
||||||
|
# Download dependencies
|
||||||
|
RUN go mod download
|
||||||
|
|
||||||
|
# Copy source code
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
# Generate templ templates
|
||||||
|
RUN go run github.com/a-h/templ/cmd/templ@latest generate
|
||||||
|
|
||||||
|
# Build the server binary
|
||||||
|
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o nerd-monitor-server ./cmd/server
|
||||||
|
|
||||||
|
# Runtime stage
|
||||||
|
FROM alpine:latest
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Install ca-certificates for HTTPS
|
||||||
|
RUN apk add --no-cache ca-certificates
|
||||||
|
|
||||||
|
# Copy binary from builder
|
||||||
|
COPY --from=builder /app/nerd-monitor-server .
|
||||||
|
|
||||||
|
# Create entrypoint script BEFORE switching users
|
||||||
|
RUN echo '#!/bin/sh' > /app/entrypoint.sh && \
|
||||||
|
echo 'ADDR=${ADDR:-0.0.0.0}' >> /app/entrypoint.sh && \
|
||||||
|
echo 'PORT=${PORT:-8080}' >> /app/entrypoint.sh && \
|
||||||
|
echo 'USERNAME=${USERNAME:-admin}' >> /app/entrypoint.sh && \
|
||||||
|
echo 'PASSWORD=${PASSWORD:-admin}' >> /app/entrypoint.sh && \
|
||||||
|
echo 'exec ./nerd-monitor-server -addr "$ADDR" -port "$PORT" -username "$USERNAME" -password "$PASSWORD"' >> /app/entrypoint.sh && \
|
||||||
|
chmod +x /app/entrypoint.sh
|
||||||
|
|
||||||
|
# Create non-root user
|
||||||
|
RUN addgroup -g 1000 appgroup && adduser -D -u 1000 -G appgroup appuser
|
||||||
|
USER appuser
|
||||||
|
|
||||||
|
# Expose port
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
# Health check
|
||||||
|
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
|
||||||
|
CMD wget --quiet --tries=1 --spider http://localhost:8080/login || exit 1
|
||||||
|
|
||||||
|
# Run the server
|
||||||
|
ENTRYPOINT ["/app/entrypoint.sh"]
|
||||||
164
GITEA_SETUP.md
Normal file
164
GITEA_SETUP.md
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
# Gitea Actions Setup - Server Configuration Checklist
|
||||||
|
|
||||||
|
This document outlines the changes needed on your Gitea server to make the CI/CD pipeline work.
|
||||||
|
|
||||||
|
## What You Need to Do
|
||||||
|
|
||||||
|
### 1. Add GITEA_TOKEN Secret to Repository
|
||||||
|
|
||||||
|
1. **Log into Gitea** with your user account
|
||||||
|
2. **Navigate to** your nerd-monitor repository
|
||||||
|
3. **Go to Settings → Secrets**
|
||||||
|
4. **Create a new secret**:
|
||||||
|
- Name: `GITEA_TOKEN`
|
||||||
|
- Value: [Your Gitea API token - see below for how to create]
|
||||||
|
5. **Save the secret**
|
||||||
|
|
||||||
|
### 2. Create an API Token (if you haven't already)
|
||||||
|
|
||||||
|
1. **Log into Gitea** with your user account
|
||||||
|
2. **Go to Settings → Applications**
|
||||||
|
3. **Click "Generate New Token"**
|
||||||
|
4. **Fill in the form**:
|
||||||
|
- Token Name: `release-automation`
|
||||||
|
- Scopes: Select at least `repo` (full repository access)
|
||||||
|
5. **Click "Generate"**
|
||||||
|
6. **Copy the token immediately** (you won't see it again)
|
||||||
|
7. **Use this token** for the secret in step 1
|
||||||
|
|
||||||
|
### 3. Verify Gitea Actions is Enabled (Server Admin)
|
||||||
|
|
||||||
|
These steps require SSH access to your Gitea server:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# SSH into your Gitea server
|
||||||
|
ssh user@your-gitea-server
|
||||||
|
|
||||||
|
# Edit the Gitea configuration
|
||||||
|
sudo vi /etc/gitea/app.ini
|
||||||
|
|
||||||
|
# Verify or add this section:
|
||||||
|
[actions]
|
||||||
|
ENABLED = true
|
||||||
|
|
||||||
|
# Save and exit (Esc, :wq, Enter)
|
||||||
|
|
||||||
|
# Restart Gitea for changes to take effect
|
||||||
|
sudo systemctl restart gitea
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Verify Your Runner is Online (Server Admin)
|
||||||
|
|
||||||
|
1. **Log into Gitea** as admin
|
||||||
|
2. **Go to Administration → Actions → Runners**
|
||||||
|
3. **Verify** at least one runner is listed and shows as "Online" or "Idle"
|
||||||
|
4. If no runners:
|
||||||
|
- You need to set up a Gitea Actions runner on a machine with Docker and Go
|
||||||
|
- See "Setting Up a Runner" below
|
||||||
|
|
||||||
|
## Setting Up a Runner (if needed)
|
||||||
|
|
||||||
|
If you don't have any runners yet, you need to set one up. This can be on the Gitea server itself or any machine with Docker and Go.
|
||||||
|
|
||||||
|
### Quick Runner Setup
|
||||||
|
|
||||||
|
1. **On your Gitea server or runner machine**:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Download the Gitea Actions runner
|
||||||
|
wget https://github.com/gitea/act_runner/releases/download/v0.6.10/act_runner-0.6.10-linux-amd64
|
||||||
|
chmod +x act_runner-0.6.10-linux-amd64
|
||||||
|
|
||||||
|
# Register the runner with your Gitea instance
|
||||||
|
./act_runner-0.6.10-linux-amd64 register \
|
||||||
|
--instance https://git.nerdnest.dev \
|
||||||
|
--token <your-runner-token>
|
||||||
|
|
||||||
|
# Run the runner in the background
|
||||||
|
./act_runner-0.6.10-linux-amd64 daemon &
|
||||||
|
```
|
||||||
|
|
||||||
|
To get a runner token:
|
||||||
|
1. Log into Gitea as **admin**
|
||||||
|
2. Go to **Administration → Actions → Runners**
|
||||||
|
3. Click **Create new runner**
|
||||||
|
4. Follow the registration steps
|
||||||
|
|
||||||
|
### Installing Docker on the Runner (Optional)
|
||||||
|
|
||||||
|
Docker is **not required** for the CI/CD pipeline. Binary builds work without Docker.
|
||||||
|
|
||||||
|
If you want to manually build Docker images:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Build server image
|
||||||
|
docker build -t nerd-monitor-server -f Dockerfile.server .
|
||||||
|
|
||||||
|
# Build agent image
|
||||||
|
docker build -t nerd-monitor-agent -f Dockerfile.agent .
|
||||||
|
|
||||||
|
# Or use docker-compose for both
|
||||||
|
docker-compose build
|
||||||
|
docker-compose up
|
||||||
|
```
|
||||||
|
|
||||||
|
## What Happens Next
|
||||||
|
|
||||||
|
Once you've set everything up:
|
||||||
|
|
||||||
|
1. **Every push to main/master** triggers the build job:
|
||||||
|
- Compiles all platform binaries (Linux, macOS, Windows)
|
||||||
|
- Creates SHA256 checksums
|
||||||
|
- Artifacts available for 30 days
|
||||||
|
|
||||||
|
2. **Every git tag push** (e.g., `v1.0.0`) triggers the full release:
|
||||||
|
- Does all of the above
|
||||||
|
- Creates a Gitea Release
|
||||||
|
- Uploads all binaries and checksums to the release (permanent storage)
|
||||||
|
|
||||||
|
3. **Releases are available in**:
|
||||||
|
- Repository Releases tab in Gitea
|
||||||
|
- All binaries ready for download
|
||||||
|
- SHA256SUMS file for verification
|
||||||
|
|
||||||
|
Note: Docker images are not built automatically. To build Docker images manually, use:
|
||||||
|
```bash
|
||||||
|
docker build -t nerd-monitor-server -f Dockerfile.server .
|
||||||
|
docker build -t nerd-monitor-agent -f Dockerfile.agent .
|
||||||
|
# Or use docker-compose: docker-compose build
|
||||||
|
```
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### "docker: command not found" in workflow
|
||||||
|
- Docker support is not required - the workflow builds binaries without it
|
||||||
|
- Binary builds will always succeed
|
||||||
|
- Docker images must be built manually using: `docker build -f Dockerfile.server .`
|
||||||
|
- See QUICKSTART.md or DOCKER_COMPOSE.md for manual Docker build instructions
|
||||||
|
|
||||||
|
### "Action not found" error
|
||||||
|
- Make sure Gitea Actions is enabled
|
||||||
|
- Restart Gitea if you just enabled it: `sudo systemctl restart gitea`
|
||||||
|
|
||||||
|
### No runners available
|
||||||
|
- Runner must be registered: Administration → Actions → Runners
|
||||||
|
- Runner machine must have Go installed (1.24.4 or later)
|
||||||
|
- Docker is optional (for building Docker images)
|
||||||
|
- Check if runner is online in the UI
|
||||||
|
|
||||||
|
### "GITEA_TOKEN" not found
|
||||||
|
- Make sure the secret is named exactly `GITEA_TOKEN` (case-sensitive)
|
||||||
|
- Go to Settings → Secrets and verify it's there
|
||||||
|
- If it's there, try re-running the workflow
|
||||||
|
|
||||||
|
### Build fails with permission denied (Docker)
|
||||||
|
- Make sure the runner user has permission to run Docker commands
|
||||||
|
- On the runner machine: `sudo usermod -aG docker $USER`
|
||||||
|
- Logout and log back in for the group change to take effect
|
||||||
|
|
||||||
|
## Support
|
||||||
|
|
||||||
|
- Gitea Actions Docs: https://docs.gitea.io/en-us/actions/
|
||||||
|
- Act Runner Docs: https://gitea.com/gitea/act_runner
|
||||||
|
- Docker Installation: https://docs.docker.com/install/
|
||||||
|
- For issues with the workflow itself, check the Actions tab logs
|
||||||
@@ -1,6 +1,45 @@
|
|||||||
# Nerd Monitor - Quick Start Guide
|
# Nerd Monitor - Quick Start Guide
|
||||||
|
|
||||||
## Building
|
## Docker Compose (Easiest)
|
||||||
|
|
||||||
|
The easiest way to get started is using Docker Compose:
|
||||||
|
|
||||||
|
### Run Full Stack (Server + Agent)
|
||||||
|
```bash
|
||||||
|
docker-compose up
|
||||||
|
```
|
||||||
|
|
||||||
|
Access the dashboard at: **http://localhost:8080**
|
||||||
|
|
||||||
|
### Run Server Only
|
||||||
|
```bash
|
||||||
|
docker-compose up server
|
||||||
|
```
|
||||||
|
|
||||||
|
### Run Agent Only (with external server)
|
||||||
|
```bash
|
||||||
|
SERVER=your-server:8080 docker-compose up agent
|
||||||
|
```
|
||||||
|
|
||||||
|
### Run Multiple Agents
|
||||||
|
```bash
|
||||||
|
# Start the server
|
||||||
|
docker-compose up -d server
|
||||||
|
|
||||||
|
# Run agents with custom IDs
|
||||||
|
docker-compose run --name agent1 -e AGENT_ID=machine1 agent
|
||||||
|
docker-compose run --name agent2 -e AGENT_ID=machine2 agent
|
||||||
|
```
|
||||||
|
|
||||||
|
### Docker Compose Configuration
|
||||||
|
Edit `docker-compose.yml` to customize:
|
||||||
|
- Server credentials: `USERNAME` and `PASSWORD`
|
||||||
|
- Agent reporting interval: `INTERVAL`
|
||||||
|
- Agent custom ID: `AGENT_ID`
|
||||||
|
|
||||||
|
## Native Binaries
|
||||||
|
|
||||||
|
### Building
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
# Build for current OS
|
# Build for current OS
|
||||||
@@ -111,6 +150,8 @@ Change these when starting the server:
|
|||||||
./bin/nerd-monitor-server -username myuser -password mysecurepass
|
./bin/nerd-monitor-server -username myuser -password mysecurepass
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Or with Docker Compose, edit the `USERNAME` and `PASSWORD` environment variables in `docker-compose.yml`.
|
||||||
|
|
||||||
## Architecture
|
## Architecture
|
||||||
|
|
||||||
- **Server**: Web UI, API endpoint for agent stats, in-memory storage
|
- **Server**: Web UI, API endpoint for agent stats, in-memory storage
|
||||||
@@ -134,3 +175,8 @@ Change these when starting the server:
|
|||||||
- Verify server is running: `http://localhost:8080`
|
- Verify server is running: `http://localhost:8080`
|
||||||
- Check firewall rules allow the agent port
|
- Check firewall rules allow the agent port
|
||||||
- Ensure correct server address and port are specified
|
- Ensure correct server address and port are specified
|
||||||
|
|
||||||
|
### Docker Compose agents can't reach server
|
||||||
|
- Verify server is healthy: `docker ps` (server should show healthy status)
|
||||||
|
- Check both containers are on the same network: `docker network inspect nerd-monitor`
|
||||||
|
- Ensure `SERVER` environment variable is set to `server:8080` (the service name)
|
||||||
|
|||||||
108
README.md
108
README.md
@@ -1,22 +1,25 @@
|
|||||||
# Nerd Monitor 📊
|
# Nerd Monitor 📊
|
||||||
|
|
||||||
A lightweight, cross-platform system monitoring solution written in Go. Monitor CPU, memory, and disk usage across multiple machines with a beautiful web dashboard.
|
A lightweight, cross-platform system monitoring solution written in Go. Monitor CPU, memory, and disk usage across multiple machines with a beautiful web dashboard featuring instant real-time updates powered by Server-Sent Events and HTMX.
|
||||||
|
|
||||||

|

|
||||||
|

|
||||||

|

|
||||||

|

|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- 🖥️ **Multi-platform Support** - Deploy agents on Linux, macOS, and Windows (AMD64 & ARM64)
|
- 🖥️ **Multi-platform Support** - Deploy agents on Linux, macOS, and Windows (AMD64 & ARM64)
|
||||||
- 📈 **Real-time Monitoring** - Track CPU, RAM, and Disk usage with 15-second refresh intervals
|
- ⚡ **Instant Real-time Updates** - Stats update immediately when agents report, not on fixed intervals
|
||||||
- 🟢 **Live Status Indicators** - See which machines are online/offline at a glance
|
- 🟢 **Live Status Indicators** - See which machines are online/offline at a glance
|
||||||
- 🔒 **Secure Authentication** - Session-based admin authentication for the dashboard
|
- 🔒 **Secure Authentication** - Session-based admin authentication for the dashboard
|
||||||
- 🧹 **Stale Agent Management** - Automatically detect and remove agents inactive for 6+ months
|
- 🧹 **Stale Agent Management** - Automatically detect and remove agents inactive for 6+ months
|
||||||
- 📱 **Responsive Dashboard** - Beautiful, modern UI with auto-refresh
|
- 📱 **Responsive Dashboard** - Beautiful, modern UI with seamless HTMX-powered updates
|
||||||
- 🚀 **Minimal Dependencies** - Only Chi router and Templ templating engine (plus Go stdlib)
|
- 🚀 **Minimal Dependencies** - Only Chi router, Templ templating, and HTMX (plus Go stdlib)
|
||||||
- ⚙️ **Auto-Generation** - Agent IDs automatically generated from hostname
|
- ⚙️ **Auto-Generation** - Agent IDs automatically generated from hostname
|
||||||
- 💾 **In-Memory Storage** - Fast, zero-database setup (perfect for small to medium deployments)
|
- 💾 **In-Memory Storage** - Fast, zero-database setup (perfect for small to medium deployments)
|
||||||
|
- 📡 **Server-Sent Events** - Real-time push notifications from server to web clients
|
||||||
|
- 📝 **Structured Logging** - Comprehensive slog-based logging throughout the application
|
||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
@@ -54,7 +57,7 @@ Binaries are created in the `bin/` directory.
|
|||||||
-password securepassword
|
-password securepassword
|
||||||
```
|
```
|
||||||
|
|
||||||
Then access the dashboard at `http://localhost:8080`
|
Then access the dashboard at `http://localhost:8080`. The interface provides real-time updates - stats refresh instantly when agents report new data!
|
||||||
|
|
||||||
### Running Agents
|
### Running Agents
|
||||||
|
|
||||||
@@ -87,7 +90,7 @@ nerd-monitor-agent.bat --server 10.0.20.80:9090 --interval 30s
|
|||||||
|
|
||||||
### Overview Page
|
### Overview Page
|
||||||
|
|
||||||
Shows all connected agents with real-time metrics:
|
Shows all connected agents with instant real-time metrics:
|
||||||
|
|
||||||
- **Status Badge** - Green (Online) or Red (Offline)
|
- **Status Badge** - Green (Online) or Red (Offline)
|
||||||
- Online: Last report within 15 seconds
|
- Online: Last report within 15 seconds
|
||||||
@@ -96,16 +99,18 @@ Shows all connected agents with real-time metrics:
|
|||||||
- **Memory Usage** - Used / Total with visual progress bar
|
- **Memory Usage** - Used / Total with visual progress bar
|
||||||
- **Disk Usage** - Used / Total with visual progress bar
|
- **Disk Usage** - Used / Total with visual progress bar
|
||||||
- **Last Seen** - Human-readable timestamp
|
- **Last Seen** - Human-readable timestamp
|
||||||
|
- **Live Updates** - Stats refresh immediately when agents report new data
|
||||||
|
|
||||||
### Agent Detail Page
|
### Agent Detail Page
|
||||||
|
|
||||||
Click any agent hostname to see detailed statistics:
|
Click any agent hostname to see detailed statistics with live updates:
|
||||||
|
|
||||||
- Large CPU usage display with progress bar
|
- Large CPU usage display with progress bar (updates in real-time)
|
||||||
- Memory and disk breakdowns
|
- Memory and disk breakdowns
|
||||||
- Agent ID and detailed metadata
|
- Agent ID and detailed metadata
|
||||||
- Last exact timestamp
|
- Last exact timestamp
|
||||||
- Delete button for removing the agent
|
- Delete button for removing the agent
|
||||||
|
- **Instant Updates** - CPU stats refresh immediately when agent reports
|
||||||
|
|
||||||
### Stale Agent Management
|
### Stale Agent Management
|
||||||
|
|
||||||
@@ -122,10 +127,15 @@ Agents inactive for 6+ months:
|
|||||||
- **HTTP Router**: Chi v5 for efficient routing
|
- **HTTP Router**: Chi v5 for efficient routing
|
||||||
- **Authentication**: Session-based auth with 24-hour expiry
|
- **Authentication**: Session-based auth with 24-hour expiry
|
||||||
- **Storage**: In-memory concurrent-safe store (sync.RWMutex)
|
- **Storage**: In-memory concurrent-safe store (sync.RWMutex)
|
||||||
|
- **Real-time Broadcasting**: Server-Sent Events (SSE) for instant UI updates
|
||||||
|
- **Structured Logging**: slog-based logging with debug/info levels
|
||||||
- **API Endpoints**:
|
- **API Endpoints**:
|
||||||
- `POST /api/report` - Agent stats reporting
|
- `POST /api/report` - Agent stats reporting (triggers real-time broadcasts)
|
||||||
- `GET /api/agents` - List all agents
|
- `GET /api/agents` - List all agents
|
||||||
- `GET /api/agents/{id}` - Get specific agent stats
|
- `GET /api/agents/{id}` - Get specific agent stats
|
||||||
|
- `GET /api/dashboard/table` - HTML fragment for HTMX dashboard updates
|
||||||
|
- `GET /api/agents/{id}/stats` - HTML fragment for HTMX agent detail updates
|
||||||
|
- `GET /api/events` - Server-Sent Events for real-time notifications
|
||||||
|
|
||||||
### Agent (`cmd/agent/`)
|
### Agent (`cmd/agent/`)
|
||||||
|
|
||||||
@@ -138,8 +148,9 @@ Agents inactive for 6+ months:
|
|||||||
### Views (`views/`)
|
### Views (`views/`)
|
||||||
|
|
||||||
- **Templ Templates**: Type-safe HTML templating
|
- **Templ Templates**: Type-safe HTML templating
|
||||||
|
- **HTMX Integration**: Smooth, instant updates without page refreshes
|
||||||
|
- **Server-Sent Events**: Real-time push notifications from server
|
||||||
- **Responsive Design**: Works on desktop, tablet, and mobile
|
- **Responsive Design**: Works on desktop, tablet, and mobile
|
||||||
- **Auto-Refresh**: Dashboard refreshes every 5 seconds
|
|
||||||
- **Color-coded Status**: Visual indicators for system health
|
- **Color-coded Status**: Visual indicators for system health
|
||||||
|
|
||||||
## Project Structure
|
## Project Structure
|
||||||
@@ -148,28 +159,28 @@ Agents inactive for 6+ months:
|
|||||||
nerd-monitor/
|
nerd-monitor/
|
||||||
├── cmd/
|
├── cmd/
|
||||||
│ ├── agent/
|
│ ├── agent/
|
||||||
│ │ └── main.go # Agent executable
|
│ │ └── main.go # Agent executable with slog logging
|
||||||
│ └── server/
|
│ └── server/
|
||||||
│ └── main.go # Server executable
|
│ └── main.go # Server executable with slog logging
|
||||||
├── internal/
|
├── internal/
|
||||||
│ ├── api/
|
│ ├── api/
|
||||||
│ │ └── api.go # API handlers
|
│ │ └── api.go # API handlers + real-time broadcaster
|
||||||
│ ├── auth/
|
│ ├── auth/
|
||||||
│ │ ├── middleware.go # Auth middleware
|
│ │ ├── middleware.go # Auth middleware with detailed logging
|
||||||
│ │ └── errors.go # Error definitions
|
│ │ └── errors.go # Error definitions
|
||||||
│ ├── stats/
|
│ ├── stats/
|
||||||
│ │ └── stats.go # System stats collection
|
│ │ └── stats.go # System stats collection
|
||||||
│ ├── store/
|
│ ├── store/
|
||||||
│ │ └── store.go # In-memory agent storage
|
│ │ └── store.go # In-memory agent storage with logging
|
||||||
│ └── ui/
|
│ └── ui/
|
||||||
│ └── handlers.go # Dashboard handlers
|
│ └── handlers.go # Dashboard handlers + HTMX endpoints
|
||||||
├── views/
|
├── views/
|
||||||
│ ├── layout.templ # Base layout template
|
│ ├── layout.templ # Base layout with HTMX + SSE
|
||||||
│ ├── dashboard.templ # Dashboard page
|
│ ├── dashboard.templ # Dashboard with real-time updates
|
||||||
│ ├── agent_detail.templ # Agent detail page
|
│ ├── agent_detail.templ # Agent detail with live stats
|
||||||
│ ├── login.templ # Login page
|
│ ├── login.templ # Login page
|
||||||
│ ├── components.templ # Reusable components
|
│ ├── components.templ # Reusable components
|
||||||
│ └── generate.go # Templ code generation
|
│ └── *_templ.go # Generated Templ code
|
||||||
├── Makefile # Build system
|
├── Makefile # Build system
|
||||||
├── go.mod / go.sum # Go dependencies
|
├── go.mod / go.sum # Go dependencies
|
||||||
├── AGENTS.md # Agent guidelines
|
├── AGENTS.md # Agent guidelines
|
||||||
@@ -231,9 +242,10 @@ Options:
|
|||||||
|
|
||||||
- **Agent Memory**: ~8-10 MB (depending on platform)
|
- **Agent Memory**: ~8-10 MB (depending on platform)
|
||||||
- **Server Memory**: Scales with connected agents (~1 MB per 1000 agents)
|
- **Server Memory**: Scales with connected agents (~1 MB per 1000 agents)
|
||||||
- **Network**: Minimal bandwidth (~1 KB per report)
|
- **Network**: Minimal bandwidth (~1 KB per report + SSE connections)
|
||||||
- **Dashboard Refresh**: 5 seconds (configurable)
|
- **Real-time Updates**: Instant UI updates when agents report stats
|
||||||
- **Agent Reporting**: 15 seconds (configurable)
|
- **Agent Reporting**: 15 seconds (configurable)
|
||||||
|
- **SSE Connections**: Lightweight persistent connections for real-time notifications
|
||||||
|
|
||||||
## Building for Specific Platforms
|
## Building for Specific Platforms
|
||||||
|
|
||||||
@@ -261,6 +273,8 @@ make build-all
|
|||||||
- `github.com/go-chi/chi/v5` - HTTP router
|
- `github.com/go-chi/chi/v5` - HTTP router
|
||||||
- `github.com/a-h/templ` - HTML templating
|
- `github.com/a-h/templ` - HTML templating
|
||||||
- `github.com/shirou/gopsutil/v3` - System statistics
|
- `github.com/shirou/gopsutil/v3` - System statistics
|
||||||
|
- **HTMX** (CDN) - Frontend interactivity and real-time updates
|
||||||
|
- **Server-Sent Events** (Go stdlib) - Real-time push notifications
|
||||||
|
|
||||||
### Go Standard Library
|
### Go Standard Library
|
||||||
|
|
||||||
@@ -270,6 +284,7 @@ make build-all
|
|||||||
- `time` - Timestamps and durations
|
- `time` - Timestamps and durations
|
||||||
- `os` - Hostname, signals, I/O
|
- `os` - Hostname, signals, I/O
|
||||||
- `crypto/rand` - Token generation
|
- `crypto/rand` - Token generation
|
||||||
|
- `log/slog` - Structured logging throughout application
|
||||||
|
|
||||||
## Troubleshooting
|
## Troubleshooting
|
||||||
|
|
||||||
@@ -279,6 +294,15 @@ make build-all
|
|||||||
2. Check agent logs for connection errors
|
2. Check agent logs for connection errors
|
||||||
3. Ensure correct server address and port
|
3. Ensure correct server address and port
|
||||||
4. Check firewall rules allow outbound connections
|
4. Check firewall rules allow outbound connections
|
||||||
|
5. Check server logs for SSE broadcasting activity
|
||||||
|
|
||||||
|
### Stats not updating in real-time
|
||||||
|
|
||||||
|
1. Verify browser supports Server-Sent Events (SSE)
|
||||||
|
2. Check browser developer tools for JavaScript errors
|
||||||
|
3. Ensure `/api/events` endpoint is accessible (requires authentication)
|
||||||
|
4. Check server logs for SSE connection/disconnection messages
|
||||||
|
5. Verify HTMX library loaded correctly
|
||||||
|
|
||||||
### Agent shows "Offline" immediately
|
### Agent shows "Offline" immediately
|
||||||
|
|
||||||
@@ -286,6 +310,7 @@ make build-all
|
|||||||
2. Verify server address is reachable: `ping 10.0.20.80`
|
2. Verify server address is reachable: `ping 10.0.20.80`
|
||||||
3. Check if server is listening on the correct port
|
3. Check if server is listening on the correct port
|
||||||
4. Review server logs for errors
|
4. Review server logs for errors
|
||||||
|
5. Note: Agents are marked offline if no report received in 15 seconds
|
||||||
|
|
||||||
### Can't login to dashboard
|
### Can't login to dashboard
|
||||||
|
|
||||||
@@ -293,6 +318,7 @@ make build-all
|
|||||||
2. Try the default: `admin` / `admin`
|
2. Try the default: `admin` / `admin`
|
||||||
3. If changed, restart server with original credentials to reconfigure
|
3. If changed, restart server with original credentials to reconfigure
|
||||||
4. Check browser cookies are enabled
|
4. Check browser cookies are enabled
|
||||||
|
5. Clear browser cache if SSE connections seem stuck
|
||||||
|
|
||||||
### Windows agent window appears then closes
|
### Windows agent window appears then closes
|
||||||
|
|
||||||
@@ -312,6 +338,18 @@ make templ
|
|||||||
go run github.com/a-h/templ/cmd/templ@latest generate
|
go run github.com/a-h/templ/cmd/templ@latest generate
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Viewing Logs
|
||||||
|
|
||||||
|
The application uses structured logging with `slog`. Set log level with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Debug logging (shows all activity)
|
||||||
|
export GOLOGLEVEL=debug
|
||||||
|
|
||||||
|
# Info logging (default, shows important events)
|
||||||
|
export GOLOGLEVEL=info
|
||||||
|
```
|
||||||
|
|
||||||
### Running Tests
|
### Running Tests
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
@@ -324,20 +362,42 @@ go test ./...
|
|||||||
gofmt -w ./cmd ./internal ./views
|
gofmt -w ./cmd ./internal ./views
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Testing Real-time Features
|
||||||
|
|
||||||
|
To test SSE and HTMX functionality:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Start server
|
||||||
|
./bin/nerd-monitor-server
|
||||||
|
|
||||||
|
# In another terminal, send test stats
|
||||||
|
curl -X POST "http://localhost:8080/api/report?id=test-agent" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"hostname": "test", "cpuUsage": 25.5, "ramUsage": 1000000, "ramTotal": 8000000, "diskUsage": 50000000, "diskTotal": 100000000}'
|
||||||
|
```
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
Contributions are welcome! Please follow the code style guidelines in `AGENTS.md`.
|
Contributions are welcome! Please follow the code style guidelines in `AGENTS.md`.
|
||||||
|
|
||||||
|
## Recent Updates
|
||||||
|
|
||||||
|
- ✅ **Real-time Updates**: Instant UI updates via Server-Sent Events and HTMX
|
||||||
|
- ✅ **Structured Logging**: Comprehensive slog-based logging throughout
|
||||||
|
- ✅ **Enhanced Monitoring**: Better debugging and operational visibility
|
||||||
|
|
||||||
## Future Enhancements
|
## Future Enhancements
|
||||||
|
|
||||||
- [ ] Database persistence (SQLite/PostgreSQL)
|
- [ ] Database persistence (SQLite/PostgreSQL)
|
||||||
- [ ] Alerting system (email/Slack notifications)
|
- [ ] Alerting system (email/Slack notifications)
|
||||||
- [ ] Historical data / graphing
|
- [ ] Historical data / graphing with time-series storage
|
||||||
- [ ] Agent grouping and tagging
|
- [ ] Agent grouping and tagging
|
||||||
- [ ] Custom metric collection
|
- [ ] Custom metric collection
|
||||||
- [ ] TLS/HTTPS support
|
- [ ] TLS/HTTPS support
|
||||||
- [ ] Multi-tenancy
|
- [ ] Multi-tenancy
|
||||||
- [ ] API documentation (Swagger)
|
- [ ] API documentation (Swagger/OpenAPI)
|
||||||
|
- [ ] Webhook integrations
|
||||||
|
- [ ] Mobile app companion
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
314
RELEASE.md
Normal file
314
RELEASE.md
Normal file
@@ -0,0 +1,314 @@
|
|||||||
|
# Nerd Monitor - Release & Deployment Guide
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
This project uses **Gitea Actions** to automatically build and release cross-platform binaries when you push to the `master` branch or create a new tag.
|
||||||
|
|
||||||
|
## Automatic Release Pipeline
|
||||||
|
|
||||||
|
### How It Works
|
||||||
|
|
||||||
|
1. **Master Branch Push**: When you push to `master`, the workflow:
|
||||||
|
- Builds all platform binaries (Linux/macOS/Windows, amd64/arm64)
|
||||||
|
- Generates checksums (SHA256)
|
||||||
|
- Binaries are available as build artifacts for 30 days
|
||||||
|
|
||||||
|
2. **Tag Creation**: When you create a tag (e.g., `v1.0.0`), the workflow:
|
||||||
|
- Does all of the above
|
||||||
|
- Creates a Gitea Release
|
||||||
|
- Uploads all binaries and checksums to the release (permanent storage)
|
||||||
|
|
||||||
|
### Supported Platforms
|
||||||
|
|
||||||
|
Binaries are built for:
|
||||||
|
- **Linux**: amd64, arm64
|
||||||
|
- **macOS**: amd64 (Intel), arm64 (Apple Silicon)
|
||||||
|
- **Windows**: amd64
|
||||||
|
|
||||||
|
### What Gets Built and Released
|
||||||
|
|
||||||
|
When you push a tag, the workflow automatically creates:
|
||||||
|
|
||||||
|
**Binaries** (10 files total):
|
||||||
|
- `nerd-monitor-server-linux-amd64` - Server for Linux x86_64
|
||||||
|
- `nerd-monitor-server-linux-arm64` - Server for Linux ARM64 (Raspberry Pi, etc.)
|
||||||
|
- `nerd-monitor-server-darwin-amd64` - Server for macOS Intel
|
||||||
|
- `nerd-monitor-server-darwin-arm64` - Server for macOS Apple Silicon
|
||||||
|
- `nerd-monitor-server-windows-amd64.exe` - Server for Windows
|
||||||
|
- `nerd-monitor-agent-linux-amd64` - Agent for Linux x86_64
|
||||||
|
- `nerd-monitor-agent-linux-arm64` - Agent for Linux ARM64
|
||||||
|
- `nerd-monitor-agent-darwin-amd64` - Agent for macOS Intel
|
||||||
|
- `nerd-monitor-agent-darwin-arm64` - Agent for macOS Apple Silicon
|
||||||
|
- `nerd-monitor-agent-windows-amd64.exe` - Agent for Windows
|
||||||
|
|
||||||
|
**Checksums**:
|
||||||
|
- `SHA256SUMS` - SHA256 checksums for all binaries (for verification)
|
||||||
|
|
||||||
|
## Workflow Configuration
|
||||||
|
|
||||||
|
The Gitea Actions workflow is defined in `.gitea/workflows/release.yml`
|
||||||
|
|
||||||
|
### Trigger Events
|
||||||
|
- Push to `master` or `main` branch (builds only, no release)
|
||||||
|
- Push of git tags (e.g., `v1.0.0`) - triggers full release with uploads
|
||||||
|
|
||||||
|
### Creating a Release
|
||||||
|
|
||||||
|
#### Step 1: Create and Push a Tag
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Create an annotated tag
|
||||||
|
git tag -a v1.0.0 -m "Release version 1.0.0"
|
||||||
|
|
||||||
|
# Push the tag to Gitea
|
||||||
|
git push origin v1.0.0
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Step 2: Monitor the Workflow
|
||||||
|
|
||||||
|
1. Go to your repository on Gitea
|
||||||
|
2. Click the **Actions** tab
|
||||||
|
3. You'll see the workflow running:
|
||||||
|
- `build` job: Compiles all binaries (5-10 minutes)
|
||||||
|
- `docker-build` job: Builds Docker images (5-10 minutes)
|
||||||
|
|
||||||
|
#### Step 3: Verify the Release
|
||||||
|
|
||||||
|
Once the workflow completes:
|
||||||
|
|
||||||
|
1. Go to the **Releases** tab
|
||||||
|
2. You'll see a new release with:
|
||||||
|
- All platform binaries (Linux, macOS, Windows)
|
||||||
|
- SHA256SUMS file with checksums
|
||||||
|
- Docker image files (.tar)
|
||||||
|
|
||||||
|
### What Gets Built and Released
|
||||||
|
|
||||||
|
When you push a tag, the workflow automatically:
|
||||||
|
|
||||||
|
**Binaries** (10 files total):
|
||||||
|
- `nerd-monitor-server-linux-amd64` - Server for Linux x86_64
|
||||||
|
- `nerd-monitor-server-linux-arm64` - Server for Linux ARM64 (Raspberry Pi, etc.)
|
||||||
|
- `nerd-monitor-server-darwin-amd64` - Server for macOS Intel
|
||||||
|
- `nerd-monitor-server-darwin-arm64` - Server for macOS Apple Silicon
|
||||||
|
- `nerd-monitor-server-windows-amd64.exe` - Server for Windows
|
||||||
|
- `nerd-monitor-agent-linux-amd64` - Agent for Linux x86_64
|
||||||
|
- `nerd-monitor-agent-linux-arm64` - Agent for Linux ARM64
|
||||||
|
- `nerd-monitor-agent-darwin-amd64` - Agent for macOS Intel
|
||||||
|
- `nerd-monitor-agent-darwin-arm64` - Agent for macOS Apple Silicon
|
||||||
|
- `nerd-monitor-agent-windows-amd64.exe` - Agent for Windows
|
||||||
|
|
||||||
|
**Checksums**:
|
||||||
|
- `SHA256SUMS` - SHA256 checksums for all binaries (for verification)
|
||||||
|
|
||||||
|
## Local Building
|
||||||
|
|
||||||
|
You can also build binaries locally:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Build for current platform
|
||||||
|
make build
|
||||||
|
|
||||||
|
# Build for all platforms
|
||||||
|
make build-all
|
||||||
|
|
||||||
|
# Clean build artifacts
|
||||||
|
make clean
|
||||||
|
```
|
||||||
|
|
||||||
|
Binaries are created in the `bin/` directory.
|
||||||
|
|
||||||
|
## Docker Images (Manual Build)
|
||||||
|
|
||||||
|
Docker images are available but not built automatically by CI/CD. To build them manually:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Build server image
|
||||||
|
docker build -t nerd-monitor-server:latest -f Dockerfile.server .
|
||||||
|
|
||||||
|
# Build agent image
|
||||||
|
docker build -t nerd-monitor-agent:latest -f Dockerfile.agent .
|
||||||
|
|
||||||
|
# Or use Docker Compose
|
||||||
|
docker-compose build
|
||||||
|
|
||||||
|
# Or use Docker Compose to run both
|
||||||
|
docker-compose up
|
||||||
|
```
|
||||||
|
|
||||||
|
See `DOCKER_COMPOSE.md` and `QUICKSTART.md` for detailed instructions.
|
||||||
|
|
||||||
|
## Gitea Configuration
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
Before the CI/CD pipeline can create releases automatically, you need to:
|
||||||
|
|
||||||
|
1. **Ensure Gitea Actions is enabled** on your Gitea server
|
||||||
|
2. **Create a Gitea API Token** with release permissions
|
||||||
|
3. **Add the token as an Actions secret** in your repository
|
||||||
|
|
||||||
|
### Setup Instructions
|
||||||
|
|
||||||
|
#### 1. Enable Gitea Actions (Server Admin)
|
||||||
|
|
||||||
|
SSH into your Gitea server and verify Actions is enabled:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Edit the Gitea configuration
|
||||||
|
sudo vi /etc/gitea/app.ini
|
||||||
|
|
||||||
|
# Add or verify:
|
||||||
|
[actions]
|
||||||
|
ENABLED = true
|
||||||
|
```
|
||||||
|
|
||||||
|
Then restart Gitea:
|
||||||
|
```bash
|
||||||
|
sudo systemctl restart gitea
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Create an API Token
|
||||||
|
|
||||||
|
1. Log in to Gitea with your user account
|
||||||
|
2. Go to **Settings** → **Applications**
|
||||||
|
3. Click **Generate New Token**
|
||||||
|
4. Fill in:
|
||||||
|
- **Token Name**: `release-automation` (or any descriptive name)
|
||||||
|
- **Scopes**: Select `repo` (full repository access)
|
||||||
|
5. Click **Generate Token**
|
||||||
|
6. **Copy the token** (you won't be able to see it again)
|
||||||
|
|
||||||
|
#### 3. Add Token as Repository Secret
|
||||||
|
|
||||||
|
1. Go to your repository on Gitea
|
||||||
|
2. Navigate to **Settings** → **Secrets**
|
||||||
|
3. Click **Add Secret**
|
||||||
|
4. Fill in:
|
||||||
|
- **Secret Name**: `GITEA_TOKEN`
|
||||||
|
- **Secret Value**: Paste the token you copied
|
||||||
|
5. Click **Save**
|
||||||
|
|
||||||
|
Now the workflow will be able to create releases automatically!
|
||||||
|
|
||||||
|
### Workflow Configuration
|
||||||
|
|
||||||
|
The Gitea Actions workflow is defined in `.gitea/workflows/release.yml`
|
||||||
|
|
||||||
|
### Trigger Events
|
||||||
|
- Push to `master` or `main` branch (builds only, no release)
|
||||||
|
- Push of git tags (e.g., `v1.0.0`) - triggers full release with uploads
|
||||||
|
|
||||||
|
Each release includes:
|
||||||
|
|
||||||
|
```
|
||||||
|
nerd-monitor-server-linux-amd64
|
||||||
|
nerd-monitor-server-linux-arm64
|
||||||
|
nerd-monitor-server-darwin-amd64
|
||||||
|
nerd-monitor-server-darwin-arm64
|
||||||
|
nerd-monitor-server-windows-amd64.exe
|
||||||
|
|
||||||
|
nerd-monitor-agent-linux-amd64
|
||||||
|
nerd-monitor-agent-linux-arm64
|
||||||
|
nerd-monitor-agent-darwin-amd64
|
||||||
|
nerd-monitor-agent-darwin-arm64
|
||||||
|
nerd-monitor-agent-windows-amd64.exe
|
||||||
|
|
||||||
|
SHA256SUMS (checksums for all binaries)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Deployment Options
|
||||||
|
|
||||||
|
### Option 1: Use Pre-Built Binaries (Recommended)
|
||||||
|
Download the native binaries from the Releases tab and run directly:
|
||||||
|
- Smallest footprint
|
||||||
|
- No Docker required
|
||||||
|
- Easiest to deploy to existing machines
|
||||||
|
|
||||||
|
### Option 2: Use Docker (Manual Build)
|
||||||
|
Build Docker images manually and deploy:
|
||||||
|
```bash
|
||||||
|
docker build -t nerd-monitor-server:latest -f Dockerfile.server .
|
||||||
|
docker run -p 8080:8080 nerd-monitor-server:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
See `docker-compose.yml` for running both server and agent together.
|
||||||
|
|
||||||
|
### Option 3: Use Docker Compose
|
||||||
|
For quick development/testing with both server and agent:
|
||||||
|
```bash
|
||||||
|
docker-compose up
|
||||||
|
```
|
||||||
|
|
||||||
|
See `DOCKER_COMPOSE.md` and `QUICKSTART.md` for details.
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Workflow Not Triggering
|
||||||
|
|
||||||
|
**Problem**: I pushed a tag but the workflow didn't start
|
||||||
|
|
||||||
|
**Solutions**:
|
||||||
|
1. Check that the tag is pushed: `git push origin v1.0.0`
|
||||||
|
2. Verify Gitea Actions is enabled: Settings → Actions → Status should show "Enabled"
|
||||||
|
3. Check Actions tab for any error messages
|
||||||
|
4. Ensure your runner is online: Settings → Runners
|
||||||
|
|
||||||
|
### Token Authentication Failed
|
||||||
|
|
||||||
|
**Problem**: `"message":"Unauthorized"` or token-related errors in the logs
|
||||||
|
|
||||||
|
**Solutions**:
|
||||||
|
1. Verify the `GITEA_TOKEN` secret is set correctly: Repository → Settings → Secrets
|
||||||
|
2. Ensure the token has `repo` scope permissions
|
||||||
|
3. Token should not be expired - regenerate if needed
|
||||||
|
4. Double-check there are no extra spaces in the token
|
||||||
|
|
||||||
|
### Build Fails with "Go not found"
|
||||||
|
|
||||||
|
**Problem**: Workflow fails when trying to build, says Go is not available
|
||||||
|
|
||||||
|
**Solutions**:
|
||||||
|
1. This is usually a temporary issue - runner environment might not have been fully initialized
|
||||||
|
2. Retry the workflow: Go to Actions tab → Click the failed workflow → Click "Re-run jobs"
|
||||||
|
3. Check if the runner has enough disk space: `df -h` on the runner machine
|
||||||
|
|
||||||
|
### Binaries Not Uploaded to Release
|
||||||
|
|
||||||
|
**Problem**: Workflow completes but binaries don't appear in the release
|
||||||
|
|
||||||
|
**Solutions**:
|
||||||
|
1. Check the workflow logs: Actions tab → Click the workflow → View logs
|
||||||
|
2. Look for "Uploading" messages and any error messages
|
||||||
|
3. Verify the release was created: Go to Releases tab
|
||||||
|
4. Check that `GITEA_TOKEN` secret is still valid (tokens can expire)
|
||||||
|
|
||||||
|
### How to Check Workflow Logs
|
||||||
|
|
||||||
|
1. Go to your Gitea repository
|
||||||
|
2. Click the **Actions** tab
|
||||||
|
3. Click on the workflow run (should show the tag name)
|
||||||
|
4. Click on the `build` job
|
||||||
|
5. Scroll through the log to find error messages
|
||||||
|
6. Look for red `❌` marks indicating failures
|
||||||
|
|
||||||
|
### Manual Trigger for Testing
|
||||||
|
|
||||||
|
If you want to test the workflow without creating a full release:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Push to main/master branch (triggers build only, no release)
|
||||||
|
git push origin main
|
||||||
|
|
||||||
|
# Then push a tag when you're ready (triggers full release)
|
||||||
|
git tag -a v1.0.0 -m "Release"
|
||||||
|
git push origin v1.0.0
|
||||||
|
```
|
||||||
|
|
||||||
|
## Additional Resources
|
||||||
|
|
||||||
|
- [Gitea Actions Documentation](https://docs.gitea.io/en-us/actions/)
|
||||||
|
- [Project README](./README.md)
|
||||||
|
- [Quick Start Guide](./QUICKSTART.md)
|
||||||
|
- [Agent Guidelines](./AGENTS.md)
|
||||||
|
- [Docker Compose Guide](./DOCKER_COMPOSE.md)
|
||||||
@@ -5,7 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
@@ -25,8 +25,12 @@ func main() {
|
|||||||
)
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
// Set up verbose logging
|
||||||
|
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||||
|
|
||||||
if *server == "" {
|
if *server == "" {
|
||||||
log.Fatal("--server flag is required")
|
slog.Error("Server flag is required")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Normalize server URL (add http:// if missing)
|
// Normalize server URL (add http:// if missing)
|
||||||
@@ -38,12 +42,13 @@ func main() {
|
|||||||
var err error
|
var err error
|
||||||
id, err = generateAgentID()
|
id, err = generateAgentID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to generate agent ID: %v", err)
|
slog.Error("Failed to generate agent ID", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Starting agent with ID: %s\n", id)
|
slog.Info("Starting agent", "id", id)
|
||||||
log.Printf("Reporting to: %s every %v\n", *server, *interval)
|
slog.Info("Reporting configuration", "server", *server, "interval", *interval)
|
||||||
|
|
||||||
// Initialize stats collector
|
// Initialize stats collector
|
||||||
collector := stats.NewCollector()
|
collector := stats.NewCollector()
|
||||||
@@ -64,7 +69,7 @@ func main() {
|
|||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
reportStats(id, *server, collector)
|
reportStats(id, *server, collector)
|
||||||
case <-sigChan:
|
case <-sigChan:
|
||||||
log.Println("Agent shutting down gracefully...")
|
slog.Info("Agent shutting down gracefully")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,37 +84,39 @@ func reportStats(agentID, serverURL string, collector *stats.Collector) {
|
|||||||
|
|
||||||
stat, err := collector.Collect(hostname)
|
stat, err := collector.Collect(hostname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error collecting stats: %v", err)
|
slog.Error("Error collecting stats", "agentID", agentID, "hostname", hostname, "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Marshal to JSON
|
// Marshal to JSON
|
||||||
body, err := json.Marshal(stat)
|
body, err := json.Marshal(stat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error marshaling stats: %v", err)
|
slog.Error("Error marshaling stats", "agentID", agentID, "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send to server
|
// Send to server
|
||||||
reportURL := fmt.Sprintf("%s/api/report?id=%s", serverURL, agentID)
|
reportURL := fmt.Sprintf("%s/api/report?id=%s", serverURL, agentID)
|
||||||
|
slog.Debug("Sending stats report", "agentID", agentID, "url", reportURL, "bodySize", len(body))
|
||||||
resp, err := http.Post(reportURL, "application/json", bytes.NewReader(body))
|
resp, err := http.Post(reportURL, "application/json", bytes.NewReader(body))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error reporting stats: %v", err)
|
slog.Error("Error reporting stats", "agentID", agentID, "url", reportURL, "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
log.Printf("Server returned status %d", resp.StatusCode)
|
slog.Error("Server returned non-OK status", "agentID", agentID, "statusCode", resp.StatusCode, "url", reportURL)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Stats reported: CPU %.1f%% | RAM %s / %s | Disk %s / %s",
|
slog.Debug("Stats reported successfully",
|
||||||
stat.CPUUsage,
|
"agentID", agentID,
|
||||||
formatBytes(stat.RAMUsage),
|
"cpu", stat.CPUUsage,
|
||||||
formatBytes(stat.RAMTotal),
|
"ramUsage", formatBytes(stat.RAMUsage),
|
||||||
formatBytes(stat.DiskUsage),
|
"ramTotal", formatBytes(stat.RAMTotal),
|
||||||
formatBytes(stat.DiskTotal),
|
"diskUsage", formatBytes(stat.DiskUsage),
|
||||||
|
"diskTotal", formatBytes(stat.DiskTotal),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
@@ -24,6 +24,9 @@ func main() {
|
|||||||
)
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
// Set up verbose logging
|
||||||
|
slog.SetLogLoggerLevel(slog.LevelDebug)
|
||||||
|
|
||||||
// Initialize dependencies
|
// Initialize dependencies
|
||||||
s := store.New()
|
s := store.New()
|
||||||
authMgr := auth.New(*username, *password)
|
authMgr := auth.New(*username, *password)
|
||||||
@@ -40,11 +43,20 @@ func main() {
|
|||||||
r.Get("/login", uiHandler.Login)
|
r.Get("/login", uiHandler.Login)
|
||||||
r.Post("/login", uiHandler.Login)
|
r.Post("/login", uiHandler.Login)
|
||||||
|
|
||||||
|
// Public routes (no auth required)
|
||||||
|
r.Post("/api/report", apiHandler.ReportStats)
|
||||||
|
r.Get("/api/agents", apiHandler.ListAgents)
|
||||||
|
r.Get("/api/agents/{id}", apiHandler.GetAgent)
|
||||||
|
|
||||||
// Protected routes (auth required)
|
// Protected routes (auth required)
|
||||||
r.Group(func(protectedRoutes chi.Router) {
|
r.Group(func(protectedRoutes chi.Router) {
|
||||||
protectedRoutes.Use(authMgr.Middleware)
|
protectedRoutes.Use(authMgr.Middleware)
|
||||||
protectedRoutes.Get("/", uiHandler.Dashboard)
|
protectedRoutes.Get("/", uiHandler.Dashboard)
|
||||||
protectedRoutes.Get("/agents/{id}", uiHandler.AgentDetail)
|
protectedRoutes.Get("/agents/{id}", uiHandler.AgentDetail)
|
||||||
|
protectedRoutes.Get("/api/dashboard/table", uiHandler.GetDashboardTable)
|
||||||
|
protectedRoutes.Get("/api/agents/{id}/stats", uiHandler.GetAgentStats)
|
||||||
|
protectedRoutes.Get("/api/events", uiHandler.Events)
|
||||||
|
protectedRoutes.Post("/agents/{id}/hostname", uiHandler.UpdateAgentHostname)
|
||||||
protectedRoutes.Post("/logout", uiHandler.Logout)
|
protectedRoutes.Post("/logout", uiHandler.Logout)
|
||||||
protectedRoutes.Post("/api/agents/remove-stale", uiHandler.RemoveStaleAgents)
|
protectedRoutes.Post("/api/agents/remove-stale", uiHandler.RemoveStaleAgents)
|
||||||
protectedRoutes.Post("/api/agents/{id}/delete", uiHandler.DeleteAgent)
|
protectedRoutes.Post("/api/agents/{id}/delete", uiHandler.DeleteAgent)
|
||||||
@@ -62,14 +74,15 @@ func main() {
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-sigChan
|
<-sigChan
|
||||||
log.Println("Shutting down server...")
|
slog.Info("Shutting down server...")
|
||||||
server.Close()
|
server.Close()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
log.Printf("Starting server on http://%s:%s\n", *addr, *port)
|
slog.Info("Starting server", "addr", *addr+":"+*port, "url", "http://"+*addr+":"+*port)
|
||||||
log.Printf("Login with %s / %s\n", *username, *password)
|
slog.Info("Login credentials", "username", *username, "password", *password)
|
||||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
log.Fatalf("Server error: %v", err)
|
slog.Error("Server error", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,57 +1,103 @@
|
|||||||
version: '3.8'
|
# ============================================================================
|
||||||
|
# Nerd Monitor Docker Compose Configuration
|
||||||
|
# ============================================================================
|
||||||
|
#
|
||||||
|
# This file provides multiple ways to run Nerd Monitor:
|
||||||
|
#
|
||||||
|
# 1. Full Stack (Server + Agent):
|
||||||
|
# docker-compose up
|
||||||
|
#
|
||||||
|
# 2. Server Only:
|
||||||
|
# docker-compose up server
|
||||||
|
#
|
||||||
|
# 3. Agent Only (requires external server):
|
||||||
|
# docker-compose up agent
|
||||||
|
# (Set SERVER env var: SERVER=your-server:8080 docker-compose up agent)
|
||||||
|
#
|
||||||
|
# 4. Multiple Agents:
|
||||||
|
# docker-compose up -d server
|
||||||
|
# docker-compose run --name agent1 -e AGENT_ID=machine1 agent
|
||||||
|
# docker-compose run --name agent2 -e AGENT_ID=machine2 agent
|
||||||
|
#
|
||||||
|
# ============================================================================
|
||||||
|
|
||||||
services:
|
services:
|
||||||
|
# =========================================================================
|
||||||
# Nerd Monitor Server
|
# Nerd Monitor Server
|
||||||
|
# =========================================================================
|
||||||
|
# Web UI and API endpoint for collecting agent statistics
|
||||||
|
#
|
||||||
|
# Environment Variables:
|
||||||
|
# ADDR: Server bind address (default: 0.0.0.0)
|
||||||
|
# PORT: Server port (default: 8080)
|
||||||
|
# USERNAME: Admin username (default: admin)
|
||||||
|
# PASSWORD: Admin password (default: admin) - CHANGE IN PRODUCTION
|
||||||
|
#
|
||||||
server:
|
server:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile.server
|
||||||
container_name: nerd-monitor-server
|
container_name: nerd-monitor-server
|
||||||
ports:
|
ports:
|
||||||
- "8080:8080"
|
- "8080:8080"
|
||||||
environment:
|
environment:
|
||||||
ADDR: 0.0.0.0
|
# Server configuration
|
||||||
PORT: 8080
|
ADDR: "0.0.0.0"
|
||||||
USERNAME: admin
|
PORT: "8080"
|
||||||
PASSWORD: admin
|
# IMPORTANT: Change these credentials in production!
|
||||||
volumes:
|
USERNAME: "admin"
|
||||||
- server-logs:/app/logs
|
PASSWORD: "admin"
|
||||||
restart: unless-stopped
|
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:8080/login"]
|
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:8080/login"]
|
||||||
interval: 30s
|
interval: 30s
|
||||||
timeout: 3s
|
timeout: 3s
|
||||||
retries: 3
|
retries: 3
|
||||||
start_period: 5s
|
start_period: 5s
|
||||||
|
restart: unless-stopped
|
||||||
networks:
|
networks:
|
||||||
- nerd-monitor-net
|
- nerd-monitor
|
||||||
labels:
|
# Resource limits (optional, uncomment to enable)
|
||||||
- "com.example.description=Nerd Monitor Server"
|
# deploy:
|
||||||
|
# resources:
|
||||||
|
# limits:
|
||||||
|
# cpus: '0.5'
|
||||||
|
# memory: 512M
|
||||||
|
|
||||||
# Example Agent 1 (Alpine-based, simulated)
|
# =========================================================================
|
||||||
# In production, agents run on monitored machines
|
# Nerd Monitor Agent
|
||||||
# This is for testing/demo purposes only
|
# =========================================================================
|
||||||
agent-demo:
|
# Lightweight monitoring agent that reports system stats to the server
|
||||||
|
#
|
||||||
|
# Environment Variables:
|
||||||
|
# SERVER: Server address (default: server:8080 when using docker-compose)
|
||||||
|
# INTERVAL: Reporting interval (default: 15s)
|
||||||
|
# AGENT_ID: Optional agent identifier (auto-generated from hostname if empty)
|
||||||
|
#
|
||||||
|
# Note: This agent depends on the server being healthy before starting
|
||||||
|
#
|
||||||
|
agent:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile.agent
|
dockerfile: Dockerfile.agent
|
||||||
container_name: nerd-monitor-agent-demo
|
|
||||||
environment:
|
environment:
|
||||||
SERVER: http://server:8080
|
# Agent configuration
|
||||||
INTERVAL: 15s
|
SERVER: "server:8080" # Connect to the server service
|
||||||
|
INTERVAL: "15s" # Report stats every 15 seconds
|
||||||
|
# AGENT_ID: "my-machine" # Optional: set a custom agent ID
|
||||||
depends_on:
|
depends_on:
|
||||||
server:
|
server:
|
||||||
condition: service_healthy
|
condition: service_healthy # Wait for server to be healthy
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
networks:
|
networks:
|
||||||
- nerd-monitor-net
|
- nerd-monitor
|
||||||
labels:
|
# Resource limits (optional, uncomment to enable)
|
||||||
- "com.example.description=Nerd Monitor Agent (Demo)"
|
# deploy:
|
||||||
|
# resources:
|
||||||
volumes:
|
# limits:
|
||||||
server-logs:
|
# cpus: '0.25'
|
||||||
driver: local
|
# memory: 128M
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
nerd-monitor-net:
|
# Shared network for server and agent communication
|
||||||
|
nerd-monitor:
|
||||||
driver: bridge
|
driver: bridge
|
||||||
|
|||||||
@@ -2,11 +2,59 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
"nerd-monitor/internal/stats"
|
"nerd-monitor/internal/stats"
|
||||||
"nerd-monitor/internal/store"
|
"nerd-monitor/internal/store"
|
||||||
"net/http"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Broadcaster manages Server-Sent Events clients.
|
||||||
|
type Broadcaster struct {
|
||||||
|
clients map[chan string]bool
|
||||||
|
mu sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
var apiBroadcaster = &Broadcaster{
|
||||||
|
clients: make(map[chan string]bool),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Broadcast sends a message to all connected SSE clients.
|
||||||
|
func (b *Broadcaster) Broadcast(message string) {
|
||||||
|
b.mu.RLock()
|
||||||
|
defer b.mu.RUnlock()
|
||||||
|
|
||||||
|
for clientChan := range b.clients {
|
||||||
|
select {
|
||||||
|
case clientChan <- message:
|
||||||
|
default:
|
||||||
|
// Client channel is full, skip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddClient adds a new SSE client.
|
||||||
|
func (b *Broadcaster) AddClient(clientChan chan string) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
b.clients[clientChan] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// RemoveClient removes an SSE client.
|
||||||
|
func (b *Broadcaster) RemoveClient(clientChan chan string) {
|
||||||
|
b.mu.Lock()
|
||||||
|
defer b.mu.Unlock()
|
||||||
|
delete(b.clients, clientChan)
|
||||||
|
close(clientChan)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAPIBroadcaster returns the API broadcaster instance.
|
||||||
|
func GetAPIBroadcaster() *Broadcaster {
|
||||||
|
return apiBroadcaster
|
||||||
|
}
|
||||||
|
|
||||||
// Handler manages HTTP requests.
|
// Handler manages HTTP requests.
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
store *store.Store
|
store *store.Store
|
||||||
@@ -19,57 +67,121 @@ func New(s *store.Store) *Handler {
|
|||||||
|
|
||||||
// ReportStats handles agent stats reports.
|
// ReportStats handles agent stats reports.
|
||||||
func (h *Handler) ReportStats(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) ReportStats(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("Incoming stats report request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
agentID := r.URL.Query().Get("id")
|
agentID := r.URL.Query().Get("id")
|
||||||
if agentID == "" {
|
if agentID == "" {
|
||||||
|
slog.Warn("Missing agent ID in stats report", "remoteAddr", r.RemoteAddr)
|
||||||
http.Error(w, "missing agent id", http.StatusBadRequest)
|
http.Error(w, "missing agent id", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var stat stats.Stats
|
var stat stats.Stats
|
||||||
if err := json.NewDecoder(r.Body).Decode(&stat); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&stat); err != nil {
|
||||||
|
slog.Error("Invalid request body in stats report", "agentID", agentID, "error", err)
|
||||||
http.Error(w, "invalid request body", http.StatusBadRequest)
|
http.Error(w, "invalid request body", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Debug("Updating agent stats", "agentID", agentID, "hostname", stat.Hostname, "cpu", stat.CPUUsage)
|
||||||
h.store.UpdateAgent(agentID, &stat)
|
h.store.UpdateAgent(agentID, &stat)
|
||||||
|
|
||||||
|
// Broadcast update to all connected SSE clients
|
||||||
|
message := "event: stats-update\ndata: {\"type\": \"stats-update\", \"agentId\": \"" + agentID + "\"}\n\n"
|
||||||
|
slog.Info("Broadcasting stats update", "agentID", agentID)
|
||||||
|
apiBroadcaster.Broadcast(message)
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
|
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
|
||||||
|
slog.Debug("Stats report processed successfully", "agentID", agentID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAgent returns stats for a single agent.
|
// GetAgent returns stats for a single agent.
|
||||||
func (h *Handler) GetAgent(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) GetAgent(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("Incoming get agent request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
agentID := r.URL.Query().Get("id")
|
agentID := r.URL.Query().Get("id")
|
||||||
if agentID == "" {
|
if agentID == "" {
|
||||||
|
slog.Warn("Missing agent ID in get agent request", "remoteAddr", r.RemoteAddr)
|
||||||
http.Error(w, "missing agent id", http.StatusBadRequest)
|
http.Error(w, "missing agent id", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Debug("Retrieving agent stats", "agentID", agentID)
|
||||||
agent := h.store.GetAgent(agentID)
|
agent := h.store.GetAgent(agentID)
|
||||||
if agent == nil {
|
if agent == nil {
|
||||||
|
slog.Warn("Agent not found", "agentID", agentID)
|
||||||
http.Error(w, "agent not found", http.StatusNotFound)
|
http.Error(w, "agent not found", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(agent)
|
json.NewEncoder(w).Encode(agent)
|
||||||
|
slog.Debug("Agent stats retrieved successfully", "agentID", agentID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListAgents returns all agents.
|
// ListAgents returns all agents.
|
||||||
func (h *Handler) ListAgents(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) ListAgents(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("Incoming list agents request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
|
slog.Debug("Retrieving all agents")
|
||||||
agents := h.store.GetAllAgents()
|
agents := h.store.GetAllAgents()
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(agents)
|
json.NewEncoder(w).Encode(agents)
|
||||||
|
slog.Debug("All agents listed successfully", "count", len(agents))
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAgent removes an agent.
|
// DeleteAgent removes an agent.
|
||||||
func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("Incoming delete agent request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
agentID := r.URL.Query().Get("id")
|
agentID := r.URL.Query().Get("id")
|
||||||
if agentID == "" {
|
if agentID == "" {
|
||||||
|
slog.Warn("Missing agent ID in delete agent request", "remoteAddr", r.RemoteAddr)
|
||||||
http.Error(w, "missing agent id", http.StatusBadRequest)
|
http.Error(w, "missing agent id", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Info("Deleting agent", "agentID", agentID)
|
||||||
h.store.DeleteAgent(agentID)
|
h.store.DeleteAgent(agentID)
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(map[string]string{"status": "deleted"})
|
json.NewEncoder(w).Encode(map[string]string{"status": "deleted"})
|
||||||
|
slog.Info("Agent deleted successfully", "agentID", agentID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateHostname updates the hostname for an agent.
|
||||||
|
func (h *Handler) UpdateHostname(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("Incoming update hostname request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
slog.Warn("Invalid method for update hostname", "method", r.Method)
|
||||||
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
agentID := chi.URLParam(r, "id")
|
||||||
|
if agentID == "" {
|
||||||
|
slog.Warn("Missing agent ID in update hostname request", "remoteAddr", r.RemoteAddr)
|
||||||
|
http.Error(w, "missing agent id", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hostname := r.FormValue("hostname")
|
||||||
|
if hostname == "" {
|
||||||
|
slog.Warn("Missing hostname in update request", "agentID", agentID)
|
||||||
|
http.Error(w, "missing hostname", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("Updating agent hostname", "agentID", agentID, "newHostname", hostname)
|
||||||
|
err := h.store.UpdateHostname(agentID, hostname)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("Failed to update hostname - agent not found", "agentID", agentID, "error", err)
|
||||||
|
http.Error(w, "agent not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"status": "updated"})
|
||||||
|
slog.Info("Agent hostname updated successfully", "agentID", agentID, "hostname", hostname)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package auth
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -36,11 +37,13 @@ func New(username, password string) *Manager {
|
|||||||
// Login validates credentials and creates a session.
|
// Login validates credentials and creates a session.
|
||||||
func (m *Manager) Login(username, password string) (string, error) {
|
func (m *Manager) Login(username, password string) (string, error) {
|
||||||
if username != m.username || password != m.password {
|
if username != m.username || password != m.password {
|
||||||
|
slog.Debug("Login failed - invalid credentials", "username", username)
|
||||||
return "", ErrInvalidCredentials
|
return "", ErrInvalidCredentials
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := generateToken()
|
token, err := generateToken()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
slog.Error("Failed to generate session token", "error", err)
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,6 +55,7 @@ func (m *Manager) Login(username, password string) (string, error) {
|
|||||||
ExpiresAt: time.Now().Add(m.expiryDur),
|
ExpiresAt: time.Now().Add(m.expiryDur),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Debug("Login successful, session created", "username", username, "token", token[:8]+"...")
|
||||||
return token, nil
|
return token, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,10 +66,17 @@ func (m *Manager) Validate(token string) bool {
|
|||||||
|
|
||||||
session, ok := m.sessions[token]
|
session, ok := m.sessions[token]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
slog.Debug("Session validation failed - token not found", "token", token[:8]+"...")
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return session.ExpiresAt.After(time.Now())
|
if !session.ExpiresAt.After(time.Now()) {
|
||||||
|
slog.Debug("Session validation failed - token expired", "token", token[:8]+"...", "expiredAt", session.ExpiresAt)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Debug("Session validation successful", "token", token[:8]+"...")
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logout invalidates a session.
|
// Logout invalidates a session.
|
||||||
@@ -74,16 +85,24 @@ func (m *Manager) Logout(token string) {
|
|||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
delete(m.sessions, token)
|
delete(m.sessions, token)
|
||||||
|
slog.Debug("Session logged out", "token", token[:8]+"...")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Middleware returns a Chi middleware for authentication.
|
// Middleware returns a Chi middleware for authentication.
|
||||||
func (m *Manager) Middleware(next http.Handler) http.Handler {
|
func (m *Manager) Middleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
cookie, err := r.Cookie("session_token")
|
cookie, err := r.Cookie("session_token")
|
||||||
if err != nil || !m.Validate(cookie.Value) {
|
if err != nil {
|
||||||
|
slog.Debug("Authentication failed - no session cookie", "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if !m.Validate(cookie.Value) {
|
||||||
|
slog.Debug("Authentication failed - invalid session", "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
slog.Debug("Authentication successful", "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,19 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"log/slog"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"nerd-monitor/internal/stats"
|
"nerd-monitor/internal/stats"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrAgentNotFound is returned when an agent is not found.
|
||||||
|
ErrAgentNotFound = errors.New("agent not found")
|
||||||
|
)
|
||||||
|
|
||||||
// AgentStats represents the latest stats for an agent.
|
// AgentStats represents the latest stats for an agent.
|
||||||
type AgentStats struct {
|
type AgentStats struct {
|
||||||
ID string
|
ID string
|
||||||
@@ -47,6 +54,7 @@ func (s *Store) UpdateAgent(id string, stat *stats.Stats) {
|
|||||||
DiskTotal: stat.DiskTotal,
|
DiskTotal: stat.DiskTotal,
|
||||||
LastSeen: time.Now(),
|
LastSeen: time.Now(),
|
||||||
}
|
}
|
||||||
|
slog.Debug("Agent stats updated", "agentID", id, "hostname", stat.Hostname, "cpu", stat.CPUUsage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAgent retrieves agent stats by ID.
|
// GetAgent retrieves agent stats by ID.
|
||||||
@@ -54,7 +62,13 @@ func (s *Store) GetAgent(id string) *AgentStats {
|
|||||||
s.mu.RLock()
|
s.mu.RLock()
|
||||||
defer s.mu.RUnlock()
|
defer s.mu.RUnlock()
|
||||||
|
|
||||||
return s.agents[id]
|
agent := s.agents[id]
|
||||||
|
if agent != nil {
|
||||||
|
slog.Debug("Agent retrieved", "agentID", id, "hostname", agent.Hostname)
|
||||||
|
} else {
|
||||||
|
slog.Debug("Agent not found", "agentID", id)
|
||||||
|
}
|
||||||
|
return agent
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllAgents returns all agents.
|
// GetAllAgents returns all agents.
|
||||||
@@ -66,6 +80,7 @@ func (s *Store) GetAllAgents() []*AgentStats {
|
|||||||
for _, agent := range s.agents {
|
for _, agent := range s.agents {
|
||||||
agents = append(agents, agent)
|
agents = append(agents, agent)
|
||||||
}
|
}
|
||||||
|
slog.Debug("All agents retrieved", "count", len(agents))
|
||||||
return agents
|
return agents
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,4 +90,21 @@ func (s *Store) DeleteAgent(id string) {
|
|||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
delete(s.agents, id)
|
delete(s.agents, id)
|
||||||
|
slog.Debug("Agent deleted", "agentID", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateHostname updates the hostname for an agent.
|
||||||
|
func (s *Store) UpdateHostname(id string, hostname string) error {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
|
agent, exists := s.agents[id]
|
||||||
|
if !exists {
|
||||||
|
slog.Debug("Agent not found for hostname update", "agentID", id)
|
||||||
|
return ErrAgentNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
agent.Hostname = hostname
|
||||||
|
slog.Debug("Agent hostname updated", "agentID", id, "hostname", hostname)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,13 @@ package ui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
"nerd-monitor/internal/api"
|
||||||
"nerd-monitor/internal/auth"
|
"nerd-monitor/internal/auth"
|
||||||
"nerd-monitor/internal/store"
|
"nerd-monitor/internal/store"
|
||||||
"nerd-monitor/views"
|
"nerd-monitor/views"
|
||||||
@@ -12,44 +16,61 @@ import (
|
|||||||
|
|
||||||
// Handler serves UI pages.
|
// Handler serves UI pages.
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
store *store.Store
|
store *store.Store
|
||||||
auth *auth.Manager
|
auth *auth.Manager
|
||||||
|
broadcaster interface {
|
||||||
|
AddClient(chan string)
|
||||||
|
RemoveClient(chan string)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a new UI handler.
|
// New creates a new UI handler.
|
||||||
func New(s *store.Store, a *auth.Manager) *Handler {
|
func New(s *store.Store, a *auth.Manager) *Handler {
|
||||||
return &Handler{
|
return &Handler{
|
||||||
store: s,
|
store: s,
|
||||||
auth: a,
|
auth: a,
|
||||||
|
broadcaster: api.GetAPIBroadcaster(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dashboard renders the dashboard page.
|
// Dashboard renders the dashboard page.
|
||||||
func (h *Handler) Dashboard(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) Dashboard(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("Incoming dashboard request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
|
slog.Debug("Rendering dashboard", "agentCount", len(h.store.GetAllAgents()), "staleAgentCount", len(h.getStaleAgents()))
|
||||||
agents := h.store.GetAllAgents()
|
agents := h.store.GetAllAgents()
|
||||||
staleAgents := h.getStaleAgents()
|
staleAgents := h.getStaleAgents()
|
||||||
|
|
||||||
component := views.Dashboard(agents, staleAgents)
|
component := views.Dashboard(agents, staleAgents)
|
||||||
component.Render(context.Background(), w)
|
component.Render(context.Background(), w)
|
||||||
|
slog.Debug("Dashboard rendered successfully")
|
||||||
}
|
}
|
||||||
|
|
||||||
// AgentDetail renders the agent detail page.
|
// AgentDetail renders the agent detail page.
|
||||||
func (h *Handler) AgentDetail(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) AgentDetail(w http.ResponseWriter, r *http.Request) {
|
||||||
agentID := r.PathValue("id")
|
slog.Debug("Incoming agent detail request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
|
agentID := chi.URLParam(r, "id")
|
||||||
|
slog.Debug("Retrieving agent detail", "agentID", agentID)
|
||||||
agent := h.store.GetAgent(agentID)
|
agent := h.store.GetAgent(agentID)
|
||||||
|
|
||||||
if agent == nil {
|
if agent == nil {
|
||||||
|
slog.Warn("Agent not found for detail view", "agentID", agentID)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
component := views.AgentDetail(agent)
|
component := views.AgentDetail(agent)
|
||||||
component.Render(context.Background(), w)
|
component.Render(context.Background(), w)
|
||||||
|
slog.Debug("Agent detail rendered successfully", "agentID", agentID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Login renders the login page.
|
// Login renders the login page.
|
||||||
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("Incoming login request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
if r.Method == http.MethodGet {
|
if r.Method == http.MethodGet {
|
||||||
|
slog.Debug("Rendering login page")
|
||||||
component := views.LoginPage("")
|
component := views.LoginPage("")
|
||||||
component.Render(context.Background(), w)
|
component.Render(context.Background(), w)
|
||||||
return
|
return
|
||||||
@@ -59,13 +80,16 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
|
|||||||
username := r.FormValue("username")
|
username := r.FormValue("username")
|
||||||
password := r.FormValue("password")
|
password := r.FormValue("password")
|
||||||
|
|
||||||
|
slog.Debug("Attempting login", "username", username, "remoteAddr", r.RemoteAddr)
|
||||||
token, err := h.auth.Login(username, password)
|
token, err := h.auth.Login(username, password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
slog.Warn("Login failed - invalid credentials", "username", username, "remoteAddr", r.RemoteAddr, "error", err)
|
||||||
component := views.LoginPage("Invalid credentials")
|
component := views.LoginPage("Invalid credentials")
|
||||||
component.Render(context.Background(), w)
|
component.Render(context.Background(), w)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Info("Login successful", "username", username, "remoteAddr", r.RemoteAddr)
|
||||||
http.SetCookie(w, &http.Cookie{
|
http.SetCookie(w, &http.Cookie{
|
||||||
Name: "session_token",
|
Name: "session_token",
|
||||||
Value: token,
|
Value: token,
|
||||||
@@ -79,19 +103,26 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Warn("Invalid method for login", "method", r.Method)
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logout handles logout.
|
// Logout handles logout.
|
||||||
func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("Incoming logout request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
|
slog.Warn("Invalid method for logout", "method", r.Method)
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cookie, err := r.Cookie("session_token")
|
cookie, err := r.Cookie("session_token")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
slog.Info("Logging out user", "remoteAddr", r.RemoteAddr)
|
||||||
h.auth.Logout(cookie.Value)
|
h.auth.Logout(cookie.Value)
|
||||||
|
} else {
|
||||||
|
slog.Debug("Logout attempted without valid session", "remoteAddr", r.RemoteAddr)
|
||||||
}
|
}
|
||||||
|
|
||||||
http.SetCookie(w, &http.Cookie{
|
http.SetCookie(w, &http.Cookie{
|
||||||
@@ -107,32 +138,73 @@ func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// RemoveStaleAgents handles bulk removal of stale agents.
|
// RemoveStaleAgents handles bulk removal of stale agents.
|
||||||
func (h *Handler) RemoveStaleAgents(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) RemoveStaleAgents(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("Incoming remove stale agents request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
|
slog.Warn("Invalid method for remove stale agents", "method", r.Method)
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
agentIDs := r.Form["agent_ids"]
|
agentIDs := r.Form["agent_ids"]
|
||||||
|
slog.Info("Removing stale agents", "count", len(agentIDs), "agentIDs", agentIDs)
|
||||||
for _, id := range agentIDs {
|
for _, id := range agentIDs {
|
||||||
h.store.DeleteAgent(id)
|
h.store.DeleteAgent(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
slog.Info("Stale agents removed successfully", "count", len(agentIDs))
|
||||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAgent handles single agent deletion.
|
// DeleteAgent handles single agent deletion.
|
||||||
func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
|
func (h *Handler) DeleteAgent(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("Incoming delete agent request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
|
slog.Warn("Invalid method for delete agent", "method", r.Method)
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
agentID := r.PathValue("id")
|
agentID := chi.URLParam(r, "id")
|
||||||
|
slog.Info("Deleting agent", "agentID", agentID)
|
||||||
h.store.DeleteAgent(agentID)
|
h.store.DeleteAgent(agentID)
|
||||||
|
|
||||||
|
slog.Info("Agent deleted successfully", "agentID", agentID)
|
||||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateAgentHostname handles hostname updates for agents.
|
||||||
|
func (h *Handler) UpdateAgentHostname(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("Incoming update agent hostname request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
slog.Warn("Invalid method for update agent hostname", "method", r.Method)
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
agentID := chi.URLParam(r, "id")
|
||||||
|
hostname := r.FormValue("hostname")
|
||||||
|
|
||||||
|
if hostname == "" {
|
||||||
|
slog.Warn("Empty hostname provided for update", "agentID", agentID)
|
||||||
|
http.Error(w, "Hostname cannot be empty", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("Updating agent hostname", "agentID", agentID, "newHostname", hostname)
|
||||||
|
err := h.store.UpdateHostname(agentID, hostname)
|
||||||
|
if err != nil {
|
||||||
|
slog.Warn("Failed to update hostname - agent not found", "agentID", agentID, "error", err)
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Info("Agent hostname updated successfully", "agentID", agentID, "hostname", hostname)
|
||||||
|
http.Redirect(w, r, "/agents/"+agentID, http.StatusSeeOther)
|
||||||
|
}
|
||||||
|
|
||||||
// getStaleAgents returns agents that haven't reported in 6 months.
|
// getStaleAgents returns agents that haven't reported in 6 months.
|
||||||
func (h *Handler) getStaleAgents() []*store.AgentStats {
|
func (h *Handler) getStaleAgents() []*store.AgentStats {
|
||||||
const staleThreshold = 6 * 30 * 24 * time.Hour
|
const staleThreshold = 6 * 30 * 24 * time.Hour
|
||||||
@@ -147,3 +219,177 @@ func (h *Handler) getStaleAgents() []*store.AgentStats {
|
|||||||
|
|
||||||
return stale
|
return stale
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDashboardTable returns HTML fragment for the agent table.
|
||||||
|
func (h *Handler) GetDashboardTable(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("HTMX dashboard table request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
|
agents := h.store.GetAllAgents()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
|
|
||||||
|
if len(agents) == 0 {
|
||||||
|
w.Write([]byte(`<div class="alert alert-info">
|
||||||
|
<strong>ℹ️ No agents connected</strong>
|
||||||
|
<p style="margin-top: 0.5rem; font-size: 0.875rem;">
|
||||||
|
Start an agent to see its statistics appear here.
|
||||||
|
</p>
|
||||||
|
</div>`))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write([]byte(`<div class="card">
|
||||||
|
<div class="card-title">Active Agents</div>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Hostname</th>
|
||||||
|
<th>CPU Usage</th>
|
||||||
|
<th>Memory</th>
|
||||||
|
<th>Disk</th>
|
||||||
|
<th>Last Seen</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>`))
|
||||||
|
|
||||||
|
for _, agent := range agents {
|
||||||
|
// Determine online status (agent is online if reported within last 15 seconds)
|
||||||
|
isOnline := time.Since(agent.LastSeen) < 15*time.Second
|
||||||
|
statusClass := "status-red"
|
||||||
|
statusText := "Offline"
|
||||||
|
if isOnline {
|
||||||
|
statusClass = "status-green"
|
||||||
|
statusText = "Online"
|
||||||
|
}
|
||||||
|
|
||||||
|
row := fmt.Sprintf(`<tr>
|
||||||
|
<td>
|
||||||
|
<div style="display: flex; align-items: center; gap: 0.5rem;">
|
||||||
|
<span class="status-badge %s" style="margin: 0;">%s</span>
|
||||||
|
<a href="/agents/%s" style="color: #3b82f6; text-decoration: none;">%s</a>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td>%.1f%%</td>
|
||||||
|
<td>%s / %s</td>
|
||||||
|
<td>%s / %s</td>
|
||||||
|
<td class="timestamp">%s</td>
|
||||||
|
</tr>`,
|
||||||
|
statusClass,
|
||||||
|
statusText,
|
||||||
|
agent.ID,
|
||||||
|
agent.Hostname,
|
||||||
|
agent.CPUUsage,
|
||||||
|
formatBytes(agent.RAMUsage),
|
||||||
|
formatBytes(agent.RAMTotal),
|
||||||
|
formatBytes(agent.DiskUsage),
|
||||||
|
formatBytes(agent.DiskTotal),
|
||||||
|
agent.LastSeen.Format("2006-01-02 15:04:05"))
|
||||||
|
w.Write([]byte(row))
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write([]byte(`</tbody></table></div>`))
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAgentStats returns HTML fragment for agent statistics.
|
||||||
|
func (h *Handler) GetAgentStats(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Debug("HTMX agent stats request", "method", r.Method, "path", r.URL.Path, "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
|
agentID := chi.URLParam(r, "id")
|
||||||
|
if agentID == "" {
|
||||||
|
slog.Warn("Missing agent ID in stats request", "remoteAddr", r.RemoteAddr)
|
||||||
|
http.Error(w, "missing agent id", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
agent := h.store.GetAgent(agentID)
|
||||||
|
if agent == nil {
|
||||||
|
slog.Warn("Agent not found for stats request", "agentID", agentID)
|
||||||
|
http.Error(w, "agent not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/html")
|
||||||
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
|
|
||||||
|
// Return just the stats content that should be updated
|
||||||
|
statsHTML := fmt.Sprintf(`<div style="font-size: 2rem; margin: 1rem 0;">%.1f%%</div>
|
||||||
|
<div class="progress-bar">
|
||||||
|
<div class="progress-fill" style="width: %.1f%%"></div>
|
||||||
|
</div>
|
||||||
|
<div style="margin-top: 1rem; font-size: 0.875rem; color: #94a3b8;">
|
||||||
|
Last updated: %s
|
||||||
|
</div>`,
|
||||||
|
agent.CPUUsage,
|
||||||
|
agent.CPUUsage,
|
||||||
|
agent.LastSeen.Format("2006-01-02 15:04:05"))
|
||||||
|
|
||||||
|
w.Write([]byte(statsHTML))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Events provides Server-Sent Events for real-time updates.
|
||||||
|
func (h *Handler) Events(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slog.Info("SSE connection established", "remoteAddr", r.RemoteAddr)
|
||||||
|
|
||||||
|
// Set headers for Server-Sent Events
|
||||||
|
w.Header().Set("Content-Type", "text/event-stream")
|
||||||
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
|
w.Header().Set("Connection", "keep-alive")
|
||||||
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
|
||||||
|
// Create a channel for this client
|
||||||
|
clientChan := make(chan string, 10)
|
||||||
|
h.broadcaster.AddClient(clientChan)
|
||||||
|
|
||||||
|
// Clean up when client disconnects
|
||||||
|
go func() {
|
||||||
|
<-r.Context().Done()
|
||||||
|
slog.Debug("SSE client disconnected", "remoteAddr", r.RemoteAddr)
|
||||||
|
h.broadcaster.RemoveClient(clientChan)
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Send initial connection event
|
||||||
|
fmt.Fprintf(w, "event: connected\ndata: {}\n\n")
|
||||||
|
w.(http.Flusher).Flush()
|
||||||
|
|
||||||
|
// Send a test event after 2 seconds
|
||||||
|
go func() {
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
select {
|
||||||
|
case clientChan <- "event: test\ndata: {\"message\": \"SSE working\"}\n\n":
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Listen for broadcast messages
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-r.Context().Done():
|
||||||
|
return
|
||||||
|
case message := <-clientChan:
|
||||||
|
slog.Debug("Sending SSE message to client", "remoteAddr", r.RemoteAddr, "message", message)
|
||||||
|
fmt.Fprintf(w, "%s\n", message)
|
||||||
|
w.(http.Flusher).Flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// formatBytes converts bytes to human-readable format.
|
||||||
|
func formatBytes(bytes uint64) string {
|
||||||
|
const (
|
||||||
|
kb = 1024
|
||||||
|
mb = kb * 1024
|
||||||
|
gb = mb * 1024
|
||||||
|
)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case bytes >= gb:
|
||||||
|
return fmt.Sprintf("%.1f GB", float64(bytes)/float64(gb))
|
||||||
|
case bytes >= mb:
|
||||||
|
return fmt.Sprintf("%.1f MB", float64(bytes)/float64(mb))
|
||||||
|
case bytes >= kb:
|
||||||
|
return fmt.Sprintf("%.1f KB", float64(bytes)/float64(kb))
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf("%d B", bytes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
57
scripts/upload-release.sh
Executable file
57
scripts/upload-release.sh
Executable file
@@ -0,0 +1,57 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# Script to upload release artifacts to Gitea
|
||||||
|
# Usage: ./scripts/upload-release.sh <tag> <gitea_token>
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
TAG="${1:?Tag is required (e.g., v1.0.0)}"
|
||||||
|
GITEA_TOKEN="${2:?Gitea API token is required}"
|
||||||
|
GITEA_URL="${GITEA_URL:-https://git.nerdnest.dev}"
|
||||||
|
REPO_OWNER="${REPO_OWNER:-ducky}"
|
||||||
|
REPO_NAME="${REPO_NAME:-nerd-monitor}"
|
||||||
|
BIN_DIR="./bin"
|
||||||
|
|
||||||
|
if [ ! -d "$BIN_DIR" ]; then
|
||||||
|
echo "Error: $BIN_DIR directory not found"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Get or create release
|
||||||
|
echo "Getting release info for tag: $TAG"
|
||||||
|
RELEASE_JSON=$(curl -s -X GET \
|
||||||
|
-H "Authorization: token $GITEA_TOKEN" \
|
||||||
|
"$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases/tags/$TAG" 2>/dev/null || echo "{}")
|
||||||
|
|
||||||
|
RELEASE_ID=$(echo "$RELEASE_JSON" | jq -r '.id // empty' 2>/dev/null)
|
||||||
|
|
||||||
|
if [ -z "$RELEASE_ID" ]; then
|
||||||
|
echo "Creating new release for tag: $TAG"
|
||||||
|
RELEASE_JSON=$(curl -s -X POST \
|
||||||
|
-H "Authorization: token $GITEA_TOKEN" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d "{\"tag_name\":\"$TAG\",\"name\":\"Release $TAG\",\"draft\":false,\"prerelease\":false}" \
|
||||||
|
"$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases")
|
||||||
|
RELEASE_ID=$(echo "$RELEASE_JSON" | jq -r '.id')
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Release ID: $RELEASE_ID"
|
||||||
|
|
||||||
|
# Upload all binaries
|
||||||
|
echo "Uploading release artifacts..."
|
||||||
|
for file in "$BIN_DIR"/*; do
|
||||||
|
if [ -f "$file" ]; then
|
||||||
|
filename=$(basename "$file")
|
||||||
|
echo " Uploading: $filename"
|
||||||
|
|
||||||
|
curl -s -X POST \
|
||||||
|
-H "Authorization: token $GITEA_TOKEN" \
|
||||||
|
-F "attachment=@$file" \
|
||||||
|
"$GITEA_URL/api/v1/repos/$REPO_OWNER/$REPO_NAME/releases/$RELEASE_ID/assets" > /dev/null
|
||||||
|
|
||||||
|
echo " ✓ $filename uploaded"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "Release created/updated successfully!"
|
||||||
|
echo "View at: $GITEA_URL/$REPO_OWNER/$REPO_NAME/releases/tag/$TAG"
|
||||||
@@ -9,6 +9,10 @@ templ AgentDetail(agent *store.AgentStats) {
|
|||||||
@BaseLayout(agent.Hostname, agentDetailContent(agent))
|
@BaseLayout(agent.Hostname, agentDetailContent(agent))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
templ AgentDetailContent(agent *store.AgentStats) {
|
||||||
|
@agentDetailContent(agent)
|
||||||
|
}
|
||||||
|
|
||||||
templ agentDetailContent(agent *store.AgentStats) {
|
templ agentDetailContent(agent *store.AgentStats) {
|
||||||
<a href="/" class="back-link">← Back to Dashboard</a>
|
<a href="/" class="back-link">← Back to Dashboard</a>
|
||||||
|
|
||||||
@@ -17,21 +21,25 @@ templ agentDetailContent(agent *store.AgentStats) {
|
|||||||
@AgentStatusBadge(agent.LastSeen)
|
@AgentStatusBadge(agent.LastSeen)
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- CPU Usage Card -->
|
||||||
<div class="grid" style="grid-template-columns: 1fr;">
|
<div class="grid" style="grid-template-columns: 1fr;">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-title">CPU Usage</div>
|
<div class="card-title">CPU Usage</div>
|
||||||
<div class="metric-value" style="font-size: 2rem; margin: 1rem 0;">
|
<div id="cpu-stats" hx-get={ templ.SafeURL("/api/agents/" + agent.ID + "/stats") } hx-trigger="stats-update" hx-swap="innerHTML">
|
||||||
{ FormatPercent(agent.CPUUsage) }
|
<div class="metric-value" style="font-size: 2rem; margin: 1rem 0;">
|
||||||
</div>
|
{ FormatPercent(agent.CPUUsage) }
|
||||||
<div class="progress-bar">
|
</div>
|
||||||
<div class={ "progress-fill", calcProgressClass(agent.CPUUsage/100) } style={ fmt.Sprintf("width: %.1f%%", agent.CPUUsage) }></div>
|
<div class="progress-bar">
|
||||||
</div>
|
<div class="progress-fill" class={ "progress-fill", calcProgressClass(agent.CPUUsage/100) } style={ fmt.Sprintf("width: %.1f%%", agent.CPUUsage) }></div>
|
||||||
<div style="margin-top: 1rem; font-size: 0.875rem; color: #94a3b8;">
|
</div>
|
||||||
Last updated: { FormatTime(agent.LastSeen) }
|
<div style="margin-top: 1rem; font-size: 0.875rem; color: #94a3b8;">
|
||||||
|
Last updated: { FormatTime(agent.LastSeen) }
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Memory and Disk Usage Cards -->
|
||||||
<div class="grid" style="margin-top: 1.5rem;">
|
<div class="grid" style="margin-top: 1.5rem;">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-title">Memory Usage</div>
|
<div class="card-title">Memory Usage</div>
|
||||||
@@ -43,24 +51,49 @@ templ agentDetailContent(agent *store.AgentStats) {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Agent Information and Settings -->
|
||||||
<div class="card" style="margin-top: 1.5rem;">
|
<div class="card" style="margin-top: 1.5rem;">
|
||||||
<div class="card-title">Agent Information</div>
|
<div class="card-title">Agent Information</div>
|
||||||
<div class="metric-row">
|
<div class="metric-row">
|
||||||
<span class="metric-label">Agent ID</span>
|
<span class="metric-label">Agent ID</span>
|
||||||
<span class="metric-value" style="font-family: monospace; font-size: 0.875rem;">{ agent.ID }</span>
|
<span class="metric-value" style="font-family: monospace; font-size: 0.875rem;">{ agent.ID }</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="metric-row">
|
|
||||||
<span class="metric-label">Hostname</span>
|
|
||||||
<span class="metric-value">{ agent.Hostname }</span>
|
|
||||||
</div>
|
|
||||||
<div class="metric-row">
|
<div class="metric-row">
|
||||||
<span class="metric-label">Last Seen</span>
|
<span class="metric-label">Last Seen</span>
|
||||||
<span class="metric-value">{ agent.LastSeen.Format("2006-01-02 15:04:05") }</span>
|
<span class="metric-value">{ agent.LastSeen.Format("2006-01-02 15:04:05") }</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="btn-group" style="margin-top: 1.5rem;">
|
<!-- Hostname Settings -->
|
||||||
<form method="POST" action={ templ.SafeURL(fmt.Sprintf("/api/agents/%s/delete", agent.ID)) } style="margin: 0;" onsubmit="return confirm('Are you sure you want to remove this agent?');">
|
<div class="card" style="margin-top: 1.5rem;">
|
||||||
|
<div class="card-title">Settings</div>
|
||||||
|
<form method="POST" action={ templ.SafeURL(fmt.Sprintf("/agents/%s/hostname", agent.ID)) } style="display: flex; flex-direction: column; gap: 1rem;">
|
||||||
|
<div style="display: flex; flex-direction: column; gap: 0.5rem;">
|
||||||
|
<label for="hostname" style="font-size: 0.875rem; font-weight: 500; color: #cbd5e1;">
|
||||||
|
Hostname
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="hostname"
|
||||||
|
name="hostname"
|
||||||
|
value={ agent.Hostname }
|
||||||
|
required
|
||||||
|
style="padding: 0.5rem 0.75rem; background-color: #1e293b; border: 1px solid #475569; border-radius: 0.375rem; color: #e2e8f0; font-size: 0.875rem;"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary" style="align-self: flex-start;">
|
||||||
|
Update Hostname
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Danger Zone -->
|
||||||
|
<div class="card" style="margin-top: 1.5rem; border-left: 3px solid #ef4444;">
|
||||||
|
<div class="card-title" style="color: #ef4444;">Danger Zone</div>
|
||||||
|
<p style="margin: 0 0 1rem 0; color: #94a3b8; font-size: 0.875rem;">
|
||||||
|
Deleting this agent will remove all its data and history.
|
||||||
|
</p>
|
||||||
|
<form method="POST" action={ templ.SafeURL(fmt.Sprintf("/api/agents/%s/delete", agent.ID)) } style="margin: 0;" onsubmit="return confirm('Are you sure you want to remove this agent? This action cannot be undone.');">
|
||||||
<button type="submit" class="btn btn-danger">Delete Agent</button>
|
<button type="submit" class="btn btn-danger">Delete Agent</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -8,74 +8,80 @@ templ Dashboard(agents []*store.AgentStats, staleAgents []*store.AgentStats) {
|
|||||||
@BaseLayout("Dashboard", dashboardContent(agents, staleAgents))
|
@BaseLayout("Dashboard", dashboardContent(agents, staleAgents))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
templ DashboardContent(agents []*store.AgentStats, staleAgents []*store.AgentStats) {
|
||||||
|
@dashboardContent(agents, staleAgents)
|
||||||
|
}
|
||||||
|
|
||||||
templ dashboardContent(agents []*store.AgentStats, staleAgents []*store.AgentStats) {
|
templ dashboardContent(agents []*store.AgentStats, staleAgents []*store.AgentStats) {
|
||||||
<h2 style="margin-bottom: 1rem;">Agent Status Overview</h2>
|
<h2 style="margin-bottom: 1rem;">Agent Status Overview</h2>
|
||||||
|
|
||||||
@StaleAgentAlert(len(staleAgents), nil)
|
@StaleAgentAlert(len(staleAgents), nil)
|
||||||
|
|
||||||
if len(agents) == 0 {
|
<div id="agent-table-container" hx-get="/api/dashboard/table" hx-trigger="stats-update" hx-swap="innerHTML">
|
||||||
<div class="alert alert-info">
|
if len(agents) == 0 {
|
||||||
<strong>ℹ️ No agents connected</strong>
|
<div class="alert alert-info">
|
||||||
<p style="margin-top: 0.5rem; font-size: 0.875rem;">
|
<strong>ℹ️ No agents connected</strong>
|
||||||
Start an agent to see its statistics appear here.
|
<p style="margin-top: 0.5rem; font-size: 0.875rem;">
|
||||||
</p>
|
Start an agent to see its statistics appear here.
|
||||||
</div>
|
</p>
|
||||||
} else {
|
|
||||||
<div class="card">
|
|
||||||
<div class="card-title">Active Agents</div>
|
|
||||||
<table>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>Hostname</th>
|
|
||||||
<th>CPU Usage</th>
|
|
||||||
<th>Memory</th>
|
|
||||||
<th>Disk</th>
|
|
||||||
<th>Last Seen</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
for _, agent := range agents {
|
|
||||||
@AgentRow(agent.ID, agent.Hostname, agent.CPUUsage, agent.RAMUsage, agent.RAMTotal, agent.DiskUsage, agent.DiskTotal, agent.LastSeen)
|
|
||||||
}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(staleAgents) > 0 {
|
|
||||||
<div class="card" style="margin-top: 2rem; border-color: #d97706;">
|
|
||||||
<div class="card-title" style="color: #f59e0b;">
|
|
||||||
Stale Agents ({ len(staleAgents) })
|
|
||||||
</div>
|
|
||||||
<form method="POST" action="/api/agents/remove-stale" style="margin-top: 1rem;">
|
|
||||||
<table>
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th style="width: 30px;"><input type="checkbox" id="select-all" onchange="toggleAll(this)" /></th>
|
|
||||||
<th>Hostname</th>
|
|
||||||
<th>Last Seen</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
for _, agent := range staleAgents {
|
|
||||||
<tr>
|
|
||||||
<td><input type="checkbox" name="agent_ids" value={ agent.ID } class="agent-checkbox" /></td>
|
|
||||||
<td>{ agent.Hostname }</td>
|
|
||||||
<td class="timestamp">{ FormatTime(agent.LastSeen) }</td>
|
|
||||||
</tr>
|
|
||||||
}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
<div class="btn-group">
|
|
||||||
<button type="submit" class="btn btn-danger" style="margin-top: 1rem;">Remove Selected</button>
|
|
||||||
</div>
|
</div>
|
||||||
</form>
|
} else {
|
||||||
|
<div class="card">
|
||||||
|
<div class="card-title">Active Agents</div>
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Hostname</th>
|
||||||
|
<th>CPU Usage</th>
|
||||||
|
<th>Memory</th>
|
||||||
|
<th>Disk</th>
|
||||||
|
<th>Last Seen</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
for _, agent := range agents {
|
||||||
|
@AgentRow(agent.ID, agent.Hostname, agent.CPUUsage, agent.RAMUsage, agent.RAMTotal, agent.DiskUsage, agent.DiskTotal, agent.LastSeen)
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
</div>
|
</div>
|
||||||
<script>
|
|
||||||
function toggleAll(checkbox) {
|
if len(staleAgents) > 0 {
|
||||||
const checkboxes = document.querySelectorAll('.agent-checkbox');
|
<div class="card" style="margin-top: 2rem; border-color: #d97706;">
|
||||||
checkboxes.forEach(cb => cb.checked = checkbox.checked);
|
<div class="card-title" style="color: #f59e0b;">
|
||||||
|
Stale Agents ({ len(staleAgents) })
|
||||||
|
</div>
|
||||||
|
<form method="POST" action="/api/agents/remove-stale" style="margin-top: 1rem;">
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th style="width: 30px;"><input type="checkbox" id="select-all" onchange="toggleAll(this)" /></th>
|
||||||
|
<th>Hostname</th>
|
||||||
|
<th>Last Seen</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
for _, agent := range staleAgents {
|
||||||
|
<tr>
|
||||||
|
<td><input type="checkbox" name="agent_ids" value={ agent.ID } class="agent-checkbox" /></td>
|
||||||
|
<td>{ agent.Hostname }</td>
|
||||||
|
<td class="timestamp">{ FormatTime(agent.LastSeen) }</td>
|
||||||
|
</tr>
|
||||||
|
}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<div class="btn-group">
|
||||||
|
<button type="submit" class="btn btn-danger" style="margin-top: 1rem;">Remove Selected</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
}
|
}
|
||||||
</script>
|
<script>
|
||||||
|
function toggleAll(checkbox) {
|
||||||
|
const checkboxes = document.querySelectorAll('.agent-checkbox');
|
||||||
|
checkboxes.forEach(cb => cb.checked = checkbox.checked);
|
||||||
}
|
}
|
||||||
|
</script>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
//go:generate templ generate
|
|
||||||
|
|
||||||
package views
|
|
||||||
@@ -6,8 +6,31 @@ templ BaseLayout(title string, content templ.Component) {
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<meta http-equiv="refresh" content="5" />
|
|
||||||
<title>{ title } - Nerd Monitor</title>
|
<title>{ title } - Nerd Monitor</title>
|
||||||
|
<script src="https://unpkg.com/htmx.org@2.0.4"></script>
|
||||||
|
<script>
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
// Connect to Server-Sent Events for real-time updates
|
||||||
|
const eventSource = new EventSource('/api/events');
|
||||||
|
eventSource.onmessage = function(event) {
|
||||||
|
console.log('SSE message received:', event.data);
|
||||||
|
};
|
||||||
|
eventSource.addEventListener('stats-update', function(event) {
|
||||||
|
console.log('Stats update event:', event.data);
|
||||||
|
// Trigger HTMX updates using HTMX's API
|
||||||
|
const agentTable = document.getElementById('agent-table-container');
|
||||||
|
const cpuStats = document.getElementById('cpu-stats');
|
||||||
|
if (agentTable) htmx.trigger(agentTable, 'stats-update');
|
||||||
|
if (cpuStats) htmx.trigger(cpuStats, 'stats-update');
|
||||||
|
});
|
||||||
|
eventSource.addEventListener('connected', function(event) {
|
||||||
|
console.log('SSE connected');
|
||||||
|
});
|
||||||
|
eventSource.onerror = function(event) {
|
||||||
|
console.log('SSE error:', event);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
</script>
|
||||||
<style>
|
<style>
|
||||||
* {
|
* {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user