update agent http client + docker compose

This commit is contained in:
Henry Dollman
2024-07-21 16:49:41 -04:00
parent 1b7724017d
commit e1ef111077
2 changed files with 17 additions and 32 deletions

View File

@@ -7,5 +7,6 @@ services:
volumes: volumes:
- /var/run/docker.sock:/var/run/docker.sock - /var/run/docker.sock:/var/run/docker.sock
environment: environment:
- FILESYSTEM=/dev/sda1 # Set to the correct filesystem for disk I/O stats - PORT=45876
- KEY="ssh-ed25519 YOUR_PUBLIC_KEY" - KEY="ssh-ed25519 YOUR_PUBLIC_KEY"
# - FILESYSTEM=/dev/sda1 # set to the correct filesystem for disk I/O stats

View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"context"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -43,14 +42,18 @@ var netIoStats = NetIoStats{
Name: "", Name: "",
} }
// Create a custom HTTP transport // client for docker engine api
var transport = &http.Transport{ var client = &http.Client{
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { Timeout: time.Second * 5,
d := net.Dialer{ Transport: &http.Transport{
Timeout: 5 * time.Second, Dial: func(proto, addr string) (net.Conn, error) {
} return net.Dial("unix", "/var/run/docker.sock")
// Connect to the Unix socket },
return d.DialContext(ctx, "unix", "/var/run/docker.sock") ForceAttemptHTTP2: false,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
DisableKeepAlives: false,
}, },
} }
@@ -180,16 +183,7 @@ func getSystemStats() (SystemInfo, SystemStats) {
} }
func getDockerStats() ([]ContainerStats, error) { func getDockerStats() ([]ContainerStats, error) {
client := &http.Client{ resp, err := client.Get("http://localhost/containers/json")
Transport: transport,
}
// Create a new HTTP request
req, err := http.NewRequest("GET", "http://localhost/containers/json", nil)
if err != nil {
return []ContainerStats{}, err
}
// Perform the request
resp, err := client.Do(req)
if err != nil { if err != nil {
return []ContainerStats{}, err return []ContainerStats{}, err
} }
@@ -233,17 +227,7 @@ func getDockerStats() ([]ContainerStats, error) {
} }
func getContainerStats(ctr Container) (ContainerStats, error) { func getContainerStats(ctr Container) (ContainerStats, error) {
// stats, _ := apiClient.ContainerStats(context.Background(), ctr.ID, false) resp, err := client.Get("http://localhost/containers/" + ctr.ID + "/stats?stream=0&one-shot=1")
client := &http.Client{
Transport: transport,
}
// Create a new HTTP request
req, err := http.NewRequest("GET", "http://localhost/containers/"+ctr.ID+"/stats?stream=0&one-shot=1", nil)
if err != nil {
return ContainerStats{}, err
}
// Perform the request
resp, err := client.Do(req)
if err != nil { if err != nil {
return ContainerStats{}, err return ContainerStats{}, err
} }
@@ -279,7 +263,7 @@ func getContainerStats(ctr Container) (ContainerStats, error) {
Name: name, Name: name,
Cpu: twoDecimals(cpuPct), Cpu: twoDecimals(cpuPct),
Mem: bytesToMegabytes(float64(usedMemory)), Mem: bytesToMegabytes(float64(usedMemory)),
// MemPct: maxDecimals(pctMemory, 2), // MemPct: twoDecimals(pctMemory),
} }
return cStats, nil return cStats, nil
} }