From 86566c82c2e65b82d6e3acf6064fe10aa576a15f Mon Sep 17 00:00:00 2001 From: Piotr Rygielski <114479+vikin91@users.noreply.github.com> Date: Thu, 29 Jan 2026 11:57:56 +0100 Subject: [PATCH 1/7] Empty commit From 1a26f15d8766ab6b23a1740eca22f246120a82ac Mon Sep 17 00:00:00 2001 From: Piotr Rygielski <114479+vikin91@users.noreply.github.com> Date: Thu, 29 Jan 2026 12:30:42 +0100 Subject: [PATCH 2/7] First iteration of web server --- .gitignore | 4 + Makefile | 14 +- VERSION | 1 + cmd/metrics-analyzer/main.go | 74 +----- go.mod | 4 + go.sum | 6 +- internal/analyzer/analyzer.go | 100 ++++++++ internal/analyzer/analyzer_test.go | 37 +++ web/DEPLOYMENT.md | 207 +++++++++++++++++ web/README.md | 141 ++++++++++++ web/UPDATE.md | 208 +++++++++++++++++ web/nginx.conf | 46 ++++ web/sensor-metrics-web.service | 36 +++ web/server/main.go | 192 ++++++++++++++++ web/static/index.html | 355 +++++++++++++++++++++++++++++ 15 files changed, 1359 insertions(+), 66 deletions(-) create mode 100644 VERSION create mode 100644 internal/analyzer/analyzer.go create mode 100644 internal/analyzer/analyzer_test.go create mode 100644 web/DEPLOYMENT.md create mode 100644 web/README.md create mode 100644 web/UPDATE.md create mode 100644 web/nginx.conf create mode 100644 web/sensor-metrics-web.service create mode 100644 web/server/main.go create mode 100644 web/static/index.html diff --git a/.gitignore b/.gitignore index 9d02db8..238aa62 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ bin/ *.dll *.so *.dylib +metrics-analyzer # Test artifacts coverage.out @@ -27,6 +28,9 @@ go.work.sum *.tmp *.log +# Frontend build output +dist/ + # Demo GIFs (hosted on vhs.charm.sh) demo/*.gif diff --git a/Makefile b/Makefile index ccf92cc..e8c075d 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,20 @@ -.PHONY: build test lint clean validate-rules +.PHONY: build test lint clean validate-rules build-web release-build + +VERSION ?= $(shell cat VERSION 2>/dev/null || echo dev) +RELEASE_OS ?= linux +RELEASE_ARCH ?= amd64 build: go build -o bin/metrics-analyzer ./cmd/metrics-analyzer +build-web: + go build -o bin/web-server ./web/server + +release-build: + mkdir -p dist + GOOS=$(RELEASE_OS) GOARCH=$(RELEASE_ARCH) go build -o dist/metrics-analyzer-$(VERSION)-$(RELEASE_OS)-$(RELEASE_ARCH) ./cmd/metrics-analyzer + GOOS=$(RELEASE_OS) GOARCH=$(RELEASE_ARCH) go build -o dist/web-server-$(VERSION)-$(RELEASE_OS)-$(RELEASE_ARCH) ./web/server + test: go test -v ./... diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..8acdd82 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.0.1 diff --git a/cmd/metrics-analyzer/main.go b/cmd/metrics-analyzer/main.go index 8029b65..4163f11 100644 --- a/cmd/metrics-analyzer/main.go +++ b/cmd/metrics-analyzer/main.go @@ -4,12 +4,9 @@ import ( "flag" "fmt" "os" - "path/filepath" "strings" - "github.com/stackrox/sensor-metrics-analyzer/internal/evaluator" - "github.com/stackrox/sensor-metrics-analyzer/internal/loadlevel" - "github.com/stackrox/sensor-metrics-analyzer/internal/parser" + "github.com/stackrox/sensor-metrics-analyzer/internal/analyzer" "github.com/stackrox/sensor-metrics-analyzer/internal/reporter" "github.com/stackrox/sensor-metrics-analyzer/internal/rules" "github.com/stackrox/sensor-metrics-analyzer/internal/tui" @@ -87,61 +84,18 @@ func analyzeCommand() { } } - // Extract cluster name from filename if not provided - if *clusterName == "" { - *clusterName = extractClusterName(metricsFile) - } - - // Load load detection rules - fmt.Fprintf(os.Stderr, "Loading load detection rules from %s...\n", *loadLevelDir) - loadRules, err := rules.LoadLoadDetectionRules(*loadLevelDir) - if err != nil { - fmt.Fprintf(os.Stderr, "Warning: Failed to load load detection rules: %v\n", err) - loadRules = []rules.LoadDetectionRule{} - } - - // Load evaluation rules - fmt.Fprintf(os.Stderr, "Loading rules from %s...\n", *rulesDir) - rulesList, err := rules.LoadRules(*rulesDir) - if err != nil { - fmt.Fprintf(os.Stderr, "Failed to load rules: %v\n", err) - os.Exit(1) - } - fmt.Fprintf(os.Stderr, "Loaded %d rules\n", len(rulesList)) - - // Parse metrics - fmt.Fprintf(os.Stderr, "Parsing metrics from %s...\n", metricsFile) - metrics, err := parser.ParseFile(metricsFile) + report, err := analyzer.AnalyzeFile(metricsFile, analyzer.Options{ + RulesDir: *rulesDir, + LoadLevelDir: *loadLevelDir, + ClusterName: *clusterName, + LoadLevelOverride: *loadLevelOverride, + ACSVersionOverride: *acsVersionOverride, + Logger: os.Stderr, + }) if err != nil { - fmt.Fprintf(os.Stderr, "Failed to parse metrics: %v\n", err) + fmt.Fprintf(os.Stderr, "Failed to analyze metrics: %v\n", err) os.Exit(1) } - fmt.Fprintf(os.Stderr, "Parsed %d metrics\n", len(metrics)) - - // Detect ACS version - acsVersion := *acsVersionOverride - if acsVersion == "" { - if detected, ok := metrics.DetectACSVersion(); ok { - acsVersion = detected - fmt.Fprintf(os.Stderr, "Detected ACS version: %s\n", acsVersion) - } else { - fmt.Fprintf(os.Stderr, "Warning: Could not detect ACS version\n") - } - } - - // Detect load level - loadDetector := loadlevel.NewDetector(loadRules) - detectedLoadLevel, err := loadlevel.DetectWithOverride(metrics, loadDetector, rules.LoadLevel(*loadLevelOverride)) - if err != nil { - fmt.Fprintf(os.Stderr, "Warning: Load level detection failed: %v\n", err) - detectedLoadLevel = rules.LoadLevelMedium - } - fmt.Fprintf(os.Stderr, "Detected load level: %s\n", detectedLoadLevel) - - // Evaluate all rules - fmt.Fprintf(os.Stderr, "Evaluating rules...\n") - report := evaluator.EvaluateAllRules(rulesList, metrics, detectedLoadLevel, acsVersion) - report.ClusterName = *clusterName // Generate report var outputContent string @@ -255,13 +209,7 @@ func listRulesCommand() { } func extractClusterName(filename string) string { - base := filepath.Base(filename) - // Remove extension - name := strings.TrimSuffix(base, filepath.Ext(base)) - // Remove common prefixes/suffixes - name = strings.TrimSuffix(name, "-sensor-metrics") - name = strings.TrimSuffix(name, "-metrics") - return name + return analyzer.ExtractClusterName(filename) } func printUsage() { diff --git a/go.mod b/go.mod index 46977b0..4f2fb62 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/charmbracelet/lipgloss v1.1.0 github.com/fatih/color v1.18.0 github.com/jedib0t/go-pretty/v6 v6.7.1 + github.com/stretchr/testify v1.11.1 golang.org/x/term v0.29.0 ) @@ -19,6 +20,7 @@ require ( github.com/charmbracelet/x/ansi v0.10.1 // indirect github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect github.com/charmbracelet/x/term v0.2.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect @@ -28,8 +30,10 @@ require ( github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/termenv v0.16.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect golang.org/x/sys v0.36.0 // indirect golang.org/x/text v0.22.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 0f2da70..447a254 100644 --- a/go.sum +++ b/go.sum @@ -48,8 +48,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= -github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= -github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= @@ -63,5 +63,7 @@ golang.org/x/term v0.29.0 h1:L6pJp37ocefwRRtYPKSWOWzOtWSxVajvz2ldH/xi3iU= golang.org/x/term v0.29.0/go.mod h1:6bl4lRlvVuDgSf3179VpIxBF0o10JUpXWOnI7nErv7s= golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM= golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/analyzer/analyzer.go b/internal/analyzer/analyzer.go new file mode 100644 index 0000000..4aaaa3d --- /dev/null +++ b/internal/analyzer/analyzer.go @@ -0,0 +1,100 @@ +package analyzer + +import ( + "fmt" + "io" + "path/filepath" + "strings" + + "github.com/stackrox/sensor-metrics-analyzer/internal/evaluator" + "github.com/stackrox/sensor-metrics-analyzer/internal/loadlevel" + "github.com/stackrox/sensor-metrics-analyzer/internal/parser" + "github.com/stackrox/sensor-metrics-analyzer/internal/rules" +) + +// Options controls analysis behavior and logging. +type Options struct { + RulesDir string + LoadLevelDir string + ClusterName string + LoadLevelOverride string + ACSVersionOverride string + Logger io.Writer +} + +// AnalyzeFile parses metrics and evaluates rules, returning the analysis report. +func AnalyzeFile(metricsFile string, opts Options) (rules.AnalysisReport, error) { + logOut := opts.Logger + if logOut == nil { + logOut = io.Discard + } + + rulesDir := opts.RulesDir + if rulesDir == "" { + return rules.AnalysisReport{}, fmt.Errorf("rules directory is required") + } + + loadLevelDir := opts.LoadLevelDir + if loadLevelDir == "" { + loadLevelDir = filepath.Join(rulesDir, "load-level") + } + + clusterName := opts.ClusterName + if clusterName == "" { + clusterName = ExtractClusterName(metricsFile) + } + + fmt.Fprintf(logOut, "Loading load detection rules from %s...\n", loadLevelDir) + loadRules, err := rules.LoadLoadDetectionRules(loadLevelDir) + if err != nil { + fmt.Fprintf(logOut, "Warning: Failed to load load detection rules: %v\n", err) + loadRules = []rules.LoadDetectionRule{} + } + + fmt.Fprintf(logOut, "Loading rules from %s...\n", rulesDir) + rulesList, err := rules.LoadRules(rulesDir) + if err != nil { + return rules.AnalysisReport{}, fmt.Errorf("failed to load rules: %w", err) + } + fmt.Fprintf(logOut, "Loaded %d rules\n", len(rulesList)) + + fmt.Fprintf(logOut, "Parsing metrics from %s...\n", metricsFile) + metrics, err := parser.ParseFile(metricsFile) + if err != nil { + return rules.AnalysisReport{}, fmt.Errorf("failed to parse metrics: %w", err) + } + fmt.Fprintf(logOut, "Parsed %d metrics\n", len(metrics)) + + acsVersion := opts.ACSVersionOverride + if acsVersion == "" { + if detected, ok := metrics.DetectACSVersion(); ok { + acsVersion = detected + fmt.Fprintf(logOut, "Detected ACS version: %s\n", acsVersion) + } else { + fmt.Fprintf(logOut, "Warning: Could not detect ACS version\n") + } + } + + loadDetector := loadlevel.NewDetector(loadRules) + detectedLoadLevel, err := loadlevel.DetectWithOverride(metrics, loadDetector, rules.LoadLevel(opts.LoadLevelOverride)) + if err != nil { + fmt.Fprintf(logOut, "Warning: Load level detection failed: %v\n", err) + detectedLoadLevel = rules.LoadLevelMedium + } + fmt.Fprintf(logOut, "Detected load level: %s\n", detectedLoadLevel) + + fmt.Fprintf(logOut, "Evaluating rules...\n") + report := evaluator.EvaluateAllRules(rulesList, metrics, detectedLoadLevel, acsVersion) + report.ClusterName = clusterName + + return report, nil +} + +// ExtractClusterName derives a cluster name from a file name. +func ExtractClusterName(filename string) string { + base := filepath.Base(filename) + name := strings.TrimSuffix(base, filepath.Ext(base)) + name = strings.TrimSuffix(name, "-sensor-metrics") + name = strings.TrimSuffix(name, "-metrics") + return name +} diff --git a/internal/analyzer/analyzer_test.go b/internal/analyzer/analyzer_test.go new file mode 100644 index 0000000..d57834d --- /dev/null +++ b/internal/analyzer/analyzer_test.go @@ -0,0 +1,37 @@ +package analyzer + +import ( + "bytes" + "path/filepath" + "runtime" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAnalyzeFile(t *testing.T) { + t.Parallel() + + _, thisFile, _, ok := runtime.Caller(0) + if !ok { + t.Fatal("AnalyzeFile() failed to resolve test file path") + } + repoRoot := filepath.Dir(filepath.Dir(filepath.Dir(thisFile))) + metricsFile := filepath.Join(repoRoot, "testdata", "fixtures", "sample_metrics.txt") + rulesDir := filepath.Join(repoRoot, "automated-rules") + + var logs bytes.Buffer + report, err := AnalyzeFile(metricsFile, Options{ + RulesDir: rulesDir, + Logger: &logs, + }) + assert.NoError(t, err) + + assert.NotEmpty(t, report.ClusterName, "AnalyzeFile() cluster name is empty") + assert.False(t, report.Timestamp.IsZero(), "AnalyzeFile() timestamp is zero") + assert.NotEmpty(t, report.LoadLevel, "AnalyzeFile() load level is empty") + assert.NotEmpty(t, report.Results, "AnalyzeFile() returned no results") + assert.Equal(t, report.Summary.TotalAnalyzed, len(report.Results), "AnalyzeFile() summary mismatch") + statusTotal := report.Summary.RedCount + report.Summary.YellowCount + report.Summary.GreenCount + assert.LessOrEqual(t, statusTotal, report.Summary.TotalAnalyzed, "AnalyzeFile() summary counts exceed total") +} diff --git a/web/DEPLOYMENT.md b/web/DEPLOYMENT.md new file mode 100644 index 0000000..c19fa13 --- /dev/null +++ b/web/DEPLOYMENT.md @@ -0,0 +1,207 @@ +# Deployment Guide + +This guide covers deploying the Sensor Metrics Analyzer Web service on a Linux server. + +## Prerequisites + +- Linux server with systemd +- Nginx installed +- Go 1.21+ (for building, or use pre-built binaries) +- Root or sudo access + +## Step 1: Prepare the Application + +1. Clone or copy the repository to the server: + ```bash + cd /opt + git clone sensor-metrics-analyzer + # Or copy the files to /opt/sensor-metrics-analyzer + ``` + +2. Download the precompiled binary from GitHub Releases: + ```bash + cd /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go + curl -L -o bin/web-server https://github.com/stackrox/sensor-metrics-analyzer/releases/latest/download/web-server-linux-amd64 + chmod +x bin/web-server + ``` + +3. Verify the binary exists: + ```bash + ls -lh /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/ + # Should show: web-server + ``` + +## Step 2: Create System User + +Create a dedicated user for running the service: + +```bash +sudo useradd -r -s /bin/false -d /opt/sensor-metrics-analyzer sensor-metrics +sudo chown -R sensor-metrics:sensor-metrics /opt/sensor-metrics-analyzer +``` + +## Step 3: Configure Systemd Service + +1. Copy the service file: + ```bash + sudo cp /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/web/sensor-metrics-web.service \ + /etc/systemd/system/ + ``` + +2. Edit the service file to match your paths: + ```bash + sudo nano /etc/systemd/system/sensor-metrics-web.service + ``` + + Update these paths if different: + - `WorkingDirectory`: `/opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/web/server` + - `ExecStart`: `/opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/web-server` + - `RULES_DIR`: `/opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/automated-rules` + - `LOAD_LEVEL_DIR`: `/opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/automated-rules/load-level` + - `TEMPLATE_PATH`: `/opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/templates/markdown.tmpl` + +3. Reload systemd and start the service: + ```bash + sudo systemctl daemon-reload + sudo systemctl enable sensor-metrics-web + sudo systemctl start sensor-metrics-web + ``` + +4. Verify the service is running: + ```bash + sudo systemctl status sensor-metrics-web + ``` + +5. Check logs: + ```bash + sudo journalctl -u sensor-metrics-web -f + ``` + +## Step 4: Configure Nginx + +1. Copy the nginx configuration: + ```bash + sudo cp /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/web/nginx.conf \ + /etc/nginx/sites-available/sensor-metrics-web + ``` + +2. Edit the configuration: + ```bash + sudo nano /etc/nginx/sites-available/sensor-metrics-web + ``` + + Update: + - `root`: `/opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/web/static` + - `server_name`: Your domain name (or leave as `_` for default) + +3. Enable the site and disable the default nginx site: + ```bash + sudo rm /etc/nginx/sites-enabled/default + sudo ln -s /etc/nginx/sites-available/sensor-metrics-web \ + /etc/nginx/sites-enabled/ + ``` + +4. Test nginx configuration: + ```bash + sudo nginx -t + ``` + +5. Reload nginx: + ```bash + sudo systemctl reload nginx + ``` + +## Step 5: Verify Deployment + +1. Test the backend health endpoint directly: + ```bash + curl http://localhost:8080/health + ``` + +2. Test the nginx health endpoint: + ```bash + curl http://localhost/health + # Or: curl http://your-domain/health + ``` + +3. Test the API directly: + ```bash + curl -X POST http://localhost/api/analyze/both \ + -F "file=@/path/to/test-metrics.prom" + ``` + +4. Access the web interface: + Open `http://your-server-ip` or `http://your-domain` in a browser + + Note: The backend on `:8080` only serves API endpoints, so `/` will return 404. + +## Step 6: Firewall Configuration + +If using a firewall, allow HTTP traffic: + +```bash +# For UFW +sudo ufw allow 80/tcp + +# For firewalld +sudo firewall-cmd --permanent --add-service=http +sudo firewall-cmd --reload +``` + +## Troubleshooting + +### Service won't start + +1. Check service status: + ```bash + sudo systemctl status sensor-metrics-web + ``` + +2. Check logs: + ```bash + sudo journalctl -u sensor-metrics-web -n 50 + ``` + +3. Verify binary exists and is executable: + ```bash + ls -l /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/web-server + ``` + +4. Test running manually: + ```bash + sudo -u sensor-metrics /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/web-server \ + --rules /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/automated-rules + ``` + +### Nginx 502 Bad Gateway + +1. Verify backend is running: + ```bash + curl http://localhost:8080/health + ``` + +2. Check nginx error logs: + ```bash + sudo tail -f /var/log/nginx/error.log + ``` + +3. Verify proxy_pass URL in nginx config matches backend listen address + +### File upload fails + +1. Check nginx `client_max_body_size` setting +2. Verify file size is within limits (default: 50MB) +3. Check backend logs for detailed errors + +## Security Hardening + +1. **Use HTTPS**: Set up SSL/TLS certificates (Let's Encrypt recommended) +2. **Restrict access**: Use firewall rules to limit access to specific IPs if needed +3. **Regular updates**: Keep the application and system updated +4. **Monitor logs**: Set up log monitoring for suspicious activity + +## Next Steps + +- See [UPDATE.md](./UPDATE.md) for update procedures +- Configure log rotation if needed +- Set up monitoring/alerting for the service diff --git a/web/README.md b/web/README.md new file mode 100644 index 0000000..4e402d2 --- /dev/null +++ b/web/README.md @@ -0,0 +1,141 @@ +# Sensor Metrics Analyzer Web + +Web interface for the Sensor Metrics Analyzer, allowing users to upload Prometheus metrics files and view analysis reports in both console and markdown formats. + +## Architecture + +- **Backend**: Go HTTP server running as a systemd service +- **Frontend**: Static HTML/JavaScript served by Nginx +- **API**: RESTful endpoint `/api/analyze/both` that accepts file uploads and returns JSON with both console and markdown outputs + +## Components + +- `server/` - Go HTTP server source code +- `static/` - Frontend HTML/JS/CSS files +- `nginx.conf` - Nginx configuration template +- `sensor-metrics-web.service` - Systemd service unit file + +## Quick Start + +### Prerequisites + +- Nginx installed and running +- Linux system with systemd + +### Download Precompiled Binaries + +We publish precompiled binaries in GitHub Releases. Download the latest `web-server` binary for your platform and place it in `sensor-metrics-analyzer-go/bin/`. + +Example: +```bash +curl -L -o bin/web-server https://github.com/stackrox/sensor-metrics-analyzer/releases/latest/download/web-server-linux-amd64 +chmod +x bin/web-server +``` + +### Deployment + +See [DEPLOYMENT.md](./DEPLOYMENT.md) for detailed deployment instructions. + +### Updating + +See [UPDATE.md](./UPDATE.md) for update procedures. + +## Development + +### Running Locally + +1. Start the backend server: + ```bash + cd sensor-metrics-analyzer-go/web/server + go run main.go \ + --listen :8080 \ + --rules ../automated-rules \ + --load-level-dir ../automated-rules/load-level + ``` + +2. Serve the frontend (in another terminal): + ```bash + cd sensor-metrics-analyzer-go/web/static + python3 -m http.server 8000 + ``` + +3. Access the web interface at `http://localhost:8000` + + Note: For local development, you may need to update the API endpoint in `index.html` to point to `http://localhost:8080/api/analyze/both` or use a local proxy. + +### Testing + +Test the API endpoint directly: +```bash +curl -X POST http://localhost:8080/api/analyze/both \ + -F "file=@/path/to/metrics.prom" +``` + +## Configuration + +The web server can be configured via command-line flags or environment variables: + +- `--listen` / `LISTEN_ADDR`: Listen address (default: `:8080`) +- `--rules` / `RULES_DIR`: Rules directory (default: `./automated-rules`) +- `--load-level-dir` / `LOAD_LEVEL_DIR`: Load level rules directory (default: `./automated-rules/load-level`) +- `--template` / `TEMPLATE_PATH`: Path to markdown template (default: `./templates/markdown.tmpl`) +- `--max-size` / `MAX_FILE_SIZE`: Maximum upload size in bytes (default: 50MB) +- `--timeout` / `REQUEST_TIMEOUT`: Request timeout duration (default: 60s) + +## API Endpoints + +### POST /api/analyze/both + +Upload a metrics file and receive both console and markdown outputs. + +**Request:** +- Method: `POST` +- Content-Type: `multipart/form-data` +- Body: Form field `file` containing the metrics file + +**Response:** +```json +{ + "console": "...", + "markdown": "...", + "error": "" // Optional, present if there were errors +} +``` + +### GET /health + +Health check endpoint. + +**Response:** +```json +{ + "status": "ok" +} +``` + +## Troubleshooting + +### Server won't start + +- Verify the rules directory exists and contains valid TOML files +- Check systemd logs: `journalctl -u sensor-metrics-web -f` + +### File upload fails + +- Check nginx `client_max_body_size` setting +- Verify the file size is within limits +- Check server logs for detailed error messages + +### Analysis returns errors + +- Ensure the uploaded file is a valid Prometheus metrics file +- Check that rules are properly configured +- Review server logs for analyzer output + +## Security Considerations + +- The service runs as a dedicated user (`sensor-metrics`) +- Temporary files are automatically cleaned up after processing +- File size limits prevent resource exhaustion +- Request timeouts prevent long-running requests +- No persistent storage of uploaded files or reports diff --git a/web/UPDATE.md b/web/UPDATE.md new file mode 100644 index 0000000..5f05695 --- /dev/null +++ b/web/UPDATE.md @@ -0,0 +1,208 @@ +# Update Guide + +This guide covers updating the Sensor Metrics Analyzer Web service. + +## Update Procedure + +### Step 1: Stop the Service + +```bash +sudo systemctl stop sensor-metrics-web +``` + +### Step 2: Backup Current Version (Optional but Recommended) + +```bash +# Backup binaries +sudo cp /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/web-server \ + /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/web-server.backup +``` + +### Step 3: Update the Application + +**Option A: Git Pull (if using git)** + +```bash +cd /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go +git pull origin main # or your branch name +``` + +**Option B: Manual Copy** + +Copy the new files to the server, preserving the directory structure. + +git add -### Step 4: Download Updated Binary + +```bash +cd /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go +curl -L -o bin/web-server https://github.com/stackrox/sensor-metrics-analyzer/releases/latest/download/web-server-linux-amd64 +chmod +x bin/web-server +``` + +### Step 5: Update Configuration Files (if needed) + +Check if any configuration files have changed: + +```bash +# Compare service file +diff /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/web/sensor-metrics-web.service \ + /etc/systemd/system/sensor-metrics-web.service + +# Compare nginx config +diff /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/web/nginx.conf \ + /etc/nginx/sites-available/sensor-metrics-web +``` + +If there are differences, update the files in `/etc/`: + +```bash +# Update systemd service (review changes first!) +sudo cp /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/web/sensor-metrics-web.service \ + /etc/systemd/system/ +sudo systemctl daemon-reload + +# Update nginx config (review changes first!) +sudo cp /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/web/nginx.conf \ + /etc/nginx/sites-available/sensor-metrics-web +sudo nginx -t # Test configuration +sudo systemctl reload nginx +``` + +### Step 6: Update Frontend Files + +```bash +sudo cp -r /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/web/static/* \ + /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/web/static/ +``` + +### Step 7: Verify Binaries + +```bash +# Check binaries exist and are executable +ls -lh /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/ + +# Test web server binary (should show usage) +/opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/web-server --help +``` + +### Step 8: Start the Service + +```bash +sudo systemctl start sensor-metrics-web +sudo systemctl status sensor-metrics-web +``` + +### Step 9: Verify Health + +```bash +# Check health endpoint +curl http://localhost:8080/health + +# Check service logs +sudo journalctl -u sensor-metrics-web -f +``` + +### Step 10: Test the Web Interface + +1. Open the web interface in a browser +2. Upload a test metrics file +3. Verify both console and markdown outputs are generated correctly + +## Rollback Procedure + +If something goes wrong, rollback to the previous version: + +```bash +# Stop service +sudo systemctl stop sensor-metrics-web + +# Restore binaries +sudo cp /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/web-server.backup \ + /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/web-server + +# Start service +sudo systemctl start sensor-metrics-web +``` + +## Automated Update Script + +You can create a simple update script: + +```bash +#!/bin/bash +# /opt/sensor-metrics-analyzer/update.sh + +set -e + +SERVICE_NAME="sensor-metrics-web" +APP_DIR="/opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go" +BIN_DIR="$APP_DIR/bin" + +echo "Stopping service..." +sudo systemctl stop $SERVICE_NAME + +echo "Backing up binaries..." +sudo cp $BIN_DIR/web-server $BIN_DIR/web-server.backup.$(date +%Y%m%d_%H%M%S) + +echo "Updating application..." +cd $APP_DIR +# git pull # Uncomment if using git + +echo "Downloading binary..." +curl -L -o $BIN_DIR/web-server https://github.com/stackrox/sensor-metrics-analyzer/releases/latest/download/web-server-linux-amd64 +chmod +x $BIN_DIR/web-server + +echo "Starting service..." +sudo systemctl start $SERVICE_NAME + +echo "Waiting for service to start..." +sleep 2 + +echo "Checking service status..." +sudo systemctl status $SERVICE_NAME --no-pager + +echo "Testing health endpoint..." +curl -s http://localhost:8080/health || echo "Health check failed!" + +echo "Update complete!" +``` + +Make it executable: +```bash +chmod +x /opt/sensor-metrics-analyzer/update.sh +``` + +## Update Checklist + +- [ ] Stop the service +- [ ] Backup current binaries +- [ ] Update application files +- [ ] Rebuild binaries +- [ ] Update configuration files (if changed) +- [ ] Update frontend files +- [ ] Verify binaries +- [ ] Start the service +- [ ] Verify health endpoint +- [ ] Test web interface +- [ ] Monitor logs for errors + +## Troubleshooting Updates + +### Service fails to start after update + +1. Check logs: `sudo journalctl -u sensor-metrics-web -n 50` +2. Verify binary exists: `ls -l /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/web-server` +3. Test binary manually: `sudo -u sensor-metrics /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/web-server --help` +4. Rollback if needed + +### Binary not found errors + +- Verify the build completed successfully +- Check file permissions: `sudo chown sensor-metrics:sensor-metrics /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/*` +- Ensure binaries are executable: `sudo chmod +x /opt/sensor-metrics-analyzer/sensor-metrics-analyzer-go/bin/*` + +### Configuration errors + +- Review configuration file changes before applying +- Test nginx config: `sudo nginx -t` +- Reload systemd after service file changes: `sudo systemctl daemon-reload` diff --git a/web/nginx.conf b/web/nginx.conf new file mode 100644 index 0000000..0aedd86 --- /dev/null +++ b/web/nginx.conf @@ -0,0 +1,46 @@ +# Nginx configuration for Sensor Metrics Analyzer Web +# Place this file at: /etc/nginx/sites-available/sensor-metrics-web +# Then create symlink: ln -s /etc/nginx/sites-available/sensor-metrics-web /etc/nginx/sites-enabled/ +# Reload nginx: sudo systemctl reload nginx + +server { + listen 80; + server_name _; # Replace with your domain name if needed + + # Maximum upload size (adjust as needed) + client_max_body_size 50m; + + # Frontend static files + root /path/to/sensor-metrics-analyzer-go/web/static; + index index.html; + + # Serve static files + location / { + try_files $uri $uri/ /index.html; + } + + # API proxy to Go backend + location /api/ { + proxy_pass http://localhost:8080; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_cache_bypass $http_upgrade; + + # Increase timeouts for large file processing + proxy_read_timeout 120s; + proxy_connect_timeout 120s; + proxy_send_timeout 120s; + } + + # Health check endpoint + location /health { + proxy_pass http://localhost:8080/health; + proxy_http_version 1.1; + proxy_set_header Host $host; + } +} diff --git a/web/sensor-metrics-web.service b/web/sensor-metrics-web.service new file mode 100644 index 0000000..5b68a3b --- /dev/null +++ b/web/sensor-metrics-web.service @@ -0,0 +1,36 @@ +[Unit] +Description=Sensor Metrics Analyzer Web Service +After=network.target + +[Service] +Type=simple +User=sensor-metrics +Group=sensor-metrics +WorkingDirectory=/opt/sensor-metrics-analyzer/web/server +ExecStart=/opt/sensor-metrics-analyzer/bin/web-server + +# Environment variables (adjust paths as needed) +Environment="LISTEN_ADDR=:8080" +Environment="RULES_DIR=/opt/sensor-metrics-analyzer/automated-rules" +Environment="LOAD_LEVEL_DIR=/opt/sensor-metrics-analyzer/automated-rules/load-level" +Environment="TEMPLATE_PATH=/opt/sensor-metrics-analyzer/templates/markdown.tmpl" +Environment="MAX_FILE_SIZE=52428800" + +# Security settings +NoNewPrivileges=true +PrivateTmp=true +ProtectSystem=strict +ProtectHome=true +ReadWritePaths=/tmp + +# Restart policy +Restart=on-failure +RestartSec=5s + +# Logging +StandardOutput=journal +StandardError=journal +SyslogIdentifier=sensor-metrics-web + +[Install] +WantedBy=multi-user.target diff --git a/web/server/main.go b/web/server/main.go new file mode 100644 index 0000000..e4bf560 --- /dev/null +++ b/web/server/main.go @@ -0,0 +1,192 @@ +package main + +import ( + "context" + "encoding/json" + "flag" + "fmt" + "io" + "log" + "net/http" + "os" + "time" + + "github.com/stackrox/sensor-metrics-analyzer/internal/analyzer" + "github.com/stackrox/sensor-metrics-analyzer/internal/reporter" +) + +const ( + defaultListenAddr = ":8080" + defaultMaxFileSize = 50 * 1024 * 1024 // 50MB + defaultRequestTimeout = 60 * time.Second + defaultRulesDir = "./automated-rules" + defaultLoadLevelDir = "./automated-rules/load-level" +) + +type Config struct { + ListenAddr string + MaxFileSize int64 + RequestTimeout time.Duration + RulesDir string + LoadLevelDir string + TemplatePath string +} + +type AnalyzeResponse struct { + Markdown string `json:"markdown"` + Console string `json:"console"` + Error string `json:"error,omitempty"` +} + +func main() { + cfg := parseFlags() + + log.Printf("Starting server on %s", cfg.ListenAddr) + log.Printf("Rules directory: %s", cfg.RulesDir) + log.Printf("Load level directory: %s", cfg.LoadLevelDir) + log.Printf("Max file size: %d bytes", cfg.MaxFileSize) + + http.HandleFunc("/api/analyze/both", handleAnalyzeBoth(cfg)) + http.HandleFunc("/health", handleHealth) + + if err := http.ListenAndServe(cfg.ListenAddr, nil); err != nil { + log.Fatalf("Server failed: %v", err) + } +} + +func parseFlags() *Config { + cfg := &Config{ + ListenAddr: defaultListenAddr, + MaxFileSize: defaultMaxFileSize, + RequestTimeout: defaultRequestTimeout, + RulesDir: defaultRulesDir, + LoadLevelDir: defaultLoadLevelDir, + TemplatePath: "./templates/markdown.tmpl", + } + + flag.StringVar(&cfg.ListenAddr, "listen", defaultListenAddr, "Listen address") + flag.Int64Var(&cfg.MaxFileSize, "max-size", defaultMaxFileSize, "Max upload file size (bytes)") + flag.DurationVar(&cfg.RequestTimeout, "timeout", defaultRequestTimeout, "Request timeout") + flag.StringVar(&cfg.RulesDir, "rules", defaultRulesDir, "Rules directory") + flag.StringVar(&cfg.LoadLevelDir, "load-level-dir", defaultLoadLevelDir, "Load level rules directory") + flag.StringVar(&cfg.TemplatePath, "template", cfg.TemplatePath, "Path to markdown template") + + flag.Parse() + + // Override with environment variables if set + if envAddr := os.Getenv("LISTEN_ADDR"); envAddr != "" { + cfg.ListenAddr = envAddr + } + if envSize := os.Getenv("MAX_FILE_SIZE"); envSize != "" { + var size int64 + if _, err := fmt.Sscanf(envSize, "%d", &size); err == nil { + cfg.MaxFileSize = size + } + } + if envRules := os.Getenv("RULES_DIR"); envRules != "" { + cfg.RulesDir = envRules + } + if envLoadLevel := os.Getenv("LOAD_LEVEL_DIR"); envLoadLevel != "" { + cfg.LoadLevelDir = envLoadLevel + } + if envTimeout := os.Getenv("REQUEST_TIMEOUT"); envTimeout != "" { + if parsed, err := time.ParseDuration(envTimeout); err == nil { + cfg.RequestTimeout = parsed + } + } + if envTemplate := os.Getenv("TEMPLATE_PATH"); envTemplate != "" { + cfg.TemplatePath = envTemplate + } + + return cfg +} + +func handleHealth(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) +} + +func handleAnalyzeBoth(cfg *Config) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + ctx, cancel := context.WithTimeout(r.Context(), cfg.RequestTimeout) + defer cancel() + if err := ctx.Err(); err != nil { + respondError(w, http.StatusRequestTimeout, "Request timed out") + return + } + + // Set max file size + r.Body = http.MaxBytesReader(w, r.Body, cfg.MaxFileSize) + + // Parse multipart form + if err := r.ParseMultipartForm(cfg.MaxFileSize); err != nil { + respondError(w, http.StatusBadRequest, fmt.Sprintf("Failed to parse form: %v", err)) + return + } + + // Get uploaded file + file, header, err := r.FormFile("file") + if err != nil { + respondError(w, http.StatusBadRequest, fmt.Sprintf("No file uploaded: %v", err)) + return + } + defer file.Close() + + // Create temporary file + tmpFile, err := os.CreateTemp("", "metrics-*.prom") + if err != nil { + respondError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to create temp file: %v", err)) + return + } + defer os.Remove(tmpFile.Name()) + defer tmpFile.Close() + + // Copy uploaded file to temp file + if _, err := io.Copy(tmpFile, file); err != nil { + respondError(w, http.StatusInternalServerError, fmt.Sprintf("Failed to save file: %v", err)) + return + } + tmpFile.Close() + + log.Printf("Processing file: %s (%d bytes)", header.Filename, header.Size) + + response := AnalyzeResponse{} + report, err := analyzer.AnalyzeFile(tmpFile.Name(), analyzer.Options{ + RulesDir: cfg.RulesDir, + LoadLevelDir: cfg.LoadLevelDir, + ClusterName: analyzer.ExtractClusterName(header.Filename), + Logger: io.Discard, + }) + if err := ctx.Err(); err != nil { + respondError(w, http.StatusRequestTimeout, "Request timed out") + return + } + if err != nil { + response.Error = fmt.Sprintf("Analysis failed: %v", err) + } else { + response.Console = reporter.GenerateConsole(report) + response.Markdown = reporter.GenerateMarkdown(report, cfg.TemplatePath) + if response.Markdown == "" { + response.Error = "Markdown generation returned empty content" + } + } + + // Return response + w.Header().Set("Content-Type", "application/json") + if response.Error != "" && response.Console == "" && response.Markdown == "" { + w.WriteHeader(http.StatusInternalServerError) + } + json.NewEncoder(w).Encode(response) + } +} + +func respondError(w http.ResponseWriter, status int, message string) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + json.NewEncoder(w).Encode(AnalyzeResponse{Error: message}) +} diff --git a/web/static/index.html b/web/static/index.html new file mode 100644 index 0000000..b11d17d --- /dev/null +++ b/web/static/index.html @@ -0,0 +1,355 @@ + + + + + + Sensor Metrics Analyzer + + + +
+

