15 Commits

Author SHA1 Message Date
Ducky SSH User
50dcfcdc83 Add logging and fix /agents/ route error
All checks were successful
Build and Release / build (push) Successful in 35s
2025-12-20 07:34:02 +00:00
Ducky SSH User
761b91b031 Add hostname editing feature to agent detail page
- Create enhanced agent detail template with all stats and hostname edit form
- Add UpdateHostname method to store for agent hostname updates
- Add UpdateAgentHostname handler in UI for web form submissions
- Add UpdateHostname endpoint in API handler for JSON requests
- Register new POST /agents/{id}/hostname route for hostname updates
- Improve agent detail page layout with Settings and Danger Zone sections
- Add error handling for missing agents and empty hostname values
2025-12-20 06:51:27 +00:00
Ducky SSH User
0a37b04506 Fix Alpine Linux addgroup/adduser syntax for non-root user creation
All checks were successful
Build and Release / build (push) Successful in 12s
2025-12-20 06:45:21 +00:00
Ducky SSH User
a5a683d1de Fix Docker Compose and Dockerfile issues: remove image pull, fix entrypoint permissions 2025-12-20 06:36:20 +00:00
Ducky SSH User
e0b8f8650b Fix wget --post-file usage with temporary files for API calls
All checks were successful
Build and Release / build (push) Successful in 11s
2025-12-20 06:31:33 +00:00
Ducky SSH User
3a7b5a0f9a Replace curl with wget in CI/CD workflow for Gitea runner compatibility 2025-12-20 06:31:02 +00:00
Ducky SSH User
999a595b9c Remove jq dependency from release workflow
Some checks failed
Build and Release / build (push) Failing after 12s
- Replace jq with grep for parsing JSON responses
- Use curl -w to capture HTTP response codes
- Improve error handling and logging
- Check HTTP response codes for upload success
- Should work on runners without jq installed
- Fixes 'command not found' error on release creation
2025-12-20 06:28:25 +00:00
Ducky SSH User
e6f705486d Fix git tag checkout in Gitea Actions workflow
Some checks failed
Build and Release / build (push) Failing after 10s
- For tags: fetch with explicit ref mapping to refs/tags/
- For branches: fetch with explicit ref mapping to refs/remotes/origin/
- Properly checkout tag refs using refs/tags/ path
- Fixes 'pathspec did not match any file(s)' error on tag builds
2025-12-20 06:24:56 +00:00
Ducky SSH User
66734923cb Update GITEA_SETUP.md for binary-only CI/CD pipeline
Some checks failed
Build and Release / build (push) Failing after 1s
- Clarify Docker is not required for CI/CD
- Note that Docker images are built manually
- Simplify troubleshooting section
- Remove Docker setup instructions from setup guide
- Link to manual Docker build documentation
2025-12-20 06:21:37 +00:00
Ducky SSH User
48d2d7f83d Update RELEASE.md to reflect binary-only CI/CD pipeline
- Remove references to Docker image CI/CD builds
- Clarify Docker images are built manually or with docker-compose
- Simplify deployment options (binaries, Docker, Docker Compose)
- Update troubleshooting to focus on binary releases
- Remove Docker-specific troubleshooting steps
- Keep Dockerfiles for manual builds
2025-12-20 06:21:18 +00:00
Ducky SSH User
444bda7263 Remove Docker-specific runner documentation
- Remove GITEA_RUNNER_DOCKER.md (no longer needed)
- Docker builds removed from CI/CD pipeline
- Dockerfiles and docker-compose remain for manual builds
2025-12-20 06:20:35 +00:00
Ducky SSH User
99fc1a28ad Simplify CI/CD pipeline to focus on binary releases only
- Remove docker-build job entirely
- Keep focus on cross-platform binary builds (Linux, macOS, Windows)
- Generate SHA256 checksums for all binaries
- Upload binaries and checksums to Gitea releases
- Keep Dockerfiles and docker-compose for manual builds
- Much simpler and more reliable workflow
2025-12-20 06:20:32 +00:00
Ducky SSH User
2075cd2901 Fix Docker builds in Gitea runner with proper Docker-in-Docker detection
- Use 'docker info' instead of 'command -v docker' for reliable detection
- Add 30-second wait for Docker daemon startup (for DinD startup delay)
- Improve Docker build step with better error handling
- Build Docker images when available, skip gracefully if not
- Add comprehensive GITEA_RUNNER_DOCKER.md setup guide
- Document Docker socket mounting for runners
- Include troubleshooting and complete docker-compose example
2025-12-20 06:16:10 +00:00
Ducky SSH User
f4ec33fe53 Update GITEA_SETUP.md with optional Docker configuration
- Document Docker installation on runner for image builds
- Clarify that Docker is optional (not required for binary builds)
- Add instructions for running the runner user with Docker permissions
- Update troubleshooting section with Docker-specific guidance
- Explain graceful handling when Docker is not available
- Add Docker installation link in support section
2025-12-20 06:13:53 +00:00
Ducky SSH User
3080cb1e87 Make Docker build optional in Gitea Actions workflow
- Check if Docker is available before attempting to use it
- Skip Docker image builds gracefully if Docker is not installed
- Provide helpful instructions for enabling Docker support
- Add error handling for Docker build failures
- Allow workflow to succeed even without Docker
- Binary builds will still complete successfully
2025-12-20 06:13:34 +00:00
16 changed files with 431 additions and 287 deletions

View File

@@ -17,10 +17,14 @@ jobs:
cd $GITHUB_WORKSPACE
git init
git remote add origin ${{ github.server_url }}/${{ github.repository }}.git
git fetch origin ${{ github.ref }}
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
git checkout ${{ github.ref_name }}
# 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
@@ -99,26 +103,32 @@ jobs:
echo "Creating release for tag: $TAG"
echo "Repository: $REPO_OWNER/$REPO_NAME"
# Get or create release
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 "{}")
# Create release using Gitea API with wget
echo "Creating new release..."
RELEASE_ID=$(echo "$RELEASE_JSON" | jq -r '.id // empty' 2>/dev/null)
# 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 "Creating new release..."
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')
echo "Created release ID: $RELEASE_ID"
else
echo "Using existing release ID: $RELEASE_ID"
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
@@ -126,15 +136,18 @@ jobs:
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
# 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 [ $? -eq 0 ]; then
if echo "$UPLOAD_RESPONSE" | grep -q '"id"'; then
echo " ✓ $filename uploaded"
else
echo " ✗ Failed to upload $filename"
echo "Response: $UPLOAD_RESPONSE"
fi
fi
done
@@ -142,89 +155,3 @@ jobs:
echo ""
echo "Release completed!"
echo "View at: $GITEA_URL/$REPO_OWNER/$REPO_NAME/releases/tag/$TAG"
docker-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
git fetch origin ${{ github.ref }}
if [[ "${{ github.ref }}" == refs/tags/* ]]; then
git checkout ${{ github.ref_name }}
else
git checkout -b ${{ github.ref_name }} origin/${{ github.ref_name }}
fi
- name: Set up Docker
run: |
docker --version
which docker
- 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 Docker images
run: |
mkdir -p /tmp/docker-images
echo "Building server image..."
docker build -t nerd-monitor-server:${{ env.version }} -f Dockerfile.server .
docker save nerd-monitor-server:${{ env.version }} -o /tmp/docker-images/nerd-monitor-server-${{ env.version }}.tar
echo "Building agent image..."
docker build -t nerd-monitor-agent:${{ env.version }} -f Dockerfile.agent .
docker save nerd-monitor-agent:${{ env.version }} -o /tmp/docker-images/nerd-monitor-agent-${{ env.version }}.tar
echo "Docker images built:"
ls -lh /tmp/docker-images/
- name: Upload Docker images to release
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 }}
# Get existing release
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 "Release not found, skipping Docker image upload"
else
echo "Uploading Docker images to release $RELEASE_ID..."
for file in /tmp/docker-images/*; 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
if [ $? -eq 0 ]; then
echo " ✓ $filename uploaded"
else
echo " ✗ Failed to upload $filename"
fi
fi
done
fi

View File

@@ -1,42 +1,20 @@
# Nerd Monitor - Agent Guidelines
## 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
# 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.
Note: Go 1.23+ project using Templ templating. Run `make templ` before building.
## Code Style Guidelines
**Imports**: Standard library first, then third-party packages, then internal modules. Organize alphabetically within each group.
**Formatting & Types**: Use `gofmt` standards. Explicit struct field comments for exported types. Prefer `error` return values (no panic for recoverable errors).
**Naming**: CamelCase for exported symbols, lowercase for unexported. Receiver names as single letters (e.g., `(m *Manager)`). Constants use ALL_CAPS_SNAKE_CASE.
**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.
- **Imports**: stdlib → third-party → internal (alphabetical within groups)
- **Formatting**: `gofmt` standards, explicit exported struct comments
- **Naming**: CamelCase exports, lowercase unexported, single-letter receivers (e.g., `(h *Handler)`)
- **Error Handling**: Explicit checks, `http.Error()` for HTTP, contextual wrapping
- **Concurrency**: `sync.RWMutex` for shared state, defer lock releases, small critical sections
- **HTTP**: Chi router, `func (h *Handler) Method(w http.ResponseWriter, r *http.Request)`, set Content-Type, validate early

View File

@@ -25,21 +25,21 @@ WORKDIR /app
COPY --from=builder /app/nerd-monitor-agent .
# Create non-root user
RUN addgroup -D appgroup && adduser -D appuser -G appgroup
USER appuser
# Create entrypoint script BEFORE switching users
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 entrypoint script to handle environment variables
RUN echo '#!/bin/sh\n\
SERVER=${SERVER:-localhost:8080}\n\
INTERVAL=${INTERVAL:-15s}\n\
AGENT_ID=${AGENT_ID:-}\n\
if [ -z "$AGENT_ID" ]; then\n\
exec ./nerd-monitor-agent --server "$SERVER" --interval "$INTERVAL"\n\
else\n\
exec ./nerd-monitor-agent --server "$SERVER" --interval "$INTERVAL" --id "$AGENT_ID"\n\
fi\n\
' > /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
# Run the agent
ENTRYPOINT ["/app/entrypoint.sh"]

View File

@@ -32,8 +32,17 @@ 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 -D appgroup && adduser -D appuser -G appgroup
RUN addgroup -g 1000 appgroup && adduser -D -u 1000 -G appgroup appuser
USER appuser
# Expose port
@@ -43,14 +52,5 @@ EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --quiet --tries=1 --spider http://localhost:8080/login || exit 1
# Create entrypoint script to handle environment variables
RUN echo '#!/bin/sh\n\
ADDR=${ADDR:-0.0.0.0}\n\
PORT=${PORT:-8080}\n\
USERNAME=${USERNAME:-admin}\n\
PASSWORD=${PASSWORD:-admin}\n\
exec ./nerd-monitor-server -addr "$ADDR" -port "$PORT" -username "$USERNAME" -password "$PASSWORD"\n\
' > /app/entrypoint.sh && chmod +x /app/entrypoint.sh
# Run the server
ENTRYPOINT ["/app/entrypoint.sh"]

View File

@@ -58,7 +58,7 @@ sudo systemctl restart gitea
## 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/Go.
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
@@ -84,34 +84,66 @@ To get a runner token:
3. Click **Create new runner**
4. Follow the registration steps
## Testing the Setup
### Installing Docker on the Runner (Optional)
Once everything is configured:
Docker is **not required** for the CI/CD pipeline. Binary builds work without Docker.
If you want to manually build Docker images:
1. **Push a test tag**:
```bash
git tag -a v0.0.1-test -m "Test release"
git push origin v0.0.1-test
# 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
```
2. **Monitor the build**:
- Go to Actions tab
- You should see the workflow running
- Check logs for any errors
## What Happens Next
3. **Verify the release**:
- Go to Releases tab
- You should see a new release with binaries and checksums
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 Docker and Go installed
- 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
@@ -119,33 +151,14 @@ git push origin v0.0.1-test
- Go to Settings → Secrets and verify it's there
- If it's there, try re-running the workflow
### Build fails with permission denied
- Make sure the runner has permission to run Docker commands
### 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
## What Happens Next
Once you've set everything up:
1. **Every push to main/master** triggers the build job:
- Compiles all platform binaries
- Builds Docker images
- Uploads artifacts as build artifacts
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 Docker images to the release
- Generates SHA256 checksums
3. **Releases are available in**:
- Repository Releases tab in Gitea
- Binaries ready for download
- Docker images ready for import
## 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

View File

@@ -2,7 +2,7 @@
## Overview
This project uses **Gitea Actions** to automatically build and release binaries and Docker images when you push to the `master` branch or create a new tag.
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
@@ -11,13 +11,12 @@ This project uses **Gitea Actions** to automatically build and release binaries
1. **Master Branch Push**: When you push to `master`, the workflow:
- Builds all platform binaries (Linux/macOS/Windows, amd64/arm64)
- Generates checksums (SHA256)
- Uploads artifacts for 30 days
- Builds Docker images (server & agent)
- 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 GitHub Release
- Uploads all binaries and Docker images to the release
- Creates a Gitea Release
- Uploads all binaries and checksums to the release (permanent storage)
### Supported Platforms
@@ -26,19 +25,32 @@ Binaries are built for:
- **macOS**: amd64 (Intel), arm64 (Apple Silicon)
- **Windows**: amd64
Docker images are built for Linux containers.
### 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` branch
- Push of git tags (e.g., `v1.0.0`)
### Jobs
- `build`: Compiles all platform binaries and generates checksums
- `docker-build`: Builds Docker images for server and agent
- 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
@@ -89,10 +101,6 @@ When you push a tag, the workflow automatically:
**Checksums**:
- `SHA256SUMS` - SHA256 checksums for all binaries (for verification)
**Docker Images**:
- `nerd-monitor-server-v1.0.0.tar` - Server Docker image (can be imported)
- `nerd-monitor-agent-v1.0.0.tar` - Agent Docker image (can be imported)
## Local Building
You can also build binaries locally:
@@ -110,21 +118,25 @@ make clean
Binaries are created in the `bin/` directory.
## Docker Images
## Docker Images (Manual Build)
Two Docker images are built:
Docker images are available but not built automatically by CI/CD. To build them manually:
### Server Image
```bash
docker pull nerd-monitor-server:latest
docker run -p 8080:8080 nerd-monitor-server
# 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
```
### Agent Image
```bash
docker pull nerd-monitor-agent:latest
docker run nerd-monitor-agent --server your-server:8080
```
See `DOCKER_COMPOSE.md` and `QUICKSTART.md` for detailed instructions.
## Gitea Configuration
@@ -205,18 +217,30 @@ nerd-monitor-agent-windows-amd64.exe
SHA256SUMS (checksums for all binaries)
```
## Coolify Integration
## Deployment Options
If you want to use Coolify for deployment:
### 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
1. **For Server Deployment**:
- Use `Dockerfile.server` as the build context
- Coolify will auto-build on `master` branch pushes
- Deploy the server container to Coolify
### 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
```
2. **For Agent Deployment**:
- Use `Dockerfile.agent` as the build context
- Deploy the agent container to machines that need monitoring
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
@@ -259,22 +283,12 @@ If you want to use Coolify for deployment:
3. Verify the release was created: Go to Releases tab
4. Check that `GITEA_TOKEN` secret is still valid (tokens can expire)
### Docker Image Build Fails
**Problem**: `docker-build` job fails
**Solutions**:
1. Verify Docker is installed on the runner: SSH to runner and run `docker --version`
2. Check if the Dockerfile has syntax errors: `docker build -f Dockerfile.server .`
3. Ensure runner has enough disk space for building: `docker system df`
4. Check Docker daemon is running: `sudo systemctl status docker`
### 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 job that failed (`build` or `docker-build`)
4. Click on the `build` job
5. Scroll through the log to find error messages
6. Look for red `❌` marks indicating failures

BIN
agent Executable file

Binary file not shown.

View File

@@ -5,7 +5,7 @@ import (
"encoding/json"
"flag"
"fmt"
"log"
"log/slog"
"net/http"
"net/url"
"os"
@@ -25,8 +25,12 @@ func main() {
)
flag.Parse()
// Set up verbose logging
slog.SetLogLoggerLevel(slog.LevelDebug)
if *server == "" {
log.Fatal("--server flag is required")
slog.Error("Server flag is required")
os.Exit(1)
}
// Normalize server URL (add http:// if missing)
@@ -38,12 +42,13 @@ func main() {
var err error
id, err = generateAgentID()
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)
log.Printf("Reporting to: %s every %v\n", *server, *interval)
slog.Info("Starting agent", "id", id)
slog.Info("Reporting configuration", "server", *server, "interval", *interval)
// Initialize stats collector
collector := stats.NewCollector()
@@ -64,7 +69,7 @@ func main() {
case <-ticker.C:
reportStats(id, *server, collector)
case <-sigChan:
log.Println("Agent shutting down gracefully...")
slog.Info("Agent shutting down gracefully")
os.Exit(0)
}
}
@@ -79,37 +84,39 @@ func reportStats(agentID, serverURL string, collector *stats.Collector) {
stat, err := collector.Collect(hostname)
if err != nil {
log.Printf("Error collecting stats: %v", err)
slog.Error("Error collecting stats", "agentID", agentID, "hostname", hostname, "error", err)
return
}
// Marshal to JSON
body, err := json.Marshal(stat)
if err != nil {
log.Printf("Error marshaling stats: %v", err)
slog.Error("Error marshaling stats", "agentID", agentID, "error", err)
return
}
// Send to server
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))
if err != nil {
log.Printf("Error reporting stats: %v", err)
slog.Error("Error reporting stats", "agentID", agentID, "url", reportURL, "error", err)
return
}
defer resp.Body.Close()
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
}
log.Printf("Stats reported: CPU %.1f%% | RAM %s / %s | Disk %s / %s",
stat.CPUUsage,
formatBytes(stat.RAMUsage),
formatBytes(stat.RAMTotal),
formatBytes(stat.DiskUsage),
formatBytes(stat.DiskTotal),
slog.Debug("Stats reported successfully",
"agentID", agentID,
"cpu", stat.CPUUsage,
"ramUsage", formatBytes(stat.RAMUsage),
"ramTotal", formatBytes(stat.RAMTotal),
"diskUsage", formatBytes(stat.DiskUsage),
"diskTotal", formatBytes(stat.DiskTotal),
)
}

View File

@@ -2,7 +2,7 @@ package main
import (
"flag"
"log"
"log/slog"
"net/http"
"os"
"os/signal"
@@ -24,6 +24,9 @@ func main() {
)
flag.Parse()
// Set up verbose logging
slog.SetLogLoggerLevel(slog.LevelDebug)
// Initialize dependencies
s := store.New()
authMgr := auth.New(*username, *password)
@@ -45,6 +48,7 @@ func main() {
protectedRoutes.Use(authMgr.Middleware)
protectedRoutes.Get("/", uiHandler.Dashboard)
protectedRoutes.Get("/agents/{id}", uiHandler.AgentDetail)
protectedRoutes.Post("/agents/{id}/hostname", uiHandler.UpdateAgentHostname)
protectedRoutes.Post("/logout", uiHandler.Logout)
protectedRoutes.Post("/api/agents/remove-stale", uiHandler.RemoveStaleAgents)
protectedRoutes.Post("/api/agents/{id}/delete", uiHandler.DeleteAgent)
@@ -62,14 +66,15 @@ func main() {
go func() {
<-sigChan
log.Println("Shutting down server...")
slog.Info("Shutting down server...")
server.Close()
os.Exit(0)
}()
log.Printf("Starting server on http://%s:%s\n", *addr, *port)
log.Printf("Login with %s / %s\n", *username, *password)
slog.Info("Starting server", "addr", *addr+":"+*port, "url", "http://"+*addr+":"+*port)
slog.Info("Login credentials", "username", *username, "password", *password)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Server error: %v", err)
slog.Error("Server error", "error", err)
os.Exit(1)
}
}

View File

@@ -1,5 +1,3 @@
version: '3.8'
# ============================================================================
# Nerd Monitor Docker Compose Configuration
# ============================================================================
@@ -40,7 +38,6 @@ services:
context: .
dockerfile: Dockerfile.server
container_name: nerd-monitor-server
image: nerd-monitor-server:latest
ports:
- "8080:8080"
environment:
@@ -82,7 +79,6 @@ services:
build:
context: .
dockerfile: Dockerfile.agent
image: nerd-monitor-agent:latest
environment:
# Agent configuration
SERVER: "server:8080" # Connect to the server service

View File

@@ -2,9 +2,12 @@ package api
import (
"encoding/json"
"log/slog"
"net/http"
"github.com/go-chi/chi/v5"
"nerd-monitor/internal/stats"
"nerd-monitor/internal/store"
"net/http"
)
// Handler manages HTTP requests.
@@ -19,57 +22,115 @@ func New(s *store.Store) *Handler {
// ReportStats handles agent stats reports.
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")
if agentID == "" {
slog.Warn("Missing agent ID in stats report", "remoteAddr", r.RemoteAddr)
http.Error(w, "missing agent id", http.StatusBadRequest)
return
}
var stat stats.Stats
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)
return
}
slog.Debug("Updating agent stats", "agentID", agentID, "hostname", stat.Hostname, "cpu", stat.CPUUsage)
h.store.UpdateAgent(agentID, &stat)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
slog.Debug("Stats report processed successfully", "agentID", agentID)
}
// GetAgent returns stats for a single agent.
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")
if agentID == "" {
slog.Warn("Missing agent ID in get agent request", "remoteAddr", r.RemoteAddr)
http.Error(w, "missing agent id", http.StatusBadRequest)
return
}
slog.Debug("Retrieving agent stats", "agentID", agentID)
agent := h.store.GetAgent(agentID)
if agent == nil {
slog.Warn("Agent not found", "agentID", agentID)
http.Error(w, "agent not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(agent)
slog.Debug("Agent stats retrieved successfully", "agentID", agentID)
}
// ListAgents returns all agents.
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()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(agents)
slog.Debug("All agents listed successfully", "count", len(agents))
}
// DeleteAgent removes an agent.
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")
if agentID == "" {
slog.Warn("Missing agent ID in delete agent request", "remoteAddr", r.RemoteAddr)
http.Error(w, "missing agent id", http.StatusBadRequest)
return
}
slog.Info("Deleting agent", "agentID", agentID)
h.store.DeleteAgent(agentID)
w.Header().Set("Content-Type", "application/json")
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)
}

View File

@@ -3,6 +3,7 @@ package auth
import (
"crypto/rand"
"encoding/hex"
"log/slog"
"net/http"
"sync"
"time"
@@ -36,11 +37,13 @@ func New(username, password string) *Manager {
// Login validates credentials and creates a session.
func (m *Manager) Login(username, password string) (string, error) {
if username != m.username || password != m.password {
slog.Debug("Login failed - invalid credentials", "username", username)
return "", ErrInvalidCredentials
}
token, err := generateToken()
if err != nil {
slog.Error("Failed to generate session token", "error", err)
return "", err
}
@@ -52,6 +55,7 @@ func (m *Manager) Login(username, password string) (string, error) {
ExpiresAt: time.Now().Add(m.expiryDur),
}
slog.Debug("Login successful, session created", "username", username, "token", token[:8]+"...")
return token, nil
}
@@ -62,10 +66,17 @@ func (m *Manager) Validate(token string) bool {
session, ok := m.sessions[token]
if !ok {
slog.Debug("Session validation failed - token not found", "token", token[:8]+"...")
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.
@@ -74,16 +85,24 @@ func (m *Manager) Logout(token string) {
defer m.mu.Unlock()
delete(m.sessions, token)
slog.Debug("Session logged out", "token", token[:8]+"...")
}
// Middleware returns a Chi middleware for authentication.
func (m *Manager) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
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)
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)
})
}

View File

@@ -1,12 +1,19 @@
package store
import (
"errors"
"log/slog"
"sync"
"time"
"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.
type AgentStats struct {
ID string
@@ -47,6 +54,7 @@ func (s *Store) UpdateAgent(id string, stat *stats.Stats) {
DiskTotal: stat.DiskTotal,
LastSeen: time.Now(),
}
slog.Debug("Agent stats updated", "agentID", id, "hostname", stat.Hostname, "cpu", stat.CPUUsage)
}
// GetAgent retrieves agent stats by ID.
@@ -54,7 +62,13 @@ func (s *Store) GetAgent(id string) *AgentStats {
s.mu.RLock()
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.
@@ -66,6 +80,7 @@ func (s *Store) GetAllAgents() []*AgentStats {
for _, agent := range s.agents {
agents = append(agents, agent)
}
slog.Debug("All agents retrieved", "count", len(agents))
return agents
}
@@ -75,4 +90,21 @@ func (s *Store) DeleteAgent(id string) {
defer s.mu.Unlock()
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
}

View File

@@ -2,9 +2,11 @@ package ui
import (
"context"
"log/slog"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"nerd-monitor/internal/auth"
"nerd-monitor/internal/store"
"nerd-monitor/views"
@@ -26,30 +28,42 @@ func New(s *store.Store, a *auth.Manager) *Handler {
// Dashboard renders the dashboard page.
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()
staleAgents := h.getStaleAgents()
component := views.Dashboard(agents, staleAgents)
component.Render(context.Background(), w)
slog.Debug("Dashboard rendered successfully")
}
// AgentDetail renders the agent detail page.
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)
if agent == nil {
slog.Warn("Agent not found for detail view", "agentID", agentID)
http.NotFound(w, r)
return
}
component := views.AgentDetail(agent)
component.Render(context.Background(), w)
slog.Debug("Agent detail rendered successfully", "agentID", agentID)
}
// Login renders the login page.
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 {
slog.Debug("Rendering login page")
component := views.LoginPage("")
component.Render(context.Background(), w)
return
@@ -59,13 +73,16 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
username := r.FormValue("username")
password := r.FormValue("password")
slog.Debug("Attempting login", "username", username, "remoteAddr", r.RemoteAddr)
token, err := h.auth.Login(username, password)
if err != nil {
slog.Warn("Login failed - invalid credentials", "username", username, "remoteAddr", r.RemoteAddr, "error", err)
component := views.LoginPage("Invalid credentials")
component.Render(context.Background(), w)
return
}
slog.Info("Login successful", "username", username, "remoteAddr", r.RemoteAddr)
http.SetCookie(w, &http.Cookie{
Name: "session_token",
Value: token,
@@ -79,19 +96,26 @@ func (h *Handler) Login(w http.ResponseWriter, r *http.Request) {
return
}
slog.Warn("Invalid method for login", "method", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
// Logout handles logout.
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 {
slog.Warn("Invalid method for logout", "method", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
cookie, err := r.Cookie("session_token")
if err == nil {
slog.Info("Logging out user", "remoteAddr", r.RemoteAddr)
h.auth.Logout(cookie.Value)
} else {
slog.Debug("Logout attempted without valid session", "remoteAddr", r.RemoteAddr)
}
http.SetCookie(w, &http.Cookie{
@@ -107,32 +131,73 @@ func (h *Handler) Logout(w http.ResponseWriter, r *http.Request) {
// RemoveStaleAgents handles bulk removal of stale agents.
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 {
slog.Warn("Invalid method for remove stale agents", "method", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
agentIDs := r.Form["agent_ids"]
slog.Info("Removing stale agents", "count", len(agentIDs), "agentIDs", agentIDs)
for _, id := range agentIDs {
h.store.DeleteAgent(id)
}
slog.Info("Stale agents removed successfully", "count", len(agentIDs))
http.Redirect(w, r, "/", http.StatusSeeOther)
}
// DeleteAgent handles single agent deletion.
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 {
slog.Warn("Invalid method for delete agent", "method", r.Method)
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
agentID := r.PathValue("id")
agentID := chi.URLParam(r, "id")
slog.Info("Deleting agent", "agentID", agentID)
h.store.DeleteAgent(agentID)
slog.Info("Agent deleted successfully", "agentID", agentID)
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.
func (h *Handler) getStaleAgents() []*store.AgentStats {
const staleThreshold = 6 * 30 * 24 * time.Hour

BIN
server Executable file

Binary file not shown.

View File

@@ -17,6 +17,7 @@ templ agentDetailContent(agent *store.AgentStats) {
@AgentStatusBadge(agent.LastSeen)
</div>
<!-- CPU Usage Card -->
<div class="grid" style="grid-template-columns: 1fr;">
<div class="card">
<div class="card-title">CPU Usage</div>
@@ -32,6 +33,7 @@ templ agentDetailContent(agent *store.AgentStats) {
</div>
</div>
<!-- Memory and Disk Usage Cards -->
<div class="grid" style="margin-top: 1.5rem;">
<div class="card">
<div class="card-title">Memory Usage</div>
@@ -43,24 +45,49 @@ templ agentDetailContent(agent *store.AgentStats) {
</div>
</div>
<!-- Agent Information and Settings -->
<div class="card" style="margin-top: 1.5rem;">
<div class="card-title">Agent Information</div>
<div class="metric-row">
<span class="metric-label">Agent ID</span>
<span class="metric-value" style="font-family: monospace; font-size: 0.875rem;">{ agent.ID }</span>
</div>
<div class="metric-row">
<span class="metric-label">Hostname</span>
<span class="metric-value">{ agent.Hostname }</span>
</div>
<div class="metric-row">
<span class="metric-label">Last Seen</span>
<span class="metric-value">{ agent.LastSeen.Format("2006-01-02 15:04:05") }</span>
</div>
</div>
<div class="btn-group" style="margin-top: 1.5rem;">
<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?');">
<!-- Hostname Settings -->
<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>
</form>
</div>