-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.go
217 lines (203 loc) · 4.88 KB
/
info.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package server
import (
"runtime"
"strings"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
"github.com/shirou/gopsutil/process"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/admpub/log"
collectd "github.com/admpub/logcool/input/collectd"
"github.com/admpub/nging/application/library/system"
)
var _ = collectd.SystemInfo
func Info(ctx echo.Context) error {
var err error
cpuInfo, err := cpu.Info()
if err != nil {
log.Error(err)
}
partitions, err := disk.Partitions(false)
if err != nil {
log.Error(err)
}
/*
ioCounter, err := disk.IOCounters()
if err != nil {
log.Error(err)
}
*/
hostInfo, err := host.Info()
if err != nil {
log.Error(err)
}
/*
avgLoad, err := load.Avg()
if err != nil {
log.Error(err)
}
*/
virtualMem, err := mem.VirtualMemory()
if err != nil {
log.Error(err)
}
swapMem, err := mem.SwapMemory()
if err != nil {
log.Error(err)
}
if swapMem.UsedPercent == 0 {
swapMem.UsedPercent = (float64(swapMem.Used) / float64(swapMem.Total)) * 100
}
netIOCounter, err := net.IOCounters(false)
if err != nil {
log.Error(err)
}
cpuPercent, err := cpu.Percent(0, false)
if err != nil {
log.Error(err)
}
info := &system.SystemInformation{
CPU: cpuInfo,
CPUPercent: cpuPercent,
Partitions: partitions,
//DiskIO: ioCounter,
Host: hostInfo,
//Load: avgLoad,
Memory: &system.MemoryInformation{Virtual: virtualMem, Swap: swapMem},
NetIO: netIOCounter,
}
info.DiskUsages = make([]*disk.UsageStat, len(info.Partitions))
for k, v := range info.Partitions {
usageStat, err := disk.Usage(v.Mountpoint)
if err != nil {
log.Error(err)
}
info.DiskUsages[k] = usageStat
}
ctx.Data().SetData(info, 1)
return ctx.Render(`server/sysinfo`, nil)
}
func processList() ([]int32, []*process.Process) {
pids, err := process.Pids()
if err != nil {
log.Error(err)
}
processes := []*process.Process{}
for _, pid := range pids {
procs, err := process.NewProcess(pid)
if err != nil {
log.Error(err)
}
processes = append(processes, procs)
}
return pids, processes
}
func processInfo(pid int32) (echo.H, error) {
procs, err := process.NewProcess(pid)
if err != nil {
return nil, err
}
cpuPercent, _ := procs.Percent(time.Second * 5)
memPercent, _ := procs.MemoryPercent()
name, _ := procs.Name()
cmdLine, _ := procs.Cmdline()
exe, _ := procs.Exe()
createTime, _ := procs.CreateTime()
row := echo.H{
"name": name,
"cmd_line": cmdLine,
"exe": exe,
"created": "",
"cpu_percent": cpuPercent,
"memory_percent": memPercent,
}
if createTime > 0 {
row["created"] = com.DateFormat(`Y-m-d H:i:s`, createTime/1000)
}
return row, nil
}
func ProcessInfo(ctx echo.Context) error {
pid := ctx.Paramx(`pid`).Int32()
row, err := processInfo(pid)
data := ctx.Data()
if err != nil {
data.SetError(err)
} else {
data.SetData(row)
}
return ctx.JSON(data)
}
func ProcessKill(ctx echo.Context) error {
pid := ctx.Paramx(`pid`).Int()
err := com.CloseProcessFromPid(pid)
data := ctx.Data()
if err != nil {
data.SetError(err)
} else {
data.SetData(nil)
}
return ctx.JSON(data)
}
func Connections(ctx echo.Context) (err error) {
var conns []net.ConnectionStat
var kind string
switch kind {
case "tcp", "tcp4", "tcp6", "udp", "udp4", "udp6", "unix", "inet", "inet4", "inet6":
default:
kind = "all"
}
conns, err = net.Connections(kind)
if err != nil {
if err.Error() == "not implemented yet" {
if runtime.GOOS == "windows" {
err = nil
var conn <-chan net.ConnectionStat
if strings.HasPrefix(kind, `udp`) {
conn, err = NetStatUDP()
} else {
conn, err = NetStatTCP()
}
if err != nil {
return
}
done := make(chan bool)
go func() {
defer func() {
done <- true
}()
for {
select {
case c, r := <-conn:
if !r {
return
}
conns = append(conns, c)
}
}
}()
<-done
}
}
}
ctx.Set(`listData`, conns)
return ctx.Render(`server/netstat`, nil)
}