You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.4 KiB
107 lines
3.4 KiB
6 months ago
|
package service
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"rition-probe/client"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func (s *Service) Run() {
|
||
|
ticker := time.NewTicker(s.config.ReportInterval)
|
||
|
defer ticker.Stop()
|
||
|
|
||
|
for range ticker.C {
|
||
|
s.Report()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *Service) Report() {
|
||
|
dataItems := make([]client.DataItem, 0)
|
||
|
nowTime := time.Now()
|
||
|
now := nowTime.UnixMilli()
|
||
|
|
||
|
// cpu
|
||
|
_5minCPULoad := s.prevCpuLoadData
|
||
|
if nowTime.Sub(s.prevCpuLoadDataCollectTime).Minutes() >= 5 {
|
||
|
_5minCPULoad = s.probe.CurrentCPUTotalPercent()
|
||
|
}
|
||
|
dataItems = append(dataItems, client.DataItem{
|
||
|
Metric: "node_load5",
|
||
|
Value: _5minCPULoad,
|
||
|
Timestamp: now,
|
||
|
})
|
||
|
|
||
|
sysUptime := s.probe.HostUptime()
|
||
|
dataItems = append(dataItems, client.DataItem{
|
||
|
Metric: "node_cpu_seconds_total",
|
||
|
Value: sysUptime,
|
||
|
Timestamp: now,
|
||
|
})
|
||
|
|
||
|
// mem
|
||
|
memTotal, memUsed, memBuffers, memCached := s.probe.RamUsage()
|
||
|
dataItems = append(dataItems, client.DataItem{
|
||
|
Metric: "node_memory_MemTotal_bytes", Value: memTotal, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_memory_MemFree_bytes", Value: memTotal - memUsed, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_memory_Buffers_bytes", Value: memBuffers, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_memory_Cached_bytes", Value: memCached, Timestamp: now,
|
||
|
})
|
||
|
|
||
|
// disk
|
||
|
usageStat, diskIoCount := s.probe.DiskStatus(s.config.MonitorDiskMountPoint, s.config.MonitorDiskDevice)
|
||
|
dataItems = append(dataItems, client.DataItem{
|
||
|
Metric: "node_filesystem_avail_bytes", Value: usageStat.Free, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_filesystem_size_bytes", Value: usageStat.Total, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_filesystem_free_bytes", Value: usageStat.Free, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_disk_read_bytes_total", Value: diskIoCount.ReadBytes, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_disk_written_bytes_total", Value: diskIoCount.WriteBytes, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_disk_reads_completed_total", Value: diskIoCount.MergedWriteCount, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_disk_writes_completed_total", Value: diskIoCount.MergedWriteCount, Timestamp: now,
|
||
|
})
|
||
|
|
||
|
// network
|
||
|
networkIoCount, conns := s.probe.GetNetworkCounterOne(s.config.MonitorNetworkInterface)
|
||
|
dataItems = append(dataItems, client.DataItem{
|
||
|
Metric: "node_network_receive_bytes_total", Value: networkIoCount.BytesRecv, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_network_transmit_bytes_total", Value: networkIoCount.BytesSent, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_network_receive_packets_total", Value: networkIoCount.PacketsRecv, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_network_transmit_packets_total", Value: networkIoCount.PacketsSent, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_network_receive_drop_total", Value: networkIoCount.Dropin, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_network_transmit_drop_total", Value: networkIoCount.Dropout, Timestamp: now,
|
||
|
})
|
||
|
|
||
|
estabCnt, twCnt := 0, 0
|
||
|
for _, conn := range conns {
|
||
|
switch conn.Status {
|
||
|
case "ESTABLISHED":
|
||
|
estabCnt++
|
||
|
case "TIME_WAIT":
|
||
|
twCnt++
|
||
|
}
|
||
|
}
|
||
|
dataItems = append(dataItems, client.DataItem{
|
||
|
Metric: "node_netstat_Tcp_CurrEstab", Value: estabCnt, Timestamp: now,
|
||
|
}, client.DataItem{
|
||
|
Metric: "node_netstat_Tcp_tw", Value: twCnt, Timestamp: now,
|
||
|
})
|
||
|
|
||
|
_, err := s.client.Report(dataItems)
|
||
|
if err != nil {
|
||
|
fmt.Println("err: ", err.Error())
|
||
|
}
|
||
|
}
|