package mcp
import (
"cmp"
"context"
"fmt"
"math"
"slices"
"strings"
"time"
"ganigeorgiev.com/pocketbase/dbx"
"ganigeorgiev.com/pocketbase/pocketbase/core"
"ganigeorgiev.com/pocketbase/pocketbase/tools/types"
"github.com/henrygd/beszel/internal/entities/container"
"github.com/henrygd/beszel/internal/entities/system"
"github.com/henrygd/beszel/internal/entities/systemd"
mcpsdk "github.com/modelcontextprotocol/go-sdk/mcp"
)
const (
maxSamples = 400
maxListRecords = 200
)
func registerTools(app core.App, server *mcpsdk.Server) {
h := &mcphub{app: app}
mcpsdk.AddTool(server, &mcpsdk.Tool{
Name: "list_systems",
Description: "List monitored systems visible to the authenticated user with current CPU, memory, disk, bandwidth, temperature, battery, Wi-Fi signal and status. " +
"temp_c is the highest sensor (or the agent's configured primary sensor); use get_system for all sensors.",
}, h.listSystems)
mcpsdk.AddTool(server, &mcpsdk.Tool{
Name: "get_system",
Description: "Get hardware details and current usage (including temperatures, GPUs, fans, battery and Wi-Fi signal) for one system, looked up by name or id.",
}, h.getSystem)
mcpsdk.AddTool(server, &mcpsdk.Tool{
Name: "get_system_stats",
Description: "Get historical metrics for one system over a time range. " +
"Records are stored at 1m, 10m, 20m, 120m and 480m resolutions, kept for 1h, 12h, 24h, 7d and 30d respectively. " +
"The range covers hours ending at until (default now), so a past incident can be inspected with e.g. hours=2 and until set shortly after it; " +
"by default the finest resolution still holding the start of the range is used. " +
"Samples are averages over each record's interval; the summary's max CPU, memory, bandwidth and disk I/O values are the peaks within those intervals, " +
"each with the time of the record holding it (the peak falls within the interval ending at that time).",
}, h.getSystemStats)
mcpsdk.AddTool(server, &mcpsdk.Tool{
Name: "get_container_stats",
Description: "Get historical CPU, memory and network usage of one system's containers over a time range: average and peak values per container, " +
"with the time of each peak, sorted by peak CPU by default. To find which containers caused a past spike seen in get_system_stats, " +
"set until and hours to a window around it. Pass container to also get that container's time series samples. " +
"Resolutions and retention are the same as get_system_stats. Peaks within an interval are not stored for containers, so at resolutions coarser than 1m " +
"their max values are the highest interval averages and short spikes appear diluted.",
}, h.getContainerStats)
mcpsdk.AddTool(server, &mcpsdk.Tool{
Name: "list_containers",
Description: "List docker/podman containers with current status, health, CPU, memory and network usage. " +
"Omit system to search all visible systems at once; results are sorted by memory (highest first) by default, " +
"so e.g. sort=memory with limit=5 answers 'which containers use the most memory'.",
}, h.listContainers)
mcpsdk.AddTool(server, &mcpsdk.Tool{
Name: "list_alerts",
Description: "List the authenticated user's alert configurations (CPU, memory, disk, status, etc.) with state.",
}, h.listAlerts)
mcpsdk.AddTool(server, &mcpsdk.Tool{
Name: "list_alert_events",
Description: "List triggered/resolved alert history events for the authenticated user's alerts over a time range. " +
"Use this to answer questions like 'what alerts fired recently and are they resolved'.",
}, h.listAlertEvents)
mcpsdk.AddTool(server, &mcpsdk.Tool{
Name: "list_services",
Description: "List systemd services with state, CPU and memory usage. " +
"Omit system to search all visible systems at once; use state=failed to spot broken services anywhere.",
}, h.listServices)
mcpsdk.AddTool(server, &mcpsdk.Tool{
Name: "get_disk_health",
Description: "Get SMART health of physical disks (state, temperature, power-on hours) and ZFS pool status. " +
"Omit system to check all visible systems at once; use problems_only to list only failing disks and degraded pools.",
}, h.getDiskHealth)
mcpsdk.AddTool(server, &mcpsdk.Tool{
Name: "list_network_monitors",
Description: "List network endpoint monitors (ping/tcp/http/dns) with latest latency and loss. " +
"Omit system to list monitors of all visible systems; sort by latency or loss to find the worst endpoints.",
}, h.listNetworkMonitors)
}
type systemSummary struct {
Id string `json:"id"`
Name string `json:"name"`
Host string `json:"host,omitempty"`
Status string `json:"status"`
Cpu float64 `json:"cpu_pct,omitzero"`
Mem float64 `json:"memory_pct,omitzero"`
Disk float64 `json:"disk_pct,omitzero"`
Bandwidth *uint64 `json:"bandwidth_bps,omitzero"`
Temp float64 `json:"temp_c,omitzero"`
LoadAvg *[3]float64 `json:"load_avg,omitzero"`
Updates *updatesInfo `json:"updates,omitempty"`
Battery *batteryInfo `json:"battery,omitempty"`
WiFi map[string]wifiInfo `json:"wifi,omitempty"`
AgentVersion string `json:"agent_version,omitzero"`
Uptime uint64 `json:"uptime_seconds,omitzero"`
LastSeen string `json:"last_seen,omitzero"`
}
type updatesInfo struct {
Total int `json:"total"`
Security *int `json:"security,omitempty"`
}
type systemDetail struct {
systemSummary
Port string `json:"port,omitempty"`
Gpu float64 `json:"gpu_pct,omitzero"`
Current *systemCurrent `json:"current,omitempty"`
Created string `json:"created,omitzero"`
Server *serverInfo `json:"server,omitempty"`
}
type systemCurrent struct {
Temperatures map[string]float64 `json:"temperatures,omitempty"`
Gpus map[string]system.GPUData `json:"gpus,omitempty"`
Filesystems map[string]fsSummary `json:"filesystems,omitempty"`
Fans map[string]uint16 `json:"fans,omitempty"`
Swap *[2]uint64 `json:"swap_bytes,omitempty"`
}
type fsSummary struct {
Total float64 `json:"total_gb"`
Used float64 `json:"used_gb"`
ReadPs uint64 `json:"read_bps,omitzero"`
WritePs uint64 `json:"write_bps,omitzero"`
}
type batteryInfo struct {
Percent uint8 `json:"percent"`
State string `json:"state,omitempty"`
}
var batteryStateNames = map[uint8]string{
1: "empty",
2: "full",
3: "charging",
4: "discharging",
5: "idle",
}
type wifiInfo struct {
SSID string `json:"ssid,omitempty"`
Signal *float64 `json:"signal_dbm,omitempty"`
}
type serverInfo struct {
Hostname string `json:"hostname,omitempty"`
Kernel string `json:"kernel,omitempty"`
CpuModel string `json:"cpu_model,omitempty"`
Os string `json:"os,omitempty"`
Arch string `json:"arch,omitempty"`
Cores int `json:"cores,omitzero"`
Threads int `json:"threads,omitzero"`
Sockets int `json:"sockets,omitzero"`
Memory *uint64 `json:"memory_bytes,omitzero"`
Podman bool `json:"podman,omitzero"`
}
type statsSummary struct {
Count int `json:"count"`
AvgCpu float64 `json:"avg_cpu_pct"`
MaxCpu float64 `json:"max_cpu_pct"`
MaxCpuTime string `json:"max_cpu_time,omitempty"`
AvgMem float64 `json:"avg_memory_pct"`
MaxMem float64 `json:"max_memory_pct"`
MaxMemTime string `json:"max_memory_time,omitempty"`
AvgDisk float64 `json:"avg_disk_pct"`
MaxDisk float64 `json:"max_disk_pct"`
AvgBandwidth *[2]float64 `json:"avg_bandwidth_bps,omitzero"`
MaxBandwidth *[2]float64 `json:"max_bandwidth_bps,omitzero"`
MaxBandwidthTime [2]string `json:"max_bandwidth_time,omitzero"`
AvgDiskIO *[2]float64 `json:"avg_disk_io_bps,omitzero"`
MaxDiskIO *[2]float64 `json:"max_disk_io_bps,omitzero"`
MaxDiskIOTime [2]string `json:"max_disk_io_time,omitzero"`
}
type statsSample struct {
Time string `json:"time"`
Cpu float64 `json:"cpu_pct"`
Mem float64 `json:"memory_pct"`
Disk float64 `json:"disk_pct"`
NetSent uint64 `json:"net_sent_bps,omitzero"`
NetRecv uint64 `json:"net_recv_bps,omitzero"`
DiskRead uint64 `json:"disk_read_bps,omitzero"`
DiskWrite uint64 `json:"disk_write_bps,omitzero"`
}
type alertSummary struct {
Id string `json:"id"`
System string `json:"system"`
Name string `json:"name"`
Value *float64 `json:"value,omitempty"`
Min *float64 `json:"min,omitempty"`
Triggered bool `json:"triggered,omitzero"`
State any `json:"state,omitempty"`
Created string `json:"created,omitempty"`
Updated string `json:"updated,omitempty"`
}
func (h *mcphub) listVisibleSystems(auth *core.Record) ([]*core.Record, bool, error) {
params := dbx.Params{}
filter := ""
if !auth.IsSuperuser() && !shareAllSystems() {
filter = "users.id ?= {:uid}"
params["uid"] = auth.Id
}
return h.findLimited("systems", filter, "name", maxListRecords, params)
}
func newSystemSummary(record *core.Record) systemSummary {
s := systemSummary{
Id: record.Id,
Name: record.GetString("name"),
Host: record.GetString("host"),
Status: record.GetString("status"),
}
var info system.Info
if err := record.UnmarshalJSONField("info", &info); err == nil {
s.Cpu = info.Cpu
s.Mem = info.MemPct
s.Disk = info.DiskPct
s.AgentVersion = info.AgentVersion
s.Temp = info.DashboardTemp
s.Uptime = info.Uptime
if info.BandwidthBytes > 0 {
bandwidth := info.BandwidthBytes
s.Bandwidth = &bandwidth
} else if info.Bandwidth > 0 {
bandwidth := uint64(info.Bandwidth * 1024 * 1024)
s.Bandwidth = &bandwidth
}
if loadAvg := info.LoadAvg; loadAvg != [3]float64{} {
s.LoadAvg = &loadAvg
}
if len(info.PackageUpdates) > 0 {
updates := &updatesInfo{Total: int(info.PackageUpdates[0])}
if len(info.PackageUpdates) > 1 {
security := int(info.PackageUpdates[1])
updates.Security = &security
}
s.Updates = updates
}
if info.Battery != (system.Battery{}) {
s.Battery = &batteryInfo{Percent: info.Battery[0], State: batteryStateNames[info.Battery[1]]}
}
if len(info.WiFi) > 0 {
s.WiFi = make(map[string]wifiInfo, len(info.WiFi))
for iface, wifi := range info.WiFi {
s.WiFi[iface] = wifiInfo{SSID: wifi.SSID, Signal: wifi.Signal}
}
}
}
if updated := record.GetDateTime("updated"); !updated.IsZero() {
s.LastSeen = updated.Time().UTC().Format(time.RFC3339)
}
return s
}
type noParams struct{}
func (h *mcphub) listSystems(ctx context.Context, req *mcpsdk.CallToolRequest, _ noParams) (*mcpsdk.CallToolResult, systemList, error) {
auth, err := h.authRecord(ctx)
if err != nil {
return nil, systemList{}, err
}
records, hasMore, err := h.listVisibleSystems(auth)
if err != nil {
return nil, systemList{}, err
}
systems := make([]systemSummary, 0, len(records))
for _, record := range records {
systems = append(systems, newSystemSummary(record))
}
return nil, systemList{Count: len(systems), Truncated: hasMore, Systems: systems}, nil
}
type systemList struct {
Count int `json:"count"`
Truncated bool `json:"truncated,omitzero"`
Systems []systemSummary `json:"systems"`
}
type getSystemParams struct {
System string `json:"system" jsonschema:"system name or id"`
}
func (h *mcphub) getSystem(ctx context.Context, req *mcpsdk.CallToolRequest, params getSystemParams) (*mcpsdk.CallToolResult, systemDetail, error) {
auth, err := h.authRecord(ctx)
if err != nil {
return nil, systemDetail{}, err
}
record, err := h.resolveSystem(auth, params.System)
if err != nil {
return nil, systemDetail{}, err
}
detail := systemDetail{
systemSummary: newSystemSummary(record),
Port: record.GetString("port"),
Created: record.GetDateTime("created").Time().UTC().Format(time.RFC3339),
}
var info system.Info
if err := record.UnmarshalJSONField("info", &info); err == nil {
detail.Gpu = info.GpuPct
}
if latest, err := h.app.FindRecordsByFilter("system_stats", "system = {:system} && type = {:type}", "-created", 1, 0,
dbx.Params{"system": record.Id, "type": "1m"}); err == nil && len(latest) > 0 {
current, err := systemCurrentFromStats(latest[0])
if err != nil {
h.app.Logger().Warn("MCP: failed to decode system stats", "system", record.Id, "err", err)
}
detail.Current = current
}
if detailsRecord, err := h.app.FindRecordById("system_details", record.Id); err == nil {
si := &serverInfo{
Hostname: detailsRecord.GetString("hostname"),
Kernel: detailsRecord.GetString("kernel"),
CpuModel: detailsRecord.GetString("cpu"),
Arch: detailsRecord.GetString("arch"),
Cores: detailsRecord.GetInt("cores"),
Threads: detailsRecord.GetInt("threads"),
Sockets: detailsRecord.GetInt("sockets"),
Podman: detailsRecord.GetBool("podman"),
}
si.Os = detailsRecord.GetString("os_name")
if mem := detailsRecord.GetFloat("memory"); mem > 0 {
si.Memory = new(uint64)
*si.Memory = uint64(mem)
}
detail.Server = si
}
return nil, detail, nil
}
func systemCurrentFromStats(record *core.Record) (*systemCurrent, error) {
var s system.Stats
if err := record.UnmarshalJSONField("stats", &s); err != nil {
return nil, err
}
current := &systemCurrent{}
if len(s.Temperatures) > 0 {
current.Temperatures = s.Temperatures
}
if len(s.GPUData) > 0 {
current.Gpus = s.GPUData
}
if len(s.Fans) > 0 {
current.Fans = s.Fans
}
if s.SwapBytes != [2]uint64{} {
current.Swap = &s.SwapBytes
}
if len(s.ExtraFs) > 0 {
filesystems := make(map[string]fsSummary, len(s.ExtraFs))
for name, fs := range s.ExtraFs {
if fs == nil {
continue
}
summary := fsSummary{Total: fs.DiskTotal, Used: fs.DiskUsed}
summary.ReadPs, summary.WritePs = fs.DiskReadBytes, fs.DiskWriteBytes
if summary.ReadPs == 0 && (fs.DiskReadPs > 0 || fs.DiskWritePs > 0) {
summary.ReadPs = uint64(fs.DiskReadPs * 1024 * 1024)
summary.WritePs = uint64(fs.DiskWritePs * 1024 * 1024)
}
filesystems[name] = summary
}
current.Filesystems = filesystems
}
if current.Temperatures == nil && current.Gpus == nil && current.Filesystems == nil &&
current.Fans == nil && current.Swap == nil {
return nil, nil
}
return current, nil
}
type historyParams struct {
Period string `json:"period,omitzero" jsonschema:"record resolution: one of 1m, 10m, 20m, 120m or 480m (default: the finest resolution that still holds the start of the range)"`
Hours float64 `json:"hours,omitzero" jsonschema:"length of the range in hours, ending at until (1-720, default 24)"`
Until string `json:"until,omitzero" jsonschema:"end of the range as an RFC3339 time, e.g. 2026-01-02T15:04:05Z (default: now)"`
}
type getSystemStatsParams struct {
System string `json:"system" jsonschema:"system name or id"`
historyParams
}
var statsPeriods = []struct {
period string
retention time.Duration
label string
}{
{"1m", time.Hour, "1 hour"},
{"10m", 12 * time.Hour, "12 hours"},
{"20m", 24 * time.Hour, "24 hours"},
{"120m", 7 * 24 * time.Hour, "7 days"},
{"480m", 30 * 24 * time.Hour, "30 days"},
}
const maxStatsHours = 720
type systemStats struct {
System string `json:"system"`
Period string `json:"period"`
From string `json:"from"`
To string `json:"to"`
Note string `json:"note,omitempty"`
Summary statsSummary `json:"summary"`
Samples []statsSample `json:"samples"`
}
func resolvePeriod(period string, reach time.Duration) (string, time.Duration, string, error) {
for _, p := range statsPeriods {
if period == p.period || (period == "" && reach <= p.retention) {
return p.period, p.retention, p.label, nil
}
}
if period == "" {
last := statsPeriods[len(statsPeriods)-1]
return last.period, last.retention, last.label, nil
}
return "", 0, "", fmt.Errorf("invalid period %q: must be one of 1m, 10m, 20m, 120m, 480m", period)
}
type statsRange struct {
period string
from time.Time
to time.Time
note string
}
func (p historyParams) resolve(now time.Time) (statsRange, error) {
hours := p.Hours
if hours <= 0 {
hours = 24
}
hours = min(hours, maxStatsHours)
to := now
if p.Until != "" {
until, err := time.Parse(time.RFC3339, p.Until)
if err != nil {
return statsRange{}, fmt.Errorf("invalid until %q: must be an RFC3339 time such as 2026-01-02T15:04:05Z", p.Until)
}
if until.Before(now) {
to = until.UTC()
}
}
from := to.Add(-time.Duration(hours * float64(time.Hour)))
period, retention, retentionLabel, err := resolvePeriod(p.Period, now.Sub(from))
if err != nil {
return statsRange{}, err
}
r := statsRange{period: period, from: from, to: to}
if oldest := now.Add(-retention); from.Before(oldest) {
r.from = oldest
switch {
case !oldest.Before(to):
r.from = to
r.note = fmt.Sprintf("%s records are only kept for %s, so this range holds no data", period, retentionLabel)
case p.Period != "":
r.note = fmt.Sprintf("%s records are only kept for %s, so the range was shortened; omit period to pick a resolution covering %g hours",
period, retentionLabel, hours)
default:
r.note = fmt.Sprintf("records are only kept for %s, so the range was shortened", retentionLabel)
}
}
return r, nil
}
func joinNotes(notes ...string) string {
return strings.Join(slices.DeleteFunc(notes, func(note string) bool { return note == "" }), "; ")
}
func (h *mcphub) getSystemStats(ctx context.Context, req *mcpsdk.CallToolRequest, params getSystemStatsParams) (*mcpsdk.CallToolResult, systemStats, error) {
auth, err := h.authRecord(ctx)
if err != nil {
return nil, systemStats{}, err
}
rng, err := params.resolve(time.Now().UTC())
if err != nil {
return nil, systemStats{}, err
}
record, err := h.resolveSystem(auth, params.System)
if err != nil {
return nil, systemStats{}, err
}
result := systemStats{
System: record.GetString("name"),
Period: rng.period,
From: rng.from.Format(time.RFC3339),
To: rng.to.Format(time.RFC3339),
Note: rng.note,
Samples: []statsSample{},
}
records, err := h.findStatsRecords("system_stats", record.Id, rng)
if err != nil {
return nil, systemStats{}, err
}
if len(records) == 0 {
result.Note = joinNotes(rng.note, fmt.Sprintf("no %s records in this range (the system may be new, down or paused)", rng.period))
return nil, result, nil
}
decoded := make([]decodedStat, 0, len(records))
var decodeErr error
for _, statsRecord := range records {
var s system.Stats
if err := statsRecord.UnmarshalJSONField("stats", &s); err != nil {
decodeErr = err
continue
}
decoded = append(decoded, decodedStat{
time: statsRecord.GetDateTime("created").Time().UTC().Format(time.RFC3339),
stats: s,
})
}
if skipped := len(records) - len(decoded); skipped > 0 {
h.app.Logger().Warn("MCP: failed to decode system stats", "system", record.Id, "skipped", skipped, "err", decodeErr)
result.Note = joinNotes(result.Note, fmt.Sprintf("%d of %d records could not be decoded and were skipped", skipped, len(records)))
}
stride := 1
if len(decoded) > maxSamples {
stride = (len(decoded) + maxSamples - 1) / maxSamples
}
samples := make([]statsSample, 0, len(decoded)/stride)
for i, item := range decoded {
if i%stride != 0 {
continue
}
samples = append(samples, newStatsSample(item.time, item.stats))
}
result.Samples = samples
result.Summary = summarizeStats(decoded)
return nil, result, nil
}
func (h *mcphub) findStatsRecords(collection string, systemId string, rng statsRange) ([]*core.Record, error) {
return h.app.FindRecordsByFilter(collection,
"system = {:system} && type = {:period} && created >= {:from} && created <= {:to}", "created", 0, 0,
dbx.Params{
"system": systemId,
"period": rng.period,
"from": rng.from.Format(types.DefaultDateLayout),
"to": rng.to.Format(types.DefaultDateLayout),
})
}
type decodedStat struct {
time string
stats system.Stats
}
func newStatsSample(recordTime string, s system.Stats) statsSample {
sample := statsSample{
Time: recordTime,
Cpu: s.Cpu, Mem: s.MemPct, Disk: s.DiskPct,
}
switch {
case s.Bandwidth[0] > 0 || s.Bandwidth[1] > 0:
sample.NetSent, sample.NetRecv = s.Bandwidth[0], s.Bandwidth[1]
case s.NetworkSent > 0 || s.NetworkRecv > 0:
sample.NetSent = uint64(s.NetworkSent * 1024 * 1024)
sample.NetRecv = uint64(s.NetworkRecv * 1024 * 1024)
}
switch {
case s.DiskIO[0] > 0 || s.DiskIO[1] > 0:
sample.DiskRead, sample.DiskWrite = s.DiskIO[0], s.DiskIO[1]
case s.DiskReadPs > 0 || s.DiskWritePs > 0:
sample.DiskRead = uint64(s.DiskReadPs * 1024 * 1024)
sample.DiskWrite = uint64(s.DiskWritePs * 1024 * 1024)
}
return sample
}
func peakMemPct(s system.Stats) float64 {
const gb = 1024 * 1024 * 1024
total, maxUsed := float64(s.MemBytes[0]), float64(s.MaxMemBytes)
if total == 0 {
total = s.Mem * gb
}
if maxUsed == 0 {
maxUsed = s.MaxMem * gb
}
if total == 0 || maxUsed == 0 {
return 0
}
return math.Round(maxUsed/total*100*100) / 100
}
func summarizeStats(decoded []decodedStat) statsSummary {
summary := statsSummary{Count: len(decoded)}
if len(decoded) == 0 {
return summary
}
var avgBandwidth, maxBandwidth, avgDiskIO, maxDiskIO [2]float64
count := float64(len(decoded))
for _, item := range decoded {
s := item.stats
summary.AvgCpu += s.Cpu
if peak := max(s.Cpu, s.MaxCpu); summary.MaxCpuTime == "" || peak > summary.MaxCpu {
summary.MaxCpu, summary.MaxCpuTime = peak, item.time
}
summary.AvgMem += s.MemPct
if peak := max(s.MemPct, peakMemPct(s)); summary.MaxMemTime == "" || peak > summary.MaxMem {
summary.MaxMem, summary.MaxMemTime = peak, item.time
}
summary.AvgDisk += s.DiskPct
summary.MaxDisk = max(summary.MaxDisk, s.DiskPct)
sample := newStatsSample("", s)
avgBandwidth[0] += float64(sample.NetSent)
avgBandwidth[1] += float64(sample.NetRecv)
avgDiskIO[0] += float64(sample.DiskRead)
avgDiskIO[1] += float64(sample.DiskWrite)
peakBandwidth := [2]float64{
max(float64(sample.NetSent), float64(s.MaxBandwidth[0]), s.MaxNetworkSent*1024*1024),
max(float64(sample.NetRecv), float64(s.MaxBandwidth[1]), s.MaxNetworkRecv*1024*1024),
}
peakDiskIO := [2]float64{
max(float64(sample.DiskRead), float64(s.MaxDiskIO[0])),
max(float64(sample.DiskWrite), float64(s.MaxDiskIO[1])),
}
for i := range 2 {
if peakBandwidth[i] > maxBandwidth[i] {
maxBandwidth[i], summary.MaxBandwidthTime[i] = peakBandwidth[i], item.time
}
if peakDiskIO[i] > maxDiskIO[i] {
maxDiskIO[i], summary.MaxDiskIOTime[i] = peakDiskIO[i], item.time
}
}
}
f := func(v [2]float64) *[2]float64 { return &v }
summary.AvgCpu /= count
summary.AvgMem /= count
summary.AvgDisk /= count
summary.AvgBandwidth = f([2]float64{avgBandwidth[0] / count, avgBandwidth[1] / count})
summary.MaxBandwidth = f(maxBandwidth)
summary.AvgDiskIO = f([2]float64{avgDiskIO[0] / count, avgDiskIO[1] / count})
summary.MaxDiskIO = f(maxDiskIO)
return summary
}
type containerSummary struct {
Name string `json:"name"`
System string `json:"system,omitempty"`
Image string `json:"image,omitempty"`
Status string `json:"status,omitempty"`
Health string `json:"health,omitempty"`
Cpu float64 `json:"cpu_pct,omitzero"`
Mem float64 `json:"mem_mb,omitzero"`
Net uint64 `json:"net_bps,omitzero"`
Ports string `json:"ports,omitempty"`
UpdateAvailable bool `json:"update_available,omitzero"`
}
type containerList struct {
System string `json:"system,omitempty"`
Count int `json:"count"`
Truncated bool `json:"truncated,omitzero"`
Containers []containerSummary `json:"containers"`
}
type listContainersParams struct {
scopeParams
Sort string `json:"sort,omitzero" jsonschema:"one of memory, cpu, net or name (default memory, highest first)"`
Name string `json:"name,omitzero" jsonschema:"only containers whose name contains this text"`
Health string `json:"health,omitzero" jsonschema:"only containers with this health check state: none, starting, healthy or unhealthy"`
}
var containerSorts = sortOptions{
{"memory", "-memory,name"},
{"cpu", "-cpu,name"},
{"net", "-net,name"},
{"name", "name"},
}
var containerHealthNames = map[uint8]string{
container.DockerHealthStarting: "starting",
container.DockerHealthHealthy: "healthy",
container.DockerHealthUnhealthy: "unhealthy",
}
func (h *mcphub) listContainers(ctx context.Context, req *mcpsdk.CallToolRequest, params listContainersParams) (*mcpsdk.CallToolResult, containerList, error) {
auth, err := h.authRecord(ctx)
if err != nil {
return nil, containerList{}, err
}
sort, err := containerSorts.resolve(params.Sort, "memory")
if err != nil {
return nil, containerList{}, err
}
scope, err := h.systemScope(auth, params.System)
if err != nil {
return nil, containerList{}, err
}
var conditions []string
if params.Name != "" {
conditions = append(conditions, "name ~ {:name}")
scope.params["name"] = params.Name
}
if params.Health != "" {
health, ok := container.DockerHealthStrings[params.Health]
if !ok {
return nil, containerList{}, fmt.Errorf("invalid health %q: must be one of none, starting, healthy, unhealthy", params.Health)
}
conditions = append(conditions, "health = {:health}")
scope.params["health"] = health
}
records, hasMore, err := h.findLimited("containers", scope.and(conditions...), sort, params.limit(), scope.params)
if err != nil {
return nil, containerList{}, err
}
names := scope.systemNames(h, records)
result := containerList{System: scope.system, Count: len(records), Truncated: hasMore,
Containers: make([]containerSummary, 0, len(records))}
for _, record := range records {
result.Containers = append(result.Containers, containerSummary{
Name: record.GetString("name"),
System: names[record.GetString("system")],
Image: record.GetString("image"),
Status: record.GetString("status"),
Health: containerHealthNames[uint8(record.GetInt("health"))],
Cpu: record.GetFloat("cpu"),
Mem: record.GetFloat("memory"),
Net: uint64(record.GetFloat("net")),
Ports: record.GetString("ports"),
UpdateAvailable: record.GetBool("updatable"),
})
}
return nil, result, nil
}
func (h *mcphub) listAlerts(ctx context.Context, req *mcpsdk.CallToolRequest, _ noParams) (*mcpsdk.CallToolResult, alertList, error) {
auth, err := h.authRecord(ctx)
if err != nil {
return nil, alertList{}, err
}
params := dbx.Params{}
filter := ""
if !auth.IsSuperuser() {
filter = "user = {:uid}"
params["uid"] = auth.Id
}
records, hasMore, err := h.findLimited("alerts", filter, "-updated", maxListRecords, params)
if err != nil {
return nil, alertList{}, err
}
result := alertList{Count: len(records), Truncated: hasMore, Alerts: make([]alertSummary, 0, len(records))}
if len(records) == 0 {
return nil, result, nil
}
result.Systems = h.resolveSystemNames(records)
for _, record := range records {
alert := alertSummary{
Id: record.Id,
System: record.GetString("system"),
Name: record.GetString("name"),
Triggered: record.GetBool("triggered"),
Created: record.GetDateTime("created").Time().UTC().Format(time.RFC3339),
Updated: record.GetDateTime("updated").Time().UTC().Format(time.RFC3339),
}
var state any
if err := record.UnmarshalJSONField("state", &state); err == nil {
alert.State = state
}
if value := record.GetFloat("value"); value != 0 {
alert.Value = &value
}
if min := record.GetFloat("min"); min != 0 {
alert.Min = &min
}
result.Alerts = append(result.Alerts, alert)
}
return nil, result, nil
}
type alertList struct {
Count int `json:"count"`
Truncated bool `json:"truncated,omitzero"`
Systems map[string]string `json:"systems,omitempty"`
Alerts []alertSummary `json:"alerts"`
}
func (h *mcphub) resolveSystemNames(records []*core.Record) map[string]string {
ids := make([]string, 0, len(records))
for _, record := range records {
if id := record.GetString("system"); id != "" && !slices.Contains(ids, id) {
ids = append(ids, id)
}
}
if len(ids) == 0 {
return nil
}
systemRecords, err := h.app.FindRecordsByIds("systems", ids)
if err != nil {
return nil
}
names := make(map[string]string, len(systemRecords))
for _, systemRecord := range systemRecords {
names[systemRecord.Id] = systemRecord.GetString("name")
}
return names
}
type scopeParams struct {
System string `json:"system,omitzero" jsonschema:"system name or id (default: all systems visible to the user)"`
Limit int `json:"limit,omitzero" jsonschema:"maximum number of results (1-200, default 200)"`
}
func (p scopeParams) limit() int {
if p.Limit <= 0 || p.Limit > maxListRecords {
return maxListRecords
}
return p.Limit
}
type recordScope struct {
filter string
params dbx.Params
system string
}
func (h *mcphub) systemScope(auth *core.Record, system string) (recordScope, error) {
if system != "" {
record, err := h.resolveSystem(auth, system)
if err != nil {
return recordScope{}, err
}
return recordScope{
filter: "system = {:system}",
params: dbx.Params{"system": record.Id},
system: record.GetString("name"),
}, nil
}
scope := recordScope{params: dbx.Params{}}
if !auth.IsSuperuser() && !shareAllSystems() {
scope.filter = "system.users.id ?= {:uid}"
scope.params["uid"] = auth.Id
}
return scope, nil
}
func (s recordScope) and(conditions ...string) string {
if s.filter != "" {
conditions = append([]string{s.filter}, conditions...)
}
return strings.Join(conditions, " && ")
}
func (s recordScope) systemNames(h *mcphub, records []*core.Record) map[string]string {
if s.system != "" {
return nil
}
return h.resolveSystemNames(records)
}
func (h *mcphub) findLimited(collection string, filter string, sort string, limit int, params dbx.Params) ([]*core.Record, bool, error) {
records, err := h.app.FindRecordsByFilter(collection, filter, sort, limit+1, 0, params)
if err != nil {
return nil, false, err
}
if len(records) > limit {
return records[:limit], true, nil
}
return records, false, nil
}
type sortOptions []struct {
name string
expr string
}
func (o sortOptions) resolve(name string, fallback string) (string, error) {
if name == "" {
name = fallback
}
names := make([]string, 0, len(o))
for _, option := range o {
if option.name == name {
return option.expr, nil
}
names = append(names, option.name)
}
return "", fmt.Errorf("invalid sort %q: must be one of %s", name, strings.Join(names, ", "))
}
type alertEventParams struct {
Hours float64 `json:"hours,omitzero" jsonschema:"how many hours to look back (1-720, default 168 = 7 days)"`
}
func (h *mcphub) listAlertEvents(ctx context.Context, req *mcpsdk.CallToolRequest, params alertEventParams) (*mcpsdk.CallToolResult, alertEventList, error) {
auth, err := h.authRecord(ctx)
if err != nil {
return nil, alertEventList{}, err
}
hours := params.Hours
if hours <= 0 {
hours = 168
}
if hours > 720 {
hours = 720
}
now := time.Now().UTC()
queryParams := dbx.Params{
"from": now.Add(-time.Duration(hours * float64(time.Hour))).Format(types.DefaultDateLayout),
}
filter := "created >= {:from}"
if !auth.IsSuperuser() {
filter = "user = {:uid} && created >= {:from}"
queryParams["uid"] = auth.Id
}
records, hasMore, err := h.findLimited("alerts_history", filter, "-created", maxListRecords, queryParams)
if err != nil {
return nil, alertEventList{}, err
}
result := alertEventList{Count: len(records), Truncated: hasMore, Events: make([]alertEvent, 0, len(records))}
if len(records) == 0 {
return nil, result, nil
}
result.Systems = h.resolveSystemNames(records)
for _, record := range records {
event := alertEvent{
Id: record.Id,
System: record.GetString("system"),
Name: record.GetString("name"),
MonitorName: record.GetString("monitor_name"),
TriggeredAt: record.GetDateTime("created").Time().UTC().Format(time.RFC3339),
}
if value := record.GetFloat("value"); value != 0 {
event.Value = &value
}
if resolved := record.GetDateTime("resolved"); !resolved.IsZero() {
event.ResolvedAt = resolved.Time().UTC().Format(time.RFC3339)
}
result.Events = append(result.Events, event)
}
return nil, result, nil
}
type alertEvent struct {
Id string `json:"id"`
System string `json:"system"`
Name string `json:"name"`
MonitorName string `json:"monitor_name,omitempty"`
Value *float64 `json:"value,omitempty"`
TriggeredAt string `json:"triggered_at"`
ResolvedAt string `json:"resolved_at,omitempty"`
}
type alertEventList struct {
Count int `json:"count"`
Truncated bool `json:"truncated,omitzero"`
Systems map[string]string `json:"systems,omitempty"`
Events []alertEvent `json:"events"`
}
var serviceStateNames = map[uint]string{
uint(systemd.StatusActive): "active",
uint(systemd.StatusInactive): "inactive",
uint(systemd.StatusFailed): "failed",
uint(systemd.StatusActivating): "activating",
uint(systemd.StatusDeactivating): "deactivating",
uint(systemd.StatusReloading): "reloading",
}
var serviceSubStateNames = map[uint]string{
uint(systemd.SubStateDead): "dead",
uint(systemd.SubStateRunning): "running",
uint(systemd.SubStateExited): "exited",
uint(systemd.SubStateFailed): "failed",
uint(systemd.SubStateUnknown): "unknown",
}
type listServicesParams struct {
scopeParams
Sort string `json:"sort,omitzero" jsonschema:"one of name, cpu or memory (default name; cpu and memory sort highest first)"`
Name string `json:"name,omitzero" jsonschema:"only services whose name contains this text"`
State string `json:"state,omitzero" jsonschema:"only services in this state: active, inactive, failed, activating, deactivating or reloading"`
}
var serviceSorts = sortOptions{
{"name", "name"},
{"cpu", "-cpu,name"},
{"memory", "-memory,name"},
}
func (h *mcphub) listServices(ctx context.Context, req *mcpsdk.CallToolRequest, params listServicesParams) (*mcpsdk.CallToolResult, serviceList, error) {
auth, err := h.authRecord(ctx)
if err != nil {
return nil, serviceList{}, err
}
sort, err := serviceSorts.resolve(params.Sort, "name")
if err != nil {
return nil, serviceList{}, err
}
scope, err := h.systemScope(auth, params.System)
if err != nil {
return nil, serviceList{}, err
}
var conditions []string
if params.Name != "" {
conditions = append(conditions, "name ~ {:name}")
scope.params["name"] = params.Name
}
if params.State != "" {
state, ok := serviceStateValue(params.State)
if !ok {
return nil, serviceList{}, fmt.Errorf("invalid state %q: must be one of active, inactive, failed, activating, deactivating, reloading", params.State)
}
conditions = append(conditions, "state = {:state}")
scope.params["state"] = state
}
records, hasMore, err := h.findLimited("systemd_services", scope.and(conditions...), sort, params.limit(), scope.params)
if err != nil {
return nil, serviceList{}, err
}
names := scope.systemNames(h, records)
result := serviceList{System: scope.system, Count: len(records), Truncated: hasMore,
Services: make([]serviceSummary, 0, len(records))}
for _, serviceRecord := range records {
service := serviceSummary{
Name: serviceRecord.GetString("name"),
System: names[serviceRecord.GetString("system")],
State: serviceStateNames[uint(serviceRecord.GetInt("state"))],
Sub: serviceSubStateNames[uint(serviceRecord.GetInt("sub"))],
Cpu: serviceRecord.GetFloat("cpu"),
}
if peak := serviceRecord.GetFloat("cpuPeak"); peak > 0 {
service.CpuPeak = peak
}
if mem := serviceRecord.GetFloat("memory"); mem > 0 {
service.Mem = new(uint64)
*service.Mem = uint64(mem)
}
if memPeak := serviceRecord.GetFloat("memPeak"); memPeak > 0 {
service.MemPeak = new(uint64)
*service.MemPeak = uint64(memPeak)
}
if updatedMs := serviceRecord.GetInt64("updated"); updatedMs > 0 {
service.Updated = time.UnixMilli(updatedMs).UTC().Format(time.RFC3339)
}
result.Services = append(result.Services, service)
}
return nil, result, nil
}
func serviceStateValue(name string) (uint, bool) {
for value, stateName := range serviceStateNames {
if stateName == name {
return value, true
}
}
return 0, false
}
type serviceSummary struct {
Name string `json:"name"`
System string `json:"system,omitempty"`
State string `json:"state"`
Sub string `json:"sub,omitempty"`
Cpu float64 `json:"cpu_pct,omitzero"`
CpuPeak float64 `json:"cpu_peak_pct,omitzero"`
Mem *uint64 `json:"mem_bytes,omitzero"`
MemPeak *uint64 `json:"mem_peak_bytes,omitzero"`
Updated string `json:"updated,omitempty"`
}
type serviceList struct {
System string `json:"system,omitempty"`
Count int `json:"count"`
Truncated bool `json:"truncated,omitzero"`
Services []serviceSummary `json:"services"`
}
type diskHealthParams struct {
scopeParams
ProblemsOnly bool `json:"problems_only,omitzero" jsonschema:"only SMART devices in WARNING or FAILED state and ZFS pools that are not ONLINE"`
}
func (h *mcphub) getDiskHealth(ctx context.Context, req *mcpsdk.CallToolRequest, params diskHealthParams) (*mcpsdk.CallToolResult, diskHealth, error) {
auth, err := h.authRecord(ctx)
if err != nil {
return nil, diskHealth{}, err
}
scope, err := h.systemScope(auth, params.System)
if err != nil {
return nil, diskHealth{}, err
}
result := diskHealth{System: scope.system}
var smartConditions []string
if params.ProblemsOnly {
smartConditions = append(smartConditions, `(state = "WARNING" || state = "FAILED")`)
}
smartRecords, smartMore, err := h.findLimited("smart_devices", scope.and(smartConditions...), "name", params.limit(), scope.params)
if err != nil {
return nil, diskHealth{}, err
}
var poolConditions []string
if params.ProblemsOnly {
poolConditions = append(poolConditions, `health != "ONLINE" && health != ""`)
}
poolRecords, poolsMore, err := h.findLimited("zfs_pools", scope.and(poolConditions...), "name", params.limit(), scope.params)
if err != nil {
return nil, diskHealth{}, err
}
result.Truncated = smartMore || poolsMore
names := scope.systemNames(h, append(slices.Clone(smartRecords), poolRecords...))
result.Smart = make([]smartDevice, 0, len(smartRecords))
for _, smartRecord := range smartRecords {
device := smartDevice{
Name: smartRecord.GetString("name"),
System: names[smartRecord.GetString("system")],
Model: smartRecord.GetString("model"),
State: smartRecord.GetString("state"),
Type: smartRecord.GetString("type"),
Serial: smartRecord.GetString("serial"),
Firmware: smartRecord.GetString("firmware"),
}
if capacity := smartRecord.GetFloat("capacity"); capacity > 0 {
device.Capacity = new(uint64)
*device.Capacity = uint64(capacity)
}
if temp := smartRecord.GetFloat("temp"); temp > 0 {
device.TempC = new(float64)
*device.TempC = temp
}
if hours := smartRecord.GetFloat("hours"); hours > 0 {
device.PowerOnHours = new(uint64)
*device.PowerOnHours = uint64(hours)
}
if cycles := smartRecord.GetFloat("cycles"); cycles > 0 {
device.PowerCycles = new(uint64)
*device.PowerCycles = uint64(cycles)
}
result.Smart = append(result.Smart, device)
}
result.ZfsPools = make([]zfsPool, 0, len(poolRecords))
for _, poolRecord := range poolRecords {
pool := zfsPool{
Name: poolRecord.GetString("name"),
System: names[poolRecord.GetString("system")],
DisplayName: poolRecord.GetString("display_name"),
Health: poolRecord.GetString("health"),
Updated: poolRecord.GetDateTime("updated").Time().UTC().Format(time.RFC3339),
}
for field, target := range map[string]**uint64{
"size": &pool.Size, "alloc": &pool.Alloc, "free": &pool.Free,
} {
if value := poolRecord.GetFloat(field); value > 0 {
*target = new(uint64)
**target = uint64(value)
}
}
result.ZfsPools = append(result.ZfsPools, pool)
}
return nil, result, nil
}
type smartDevice struct {
Name string `json:"name"`
System string `json:"system,omitempty"`
Model string `json:"model,omitempty"`
State string `json:"state,omitempty"`
Type string `json:"type,omitempty"`
Serial string `json:"serial,omitempty"`
Firmware string `json:"firmware,omitempty"`
Capacity *uint64 `json:"capacity_bytes,omitzero"`
TempC *float64 `json:"temp_c,omitzero"`
PowerOnHours *uint64 `json:"power_on_hours,omitzero"`
PowerCycles *uint64 `json:"power_cycles,omitzero"`
}
type zfsPool struct {
Name string `json:"name"`
System string `json:"system,omitempty"`
DisplayName string `json:"display_name,omitempty"`
Health string `json:"health,omitempty"`
Size *uint64 `json:"size_bytes,omitzero"`
Alloc *uint64 `json:"alloc_bytes,omitzero"`
Free *uint64 `json:"free_bytes,omitzero"`
Updated string `json:"updated,omitempty"`
}
type diskHealth struct {
System string `json:"system,omitempty"`
Truncated bool `json:"truncated,omitzero"`
Smart []smartDevice `json:"smart_devices"`
ZfsPools []zfsPool `json:"zfs_pools"`
}
type listNetworkMonitorsParams struct {
scopeParams
Sort string `json:"sort,omitzero" jsonschema:"one of target, latency or loss (default target; latency and loss sort highest first)"`
}
var networkMonitorSorts = sortOptions{
{"target", "target"},
{"latency", "-res,target"},
{"loss", "-loss1h,target"},
}
func (h *mcphub) listNetworkMonitors(ctx context.Context, req *mcpsdk.CallToolRequest, params listNetworkMonitorsParams) (*mcpsdk.CallToolResult, networkMonitorList, error) {
auth, err := h.authRecord(ctx)
if err != nil {
return nil, networkMonitorList{}, err
}
sort, err := networkMonitorSorts.resolve(params.Sort, "target")
if err != nil {
return nil, networkMonitorList{}, err
}
scope, err := h.systemScope(auth, params.System)
if err != nil {
return nil, networkMonitorList{}, err
}
records, hasMore, err := h.findLimited("network_monitors", scope.and(), sort, params.limit(), scope.params)
if err != nil {
return nil, networkMonitorList{}, err
}
names := scope.systemNames(h, records)
result := networkMonitorList{System: scope.system, Count: len(records), Truncated: hasMore,
Monitors: make([]networkMonitor, 0, len(records))}
for _, monitorRecord := range records {
monitor := networkMonitor{
Target: monitorRecord.GetString("target"),
System: names[monitorRecord.GetString("system")],
Protocol: monitorRecord.GetString("protocol"),
Port: uint16(monitorRecord.GetInt("port")),
Enabled: monitorRecord.GetBool("enabled"),
}
monitor.Latency = microsecondsToMs(monitorRecord.GetFloat("res"))
monitor.LatencyAvg1h = microsecondsToMs(monitorRecord.GetFloat("resAvg1h"))
monitor.LatencyMax1h = microsecondsToMs(monitorRecord.GetFloat("resMax1h"))
if loss := monitorRecord.GetFloat("loss1h"); loss > 0 {
monitor.Loss1h = &loss
}
if updated := monitorRecord.GetDateTime("updated"); !updated.IsZero() {
monitor.Updated = updated.Time().UTC().Format(time.RFC3339)
}
result.Monitors = append(result.Monitors, monitor)
}
return nil, result, nil
}
func microsecondsToMs(us float64) *float64 {
if us <= 0 {
return nil
}
ms := us / 1000
return &ms
}
type networkMonitor struct {
Target string `json:"target"`
System string `json:"system,omitempty"`
Protocol string `json:"protocol,omitempty"`
Port uint16 `json:"port,omitzero"`
Enabled bool `json:"enabled"`
Latency *float64 `json:"latency_ms,omitzero"`
LatencyAvg1h *float64 `json:"latency_avg_1h_ms,omitzero"`
LatencyMax1h *float64 `json:"latency_max_1h_ms,omitzero"`
Loss1h *float64 `json:"loss_pct_1h,omitzero"`
Updated string `json:"updated,omitempty"`
}
type networkMonitorList struct {
System string `json:"system,omitempty"`
Count int `json:"count"`
Truncated bool `json:"truncated,omitzero"`
Monitors []networkMonitor `json:"monitors"`
}
type getContainerStatsParams struct {
System string `json:"system" jsonschema:"system name or id"`
Container string `json:"container,omitzero" jsonschema:"only containers whose name contains this text; their time series samples are included too"`
Sort string `json:"sort,omitzero" jsonschema:"one of cpu, memory or net (by peak value, highest first; default cpu)"`
Limit int `json:"limit,omitzero" jsonschema:"maximum number of containers (1-200, default 200)"`
historyParams
}
var containerHistorySorts = sortOptions{
{"cpu", "cpu"},
{"memory", "memory"},
{"net", "net"},
}
type containerStatsResult struct {
System string `json:"system"`
Period string `json:"period"`
From string `json:"from"`
To string `json:"to"`
Note string `json:"note,omitempty"`
Count int `json:"count"`
Truncated bool `json:"truncated,omitzero"`
Containers []containerHistory `json:"containers"`
}
type containerHistory struct {
Name string `json:"name"`
SampleCount int `json:"sample_count"`
AvgCpu float64 `json:"avg_cpu_pct"`
MaxCpu float64 `json:"max_cpu_pct"`
MaxCpuTime string `json:"max_cpu_time"`
AvgMem float64 `json:"avg_mem_mb"`
MaxMem float64 `json:"max_mem_mb"`
MaxMemTime string `json:"max_mem_time"`
AvgNet float64 `json:"avg_net_bps,omitzero"`
MaxNet uint64 `json:"max_net_bps,omitzero"`
MaxNetTime string `json:"max_net_time,omitempty"`
Samples []containerSample `json:"samples,omitempty"`
}
type containerSample struct {
Time string `json:"time"`
Cpu float64 `json:"cpu_pct"`
Mem float64 `json:"mem_mb"`
Net uint64 `json:"net_bps,omitzero"`
}
func containerNetBytes(c *container.Stats) uint64 {
if net := c.Bandwidth[0] + c.Bandwidth[1]; net > 0 {
return net
}
return uint64((c.NetworkSent + c.NetworkRecv) * 1024 * 1024)
}
func (c *containerHistory) add(sample containerSample, keepSamples bool) {
c.SampleCount++
c.AvgCpu += sample.Cpu
c.AvgMem += sample.Mem
c.AvgNet += float64(sample.Net)
if c.MaxCpuTime == "" || sample.Cpu > c.MaxCpu {
c.MaxCpu, c.MaxCpuTime = sample.Cpu, sample.Time
}
if c.MaxMemTime == "" || sample.Mem > c.MaxMem {
c.MaxMem, c.MaxMemTime = sample.Mem, sample.Time
}
if sample.Net > c.MaxNet {
c.MaxNet, c.MaxNetTime = sample.Net, sample.Time
}
if keepSamples {
c.Samples = append(c.Samples, sample)
}
}
func (c *containerHistory) finish() {
count := float64(c.SampleCount)
c.AvgCpu /= count
c.AvgMem /= count
c.AvgNet /= count
if len(c.Samples) > maxSamples {
stride := (len(c.Samples) + maxSamples - 1) / maxSamples
samples := make([]containerSample, 0, maxSamples)
for i := 0; i < len(c.Samples); i += stride {
samples = append(samples, c.Samples[i])
}
c.Samples = samples
}
}
func (h *mcphub) getContainerStats(ctx context.Context, req *mcpsdk.CallToolRequest, params getContainerStatsParams) (*mcpsdk.CallToolResult, containerStatsResult, error) {
auth, err := h.authRecord(ctx)
if err != nil {
return nil, containerStatsResult{}, err
}
sortBy, err := containerHistorySorts.resolve(params.Sort, "cpu")
if err != nil {
return nil, containerStatsResult{}, err
}
rng, err := params.resolve(time.Now().UTC())
if err != nil {
return nil, containerStatsResult{}, err
}
record, err := h.resolveSystem(auth, params.System)
if err != nil {
return nil, containerStatsResult{}, err
}
result := containerStatsResult{
System: record.GetString("name"),
Period: rng.period,
From: rng.from.Format(time.RFC3339),
To: rng.to.Format(time.RFC3339),
Note: rng.note,
Containers: []containerHistory{},
}
records, err := h.findStatsRecords("container_stats", record.Id, rng)
if err != nil {
return nil, containerStatsResult{}, err
}
if len(records) == 0 {
result.Note = joinNotes(rng.note, fmt.Sprintf("no %s container records in this range (the system may have no containers, or be new, down or paused)", rng.period))
return nil, result, nil
}
filter := strings.ToLower(params.Container)
histories := map[string]*containerHistory{}
var decodeErr error
skipped := 0
for _, statsRecord := range records {
var containers []*container.Stats
if err := statsRecord.UnmarshalJSONField("stats", &containers); err != nil {
decodeErr = err
skipped++
continue
}
recordTime := statsRecord.GetDateTime("created").Time().UTC().Format(time.RFC3339)
for _, c := range containers {
if c == nil || (filter != "" && !strings.Contains(strings.ToLower(c.Name), filter)) {
continue
}
history := histories[c.Name]
if history == nil {
history = &containerHistory{Name: c.Name}
histories[c.Name] = history
}
history.add(containerSample{Time: recordTime, Cpu: c.Cpu, Mem: c.Mem, Net: containerNetBytes(c)}, filter != "")
}
}
if skipped > 0 {
h.app.Logger().Warn("MCP: failed to decode container stats", "system", record.Id, "skipped", skipped, "err", decodeErr)
result.Note = joinNotes(result.Note, fmt.Sprintf("%d of %d records could not be decoded and were skipped", skipped, len(records)))
}
if len(histories) == 0 && filter != "" {
result.Note = joinNotes(result.Note, fmt.Sprintf("no containers matching %q in this range", params.Container))
}
for _, history := range histories {
history.finish()
result.Containers = append(result.Containers, *history)
}
peak := func(c containerHistory) float64 {
switch sortBy {
case "memory":
return c.MaxMem
case "net":
return float64(c.MaxNet)
}
return c.MaxCpu
}
slices.SortFunc(result.Containers, func(a, b containerHistory) int {
if order := cmp.Compare(peak(b), peak(a)); order != 0 {
return order
}
return strings.Compare(a.Name, b.Name)
})
limit := scopeParams{Limit: params.Limit}.limit()
if len(result.Containers) > limit {
result.Containers = result.Containers[:limit]
result.Truncated = true
}
result.Count = len(result.Containers)
return nil, result, nil
}