Sensor Metrics Analyzer

+

Upload a Prometheus metrics file to analyze sensor metrics

+ +
+ + +
+ +
+ + + + + +
+
+ + +
+
+

+            
+
+

+            
+
+
+ + + + From 530e9a118ff446bd74ffddd8e2e61a00c9f6a15d Mon Sep 17 00:00:00 2001 From: Piotr Rygielski <114479+vikin91@users.noreply.github.com> Date: Thu, 29 Jan 2026 12:58:02 +0100 Subject: [PATCH 3/7] Add version API and frontend disclaimers --- Makefile | 6 ++-- web/server/main.go | 31 ++++++++++++++++++ web/static/index.html | 76 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index e8c075d..76f40cf 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ .PHONY: build test lint clean validate-rules build-web release-build VERSION ?= $(shell cat VERSION 2>/dev/null || echo dev) +BUILD_TIME ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ") +WEB_LDFLAGS := -X 'main.buildVersion=$(VERSION)' -X 'main.buildTime=$(BUILD_TIME)' RELEASE_OS ?= linux RELEASE_ARCH ?= amd64 @@ -8,12 +10,12 @@ build: go build -o bin/metrics-analyzer ./cmd/metrics-analyzer build-web: - go build -o bin/web-server ./web/server + go build -ldflags "$(WEB_LDFLAGS)" -o bin/web-server ./web/server release-build: mkdir -p dist GOOS=$(RELEASE_OS) GOARCH=$(RELEASE_ARCH) go build -o dist/metrics-analyzer-$(VERSION)-$(RELEASE_OS)-$(RELEASE_ARCH) ./cmd/metrics-analyzer - GOOS=$(RELEASE_OS) GOARCH=$(RELEASE_ARCH) go build -o dist/web-server-$(VERSION)-$(RELEASE_OS)-$(RELEASE_ARCH) ./web/server + GOOS=$(RELEASE_OS) GOARCH=$(RELEASE_ARCH) go build -ldflags "$(WEB_LDFLAGS)" -o dist/web-server-$(VERSION)-$(RELEASE_OS)-$(RELEASE_ARCH) ./web/server test: go test -v ./... diff --git a/web/server/main.go b/web/server/main.go index e4bf560..65d6930 100644 --- a/web/server/main.go +++ b/web/server/main.go @@ -38,6 +38,16 @@ type AnalyzeResponse struct { Error string `json:"error,omitempty"` } +type VersionResponse struct { + Version string `json:"version"` + LastUpdate string `json:"lastUpdate"` +} + +var ( + buildVersion = "dev" + buildTime = "" +) + func main() { cfg := parseFlags() @@ -48,6 +58,7 @@ func main() { http.HandleFunc("/api/analyze/both", handleAnalyzeBoth(cfg)) http.HandleFunc("/health", handleHealth) + http.HandleFunc("/version", handleVersion()) if err := http.ListenAndServe(cfg.ListenAddr, nil); err != nil { log.Fatalf("Server failed: %v", err) @@ -106,6 +117,26 @@ func handleHealth(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) } +func handleVersion() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + version := "Unknown" + lastUpdate := "Unknown" + + if buildVersion != "" { + version = buildVersion + } + if buildTime != "" { + lastUpdate = buildTime + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(VersionResponse{ + Version: version, + LastUpdate: lastUpdate, + }) + } +} + func handleAnalyzeBoth(cfg *Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { diff --git a/web/static/index.html b/web/static/index.html index b11d17d..8ac0b70 100644 --- a/web/static/index.html +++ b/web/static/index.html @@ -38,6 +38,44 @@ margin-bottom: 30px; } + .meta { + display: flex; + flex-wrap: wrap; + gap: 12px 24px; + color: #95a5a6; + font-size: 14px; + margin-bottom: 20px; + } + + .meta a { + color: #3498db; + text-decoration: none; + } + + .meta a:hover { + text-decoration: underline; + } + + .privacy-note { + background: #f8f9fb; + border-left: 4px solid #3498db; + padding: 12px 16px; + border-radius: 4px; + color: #5d6d7e; + font-size: 14px; + margin-bottom: 24px; + } + + .disclaimer-note { + background: #fff7e6; + border-left: 4px solid #f39c12; + padding: 12px 16px; + border-radius: 4px; + color: #7d5a00; + font-size: 14px; + margin-bottom: 24px; + } + .upload-section { border: 2px dashed #bdc3c7; border-radius: 8px; @@ -195,6 +233,17 @@

