Skip to content

Commit

Permalink
refactor: separate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Siumauricio committed Jan 4, 2025
1 parent f29aae9 commit 35e9259
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 145 deletions.
32 changes: 16 additions & 16 deletions apps/golang/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (

type ServerMetric struct {
Timestamp int64 `json:"timestamp"`
CPU float64 `json:"cpu"`
CPUModel string `json:"cpuModel"`
CPUCores int32 `json:"cpuCores"`
CPU float64 `json:"cpu"`
CPUModel string `json:"cpuModel"`
CPUCores int32 `json:"cpuCores"`
CPUPhysicalCores int32 `json:"cpuPhysicalCores"`
CPUSpeed float64 `json:"cpuSpeed"`
OS string `json:"os"`
Distro string `json:"distro"`
Kernel string `json:"kernel"`
Arch string `json:"arch"`
MemUsed float64 `json:"memUsed"`
MemUsedGB float64 `json:"memUsedGB"`
MemTotal float64 `json:"memTotal"`
Uptime uint64 `json:"uptime"`
DiskUsed float64 `json:"diskUsed"`
TotalDisk float64 `json:"totalDisk"`
NetworkIn float64 `json:"networkIn"`
NetworkOut float64 `json:"networkOut"`
CPUSpeed float64 `json:"cpuSpeed"`
OS string `json:"os"`
Distro string `json:"distro"`
Kernel string `json:"kernel"`
Arch string `json:"arch"`
MemUsed float64 `json:"memUsed"`
MemUsedGB float64 `json:"memUsedGB"`
MemTotal float64 `json:"memTotal"`
Uptime uint64 `json:"uptime"`
DiskUsed float64 `json:"diskUsed"`
TotalDisk float64 `json:"totalDisk"`
NetworkIn float64 `json:"networkIn"`
NetworkOut float64 `json:"networkOut"`
}

type DB struct {
Expand Down
10 changes: 8 additions & 2 deletions apps/golang/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/dokploy/monitoring
module github.com/mauriciogm/dokploy/apps/golang

go 1.21
go 1.20

require (
github.com/gofiber/fiber/v2 v2.52.6
Expand All @@ -11,17 +11,23 @@ require (

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/sys v0.28.0 // indirect
)

replace github.com/mauriciogm/dokploy/apps/golang => ./
35 changes: 11 additions & 24 deletions apps/golang/go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
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/gofiber/fiber/v2 v2.52.0/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ=
github.com/gofiber/fiber/v2 v2.52.6 h1:Rfp+ILPiYSvvVuIPvxrBns+HJp8qGLDnLJawAu27XVI=
github.com/gofiber/fiber/v2 v2.52.6/go.mod h1:YEcBbO/FB+5M1IZNBP9FO3J9281zgPAreiI1oqg8nDw=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
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/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ81pIr0yLvtUWk2if982qA3F3QD6H4=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
Expand All @@ -23,29 +22,21 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/shirou/gopsutil/v3 v3.24.1/go.mod h1:UU7a2MSBQa+kW1uuDq8DeEBS8kmrnQwsv2b5O513rwU=
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU=
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY=
github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
Expand All @@ -54,17 +45,13 @@ github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1S
github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
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=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
110 changes: 7 additions & 103 deletions apps/golang/main.go
Original file line number Diff line number Diff line change
@@ -1,115 +1,18 @@
package main

import (
"fmt"
"log"
"os"
"runtime"
"strconv"
"time"

"github.com/dokploy/monitoring/database"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/joho/godotenv"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
"github.com/shirou/gopsutil/v3/net"
"github.com/mauriciogm/dokploy/apps/golang/database"
"github.com/mauriciogm/dokploy/apps/golang/monitoring"
)

type SystemMetrics struct {
CPU string `json:"cpu"`
CPUModel string `json:"cpuModel"`
CPUCores int32 `json:"cpuCores"`
CPUPhysicalCores int32 `json:"cpuPhysicalCores"`
CPUSpeed float64 `json:"cpuSpeed"`
OS string `json:"os"`
Distro string `json:"distro"`
Kernel string `json:"kernel"`
Arch string `json:"arch"`
MemUsed string `json:"memUsed"`
MemUsedGB string `json:"memUsedGB"`
MemTotal string `json:"memTotal"`
Uptime uint64 `json:"uptime"`
DiskUsed string `json:"diskUsed"`
TotalDisk string `json:"totalDisk"`
NetworkIn string `json:"networkIn"`
NetworkOut string `json:"networkOut"`
Timestamp string `json:"timestamp"`
}

func convertToSystemMetrics(metric database.ServerMetric) SystemMetrics {
return SystemMetrics{
CPU: fmt.Sprintf("%.2f", metric.CPU),
CPUModel: metric.CPUModel,
CPUCores: metric.CPUCores,
CPUPhysicalCores: metric.CPUPhysicalCores,
CPUSpeed: metric.CPUSpeed,
OS: metric.OS,
Distro: metric.Distro,
Kernel: metric.Kernel,
Arch: metric.Arch,
MemUsed: fmt.Sprintf("%.2f", metric.MemUsed),
MemUsedGB: fmt.Sprintf("%.2f", metric.MemUsedGB),
MemTotal: fmt.Sprintf("%.2f", metric.MemTotal),
Uptime: metric.Uptime,
DiskUsed: fmt.Sprintf("%.2f", metric.DiskUsed),
TotalDisk: fmt.Sprintf("%.2f", metric.TotalDisk),
NetworkIn: fmt.Sprintf("%.2f", metric.NetworkIn),
NetworkOut: fmt.Sprintf("%.2f", metric.NetworkOut),
Timestamp: time.Unix(metric.Timestamp, 0).Format(time.RFC3339),
}
}

func getServerMetrics() database.ServerMetric {
v, _ := mem.VirtualMemory()
c, _ := cpu.Percent(0, false)
cpuInfo, _ := cpu.Info()
diskInfo, _ := disk.Usage("/")
netInfo, _ := net.IOCounters(false)
hostInfo, _ := host.Info()

cpuModel := ""
if len(cpuInfo) > 0 {
cpuModel = fmt.Sprintf("%s %s", cpuInfo[0].VendorID, cpuInfo[0].ModelName)
}

// Calcular memoria en GB
memTotalGB := float64(v.Total) / 1024 / 1024 / 1024
memUsedGB := float64(v.Used) / 1024 / 1024 / 1024
memUsedPercent := (memUsedGB / memTotalGB) * 100

// Calcular red en MB
var networkIn, networkOut float64
if len(netInfo) > 0 {
networkIn = float64(netInfo[0].BytesRecv) / 1024 / 1024
networkOut = float64(netInfo[0].BytesSent) / 1024 / 1024
}

return database.ServerMetric{
Timestamp: time.Now().Unix(),
CPU: c[0],
CPUModel: cpuModel,
CPUCores: int32(runtime.NumCPU()),
CPUPhysicalCores: int32(len(cpuInfo)),
CPUSpeed: float64(cpuInfo[0].Mhz),
OS: hostInfo.OS,
Distro: hostInfo.Platform,
Kernel: hostInfo.KernelVersion,
Arch: hostInfo.KernelArch,
MemUsed: memUsedPercent,
MemUsedGB: memUsedGB,
MemTotal: memTotalGB,
Uptime: hostInfo.Uptime,
DiskUsed: float64(diskInfo.UsedPercent),
TotalDisk: float64(diskInfo.Total) / 1024 / 1024 / 1024,
NetworkIn: networkIn,
NetworkOut: networkOut,
}
}

func main() {
godotenv.Load()

Expand Down Expand Up @@ -138,7 +41,7 @@ func main() {
app.Get("/metrics", func(c *fiber.Ctx) error {
limit := c.Query("limit", "50")

var metrics []SystemMetrics
var metrics []monitoring.SystemMetrics
if limit == "all" {
// Get all metrics
dbMetrics, err := db.GetLastNMetrics(10000)
Expand All @@ -148,7 +51,7 @@ func main() {
})
}
for _, m := range dbMetrics {
metrics = append(metrics, convertToSystemMetrics(m))
metrics = append(metrics, monitoring.ConvertToSystemMetrics(m))
}
} else {
// Get limited metrics
Expand All @@ -163,7 +66,7 @@ func main() {
})
}
for _, m := range dbMetrics {
metrics = append(metrics, convertToSystemMetrics(m))
metrics = append(metrics, monitoring.ConvertToSystemMetrics(m))
}
}

Expand All @@ -185,7 +88,8 @@ func main() {
}
ticker := time.NewTicker(duration)
for range ticker.C {
metrics := getServerMetrics()
metrics := monitoring.GetServerMetrics()
log.Printf("Saving metrics: %v", metrics)
if err := db.SaveMetric(metrics); err != nil {
log.Printf("Error saving metrics: %v", err)
}
Expand Down
Binary file modified apps/golang/monitoring.db
Binary file not shown.
Loading

0 comments on commit 35e9259

Please sign in to comment.