sysmon

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 20, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package sysmon provides cross-platform system metrics collection.

Index

Constants

View Source
const (
	// DefaultSampleInterval is the default interval between metric collections.
	DefaultSampleInterval = 10 * time.Second

	// MinSampleInterval is the minimum allowed sample interval.
	MinSampleInterval = 50 * time.Millisecond

	// MaxSampleInterval is the maximum allowed sample interval.
	MaxSampleInterval = 5 * time.Minute

	// DefaultConfigRefreshInterval is how often agents check for config updates.
	DefaultConfigRefreshInterval = 5 * time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CPUClusterMetric

type CPUClusterMetric struct {
	// Name is the cluster identifier (e.g., ECPU, PCPU).
	Name string `json:"name"`

	// FrequencyHz is the instantaneous frequency in Hz, if available.
	FrequencyHz float64 `json:"frequency_hz"`
}

CPUClusterMetric represents aggregated CPU cluster telemetry.

type CPUCollector

type CPUCollector struct {
	// contains filtered or unexported fields
}

CPUCollector handles CPU metrics collection with frequency support.

func NewCPUCollector

func NewCPUCollector(sampleInterval time.Duration) *CPUCollector

NewCPUCollector creates a new CPU metrics collector.

func (*CPUCollector) Collect

func (c *CPUCollector) Collect(ctx context.Context) ([]CPUMetric, []CPUClusterMetric, error)

Collect gathers CPU metrics including usage and frequency.

type CPUMetric

type CPUMetric struct {
	// CoreID is the zero-based logical core ID.
	CoreID int32 `json:"core_id"`

	// Label is a platform-specific identifier (e.g., ECPU0, PCPU3).
	Label string `json:"label,omitempty"`

	// Cluster is the logical cluster this core belongs to (e.g., ECPU, PCPU).
	Cluster string `json:"cluster,omitempty"`

	// UsagePercent is the CPU usage percentage (0-100).
	UsagePercent float64 `json:"usage_percent"`

	// FrequencyHz is the instantaneous frequency in Hz, if available.
	FrequencyHz float64 `json:"frequency_hz,omitempty"`
}

CPUMetric represents CPU utilization for a single core.

type Collector

type Collector interface {
	// Start begins periodic metric collection in the background.
	Start(ctx context.Context) error

	// Stop halts metric collection and releases resources.
	Stop() error

	// Collect performs a single metric collection cycle and returns the sample.
	Collect(ctx context.Context) (*MetricSample, error)

	// Reconfigure updates the collector with new configuration.
	// The collector applies changes without requiring a restart.
	Reconfigure(config *ParsedConfig) error

	// Latest returns the most recent metric sample, or nil if none available.
	Latest() *MetricSample

	// Drain returns all buffered metric samples since the last call.
	Drain() []*MetricSample
}

Collector defines the interface for system metrics collection.

type CollectorOption

type CollectorOption func(*DefaultCollector)

CollectorOption configures a DefaultCollector.

func WithAgentID

func WithAgentID(agentID string) CollectorOption

WithAgentID sets the agent identifier for metric samples.

func WithLogger

func WithLogger(log logger.Logger) CollectorOption

WithLogger sets a custom logger for the collector.

func WithPartition

func WithPartition(partition string) CollectorOption

WithPartition sets the partition identifier for metric samples.

type Config

type Config struct {
	// Enabled controls whether sysmon collection is active.
	Enabled bool `json:"enabled"`

	// SampleInterval is the duration between metric collections.
	// Supports Go duration strings like "10s", "1m", "500ms".
	SampleInterval string `json:"sample_interval,omitempty"`

	// CollectCPU enables CPU metrics collection.
	CollectCPU bool `json:"collect_cpu"`

	// CollectMemory enables memory metrics collection.
	CollectMemory bool `json:"collect_memory"`

	// CollectDisk enables disk metrics collection.
	CollectDisk bool `json:"collect_disk"`

	// CollectNetwork enables network interface metrics collection.
	CollectNetwork bool `json:"collect_network"`

	// CollectProcesses enables process metrics collection.
	CollectProcesses bool `json:"collect_processes"`

	// DiskPaths specifies which mount points to monitor.
	// If empty, all mounted filesystems are monitored.
	DiskPaths []string `json:"disk_paths,omitempty"`

	// DiskExcludePaths specifies mount points to exclude from collection.
	DiskExcludePaths []string `json:"disk_exclude_paths,omitempty"`

	// Thresholds defines warning/critical thresholds for alerting.
	Thresholds map[string]string `json:"thresholds,omitempty"`
}

Config controls the sysmon collector runtime behavior.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a configuration with sensible defaults.

func LoadConfigFromFile

func LoadConfigFromFile(path string) (*Config, error)

LoadConfigFromFile reads and parses a sysmon configuration from a JSON file.

func (*Config) MergeWithDefaults

func (c *Config) MergeWithDefaults() Config

MergeWithDefaults returns a new config with default values for any unset fields.

func (*Config) Parse

func (c *Config) Parse() (*ParsedConfig, error)

Parse validates and parses the configuration into usable values.

type DefaultCollector

type DefaultCollector struct {
	// contains filtered or unexported fields
}

DefaultCollector implements the Collector interface using gopsutil.

func NewCollector

func NewCollector(config *ParsedConfig, opts ...CollectorOption) (*DefaultCollector, error)

NewCollector creates a new DefaultCollector with the given configuration.

func (*DefaultCollector) Collect

func (c *DefaultCollector) Collect(ctx context.Context) (*MetricSample, error)

Collect performs a single metric collection cycle.

func (*DefaultCollector) Drain added in v1.0.90

func (c *DefaultCollector) Drain() []*MetricSample

Drain returns all buffered metric samples since the last call.

func (*DefaultCollector) Latest

func (c *DefaultCollector) Latest() *MetricSample

Latest returns the most recent metric sample.

func (*DefaultCollector) Reconfigure

func (c *DefaultCollector) Reconfigure(config *ParsedConfig) error

Reconfigure updates the collector with new configuration.

func (*DefaultCollector) Start

func (c *DefaultCollector) Start(ctx context.Context) error

Start begins periodic metric collection in the background.

func (*DefaultCollector) Stop

func (c *DefaultCollector) Stop() error

Stop halts metric collection and releases resources.

type DiskMetric

type DiskMetric struct {
	// MountPoint is the filesystem mount path.
	MountPoint string `json:"mount_point"`

	// UsedBytes is the number of bytes currently in use.
	UsedBytes uint64 `json:"used_bytes"`

	// TotalBytes is the total capacity in bytes.
	TotalBytes uint64 `json:"total_bytes"`
}

DiskMetric represents disk usage for a single mount point.

func CollectDisks

func CollectDisks(ctx context.Context, paths []string, excludePaths []string) ([]DiskMetric, error)

CollectDisks gathers disk usage metrics for specified paths. If paths is empty, all mounted filesystems are collected.

type MemoryMetric

type MemoryMetric struct {
	// UsedBytes is the number of bytes currently in use.
	UsedBytes uint64 `json:"used_bytes"`

	// TotalBytes is the total memory capacity in bytes.
	TotalBytes uint64 `json:"total_bytes"`

	// SwapUsedBytes is the swap space currently in use.
	SwapUsedBytes uint64 `json:"swap_used_bytes,omitempty"`

	// SwapTotalBytes is the total swap capacity.
	SwapTotalBytes uint64 `json:"swap_total_bytes,omitempty"`
}

MemoryMetric represents system memory usage.

func CollectMemory

func CollectMemory(ctx context.Context) (*MemoryMetric, error)

CollectMemory gathers system memory metrics.

type MetricSample

type MetricSample struct {
	// Timestamp is when this sample was collected (RFC3339 format).
	Timestamp string `json:"timestamp"`

	// HostID is the hostname or unique identifier for this host.
	HostID string `json:"host_id"`

	// HostIP is the primary IP address of this host.
	HostIP string `json:"host_ip"`

	// Partition is the optional partition/segment identifier.
	Partition *string `json:"partition,omitempty"`

	// AgentID is the ServiceRadar agent identifier.
	AgentID string `json:"agent_id,omitempty"`

	// CPUs contains per-core CPU metrics.
	CPUs []CPUMetric `json:"cpus"`

	// Clusters contains aggregate CPU cluster metrics (e.g., big.LITTLE).
	Clusters []CPUClusterMetric `json:"clusters,omitempty"`

	// Disks contains per-mount-point disk metrics.
	Disks []DiskMetric `json:"disks"`

	// Memory contains system memory metrics.
	Memory MemoryMetric `json:"memory"`

	// Network contains per-interface network metrics.
	Network []NetworkMetric `json:"network,omitempty"`

	// Processes contains top process metrics.
	Processes []ProcessMetric `json:"processes"`
}

MetricSample represents a complete snapshot of system metrics at a point in time. This structure is compatible with the existing Rust sysmon output format.

func NewMetricSample

func NewMetricSample(hostID, hostIP, agentID string, partition *string) *MetricSample

NewMetricSample creates a new MetricSample with the current timestamp.

type NetworkMetric

type NetworkMetric struct {
	// Interface is the network interface name.
	Interface string `json:"interface"`

	// BytesSent is the total bytes transmitted.
	BytesSent uint64 `json:"bytes_sent"`

	// BytesRecv is the total bytes received.
	BytesRecv uint64 `json:"bytes_recv"`

	// PacketsSent is the total packets transmitted.
	PacketsSent uint64 `json:"packets_sent"`

	// PacketsRecv is the total packets received.
	PacketsRecv uint64 `json:"packets_recv"`

	// ErrorsIn is the count of receive errors.
	ErrorsIn uint64 `json:"errors_in"`

	// ErrorsOut is the count of transmit errors.
	ErrorsOut uint64 `json:"errors_out"`

	// DropsIn is the count of dropped incoming packets.
	DropsIn uint64 `json:"drops_in"`

	// DropsOut is the count of dropped outgoing packets.
	DropsOut uint64 `json:"drops_out"`
}

NetworkMetric represents network interface statistics.

func CollectNetwork

func CollectNetwork(ctx context.Context) ([]NetworkMetric, error)

CollectNetwork gathers network interface statistics.

type ParsedConfig

type ParsedConfig struct {
	Enabled          bool
	SampleInterval   time.Duration
	CollectCPU       bool
	CollectMemory    bool
	CollectDisk      bool
	CollectNetwork   bool
	CollectProcesses bool
	DiskPaths        []string
	DiskExcludePaths []string
	Thresholds       map[string]string
}

ParsedConfig holds the parsed and validated configuration values.

type ProcessMetric

type ProcessMetric struct {
	// PID is the process ID.
	PID uint32 `json:"pid"`

	// Name is the process name.
	Name string `json:"name"`

	// CPUUsage is the CPU usage percentage.
	CPUUsage float32 `json:"cpu_usage"`

	// MemoryUsage is the memory usage in bytes.
	MemoryUsage uint64 `json:"memory_usage"`

	// Status is the process status (e.g., Running, Sleeping).
	Status string `json:"status"`

	// StartTime is when the process started (RFC3339 format).
	StartTime string `json:"start_time"`
}

ProcessMetric represents metrics for a single process.

func CollectProcesses

func CollectProcesses(ctx context.Context) ([]ProcessMetric, error)

CollectProcesses gathers metrics for all running processes. Results are sorted by CPU usage (descending) for convenience, but all processes are returned. The backend decides what to display (e.g., top N by CPU/memory).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL