forked from jun283/alita
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.go
128 lines (117 loc) · 2.73 KB
/
host.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"encoding/json"
"fmt"
"net/http"
"os/exec"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
type StatusServer struct {
HostInfoStat *host.InfoStat
Percent StatusPercent
CPU []CPUInfo
Mem MemInfo
Swap SwapInfo
Load *load.AvgStat
Network map[string]InterfaceInfo
}
type StatusPercent struct {
CPU float64
Disk float64
Mem float64
Swap float64
}
type CPUInfo struct {
ModelName string
Cores int32
}
type MemInfo struct {
Total uint64
Used uint64
Available uint64
}
type SwapInfo struct {
Total uint64
Used uint64
Available uint64
}
type InterfaceInfo struct {
Addrs []string
ByteSent uint64
ByteRecv uint64
}
func getHostInfoHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, hostInfoMiniJSON())
}
func hostInfoMiniJSON() string {
v, _ := mem.VirtualMemory()
s, _ := mem.SwapMemory()
c, _ := cpu.Info()
cc, _ := cpu.Percent(time.Second, false)
d, _ := disk.Usage("/")
//n, _ := host.Info()
nv, _ := net.IOCounters(true)
l, _ := load.Avg()
i, _ := net.Interfaces()
ss := new(StatusServer)
ss.HostInfoStat, _ = host.Info()
ss.Load = l
ss.Percent.Mem = v.UsedPercent
ss.Percent.CPU = cc[0]
ss.Percent.Swap = s.UsedPercent
ss.Percent.Disk = d.UsedPercent
ss.CPU = make([]CPUInfo, len(c))
for i, ci := range c {
ss.CPU[i].ModelName = ci.ModelName
ss.CPU[i].Cores = ci.Cores
}
ss.Mem.Total = v.Total
ss.Mem.Available = v.Available
ss.Mem.Used = v.Used
ss.Swap.Total = s.Total
ss.Swap.Available = s.Free
ss.Swap.Used = s.Used
ss.Network = make(map[string]InterfaceInfo)
for _, v := range nv {
var ii InterfaceInfo
ii.ByteSent = v.BytesSent
ii.ByteRecv = v.BytesRecv
ss.Network[v.Name] = ii
}
for _, v := range i {
if ii, ok := ss.Network[v.Name]; ok {
ii.Addrs = make([]string, len(v.Addrs))
for i, vv := range v.Addrs {
ii.Addrs[i] = vv.Addr
}
ss.Network[v.Name] = ii
}
}
b, err := json.Marshal(ss)
if err != nil {
return ""
} else {
return string(b)
}
}
func updateHostInfoHandler(w http.ResponseWriter, r *http.Request) {
//hostnamectl set-hostname 'newname'
//count, _ := strconv.Atoi(r.FormValue("count"))
name := r.FormValue("hostname")
cmd := exec.Command("/usr/bin/hostnamectl", "set-hostname", name)
out, err := cmd.CombinedOutput()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
fmt.Fprintf(w, string(out))
}