Skip to content

Commit

Permalink
refactor: update
Browse files Browse the repository at this point in the history
  • Loading branch information
Siumauricio committed Jan 4, 2025
1 parent 3a4bd97 commit a044568
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/golang/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
github.com/zcalusic/sysinfo v1.1.3 // indirect
golang.org/x/sys v0.28.0 // indirect
)

Expand Down
2 changes: 2 additions & 0 deletions apps/golang/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVS
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
github.com/zcalusic/sysinfo v1.1.3 h1:u/AVENkuoikKuIZ4sUEJ6iibpmQP6YpGD8SSMCrqAF0=
github.com/zcalusic/sysinfo v1.1.3/go.mod h1:NX+qYnWGtJVPV0yWldff9uppNKU4h40hJIRPf/pGLv4=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
34 changes: 31 additions & 3 deletions apps/golang/monitoring/monitor.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package monitoring

import (
"encoding/json"
"fmt"
"log"
"os/user"
"runtime"
"time"

Expand All @@ -11,6 +13,7 @@ import (
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
"github.com/zcalusic/sysinfo"

"github.com/mauriciogm/dokploy/apps/golang/database"
)
Expand All @@ -36,6 +39,30 @@ type SystemMetrics struct {
Timestamp string `json:"timestamp"`
}

func getRealOS() string {
current, err := user.Current()
if err != nil {
log.Fatal(err)
}

if current.Uid != "0" {
log.Fatal("requires superuser privilege")
}

var si sysinfo.SysInfo

si.GetSysInfo()

data, err := json.MarshalIndent(&si, "", " ")
if err != nil {
log.Fatal(err)
}

fmt.Println(string(data))

return si.OS.Name
}

func GetServerMetrics() database.ServerMetric {
v, _ := mem.VirtualMemory()
c, _ := cpu.Percent(0, false)
Expand All @@ -44,6 +71,8 @@ func GetServerMetrics() database.ServerMetric {
netInfo, _ := net.IOCounters(false)
hostInfo, _ := host.Info()

log.Print("CPU: ", getRealOS())

// CPU
// Apple M1 Pro

Expand Down Expand Up @@ -80,14 +109,13 @@ func GetServerMetrics() database.ServerMetric {
networkIn = float64(netInfo[0].BytesRecv) / 1024 / 1024
networkOut = float64(netInfo[0].BytesSent) / 1024 / 1024
}
log.Printf("Host Info: %v, Network In: %f MB, Network Out: %f MB", hostInfo, networkIn, networkOut)
log.Printf("CPU Info: %v,", runtime.GOOS)
// log.Printf("Host Info: %v, Network In: %f MB, Network Out: %f MB", hostInfo, networkIn, networkOut)
return database.ServerMetric{
Timestamp: time.Now().Unix(),
CPU: c[0],
CPUModel: cpuModel,
CPUCores: int32(runtime.NumCPU()),
CPUPhysicalCores: int32(runtime.NumCPU()), // En Apple Silicon, los cores físicos son iguales a los lógicos
CPUPhysicalCores: int32(len(cpuInfo)), // En Apple Silicon, los cores físicos son iguales a los lógicos
CPUSpeed: float64(cpuInfo[0].Mhz),
OS: hostInfo.OS,
Distro: hostInfo.Platform,
Expand Down

0 comments on commit a044568

Please sign in to comment.