优化监控上传

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 ( import (
"log" "log"
"os/exec" "os/exec"
"github.com/shirou/gopsutil/disk"
) )
func main() { 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() { func cmdExec() {

View File

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

View File

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

View File

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

View File

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