-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (93 loc) · 2.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/v4/mem"
)
const (
colorReset = "\033[0m"
colorRed = "\033[31m"
colorYellow = "\033[33m"
colorCyan = "\033[36m"
)
func colorLog(level, message string) {
var color string
switch level {
case "INFO":
color = colorCyan
case "WARN":
color = colorYellow
case "ERROR":
color = colorRed
default:
color = colorReset
}
log.Printf("%s[%s] %s%s", color, level, message, colorReset)
}
func main() {
http.HandleFunc("/events", sseHandler)
colorLog("INFO", "Server started and listening on port :8080")
if err := http.ListenAndServe(":8080", nil); err != nil {
colorLog("ERROR", fmt.Sprintf("unable to start server: %s", err.Error()))
}
}
func sseHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")
memT := time.NewTicker(time.Second)
defer memT.Stop()
cpuT := time.NewTicker(time.Second)
defer cpuT.Stop()
clientGone := r.Context().Done()
rc := http.NewResponseController(w)
for {
select {
case <-clientGone:
colorLog("INFO", fmt.Sprintf("client has disconnected"))
return
case <-memT.C:
m, err := mem.VirtualMemory()
if err != nil {
colorLog("ERROR", fmt.Sprintf("unable to get mem: %s", err.Error()))
return
}
if _, err := fmt.Fprintf(w, "event:memTotal\ndata:%d\n\n", m.Total); err != nil {
colorLog("ERROR", fmt.Sprintf("unable to write memTotal: %s", err.Error()))
return
}
if _, err := fmt.Fprintf(w, "event:memUsed\ndata:%d\n\n", m.Used); err != nil {
colorLog("ERROR", fmt.Sprintf("unable to write memUsed: %s", err.Error()))
return
}
if _, err := fmt.Fprintf(w, "event:memPerc\ndata:%.2f%%\n\n", m.UsedPercent); err != nil {
colorLog("ERROR", fmt.Sprintf("unable to write memPerc: %s", err.Error()))
return
}
rc.Flush()
case <-cpuT.C:
c, err := cpu.Times(false)
if err != nil {
colorLog("ERROR", fmt.Sprintf("unable to get cpu: %s", err.Error()))
return
}
if _, err := fmt.Fprintf(w, "event:cpuUser\ndata:%.2f\n\n", c[0].User); err != nil {
colorLog("ERROR", fmt.Sprintf("unable to write cpuUser: %s", err.Error()))
return
}
if _, err := fmt.Fprintf(w, "event:cpuSys\ndata:%.2f\n\n", c[0].System); err != nil {
colorLog("ERROR", fmt.Sprintf("unable to write cpuSys: %s", err.Error()))
return
}
if _, err := fmt.Fprintf(w, "event:cpuIdle\ndata:%.2f\n\n", c[0].Idle); err != nil {
colorLog("ERROR", fmt.Sprintf("unable to write cpuIdle: %s", err.Error()))
return
}
rc.Flush()
}
}
}