Skip to content

Commit

Permalink
working version of metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
linomp committed Aug 6, 2024
1 parent b3acd18 commit 4733afc
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 164 deletions.
19 changes: 3 additions & 16 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
# created by virtualenv automatically
Lib
Scripts
include
lib
local
pyvenv.cfg
share

**/.pytest_cache/

*.pem
*.env


**/__pycache__
*.exe
*.mod
*.sum
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"]
27 changes: 9 additions & 18 deletions src/main.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
package main

import (
"log"
"net/http"
)

type healthcheckHandler struct{}

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

switch {
case r.Method == http.MethodGet:
msg := []byte("hello there")
w.WriteHeader(http.StatusOK)
w.Write(msg)
return
default:
return
}
func GenerateServerStatusHTML(r *http.Request) string {
metrics := getStatus(r)
return generateServerStatusHTML(metrics)
}

func main() {
mux := http.NewServeMux()
mux.Handle("/", &healthcheckHandler{})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
html := GenerateServerStatusHTML(r)
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(html))
})

log.Fatal(http.ListenAndServe("0.0.0.0:8001", mux))
http.ListenAndServe(":8001", nil)
}
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)
}

0 comments on commit 4733afc

Please sign in to comment.