优化监控上传

This commit is contained in:
奶爸 2019-12-11 21:50:49 +08:00
parent e6434e70f0
commit b521ba371c
5 changed files with 58 additions and 48 deletions

View File

@ -3,10 +3,17 @@ package main
import (
"log"
"os/exec"
"github.com/shirou/gopsutil/disk"
)
func main() {
dparts, _ := disk.Partitions(false)
for _, part := range dparts {
u, _ := disk.Usage(part.Mountpoint)
log.Printf("Part:%v", part)
log.Printf("Usage:%v", u)
}
}
func cmdExec() {

View File

@ -11,11 +11,8 @@ const (
// State ..
type State struct {
CPU float64
MemTotal uint64
MemUsed uint64
SwapTotal uint64
SwapUsed uint64
DiskTotal uint64
DiskUsed uint64
NetInTransfer uint64
NetOutTransfer uint64
@ -28,11 +25,8 @@ type State struct {
func (s *State) PB() *pb.State {
return &pb.State{
Cpu: s.CPU,
MemTotal: s.MemTotal,
MemUsed: s.MemUsed,
SwapTotal: s.SwapTotal,
SwapUsed: s.SwapUsed,
DiskTotal: s.DiskTotal,
DiskUsed: s.DiskUsed,
NetInTransfer: s.NetInTransfer,
NetOutTransfer: s.NetOutTransfer,
@ -46,11 +40,8 @@ func (s *State) PB() *pb.State {
func PB2State(s *pb.State) State {
return State{
CPU: s.GetCpu(),
MemTotal: s.GetMemTotal(),
MemUsed: s.GetMemUsed(),
SwapTotal: s.GetSwapTotal(),
SwapUsed: s.GetSwapUsed(),
DiskTotal: s.GetDiskTotal(),
DiskUsed: s.GetDiskUsed(),
NetInTransfer: s.GetNetInTransfer(),
NetOutTransfer: s.GetNetOutTransfer(),
@ -65,6 +56,9 @@ type Host struct {
Platform string
PlatformVersion string
CPU []string
MemTotal uint64
DiskTotal uint64
SwapTotal uint64
Arch string
Virtualization string
BootTime uint64
@ -79,6 +73,9 @@ func (h *Host) PB() *pb.Host {
Platform: h.Platform,
PlatformVersion: h.PlatformVersion,
Cpu: h.CPU,
MemTotal: h.MemTotal,
DiskTotal: h.DiskTotal,
SwapTotal: h.SwapTotal,
Arch: h.Arch,
Virtualization: h.Virtualization,
BootTime: h.BootTime,
@ -88,12 +85,16 @@ func (h *Host) PB() *pb.Host {
}
}
// PB2Host ...
func PB2Host(h *pb.Host) Host {
return Host{
Platform: h.GetPlatform(),
PlatformVersion: h.GetPlatformVersion(),
CPU: h.GetCpu(),
MemTotal: h.GetMemTotal(),
DiskTotal: h.GetDiskTotal(),
SwapTotal: h.GetSwapTotal(),
Arch: h.GetArch(),
Virtualization: h.GetVirtualization(),
BootTime: h.GetBootTime(),
@ -102,3 +103,6 @@ func PB2Host(h *pb.Host) Host {
Version: h.GetVersion(),
}
}

View File

@ -12,27 +12,27 @@ message Host {
string platform = 1;
string platform_version = 2;
repeated string cpu = 3;
string arch = 4;
string virtualization = 5;
uint64 boot_time = 6;
string ip = 7;
string country_code = 8;
string version = 9;
uint64 mem_total = 4;
uint64 disk_total = 5;
uint64 swap_total = 6;
string arch = 7;
string virtualization = 8;
uint64 boot_time = 9;
string ip = 10;
string country_code = 11;
string version = 12;
}
message State {
double cpu = 1;
uint64 mem_total = 2;
uint64 mem_used = 3;
uint64 swap_total = 4;
uint64 swap_used = 5;
uint64 disk_total = 6;
uint64 disk_used = 7;
uint64 net_in_transfer = 8;
uint64 net_out_transfer = 9;
uint64 net_in_speed = 10;
uint64 net_out_speed = 11;
uint64 uptime = 12;
uint64 swap_used = 4;
uint64 disk_used = 5;
uint64 net_in_transfer = 6;
uint64 net_out_transfer = 7;
uint64 net_in_speed = 8;
uint64 net_out_speed = 9;
uint64 uptime = 10;
}
message Receipt{

View File

@ -16,9 +16,9 @@
系统:@#server.Host.Platform#@-@#server.Host.PlatformVersion#@ [<span
v-if='server.Host.Virtualization'>@#server.Host.Virtualization#@:</span>@#server.Host.Arch#@]<br>
CPU@#server.Host.CPU#@<br>
硬盘:@#formatByteSize(server.State.DiskUsed)#@/@#formatByteSize(server.State.DiskTotal)#@<br>
内存:@#formatByteSize(server.State.MemUsed)#@/@#formatByteSize(server.State.MemTotal)#@<br>
交换:@#formatByteSize(server.State.SwapUsed)#@/@#formatByteSize(server.State.SwapTotal)#@<br>
硬盘:@#formatByteSize(server.State.DiskUsed)#@/@#formatByteSize(server.Host.DiskTotal)#@<br>
内存:@#formatByteSize(server.State.MemUsed)#@/@#formatByteSize(server.Host.MemTotal)#@<br>
交换:@#formatByteSize(server.State.SwapUsed)#@/@#formatByteSize(server.Host.SwapTotal)#@<br>
流量:<i
class='arrow alternate circle down outline icon'></i>@#formatByteSize(server.State.NetInTransfer)#@<i
class='arrow alternate circle up outline icon'></i>@#formatByteSize(server.State.NetOutTransfer)#@<br>
@ -39,7 +39,7 @@
<div class="three wide column">内存</div>
<div class="thirteen wide column">
<div class="ui mem progress" :data-value="server.State.MemUsed"
:data-total="server.State.MemTotal">
:data-total="server.Host.MemTotal">
<div class="bar">
<div class="progress"></div>
</div>
@ -48,7 +48,7 @@
<div class="three wide column">交换</div>
<div class="thirteen wide column">
<div class="ui swap progress" :data-value="server.State.SwapUsed"
:data-total="server.State.SwapTotal">
:data-total="server.Host.SwapTotal">
<div class="bar">
<div class="progress"></div>
</div>
@ -64,7 +64,7 @@
<div class="three wide column">硬盘</div>
<div class="thirteen wide column">
<div class="ui disk progress" :data-value="server.State.DiskUsed"
:data-total="server.State.DiskTotal">
:data-total="server.Host.DiskTotal">
<div class="bar">
<div class="progress"></div>
</div>

View File

@ -33,6 +33,14 @@ func GetHost() *model.Host {
for i := 0; i < len(ci); i++ {
cpus = append(cpus, fmt.Sprintf("%v-%vC%vT", ci[i].ModelName, ci[i].Cores, ci[i].Stepping))
}
mv, _ := mem.VirtualMemory()
ms, _ := mem.SwapMemory()
var diskTotal uint64
dparts, _ := disk.Partitions(true)
for _, part := range dparts {
u, _ := disk.Usage(part.Mountpoint)
diskTotal += u.Total
}
var ip ipDotSbGeoIP
resp, err := http.Get("https://api.ip.sb/geoip")
if err == nil {
@ -44,6 +52,9 @@ func GetHost() *model.Host {
Platform: hi.OS,
PlatformVersion: hi.PlatformVersion,
CPU: cpus,
MemTotal: mv.Total,
DiskTotal: diskTotal,
SwapTotal: ms.Total,
Arch: hi.KernelArch,
Virtualization: hi.VirtualizationSystem,
BootTime: hi.BootTime,
@ -66,25 +77,17 @@ func GetState(delay int64) *model.State {
cpuPercent = cp[0]
}
// Disk
var diskTotal, diskUsed uint64
var diskUsed uint64
dparts, _ := disk.Partitions(true)
var lastDevice string
for _, part := range dparts {
u, _ := disk.Usage(part.Mountpoint)
if lastDevice != part.Device {
diskTotal += u.Total
lastDevice = part.Device
}
diskUsed += u.Used
}
return &model.State{
CPU: cpuPercent,
MemTotal: mv.Total,
MemUsed: mv.Used,
SwapTotal: ms.Total,
SwapUsed: ms.Used,
DiskTotal: diskTotal,
DiskUsed: diskUsed,
NetInTransfer: netInTransfer,
NetOutTransfer: netOutTransfer,
@ -97,14 +100,10 @@ func GetState(delay int64) *model.State {
// TrackNetworkSpeed ..
func TrackNetworkSpeed() {
var innerNetInTransfer, innerNetOutTransfer uint64
nc, err := net.IOCounters(true)
nc, err := net.IOCounters(false)
if err == nil {
for i := 0; i < len(nc); i++ {
if strings.HasPrefix(nc[i].Name, "e") {
innerNetInTransfer += nc[i].BytesRecv
innerNetOutTransfer += nc[i].BytesSent
}
}
innerNetInTransfer += nc[0].BytesRecv
innerNetOutTransfer += nc[0].BytesSent
if netInTransfer == 0 {
netInTransfer = innerNetInTransfer
}