Skip to content

Commit

Permalink
refactoring agent
Browse files Browse the repository at this point in the history
  • Loading branch information
henrygd committed Jul 24, 2024
1 parent 41df7b7 commit 0355d9c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 52 deletions.
65 changes: 13 additions & 52 deletions agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,46 +56,7 @@ var client = &http.Client{
},
}

type SystemData struct {
Stats SystemStats `json:"stats"`
Info SystemInfo `json:"info"`
Containers []ContainerStats `json:"container"`
}

type SystemInfo struct {
Cores int `json:"c"`
Threads int `json:"t"`
CpuModel string `json:"m"`
// Os string `json:"o"`
Uptime uint64 `json:"u"`
Cpu float64 `json:"cpu"`
MemPct float64 `json:"mp"`
DiskPct float64 `json:"dp"`
}

type SystemStats struct {
Cpu float64 `json:"cpu"`
Mem float64 `json:"m"`
MemUsed float64 `json:"mu"`
MemPct float64 `json:"mp"`
MemBuffCache float64 `json:"mb"`
Disk float64 `json:"d"`
DiskUsed float64 `json:"du"`
DiskPct float64 `json:"dp"`
DiskRead float64 `json:"dr"`
DiskWrite float64 `json:"dw"`
NetworkSent float64 `json:"ns"`
NetworkRecv float64 `json:"nr"`
}

type ContainerStats struct {
Name string `json:"n"`
Cpu float64 `json:"c"`
Mem float64 `json:"m"`
// MemPct float64 `json:"mp"`
}

func getSystemStats() (SystemInfo, SystemStats) {
func getSystemStats() (*SystemInfo, *SystemStats) {
c, _ := cpu.Percent(0, false)
v, _ := mem.VirtualMemory()
d, _ := disk.Usage("/")
Expand All @@ -104,7 +65,7 @@ func getSystemStats() (SystemInfo, SystemStats) {
memPct := twoDecimals(v.UsedPercent)
diskPct := twoDecimals(d.UsedPercent)

systemStats := SystemStats{
systemStats := &SystemStats{
Cpu: cpuPct,
Mem: bytesToGigabytes(v.Total),
MemUsed: bytesToGigabytes(v.Used),
Expand All @@ -115,7 +76,7 @@ func getSystemStats() (SystemInfo, SystemStats) {
DiskPct: diskPct,
}

systemInfo := SystemInfo{
systemInfo := &SystemInfo{
Cpu: cpuPct,
MemPct: memPct,
DiskPct: diskPct,
Expand Down Expand Up @@ -181,10 +142,10 @@ func getSystemStats() (SystemInfo, SystemStats) {

}

func getDockerStats() ([]ContainerStats, error) {
func getDockerStats() ([]*ContainerStats, error) {
resp, err := client.Get("http://localhost/containers/json")
if err != nil {
return []ContainerStats{}, err
return []*ContainerStats{}, err
}
defer resp.Body.Close()

Expand All @@ -193,7 +154,7 @@ func getDockerStats() ([]ContainerStats, error) {
panic(err)
}

var containerStats []ContainerStats
containerStats := make([]*ContainerStats, 0, len(containers))

for _, ctr := range containers {
ctr.IdShort = ctr.ID[:12]
Expand Down Expand Up @@ -223,10 +184,10 @@ func getDockerStats() ([]ContainerStats, error) {
return containerStats, nil
}

func getContainerStats(ctr *Container) (ContainerStats, error) {
func getContainerStats(ctr *Container) (*ContainerStats, error) {
resp, err := client.Get("http://localhost/containers/" + ctr.IdShort + "/stats?stream=0&one-shot=1")
if err != nil {
return ContainerStats{}, err
return &ContainerStats{}, err
}
defer resp.Body.Close()

Expand All @@ -252,11 +213,11 @@ func getContainerStats(ctr *Container) (ContainerStats, error) {
systemDelta := statsJson.CPUStats.SystemUsage - containerCpuMap[ctr.IdShort][1]
cpuPct := float64(cpuDelta) / float64(systemDelta) * 100
if cpuPct > 100 {
return ContainerStats{}, fmt.Errorf("%s cpu pct greater than 100: %+v", name, cpuPct)
return &ContainerStats{}, fmt.Errorf("%s cpu pct greater than 100: %+v", name, cpuPct)
}
containerCpuMap[ctr.IdShort] = [2]uint64{statsJson.CPUStats.CPUUsage.TotalUsage, statsJson.CPUStats.SystemUsage}

cStats := ContainerStats{
cStats := &ContainerStats{
Name: name,
Cpu: twoDecimals(cpuPct),
Mem: bytesToMegabytes(float64(usedMemory)),
Expand All @@ -265,12 +226,12 @@ func getContainerStats(ctr *Container) (ContainerStats, error) {
return cStats, nil
}

func gatherStats() SystemData {
func gatherStats() *SystemData {
systemInfo, systemStats := getSystemStats()
stats := SystemData{
stats := &SystemData{
Stats: systemStats,
Info: systemInfo,
Containers: []ContainerStats{},
Containers: []*ContainerStats{},
}
containerStats, err := getDockerStats()
if err == nil {
Expand Down
39 changes: 39 additions & 0 deletions agent/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,45 @@ package main

import "time"

type SystemData struct {
Stats *SystemStats `json:"stats"`
Info *SystemInfo `json:"info"`
Containers []*ContainerStats `json:"container"`
}

type SystemInfo struct {
Cores int `json:"c"`
Threads int `json:"t"`
CpuModel string `json:"m"`
// Os string `json:"o"`
Uptime uint64 `json:"u"`
Cpu float64 `json:"cpu"`
MemPct float64 `json:"mp"`
DiskPct float64 `json:"dp"`
}

type SystemStats struct {
Cpu float64 `json:"cpu"`
Mem float64 `json:"m"`
MemUsed float64 `json:"mu"`
MemPct float64 `json:"mp"`
MemBuffCache float64 `json:"mb"`
Disk float64 `json:"d"`
DiskUsed float64 `json:"du"`
DiskPct float64 `json:"dp"`
DiskRead float64 `json:"dr"`
DiskWrite float64 `json:"dw"`
NetworkSent float64 `json:"ns"`
NetworkRecv float64 `json:"nr"`
}

type ContainerStats struct {
Name string `json:"n"`
Cpu float64 `json:"c"`
Mem float64 `json:"m"`
// MemPct float64 `json:"mp"`
}

type Container struct {
ID string `json:"Id"`
IdShort string
Expand Down

0 comments on commit 0355d9c

Please sign in to comment.