Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

working version of metrics #2

Merged
merged 7 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.22.5'

- name: Build
run: cd src
| go mod tidy
| go build -v *.go
run: cd src && go mod tidy && go build -v
17 changes: 1 addition & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
# created by virtualenv automatically
Lib
Scripts
include
lib
local
pyvenv.cfg
share

**/.pytest_cache/

*.pem
*.env


**/__pycache__
*.exe
6 changes: 5 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ WORKDIR /app

COPY ./src /app

CMD ["go", "run", "main.go"]
RUN go mod tidy

RUN go build -o main main.go utils.go

CMD ["./main"]
14 changes: 14 additions & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module server

go 1.22.5

require github.com/shirou/gopsutil v3.21.11+incompatible

require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/sys v0.23.0 // indirect
)
21 changes: 21 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
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.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY=
github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE=
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.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM=
golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18 changes: 10 additions & 8 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
"net/http"
)

type healthcheckHandler struct{}

func (h *healthcheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "text/plain")
type metricsHandler struct {
}

func (h *metricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
switch {
case r.Method == http.MethodGet:
msg := []byte("hello there")
w.WriteHeader(http.StatusOK)
w.Write(msg)
metrics := getStatus(r)
html := generateServerStatusHTML(metrics)
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(html))
return
default:
return
Expand All @@ -23,7 +24,8 @@ func (h *healthcheckHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

func main() {
mux := http.NewServeMux()
mux.Handle("/", &healthcheckHandler{})

mux.Handle("/", &metricsHandler{})

log.Fatal(http.ListenAndServe("0.0.0.0:8001", mux))
}
129 changes: 0 additions & 129 deletions src/old_code.py

This file was deleted.

148 changes: 148 additions & 0 deletions src/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package main

import (
"fmt"
"net"
"net/http"
"time"

"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/mem"
)

type ServerMetrics struct {
Host string
Timestamp string
CPUUsage string
MemoryUsage string
}

func getStatus(r *http.Request) ServerMetrics {
// Get the client IP address
ip, _, _ := net.SplitHostPort(r.RemoteAddr)

// Get the current timestamp in ISO 8601 format
time := time.Now().UTC().Format(time.RFC3339)

// Get CPU and Memory usage
cpuUsage, _ := cpu.Percent(0, false)
memoryUsage, _ := mem.VirtualMemory()

return ServerMetrics{
Host: ip,
Timestamp: time,
CPUUsage: fmt.Sprintf("%.2f %%", cpuUsage[0]),
MemoryUsage: fmt.Sprintf("%.2f %%", memoryUsage.UsedPercent),
}
}

func formatTimestamp(timestamp string) string {
return timestamp[11:19] + " (UTC)"
}

func generateServerStatusHTML(metrics ServerMetrics) string {
return fmt.Sprintf(`
<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=UTF-8>
<meta name=viewport content=width=device-width, initial-scale=1.0>
<style>
@import url('https://fonts.googleapis.com/css?family=Open+Sans&display=swap');
html {
position: relative;
overflow-x: hidden !important;
}
* {
box-sizing: border-box;
}
body {
font-family: 'Open Sans', sans-serif;
color: #324e63;
}
a {
color: inherit;
text-decoration: inherit;
}
.wrapper {
width: 100%%;
width: 100%%;
height: auto;
min-height: 90vh;
padding: 50px 20px;
padding-top: 100px;
display: flex;
}
.instance-card {
width: 100%%;
min-height: 380px;
margin: auto;
box-shadow: 12px 12px 2px 1px rgba(13, 28, 39, 0.4);
background: #fff;
border-radius: 15px;
border-width: 1px;
max-width: 500px;
position: relative;
border: thin groove #9c83ff;
}
.instance-card__cnt {
margin-top: 35px;
text-align: center;
padding: 0 20px;
padding-bottom: 40px;
transition: all .3s;
}
.instance-card__name {
font-weight: 700;
font-size: 24px;
color: #6944ff;
margin-bottom: 15px;
}
.instance-card-inf__item {
padding: 10px 35px;
min-width: 150px;
}
.instance-card-inf__title {
font-weight: 700;
font-size: 27px;
color: #324e63;
}
.instance-card-inf__txt {
font-weight: 500;
margin-top: 7px;
}
.secondary {
color: #9c83ff;
}
</style>
<title>😈️ pointless-status 😈</title>
</head>
<body>
<div class=wrapper>
<div class=instance-card>
<div class=instance-card__cnt>
<div class=instance-card__name>😈️ Server is running! 😈️</div>
<div class=instance-card-inf>
<div class=instance-card-inf__item>
<div class=instance-card-inf__txt>Client</div>
<div class=instance-card-inf__title>%s</div>
</div>
<div class=instance-card-inf__item>
<div class=instance-card-inf__txt>Time</div>
<div class=instance-card-inf__title>%s</div>
</div>
<div class=instance-card-inf__item>
<div class=instance-card-inf__txt>CPU Usage</div>
<div class=instance-card-inf__title>%s</div>
</div>
<div class=instance-card-inf__item>
<div class=instance-card-inf__txt>Memory usage</div>
<div class=instance-card-inf__title>%s</div>
</div>
</div>
</div>
</div>
</body>
</html>
`, metrics.Host, formatTimestamp(metrics.Timestamp), metrics.CPUUsage, metrics.MemoryUsage)
}
Loading