-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (81 loc) · 2.11 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
package main
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
"github.com/valyala/fasthttp"
)
var start time.Time
var hostname string
func init() {
hostname, _ = os.Hostname()
}
func main() {
port, ok := os.LookupEnv("PORT")
if !ok {
port = "80"
}
if err := fasthttp.ListenAndServe(fmt.Sprintf(":%s", port), handler); err != nil {
log.Fatalf("Error in ListenAndServe: %v", err)
}
}
func handler(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/echo":
ctx.WriteString(hostname)
default:
w := ctx.Response.BodyWriter()
cinfo, _ := cpu.Info()
vm, _ := mem.VirtualMemory()
host, _ := host.Info()
fmt.Fprintln(w, "Environ \t "+strings.Join(os.Environ(), "-"))
fmt.Fprintln(w, "Hostname \t "+hostname)
fmt.Fprintln(w, "Headers")
ctx.Request.Header.VisitAll(func(key, value []byte) {
fmt.Fprintln(w, "\t \t", string(key), string(value))
})
fmt.Fprintln(w, "Request.Addr \t "+ctx.RemoteAddr().String())
fmt.Fprintln(w, "RequestURI \t "+ctx.URI().String())
fmt.Fprintln(w, "Uptime \t\t "+time.Now().Sub(start).String())
fmt.Fprintf(w, "Memory \t\t total %s available %s used %s free %s percent %%%f \t\t \n",
byteCountSI(vm.Total),
byteCountSI(vm.Available),
byteCountSI(vm.Used),
byteCountSI(vm.Free),
vm.UsedPercent)
fmt.Fprintf(w, "Swap \t\t total %s free %s cached %s\n",
byteCountSI(vm.SwapTotal),
byteCountSI(vm.SwapFree),
byteCountSI(vm.SwapCached))
for _, cpu := range cinfo {
fmt.Fprintf(w, "Cpu \t\t model %s core %d mhz %f\n",
cpu.ModelName,
cpu.Cores,
cpu.Mhz)
}
fmt.Fprintf(w, "Host \t\t id %s name %s os %s vrole %s vsystem %s\n",
host.HostID,
host.Hostname,
host.OS,
host.VirtualizationRole,
host.VirtualizationSystem)
}
}
func byteCountSI(b uint64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}