Skip to content

Lightweight server monitoring with historical data, docker stats, and alerts.

fix: use omitzero for scalar JSON fields under json v2

henrygdf8d3fe1817b2
diff --git a/internal/alerts/alerts.go b/internal/alerts/alerts.go
index 496a11608..f0ad7bec0 100644
--- a/internal/alerts/alerts.go
+++ b/internal/alerts/alerts.go
@@ -67,7 +67,7 @@ type SystemAlertGPUData struct {
}
type SystemAlertZfsPool struct {
- Raw bool `json:"raw,omitempty"`
+ Raw bool `json:"raw,omitzero"`
Total float64 `json:"d"`
Used float64 `json:"du"`
}
diff --git a/internal/entities/container/container.go b/internal/entities/container/container.go
index 7105c4af9..aa980d183 100644
--- a/internal/entities/container/container.go
+++ b/internal/entities/container/container.go
@@ -123,9 +123,9 @@ type CPUStats struct {
// CPU Usage. Linux and Windows.
CPUUsage CPUUsage `json:"cpu_usage"`
// System Usage. Linux only. Populated from /proc/stat on Docker; from cgroup cpu.stat on Podman.
- SystemUsage uint64 `json:"system_cpu_usage,omitempty"`
+ SystemUsage uint64 `json:"system_cpu_usage,omitzero"`
// Number of online CPUs. Linux only. Used by Podman for time-based CPU calculation.
- OnlineCPUs uint32 `json:"online_cpus,omitempty"`
+ OnlineCPUs uint32 `json:"online_cpus,omitzero"`
}
type CPUUsage struct {
@@ -137,16 +137,16 @@ type CPUUsage struct {
type MemoryStats struct {
// current res_counter usage for memory
- Usage uint64 `json:"usage,omitempty"`
+ Usage uint64 `json:"usage,omitzero"`
// all the stats exported via memory.stat.
Stats MemoryStatsStats `json:"stats"`
// private working set (Windows only)
- PrivateWorkingSet uint64 `json:"privateworkingset,omitempty"`
+ PrivateWorkingSet uint64 `json:"privateworkingset,omitzero"`
}
type MemoryStatsStats struct {
- Cache uint64 `json:"cache,omitempty"`
- InactiveFile uint64 `json:"inactive_file,omitempty"`
+ Cache uint64 `json:"cache,omitzero"`
+ InactiveFile uint64 `json:"inactive_file,omitzero"`
}
type NetworkStats struct {
diff --git a/internal/entities/smart/smart.go b/internal/entities/smart/smart.go
index 55aee622f..7fa94b8c9 100644
--- a/internal/entities/smart/smart.go
+++ b/internal/entities/smart/smart.go
@@ -523,11 +523,11 @@ type SmartData struct {
ModelName string `json:"mn,omitempty" cbor:"1,keyasint,omitempty"`
SerialNumber string `json:"sn,omitempty" cbor:"2,keyasint,omitempty"`
FirmwareVersion string `json:"fv,omitempty" cbor:"3,keyasint,omitempty"`
- Capacity uint64 `json:"c,omitempty" cbor:"4,keyasint,omitempty"`
+ Capacity uint64 `json:"c,omitzero" cbor:"4,keyasint,omitempty"`
SmartStatus string `json:"s,omitempty" cbor:"5,keyasint,omitempty"`
DiskName string `json:"dn,omitempty" cbor:"6,keyasint,omitempty"`
DiskType string `json:"dt,omitempty" cbor:"7,keyasint,omitempty"`
- Temperature uint8 `json:"t,omitempty" cbor:"8,keyasint,omitempty"`
+ Temperature uint8 `json:"t,omitzero" cbor:"8,keyasint,omitempty"`
Attributes []*SmartAttribute `json:"a,omitempty" cbor:"9,keyasint,omitempty"`
}
@@ -539,11 +539,11 @@ type SmartDataResponse struct {
}
type SmartAttribute struct {
- ID uint16 `json:"id,omitempty" cbor:"0,keyasint,omitempty"`
+ ID uint16 `json:"id,omitzero" cbor:"0,keyasint,omitempty"`
Name string `json:"n" cbor:"1,keyasint"`
- Value uint16 `json:"v,omitempty" cbor:"2,keyasint,omitempty"`
- Worst uint16 `json:"w,omitempty" cbor:"3,keyasint,omitempty"`
- Threshold uint16 `json:"t,omitempty" cbor:"4,keyasint,omitempty"`
+ Value uint16 `json:"v,omitzero" cbor:"2,keyasint,omitempty"`
+ Worst uint16 `json:"w,omitzero" cbor:"3,keyasint,omitempty"`
+ Threshold uint16 `json:"t,omitzero" cbor:"4,keyasint,omitempty"`
RawValue uint64 `json:"rv" cbor:"5,keyasint"`
RawString string `json:"rs,omitempty" cbor:"6,keyasint,omitempty"`
WhenFailed string `json:"wf,omitempty" cbor:"7,keyasint,omitempty"`
diff --git a/internal/entities/system/package_updates.go b/internal/entities/system/package_updates.go
index c07fe130a..73b43f16d 100644
--- a/internal/entities/system/package_updates.go
+++ b/internal/entities/system/package_updates.go
@@ -5,7 +5,7 @@ type PackageUpdate struct {
Name string `json:"name" cbor:"0,keyasint"`
Current string `json:"current,omitempty" cbor:"1,keyasint,omitempty"` // installed version, empty if unknown
Available string `json:"available" cbor:"2,keyasint"`
- Security bool `json:"security,omitempty" cbor:"3,keyasint,omitempty"`
+ Security bool `json:"security,omitzero" cbor:"3,keyasint,omitempty"`
}
// PackageUpdates is the detail payload returned by the agent for the
@@ -13,8 +13,8 @@ type PackageUpdate struct {
type PackageUpdates struct {
Manager string `json:"manager,omitempty" cbor:"0,keyasint,omitempty"`
// CheckedAt is the Unix time in seconds of the last check, 0 if none has finished.
- CheckedAt int64 `json:"checkedAt,omitempty" cbor:"1,keyasint,omitempty"`
+ CheckedAt int64 `json:"checkedAt,omitzero" cbor:"1,keyasint,omitempty"`
// SecurityKnown is true if the package manager flags security updates per package.
- SecurityKnown bool `json:"securityKnown,omitempty" cbor:"2,keyasint,omitempty"`
+ SecurityKnown bool `json:"securityKnown,omitzero" cbor:"2,keyasint,omitempty"`
Packages []PackageUpdate `json:"packages" cbor:"3,keyasint"`
}
diff --git a/internal/entities/system/system.go b/internal/entities/system/system.go
index 29bb9f675..fafa1d381 100644
--- a/internal/entities/system/system.go
+++ b/internal/entities/system/system.go
@@ -21,26 +21,26 @@ type WiFi struct {
type Stats struct {
Cpu float64 `json:"cpu" cbor:"0,keyasint"`
- MaxCpu float64 `json:"cpum,omitempty" cbor:"-"`
+ MaxCpu float64 `json:"cpum,omitzero" cbor:"-"`
Mem float64 `json:"m" cbor:"2,keyasint"`
- MaxMem float64 `json:"mm,omitempty" cbor:"-"`
+ MaxMem float64 `json:"mm,omitzero" cbor:"-"`
MemUsed float64 `json:"mu" cbor:"3,keyasint"`
MemPct float64 `json:"mp" cbor:"4,keyasint"`
MemBuffCache float64 `json:"mb" cbor:"5,keyasint"`
- MemZfsArc float64 `json:"mz,omitempty" cbor:"6,keyasint,omitempty"` // ZFS ARC memory
- Swap float64 `json:"s,omitempty" cbor:"7,keyasint,omitempty"`
- SwapUsed float64 `json:"su,omitempty" cbor:"8,keyasint,omitempty"`
+ MemZfsArc float64 `json:"mz,omitzero" cbor:"6,keyasint,omitempty"` // ZFS ARC memory
+ Swap float64 `json:"s,omitzero" cbor:"7,keyasint,omitempty"`
+ SwapUsed float64 `json:"su,omitzero" cbor:"8,keyasint,omitempty"`
DiskTotal float64 `json:"d" cbor:"9,keyasint"`
DiskUsed float64 `json:"du" cbor:"10,keyasint"`
DiskPct float64 `json:"dp" cbor:"11,keyasint"`
DiskReadPs float64 `json:"dr,omitzero" cbor:"12,keyasint,omitzero"`
DiskWritePs float64 `json:"dw,omitzero" cbor:"13,keyasint,omitzero"`
- MaxDiskReadPs float64 `json:"drm,omitempty" cbor:"-"`
- MaxDiskWritePs float64 `json:"dwm,omitempty" cbor:"-"`
+ MaxDiskReadPs float64 `json:"drm,omitzero" cbor:"-"`
+ MaxDiskWritePs float64 `json:"dwm,omitzero" cbor:"-"`
NetworkSent float64 `json:"ns,omitzero" cbor:"16,keyasint,omitzero"`
NetworkRecv float64 `json:"nr,omitzero" cbor:"17,keyasint,omitzero"`
- MaxNetworkSent float64 `json:"nsm,omitempty" cbor:"-"`
- MaxNetworkRecv float64 `json:"nrm,omitempty" cbor:"-"`
+ MaxNetworkSent float64 `json:"nsm,omitzero" cbor:"-"`
+ MaxNetworkRecv float64 `json:"nrm,omitzero" cbor:"-"`
Temperatures map[string]float64 `json:"t,omitempty" cbor:"20,keyasint,omitempty"`
ExtraFs map[string]*FsStats `json:"efs,omitempty" cbor:"21,keyasint,omitempty"`
GPUData map[string]GPUData `json:"g,omitempty" cbor:"22,keyasint,omitempty"`
@@ -70,9 +70,9 @@ type Stats struct {
// ZfsPool holds per-pool ZFS metrics for a single collection interval.
type ZfsPool struct {
DisplayName string `json:"n,omitempty" cbor:"8,keyasint,omitempty"`
- HideUsage bool `json:"hu,omitempty" cbor:"6,keyasint,omitempty"` // equivalent filesystem usage chart exists
- HideIO bool `json:"hi,omitempty" cbor:"7,keyasint,omitempty"` // equivalent filesystem I/O chart exists
- Raw bool `json:"raw,omitempty" cbor:"5,keyasint,omitempty"`
+ HideUsage bool `json:"hu,omitzero" cbor:"6,keyasint,omitempty"` // equivalent filesystem usage chart exists
+ HideIO bool `json:"hi,omitzero" cbor:"7,keyasint,omitempty"` // equivalent filesystem I/O chart exists
+ Raw bool `json:"raw,omitzero" cbor:"5,keyasint,omitempty"`
Total float64 `json:"d" cbor:"0,keyasint"` // total capacity in GiB
Used float64 `json:"du" cbor:"1,keyasint"` // allocated in GiB
ReadBytes uint64 `json:"rb,omitzero" cbor:"2,keyasint,omitzero"` // read throughput in bytes/s
@@ -109,13 +109,13 @@ func (b Battery) MarshalJSON() ([]byte, error) {
type GPUData struct {
Name string `json:"n" cbor:"0,keyasint"`
Temperature float64 `json:"-"`
- MemoryUsed float64 `json:"mu,omitempty,omitzero" cbor:"1,keyasint,omitempty,omitzero"`
- MemoryTotal float64 `json:"mt,omitempty,omitzero" cbor:"2,keyasint,omitempty,omitzero"`
+ MemoryUsed float64 `json:"mu,omitzero" cbor:"1,keyasint,omitempty,omitzero"`
+ MemoryTotal float64 `json:"mt,omitzero" cbor:"2,keyasint,omitempty,omitzero"`
Usage float64 `json:"u" cbor:"3,keyasint,omitempty"`
- Power float64 `json:"p,omitempty" cbor:"4,keyasint,omitempty"`
+ Power float64 `json:"p,omitzero" cbor:"4,keyasint,omitempty"`
Count float64 `json:"-"`
Engines map[string]float64 `json:"e,omitempty" cbor:"5,keyasint,omitempty"`
- PowerPkg float64 `json:"pp,omitempty" cbor:"6,keyasint,omitempty"`
+ PowerPkg float64 `json:"pp,omitzero" cbor:"6,keyasint,omitempty"`
}
type FsStats struct {
@@ -128,13 +128,13 @@ type FsStats struct {
TotalWrite uint64 `json:"tw,omitzero" cbor:"10,keyasint,omitzero"` // cumulative device write bytes
DiskReadPs float64 `json:"r" cbor:"2,keyasint"`
DiskWritePs float64 `json:"w" cbor:"3,keyasint"`
- MaxDiskReadPS float64 `json:"rm,omitempty" cbor:"-"`
- MaxDiskWritePS float64 `json:"wm,omitempty" cbor:"-"`
+ MaxDiskReadPS float64 `json:"rm,omitzero" cbor:"-"`
+ MaxDiskWritePS float64 `json:"wm,omitzero" cbor:"-"`
// TODO: remove DiskReadPs and DiskWritePs in future release in favor of DiskReadBytes and DiskWriteBytes
DiskReadBytes uint64 `json:"rb" cbor:"6,keyasint,omitempty"`
DiskWriteBytes uint64 `json:"wb" cbor:"7,keyasint,omitempty"`
- MaxDiskReadBytes uint64 `json:"rbm,omitempty" cbor:"-"`
- MaxDiskWriteBytes uint64 `json:"wbm,omitempty" cbor:"-"`
+ MaxDiskReadBytes uint64 `json:"rbm,omitzero" cbor:"-"`
+ MaxDiskWriteBytes uint64 `json:"wbm,omitzero" cbor:"-"`
DiskIoStats [6]float64 `json:"dios,omitzero" cbor:"8,keyasint,omitzero"` // [read time %, write time %, io utilization %, r_await ms, w_await ms, weighted io %]
MaxDiskIoStats [6]float64 `json:"diosm,omitzero" cbor:"-"` // max values for DiskIoStats
}
@@ -169,7 +169,7 @@ type Info struct {
KernelVersion string `json:"k,omitempty" cbor:"1,keyasint,omitempty"` // deprecated - moved to Details struct
Cores int `json:"c,omitzero" cbor:"2,keyasint,omitzero"` // deprecated - moved to Details struct
// Threads is needed in Info struct to calculate load average thresholds
- Threads int `json:"t,omitempty" cbor:"3,keyasint,omitempty"`
+ Threads int `json:"t,omitzero" cbor:"3,keyasint,omitempty"`
CpuModel string `json:"m,omitempty" cbor:"4,keyasint,omitempty"` // deprecated - moved to Details struct
Uptime uint64 `json:"u" cbor:"5,keyasint"`
Cpu float64 `json:"cpu" cbor:"6,keyasint"`
@@ -177,24 +177,24 @@ type Info struct {
DiskPct float64 `json:"dp" cbor:"8,keyasint"`
Bandwidth float64 `json:"b,omitzero" cbor:"9,keyasint"` // deprecated in favor of BandwidthBytes
AgentVersion string `json:"v" cbor:"10,keyasint"`
- Podman bool `json:"p,omitempty" cbor:"11,keyasint,omitempty"` // deprecated - moved to Details struct
- GpuPct float64 `json:"g,omitempty" cbor:"12,keyasint,omitempty"`
- DashboardTemp float64 `json:"dt,omitempty" cbor:"13,keyasint,omitempty"`
- Os Os `json:"os,omitempty" cbor:"14,keyasint,omitempty"` // deprecated - moved to Details struct
+ Podman bool `json:"p,omitzero" cbor:"11,keyasint,omitempty"` // deprecated - moved to Details struct
+ GpuPct float64 `json:"g,omitzero" cbor:"12,keyasint,omitempty"`
+ DashboardTemp float64 `json:"dt,omitzero" cbor:"13,keyasint,omitempty"`
+ Os Os `json:"os,omitzero" cbor:"14,keyasint,omitempty"` // deprecated - moved to Details struct
// LoadAvg1 float64 `json:"l1,omitempty" cbor:"15,keyasint,omitempty"` // deprecated - use `la` array instead
// LoadAvg5 float64 `json:"l5,omitempty" cbor:"16,keyasint,omitempty"` // deprecated - use `la` array instead
// LoadAvg15 float64 `json:"l15,omitempty" cbor:"17,keyasint,omitempty"` // deprecated - use `la` array instead
BandwidthBytes uint64 `json:"bb" cbor:"18,keyasint"`
LoadAvg [3]float64 `json:"la,omitempty" cbor:"19,keyasint"`
- ConnectionType ConnectionType `json:"ct,omitempty" cbor:"20,keyasint,omitempty,omitzero"`
+ ConnectionType ConnectionType `json:"ct,omitzero" cbor:"20,keyasint,omitempty,omitzero"`
ExtraFsPct map[string]float64 `json:"efs,omitempty" cbor:"21,keyasint,omitempty"`
Services []uint16 `json:"sv,omitempty" cbor:"22,keyasint,omitempty"` // [totalServices, numFailedServices]
Battery Battery `json:"bat,omitzero" cbor:"23,keyasint,omitzero"` // [percent, charge state]
RootDiskName string `json:"rdn,omitempty" cbor:"24,keyasint,omitempty"` // custom name for root disk (set via FILESYSTEM=device__name)
PackageUpdates []uint16 `json:"pu,omitempty" cbor:"25,keyasint,omitempty"` // [totalUpdates, securityUpdates] (security omitted if unknown)
WiFi map[string]WiFi `json:"wf,omitempty" cbor:"26,keyasint,omitempty"` // connected Wi-Fi interfaces
- SystemdLogs bool `json:"jl,omitempty" cbor:"27,keyasint,omitempty"` // agent can read the system journal
+ SystemdLogs bool `json:"jl,omitzero" cbor:"27,keyasint,omitempty"` // agent can read the system journal
}
// Data that does not change during process lifetime and is not needed in All Systems table
@@ -222,6 +222,6 @@ type CombinedData struct {
Details *Details `cbor:"4,keyasint,omitempty"`
// SystemdServicesUpdated distinguishes a fresh empty snapshot from a response
// that omitted systemd data (for example, a short-cache dashboard request).
- SystemdServicesUpdated bool `json:"systemdUpdated,omitempty" cbor:"5,keyasint,omitempty"`
+ SystemdServicesUpdated bool `json:"systemdUpdated,omitzero" cbor:"5,keyasint,omitempty"`
Monitors map[string]monitor.Result `cbor:"6,keyasint"`
}
diff --git a/internal/entities/system/system_test.go b/internal/entities/system/system_test.go
index 7a7f00494..640f680d3 100644
--- a/internal/entities/system/system_test.go
+++ b/internal/entities/system/system_test.go
@@ -7,6 +7,8 @@ import (
"github.com/fxamacker/cbor/v2"
"github.com/henrygd/beszel/internal/entities/container"
+ "github.com/henrygd/beszel/internal/entities/smart"
+ "github.com/henrygd/beszel/internal/entities/zfs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@@ -117,3 +119,32 @@ func TestCombinedDataContainerValidityTransport(t *testing.T) {
require.NoError(t, cbor.Unmarshal(invalidData, &decodedInvalid))
assert.Nil(t, decodedInvalid.Containers)
}
+
+// PocketBase marshals record JSON fields with encoding/json/v2, where omitempty
+// no longer omits zero numbers or false. Scalar fields must use omitzero so
+// stored records keep the same shape as encoding/json (v1).
+func TestZeroValueJSONMatchesV1(t *testing.T) {
+ for name, value := range map[string]any{
+ "stats": Stats{
+ ExtraFs: map[string]*FsStats{"sda": {}},
+ GPUData: map[string]GPUData{"0": {}},
+ ZfsPools: map[string]*ZfsPool{"tank": {}},
+ },
+ "info": Info{WiFi: map[string]WiFi{"wlan0": {}}},
+ "package_updates": PackageUpdates{Packages: []PackageUpdate{{}}},
+ "zfs": zfs.ZfsData{Pools: []*zfs.PoolDetail{{
+ Scrub: &zfs.Scrub{State: "NONE"}, // a scrub encoding to {} is dropped by v2 omitempty
+ Vdevs: []*zfs.Vdev{{}},
+ Datasets: []*zfs.Dataset{{}},
+ }}},
+ "smart": smart.SmartData{Attributes: []*smart.SmartAttribute{{}}},
+ } {
+ t.Run(name, func(t *testing.T) {
+ v1Data, err := json.Marshal(value)
+ require.NoError(t, err)
+ v2Data, err := jsonv2.Marshal(value)
+ require.NoError(t, err)
+ assert.JSONEq(t, string(v1Data), string(v2Data))
+ })
+ }
+}
diff --git a/internal/entities/zfs/zfs.go b/internal/entities/zfs/zfs.go
index a3d9adea5..c5a6bd894 100644
--- a/internal/entities/zfs/zfs.go
+++ b/internal/entities/zfs/zfs.go
@@ -6,7 +6,7 @@ import "strings"
// ZfsData is the detail payload returned by the agent for the GetZfsData action.
type ZfsData struct {
Pools []*PoolDetail `json:"pools,omitempty"`
- Complete bool `json:"complete,omitempty"`
+ Complete bool `json:"complete,omitzero"`
// Backends whose inventories are complete, even when another backend failed.
CompleteBackends []string `json:"completeBackends,omitempty"`
}
@@ -33,13 +33,13 @@ func (data *ZfsData) CanRefreshPool(name string) bool {
// scrub, vdev, and dataset information.
type PoolDetail struct {
DisplayName string `json:"displayName,omitempty"`
- Raw bool `json:"raw,omitempty"`
+ Raw bool `json:"raw,omitzero"`
Name string `json:"name"`
Health string `json:"health,omitempty"`
Status string `json:"status,omitempty"`
- Size uint64 `json:"size,omitempty"` // bytes
- Alloc uint64 `json:"alloc,omitempty"` // bytes
- Free uint64 `json:"free,omitempty"` // bytes
+ Size uint64 `json:"size,omitzero"` // bytes
+ Alloc uint64 `json:"alloc,omitzero"` // bytes
+ Free uint64 `json:"free,omitzero"` // bytes
Scrub *Scrub `json:"scrub,omitempty"`
Vdevs []*Vdev `json:"vdevs,omitempty"`
Datasets []*Dataset `json:"datasets,omitempty"`
@@ -49,22 +49,22 @@ type PoolDetail struct {
type Scrub struct {
State string `json:"state,omitempty"` // NONE, SCANNING, FINISHED, CANCELED
Progress string `json:"progress,omitempty"`
- Errors uint64 `json:"errors,omitempty"`
+ Errors uint64 `json:"errors,omitzero"`
}
// Vdev is a single vdev (mirror, raidz, or leaf disk) with error counters.
type Vdev struct {
Name string `json:"name"`
State string `json:"state,omitempty"`
- ReadErrs uint64 `json:"readErrs,omitempty"`
- WriteErrs uint64 `json:"writeErrs,omitempty"`
- ChecksumErrs uint64 `json:"checksumErrs,omitempty"`
+ ReadErrs uint64 `json:"readErrs,omitzero"`
+ WriteErrs uint64 `json:"writeErrs,omitzero"`
+ ChecksumErrs uint64 `json:"checksumErrs,omitzero"`
}
// Dataset is a single ZFS dataset with usage information.
type Dataset struct {
Name string `json:"name"`
- Used uint64 `json:"used,omitempty"`
- Avail uint64 `json:"avail,omitempty"`
+ Used uint64 `json:"used,omitzero"`
+ Avail uint64 `json:"avail,omitzero"`
Mountpoint string `json:"mount,omitempty"`
}
diff --git a/internal/hub/server.go b/internal/hub/server.go
index 3e4f64d02..0d55bbf57 100644
--- a/internal/hub/server.go
+++ b/internal/hub/server.go
@@ -14,7 +14,7 @@ type PublicAppInfo struct {
BASE_PATH string
HUB_VERSION string
HUB_URL string
- OAUTH_DISABLE_POPUP bool `json:"OAUTH_DISABLE_POPUP,omitempty"`
+ OAUTH_DISABLE_POPUP bool `json:"OAUTH_DISABLE_POPUP,omitzero"`
}
// modifyIndexHTML injects the public app information into the index.html content