Sensor Metrics Analyzer

Upload a Prometheus metrics file to analyze sensor metrics

+
+
Version: Unknown
+
Last update: Unknown
+
GitHub Releases
+
+
+ Privacy: Uploaded files are analyzed and not retained. Temporary files are deleted immediately after processing, and metric data is not stored. +
+
+ Disclaimer: This project is AI-generated and only a small fraction of the code and metric rules were verified by a human. Analysis results may be inaccurate and, in extreme cases, totally wrong. +
@@ -236,6 +285,33 @@

Sensor Metrics Analyzer

const markdownOutput = document.getElementById('markdownOutput'); const tabs = document.querySelectorAll('.tab'); const tabContents = document.querySelectorAll('.tab-content'); + const versionValue = document.getElementById('versionValue'); + const lastUpdateValue = document.getElementById('lastUpdateValue'); + const releaseLink = document.getElementById('releaseLink'); + + const releasesUrl = 'https://github.com/stackrox/sensor-metrics-analyzer/releases'; + releaseLink.href = releasesUrl; + + fetch('/version') + .then(response => response.ok ? response.json() : null) + .then(data => { + if (!data) { + throw new Error('Failed to fetch version info'); + } + if (data.version) { + versionValue.textContent = data.version; + } + if (data.lastUpdate) { + const date = new Date(data.lastUpdate); + lastUpdateValue.textContent = Number.isNaN(date.getTime()) + ? data.lastUpdate + : date.toLocaleDateString(); + } + }) + .catch(() => { + versionValue.textContent = 'Unavailable'; + lastUpdateValue.textContent = 'Unavailable'; + }); // File input change fileInput.addEventListener('change', (e) => { From 4bea3cad9237a8e89924262ae2d13ee5909f1657 Mon Sep 17 00:00:00 2001 From: Piotr Rygielski <114479+vikin91@users.noreply.github.com> Date: Thu, 29 Jan 2026 13:13:57 +0100 Subject: [PATCH 4/7] Bump version to 0.0.2 and use intl date format --- VERSION | 2 +- web/static/index.html | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 8acdd82..4e379d2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.1 +0.0.2 diff --git a/web/static/index.html b/web/static/index.html index 8ac0b70..c1ef0b0 100644 --- a/web/static/index.html +++ b/web/static/index.html @@ -305,7 +305,11 @@

