Initial commit — cynfo RMM (clean history, no secrets)
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CaddySite struct {
|
||||
@@ -12,12 +16,21 @@ type CaddySite struct {
|
||||
TLS bool `json:"tls"`
|
||||
}
|
||||
|
||||
type CaddyUpstream struct {
|
||||
Address string `json:"address"`
|
||||
NumRequests int `json:"num_requests"`
|
||||
Fails int `json:"fails"`
|
||||
Healthy bool `json:"healthy"`
|
||||
}
|
||||
|
||||
type CaddyData struct {
|
||||
Version string `json:"version"`
|
||||
Sites []CaddySite `json:"sites"`
|
||||
Version string `json:"version"`
|
||||
Sites []CaddySite `json:"sites"`
|
||||
Upstreams []CaddyUpstream `json:"upstreams,omitempty"`
|
||||
}
|
||||
|
||||
const caddyFile = "/usr/local/etc/caddy/Caddyfile"
|
||||
const caddyAdminAPI = "http://127.0.0.1:2019"
|
||||
|
||||
func CollectCaddy() *CaddyData {
|
||||
if _, err := os.Stat(caddyFile); err != nil {
|
||||
@@ -34,7 +47,7 @@ func CollectCaddy() *CaddyData {
|
||||
}
|
||||
}
|
||||
|
||||
// Parse Caddyfile
|
||||
// Parse Caddyfile fuer Sites
|
||||
content, err := os.ReadFile(caddyFile)
|
||||
if err != nil {
|
||||
return data
|
||||
@@ -51,7 +64,6 @@ func CollectCaddy() *CaddyData {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check for domain block start: "domain.com {" or "http://domain.com {"
|
||||
if depth == 0 && strings.Contains(trimmed, "{") {
|
||||
parts := strings.Fields(trimmed)
|
||||
if len(parts) >= 2 && parts[len(parts)-1] == "{" {
|
||||
@@ -63,7 +75,6 @@ func CollectCaddy() *CaddyData {
|
||||
}
|
||||
|
||||
if depth > 0 {
|
||||
// Track nested braces
|
||||
depth += strings.Count(trimmed, "{") - strings.Count(trimmed, "}")
|
||||
|
||||
if strings.HasPrefix(trimmed, "reverse_proxy") {
|
||||
@@ -85,5 +96,43 @@ func CollectCaddy() *CaddyData {
|
||||
}
|
||||
}
|
||||
|
||||
// Admin-API: Upstream-Statistiken
|
||||
data.Upstreams = collectCaddyUpstreams()
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func collectCaddyUpstreams() []CaddyUpstream {
|
||||
client := &http.Client{Timeout: 3 * time.Second}
|
||||
resp, err := client.Get(caddyAdminAPI + "/api/reverse_proxy/upstreams")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 64*1024))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Admin-API liefert: [{"address":"host:port","num_requests":N,"fails":N}]
|
||||
var raw []struct {
|
||||
Address string `json:"address"`
|
||||
NumRequests int `json:"num_requests"`
|
||||
Fails int `json:"fails"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &raw); err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
upstreams := make([]CaddyUpstream, 0, len(raw))
|
||||
for _, u := range raw {
|
||||
upstreams = append(upstreams, CaddyUpstream{
|
||||
Address: u.Address,
|
||||
NumRequests: u.NumRequests,
|
||||
Fails: u.Fails,
|
||||
Healthy: u.Fails == 0,
|
||||
})
|
||||
}
|
||||
return upstreams
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user