From 4733afc5ffc1952fd8f982c97227216c6c3a7992 Mon Sep 17 00:00:00 2001 From: linomp Date: Tue, 6 Aug 2024 19:22:22 +0200 Subject: [PATCH] working version of metrics --- .gitignore | 19 +------ Dockerfile | 6 +- src/main.go | 27 +++------ src/old_code.py | 129 ----------------------------------------- src/utils.go | 148 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 165 insertions(+), 164 deletions(-) delete mode 100644 src/old_code.py create mode 100644 src/utils.go diff --git a/.gitignore b/.gitignore index bd3f8bc..c0aacec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,16 +1,3 @@ -# created by virtualenv automatically -Lib -Scripts -include -lib -local -pyvenv.cfg -share - -**/.pytest_cache/ - -*.pem -*.env - - -**/__pycache__ \ No newline at end of file +*.exe +*.mod +*.sum \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 98a939c..8878dcc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,4 +4,8 @@ WORKDIR /app COPY ./src /app -CMD ["go", "run", "main.go"] \ No newline at end of file +RUN go mod tidy + +RUN go build -o main main.go utils.go + +CMD ["./main"] diff --git a/src/main.go b/src/main.go index d184fea..c0975b0 100644 --- a/src/main.go +++ b/src/main.go @@ -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) } diff --git a/src/old_code.py b/src/old_code.py deleted file mode 100644 index 87b27c6..0000000 --- a/src/old_code.py +++ /dev/null @@ -1,129 +0,0 @@ -class ServerMetrics(BaseModel): - host: str - timestamp: str - cpu_usage: str - memory_usage: str - - -def get_status(request: Request) -> ServerMetrics: - time = datetime.datetime.now(datetime.timezone.utc).isoformat() - - cpu_usage = psutil.cpu_percent() - memory_usage = psutil.virtual_memory().percent - - return ServerMetrics(host=request.client.host, timestamp=time, cpu_usage=f"{cpu_usage} %", - memory_usage=f"{memory_usage} %") - - -def format_timestamp(timestamp): - return timestamp[11:19] + " (UTC)" - - -def generate_server_status_html(metrics: ServerMetrics) -> str: - return f""" - - - - - - - - 😈️ pointless-status 😈 - - -
-
-
-
😈️ Server is running! 😈️
-
-
-
Client
-
{metrics.host}
-
-
-
Time
-
{format_timestamp(metrics.timestamp)}
-
-
-
CPU Usage
-
{metrics.cpu_usage}
-
-
-
Memory usage
-
{metrics.memory_usage}
-
-
-
- -
-
-
-
- - - """ diff --git a/src/utils.go b/src/utils.go new file mode 100644 index 0000000..da590dd --- /dev/null +++ b/src/utils.go @@ -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(` + + + + + + + 😈️ pointless-status 😈 + + +
+
+
+
😈️ Server is running! 😈️
+
+
+
Client
+
%s
+
+
+
Time
+
%s
+
+
+
CPU Usage
+
%s
+
+
+
Memory usage
+
%s
+
+
+
+
+ + + `, metrics.Host, formatTimestamp(metrics.Timestamp), metrics.CPUUsage, metrics.MemoryUsage) +}