-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (72 loc) · 1.85 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
package main
import (
"fmt"
"github.com/cloudfoundry/gosigar"
"github.com/mitchellh/go-ps"
"html/template"
"log"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", htmlHandler)
http.HandleFunc("/api", apiHandler)
// サーバーを起動
// http://localhost:8989
http.ListenAndServe(":8989", nil)
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
processes, err := ps.Processes()
if err != nil {
log.Fatal(err)
}
// 重複削除
m := make(map[string]bool)
uniq := []ps.Process{}
for _, ele := range processes {
if !m[ele.Executable()] {
m[ele.Executable()] = true
uniq = append(uniq, ele)
}
}
// メモリ使用量(全体)
mem := sigar.Mem{}
mem.Get()
memAll := fmt.Sprintf("total=%.2f [GB],", formatGB(mem.Total))
memAll = memAll + fmt.Sprintf(" used=%.2f [GB],", formatGB(mem.Used))
memAll = memAll + fmt.Sprintf(" free=%.2f [GB]", formatGB(mem.Free))
message := memAll + "<br><br>"
for _, p := range uniq {
if p.Pid() == 0 {
continue
}
mem := sigar.ProcMem{}
if err := mem.Get(p.Pid()); err != nil {
continue
}
message += p.Executable() + " : " + fmt.Sprintf("%.2f", float64(mem.Size)/1024/1024) + "[MB]<br>"
}
fmt.Fprintln(w, message)
}
func htmlHandler(w http.ResponseWriter, r *http.Request) {
// ホスト名の取得
name, err := os.Hostname()
if err != nil {
log.Fatal(err)
}
// テンプレートをパース
t := template.Must(template.ParseFiles("templates/template.html.tpl"))
// HTMLに入れるオブジェクト
dat := struct {
HostName string
}{
HostName: name,
}
// テンプレートを描画
if err := t.ExecuteTemplate(w, "template.html.tpl", dat); err != nil {
log.Fatal(err)
}
}
func formatGB(val uint64) float64 {
return float64(val) / 1024 / 1024 / 1024
}