From 159df116c85ea0cca64dd1c95b4d1c44e19e0cc5 Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Mon, 15 Sep 2025 08:25:20 +0200 Subject: [PATCH] feat(system-metrics,service-metrics,elastic): update elastic-client to version 8, improve parser logic to add more information, improve system monitor to add more information --- config.go | 27 ++-- elasticsearch.go | 2 +- elasticsearch_exporter.go | 2 +- go.mod | 10 +- go.sum | 21 ++- log_processor.go | 2 +- models.go | 108 ++++++++++--- service_monitor.go | 280 ++++++++++++++++----------------- system_metrics.go | 323 +++++++++++++++++++++++++++++++++++++- web_service.go | 2 +- 10 files changed, 587 insertions(+), 190 deletions(-) diff --git a/config.go b/config.go index a851929..e1b449e 100644 --- a/config.go +++ b/config.go @@ -44,15 +44,24 @@ type ElasticsearchConfig struct { } type SystemMetrics struct { - Enabled bool `mapstructure:"enabled"` - CollectCPU bool `mapstructure:"collect_cpu"` - CollectMemory bool `mapstructure:"collect_memory"` - CollectDisk bool `mapstructure:"collect_disk"` - CollectNetwork bool `mapstructure:"collect_network"` - CollectProcesses bool `mapstructure:"collect_processes"` - DiskPaths []string `mapstructure:"disk_paths"` - NetworkInterfaces []string `mapstructure:"network_interfaces"` - TopProcessesLimit int `mapstructure:"top_processes_limit"` + Enabled bool `mapstructure:"enabled"` + CollectCPU bool `mapstructure:"collect_cpu"` + CollectMemory bool `mapstructure:"collect_memory"` + CollectDisk bool `mapstructure:"collect_disk"` + CollectNetwork bool `mapstructure:"collect_network"` + CollectProcesses bool `mapstructure:"collect_processes"` + DiskPaths []string `mapstructure:"disk_paths"` + NetworkInterfaces []string `mapstructure:"network_interfaces"` + TopProcessesLimit int `mapstructure:"top_processes_limit"` + CollectNetworkConnections bool `mapstructure:"collect_network_connections"` + CollectLoadAverage bool `mapstructure:"collect_load_average"` + CollectTCPStats bool `mapstructure:"collect_tcp_stats"` + CollectFileHandles bool `mapstructure:"collect_filehandles"` + CollectDiskIO bool `mapstructure:"collect_disk_io"` + CollectNetworkLatency bool `mapstructure:"collect_network_latency"` + CollectBandwidthUsage bool `mapstructure:"collect_bandwidth_usage"` + TransferPorts []int `mapstructure:"transfer_ports"` + LatencyTestHosts []string `mapstructure:"latency_test_hosts"` } type Config struct { diff --git a/elasticsearch.go b/elasticsearch.go index 9bc8ecd..1d1be2c 100644 --- a/elasticsearch.go +++ b/elasticsearch.go @@ -8,7 +8,7 @@ import ( "strings" "time" - "github.com/elastic/go-elasticsearch/v7" + "github.com/elastic/go-elasticsearch/v8" ) func NewElasticsearchClient(config ElasticsearchConfig) (*elasticsearch.Client, error) { diff --git a/elasticsearch_exporter.go b/elasticsearch_exporter.go index d1f003b..b079724 100644 --- a/elasticsearch_exporter.go +++ b/elasticsearch_exporter.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "github.com/elastic/go-elasticsearch/v7" + "github.com/elastic/go-elasticsearch/v8" ) type ElasticsearchExporter struct { diff --git a/go.mod b/go.mod index c0e658a..fb0d12c 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,18 @@ module tixel_watch go 1.24.1 require ( - github.com/elastic/go-elasticsearch/v7 v7.17.10 + github.com/elastic/go-elasticsearch/v8 v8.19.0 github.com/hpcloud/tail v1.0.0 github.com/shirou/gopsutil v3.21.11+incompatible github.com/spf13/viper v1.20.1 + golang.org/x/sys v0.31.0 ) require ( + github.com/elastic/elastic-transport-go/v8 v8.7.0 // indirect github.com/fsnotify/fsnotify v1.8.0 // indirect + github.com/go-logr/logr v1.4.2 // indirect + github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-viper/mapstructure/v2 v2.2.1 // indirect github.com/pelletier/go-toml/v2 v2.2.3 // indirect @@ -23,9 +27,11 @@ require ( github.com/tklauser/go-sysconf v0.3.15 // indirect github.com/tklauser/numcpus v0.10.0 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect + go.opentelemetry.io/otel v1.29.0 // indirect + go.opentelemetry.io/otel/metric v1.29.0 // indirect + go.opentelemetry.io/otel/trace v1.29.0 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.9.0 // indirect - golang.org/x/sys v0.31.0 // indirect golang.org/x/text v0.21.0 // indirect gopkg.in/fsnotify.v1 v1.4.7 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect diff --git a/go.sum b/go.sum index a57c825..9aa28be 100644 --- a/go.sum +++ b/go.sum @@ -1,18 +1,27 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/elastic/go-elasticsearch/v7 v7.17.10 h1:TCQ8i4PmIJuBunvBS6bwT2ybzVFxxUhhltAs3Gyu1yo= -github.com/elastic/go-elasticsearch/v7 v7.17.10/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4= +github.com/elastic/elastic-transport-go/v8 v8.7.0 h1:OgTneVuXP2uip4BA658Xi6Hfw+PeIOod2rY3GVMGoVE= +github.com/elastic/elastic-transport-go/v8 v8.7.0/go.mod h1:YLHer5cj0csTzNFXoNQ8qhtGY1GTvSqPnKWKaqQE3Hk= +github.com/elastic/go-elasticsearch/v8 v8.19.0 h1:VmfBLNRORY7RZL+9hTxBD97ehl9H8Nxf2QigDh6HuMU= +github.com/elastic/go-elasticsearch/v8 v8.19.0/go.mod h1:F3j9e+BubmKvzvLjNui/1++nJuJxbkhHefbaT0kFKGY= github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M= github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY= +github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss= github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -51,6 +60,14 @@ github.com/tklauser/numcpus v0.10.0 h1:18njr6LDBk1zuna922MgdjQuJFjrdppsZG60sHGfj github.com/tklauser/numcpus v0.10.0/go.mod h1:BiTKazU708GQTYF4mB+cmlpT2Is1gLk7XVuEeem8LsQ= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +go.opentelemetry.io/otel v1.29.0 h1:PdomN/Al4q/lN6iBJEN3AwPvUiHPMlt93c8bqTG5Llw= +go.opentelemetry.io/otel v1.29.0/go.mod h1:N/WtXPs1CNCUEx+Agz5uouwCba+i+bJGFicT8SR4NP8= +go.opentelemetry.io/otel/metric v1.29.0 h1:vPf/HFWTNkPu1aYeIsc98l4ktOQaL6LeSoeV2g+8YLc= +go.opentelemetry.io/otel/metric v1.29.0/go.mod h1:auu/QWieFVWx+DmQOUMgj0F8LHWdgalxXqvp7BII/W8= +go.opentelemetry.io/otel/sdk v1.29.0 h1:vkqKjk7gwhS8VaWb0POZKmIEDimRCMsopNYnriHyryo= +go.opentelemetry.io/otel/sdk v1.29.0/go.mod h1:pM8Dx5WKnvxLCb+8lG1PRNIDxu9g9b9g59Qr7hfAAok= +go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt39JTi4= +go.opentelemetry.io/otel/trace v1.29.0/go.mod h1:eHl3w0sp3paPkYstJOmAimxhiFXPg+MMTlEh3nsQgWQ= go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= diff --git a/log_processor.go b/log_processor.go index 07248a3..27b2e57 100644 --- a/log_processor.go +++ b/log_processor.go @@ -5,7 +5,7 @@ import ( "log/slog" "time" - "github.com/elastic/go-elasticsearch/v7" + "github.com/elastic/go-elasticsearch/v8" ) type LogProcessor struct { diff --git a/models.go b/models.go index f1adf9f..6138d3b 100644 --- a/models.go +++ b/models.go @@ -5,19 +5,88 @@ import ( ) type SystemResources struct { - Timestamp time.Time `json:"@timestamp"` - Type string `json:"type"` - Host string `json:"host"` - CPUPercent float64 `json:"cpu_percent,omitempty"` - MemoryUsed uint64 `json:"memory_used,omitempty"` - MemoryTotal uint64 `json:"memory_total,omitempty"` - MemoryPercent float64 `json:"memory_percent,omitempty"` - DiskUsage map[string]DiskUsage `json:"disk_usage,omitempty"` - NetworkStats map[string]NetworkStat `json:"network_stats,omitempty"` - LoadAverage []float64 `json:"load_average,omitempty"` - Uptime uint64 `json:"uptime,omitempty"` - TopProcesses []ProcessInfo `json:"top_processes"` - OpenFileDescriptors int32 `json:"open_file_descriptors"` + Timestamp time.Time `json:"@timestamp"` + Type string `json:"type"` + Host string `json:"host"` + CPUPercent float64 `json:"cpu_percent,omitempty"` + MemoryUsed uint64 `json:"memory_used,omitempty"` + MemoryTotal uint64 `json:"memory_total,omitempty"` + MemoryPercent float64 `json:"memory_percent,omitempty"` + DiskUsage map[string]DiskUsage `json:"disk_usage,omitempty"` + NetworkStats map[string]NetworkStat `json:"network_stats,omitempty"` + LoadAverage []float64 `json:"load_average,omitempty"` + Uptime uint64 `json:"uptime,omitempty"` + TopProcesses []ProcessInfo `json:"top_processes"` + OpenFileDescriptors int32 `json:"open_file_descriptors"` + DiskIOStats map[string]DiskIOStat `json:"disk_io_stats,omitempty"` + NetworkLatency map[string]LatencyInfo `json:"network_latency,omitempty"` + BandwidthUtilization map[string]BandwidthInfo `json:"bandwidth_utilization,omitempty"` + NetworkConnections ConnectionStats `json:"network_connections"` + TCPStats TCPStatistics `json:"tcp_stats"` + SystemLimits SystemLimitInfo `json:"system_limits"` +} + +type DiskIOStat struct { + ReadBytes uint64 `json:"read_bytes"` + WriteBytes uint64 `json:"write_bytes"` + ReadOps uint64 `json:"read_ops"` + WriteOps uint64 `json:"write_ops"` + ReadTime uint64 `json:"read_time"` + WriteTime uint64 `json:"write_time"` + IOUtilization float64 `json:"io_utilization"` + AvgReadLatency float64 `json:"avg_read_latency"` + AvgWriteLatency float64 `json:"avg_write_latency"` +} + +type LatencyInfo struct { + Host string `json:"host"` + MinLatency time.Duration `json:"min_latency"` + MaxLatency time.Duration `json:"max_latency"` + AvgLatency time.Duration `json:"avg_latency"` + PacketLoss float64 `json:"packet_loss"` + Jitter time.Duration `json:"jitter"` +} + +type BandwidthInfo struct { + Interface string `json:"interface"` + CurrentThroughputIn float64 `json:"current_throughput_in_mbps"` + CurrentThroughputOut float64 `json:"current_throughput_out_mbps"` + PeakThroughputIn float64 `json:"peak_throughput_in_mbps"` + PeakThroughputOut float64 `json:"peak_throughput_out_mbps"` + UtilizationPercent float64 `json:"utilization_percent"` +} + +type ConnectionStats struct { + TotalConnections int32 `json:"total_connections"` + EstablishedTCP int32 `json:"established_tcp"` + ListeningTCP int32 `json:"listening_tcp"` + TimeWaitTCP int32 `json:"time_wait_tcp"` + TransferConnections int32 `json:"transfer_connections"` + ConnectionsByState map[string]int32 `json:"connections_by_state"` +} + +type LoadAverageInfo struct { + Load1 float64 `json:"load_1min"` + Load5 float64 `json:"load_5min"` + Load15 float64 `json:"load_15min"` +} + +type TCPStatistics struct { + RetransmitRate float64 `json:"retransmit_rate"` + OutOfOrderPackets uint64 `json:"out_of_order_packets"` + LostPackets uint64 `json:"lost_packets"` + CongestionWindow uint64 `json:"congestion_window"` + TCPMemoryUsage uint64 `json:"tcp_memory_usage"` +} + +type SystemLimitInfo struct { + MaxOpenFiles uint64 `json:"max_open_files"` + CurrentOpenFiles uint64 `json:"current_open_files"` + MaxProcesses uint64 `json:"max_processes"` + CurrentProcesses uint64 `json:"current_processes"` + MaxTCPConnections uint64 `json:"max_tcp_connections"` + CurrentTCPConnections uint64 `json:"current_tcp_connections"` + FileDescriptorUsage float64 `json:"file_descriptor_usage_percent"` } type DiskUsage struct { @@ -70,10 +139,13 @@ func NewLogEntry(entryType string) LogEntry { func NewSystemResources() SystemResources { return SystemResources{ - Timestamp: time.Now(), - Type: "system_metrics", - Host: hostname, - DiskUsage: make(map[string]DiskUsage), - NetworkStats: make(map[string]NetworkStat), + Timestamp: time.Now(), + Type: "system_metrics", + Host: hostname, + DiskUsage: make(map[string]DiskUsage), + DiskIOStats: make(map[string]DiskIOStat), + NetworkStats: make(map[string]NetworkStat), + NetworkLatency: make(map[string]LatencyInfo), + BandwidthUtilization: make(map[string]BandwidthInfo), } } diff --git a/service_monitor.go b/service_monitor.go index 1279790..120496a 100644 --- a/service_monitor.go +++ b/service_monitor.go @@ -226,13 +226,19 @@ var ( tjmTransferNamePattern = regexp.MustCompile(`^(\d{8}T\d{6}-[A-Za-z0-9]+-[A-Za-z]+-(?:in|out)) ?: (.*)$`) tsServicePattern = regexp.MustCompile(`^(?\S+)\s+(?