Sensor Metrics Analyzer

const date = new Date(data.lastUpdate); lastUpdateValue.textContent = Number.isNaN(date.getTime()) ? data.lastUpdate - : date.toLocaleDateString(); + : new Intl.DateTimeFormat(undefined, { + year: 'numeric', + month: 'short', + day: 'numeric' + }).format(date); } }) .catch(() => { From 5aa7cd3d58a149d7db2c1dd26e0a5194ac51a5fe Mon Sep 17 00:00:00 2001 From: Piotr Rygielski <114479+vikin91@users.noreply.github.com> Date: Thu, 29 Jan 2026 14:52:30 +0100 Subject: [PATCH 5/7] Markdown-html output --- cmd/metrics-analyzer/main.go | 8 ++- internal/reporter/markdown.go | 103 ++++---------------------------- templates/markdown.tmpl | 43 ++++++++++--- web/server/main.go | 8 ++- web/static/index.html | 54 +++++++++++++++-- web/static/vendor/marked.min.js | 69 +++++++++++++++++++++ 6 files changed, 175 insertions(+), 110 deletions(-) create mode 100644 web/static/vendor/marked.min.js diff --git a/cmd/metrics-analyzer/main.go b/cmd/metrics-analyzer/main.go index 4163f11..52d9705 100644 --- a/cmd/metrics-analyzer/main.go +++ b/cmd/metrics-analyzer/main.go @@ -119,10 +119,12 @@ func analyzeCommand() { return } case "markdown": - outputContent = reporter.GenerateMarkdown(report, *templatePath) - if outputContent == "" { - fmt.Fprintf(os.Stderr, "Warning: Markdown generation returned empty content\n") + markdown, mdErr := reporter.GenerateMarkdown(report, *templatePath) + if mdErr != nil { + fmt.Fprintf(os.Stderr, "Markdown generation failed: %v\n", mdErr) + os.Exit(1) } + outputContent = markdown default: fmt.Fprintf(os.Stderr, "Unknown format: %s\n", *format) os.Exit(1) diff --git a/internal/reporter/markdown.go b/internal/reporter/markdown.go index 0c1e7dd..39202d9 100644 --- a/internal/reporter/markdown.go +++ b/internal/reporter/markdown.go @@ -7,96 +7,21 @@ import ( "github.com/stackrox/sensor-metrics-analyzer/internal/rules" ) -// GenerateMarkdown creates a markdown report from analysis results -func GenerateMarkdown(report rules.AnalysisReport, templatePath string) string { - // Try to use template if available - if templatePath != "" { - result, err := GenerateMarkdownFromTemplate(report, templatePath) - if err == nil && result != "" { - return result - } - // Fall back to default if template fails (don't log error, just fall back) +// GenerateMarkdown creates a markdown report from analysis results. +// The markdown template is the single source of truth; if it is missing +// or fails to render, return an error. +func GenerateMarkdown(report rules.AnalysisReport, templatePath string) (string, error) { + if templatePath == "" { + return "", fmt.Errorf("markdown template path is empty") } - - // Default markdown generation (fallback) - return generateMarkdownDefault(report) -} - -// generateMarkdownDefault generates markdown without template (fallback) -func generateMarkdownDefault(report rules.AnalysisReport) string { - var result string - - // Header - result += "# Automated Metrics Analysis Report\n\n" - result += "**Cluster:** " + report.ClusterName + "\n" - result += "**ACS Version:** " + report.ACSVersion + "\n" - result += "**Load Level:** " + string(report.LoadLevel) + "\n" - result += "**Generated:** " + report.Timestamp.Format("2006-01-02 15:04:05") + "\n\n" - - // Summary - result += "## Summary\n\n" - result += "- 🔴 **RED:** " + formatInt(report.Summary.RedCount) + " metrics\n" - result += "- 🟡 **YELLOW:** " + formatInt(report.Summary.YellowCount) + " metrics\n" - result += "- 🟢 **GREEN:** " + formatInt(report.Summary.GreenCount) + " metrics\n\n" - - // Critical Issues (RED metrics) - redResults := filterByStatus(report.Results, rules.StatusRed) - if len(redResults) > 0 { - result += "## 🔴 Critical Issues\n\n" - for _, r := range redResults { - result += "### " + r.RuleName + "\n\n" - result += "**Status:** RED\n" - result += "**Message:** " + r.Message + "\n" - if len(r.Details) > 0 { - result += "**Details:**\n" - for _, detail := range r.Details { - result += "- " + detail + "\n" - } - } - if r.PotentialActionUser != "" { - result += "**Potential action:** " + r.PotentialActionUser + "\n" - } - if r.PotentialActionDeveloper != "" { - result += "**Potential action (developer):** " + r.PotentialActionDeveloper + "\n" - } - result += "\n" - } + result, err := GenerateMarkdownFromTemplate(report, templatePath) + if err != nil { + return "", err } - - // Warnings (YELLOW metrics) - yellowResults := filterByStatus(report.Results, rules.StatusYellow) - if len(yellowResults) > 0 { - result += "## 🟡 Warnings\n\n" - for _, r := range yellowResults { - result += "### " + r.RuleName + "\n\n" - result += "**Status:** YELLOW\n" - result += "**Message:** " + r.Message + "\n" - if len(r.Details) > 0 { - result += "**Details:**\n" - for _, detail := range r.Details { - result += "- " + detail + "\n" - } - } - if r.PotentialActionUser != "" { - result += "**Potential action:** " + r.PotentialActionUser + "\n" - } - if r.PotentialActionDeveloper != "" { - result += "**Potential action (developer):** " + r.PotentialActionDeveloper + "\n" - } - result += "\n" - } + if result == "" { + return "", fmt.Errorf("markdown template returned empty content") } - - // Healthy Metrics (GREEN) - greenResults := filterByStatus(report.Results, rules.StatusGreen) - if len(greenResults) > 0 { - result += "## 🟢 Healthy Metrics\n\n" - for _, r := range greenResults { - result += "- **" + r.RuleName + ":** " + r.Message + "\n" - } - } - - return result + return result, nil } func filterByStatus(results []rules.EvaluationResult, status rules.Status) []rules.EvaluationResult { @@ -115,7 +40,3 @@ func filterByStatus(results []rules.EvaluationResult, status rules.Status) []rul return filtered } -func formatInt(i int) string { - // Use fmt.Sprintf for simplicity - return fmt.Sprintf("%d", i) -} diff --git a/templates/markdown.tmpl b/templates/markdown.tmpl index a909ae2..48e57ba 100644 --- a/templates/markdown.tmpl +++ b/templates/markdown.tmpl @@ -17,13 +17,26 @@ {{ range .RedResults }} ### {{.RuleName}} -**Status:** RED -**Message:** {{.Message}} +#### Status +RED + +#### Message +{{.Message}} +{{ if gt (len .Details) 0 }} +#### Details +{{ range .Details }} +- {{ . }} +{{ end }} +{{ end }} {{ if .PotentialActionUser }} -**Potential action:** {{.PotentialActionUser}} +#### Potential action +{{.PotentialActionUser}} {{ end }} {{ if .PotentialActionDeveloper }} -**Potential action (developer):** {{.PotentialActionDeveloper}} +#### Potential action (developer) +{{.PotentialActionDeveloper}} +{{ end }} + {{ end }} {{ end }} @@ -34,13 +47,26 @@ {{ range .YellowResults }} ### {{.RuleName}} -**Status:** YELLOW -**Message:** {{.Message}} +#### Status +YELLOW + +#### Message +{{.Message}} +{{ if gt (len .Details) 0 }} +#### Details +{{ range .Details }} +- {{ . }} +{{ end }} +{{ end }} {{ if .PotentialActionUser }} -**Potential action:** {{.PotentialActionUser}} +#### Potential action +{{.PotentialActionUser}} {{ end }} {{ if .PotentialActionDeveloper }} -**Potential action (developer):** {{.PotentialActionDeveloper}} +#### Potential action (developer) +{{.PotentialActionDeveloper}} +{{ end }} + {{ end }} {{ end }} @@ -52,3 +78,4 @@ - **{{.RuleName}}:** {{.Message}} {{ end }} +{{ end }} diff --git a/web/server/main.go b/web/server/main.go index 65d6930..0a04e7a 100644 --- a/web/server/main.go +++ b/web/server/main.go @@ -201,9 +201,11 @@ func handleAnalyzeBoth(cfg *Config) http.HandlerFunc { response.Error = fmt.Sprintf("Analysis failed: %v", err) } else { response.Console = reporter.GenerateConsole(report) - response.Markdown = reporter.GenerateMarkdown(report, cfg.TemplatePath) - if response.Markdown == "" { - response.Error = "Markdown generation returned empty content" + markdown, mdErr := reporter.GenerateMarkdown(report, cfg.TemplatePath) + if mdErr != nil { + response.Error = fmt.Sprintf("Markdown generation failed: %v", mdErr) + } else { + response.Markdown = markdown } } diff --git a/web/static/index.html b/web/static/index.html index c1ef0b0..968df22 100644 --- a/web/static/index.html +++ b/web/static/index.html @@ -194,6 +194,41 @@ display: block; } + .markdown-rendered { + background: #ffffff; + color: #2c3e50; + padding: 20px; + border-radius: 4px; + border: 1px solid #ecf0f1; + } + + .markdown-rendered h1, + .markdown-rendered h2, + .markdown-rendered h3, + .markdown-rendered h4 { + margin: 16px 0 8px; + } + + .markdown-rendered p { + margin: 8px 0; + } + + .markdown-rendered code { + background: #f2f4f6; + padding: 2px 4px; + border-radius: 3px; + font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace; + font-size: 13px; + } + + .markdown-rendered pre { + background: #2c3e50; + color: #ecf0f1; + padding: 16px; + border-radius: 4px; + overflow-x: auto; + } + pre { background: #2c3e50; color: #ecf0f1; @@ -261,18 +296,20 @@

Sensor Metrics Analyzer

- - + +
-
+

             
-
-

+            
+
+

             
+