2019-12-07 05:14:40 -05:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-12-09 03:02:49 -05:00
|
|
|
"strings"
|
2019-12-13 01:51:51 -05:00
|
|
|
"sync/atomic"
|
2019-12-07 05:14:40 -05:00
|
|
|
"time"
|
|
|
|
|
2020-12-12 22:25:30 -05:00
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
|
|
|
"github.com/shirou/gopsutil/v3/disk"
|
|
|
|
"github.com/shirou/gopsutil/v3/host"
|
|
|
|
"github.com/shirou/gopsutil/v3/mem"
|
|
|
|
"github.com/shirou/gopsutil/v3/net"
|
2019-12-07 05:14:40 -05:00
|
|
|
|
2020-11-10 21:07:45 -05:00
|
|
|
"github.com/naiba/nezha/model"
|
|
|
|
"github.com/naiba/nezha/service/dao"
|
2019-12-07 05:14:40 -05:00
|
|
|
)
|
|
|
|
|
2019-12-09 10:45:23 -05:00
|
|
|
var netInSpeed, netOutSpeed, netInTransfer, netOutTransfer, lastUpdate uint64
|
2021-03-19 22:05:16 -04:00
|
|
|
|
2019-12-07 05:14:40 -05:00
|
|
|
func GetHost() *model.Host {
|
|
|
|
hi, _ := host.Info()
|
2021-02-27 06:24:19 -05:00
|
|
|
var cpuType string
|
|
|
|
if hi.VirtualizationSystem != "" {
|
|
|
|
cpuType = "Virtual"
|
|
|
|
} else {
|
|
|
|
cpuType = "Physical"
|
|
|
|
}
|
|
|
|
cpuModelCount := make(map[string]int)
|
2019-12-07 05:14:40 -05:00
|
|
|
ci, _ := cpu.Info()
|
|
|
|
for i := 0; i < len(ci); i++ {
|
2021-02-27 06:24:19 -05:00
|
|
|
cpuModelCount[ci[i].ModelName]++
|
|
|
|
}
|
|
|
|
var cpus []string
|
|
|
|
for model, count := range cpuModelCount {
|
|
|
|
cpus = append(cpus, fmt.Sprintf("%s %d %s Core", model, count, cpuType))
|
2019-12-07 05:14:40 -05:00
|
|
|
}
|
2019-12-11 08:50:49 -05:00
|
|
|
mv, _ := mem.VirtualMemory()
|
|
|
|
ms, _ := mem.SwapMemory()
|
2021-02-27 05:56:59 -05:00
|
|
|
u, _ := disk.Usage("/")
|
2021-03-19 22:05:16 -04:00
|
|
|
|
2019-12-07 05:14:40 -05:00
|
|
|
return &model.Host{
|
|
|
|
Platform: hi.OS,
|
|
|
|
PlatformVersion: hi.PlatformVersion,
|
|
|
|
CPU: cpus,
|
2019-12-11 08:50:49 -05:00
|
|
|
MemTotal: mv.Total,
|
2021-02-27 05:56:59 -05:00
|
|
|
DiskTotal: u.Total,
|
2019-12-11 08:50:49 -05:00
|
|
|
SwapTotal: ms.Total,
|
2019-12-07 05:14:40 -05:00
|
|
|
Arch: hi.KernelArch,
|
|
|
|
Virtualization: hi.VirtualizationSystem,
|
2019-12-09 10:45:23 -05:00
|
|
|
BootTime: hi.BootTime,
|
2021-03-19 22:05:16 -04:00
|
|
|
IP: cachedIP,
|
2021-03-20 11:50:16 -04:00
|
|
|
CountryCode: strings.ToLower(cachedCountry),
|
2019-12-09 10:45:23 -05:00
|
|
|
Version: dao.Version,
|
2019-12-07 05:14:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-15 11:45:49 -05:00
|
|
|
func GetState(delay int64) *model.HostState {
|
2019-12-09 10:45:23 -05:00
|
|
|
hi, _ := host.Info()
|
2019-12-07 05:14:40 -05:00
|
|
|
// Memory
|
|
|
|
mv, _ := mem.VirtualMemory()
|
|
|
|
ms, _ := mem.SwapMemory()
|
2019-12-09 10:45:23 -05:00
|
|
|
// CPU
|
|
|
|
var cpuPercent float64
|
|
|
|
cp, err := cpu.Percent(time.Second*time.Duration(delay), false)
|
|
|
|
if err == nil {
|
|
|
|
cpuPercent = cp[0]
|
|
|
|
}
|
2019-12-07 05:14:40 -05:00
|
|
|
// Disk
|
2021-02-27 05:56:59 -05:00
|
|
|
u, _ := disk.Usage("/")
|
2019-12-07 05:14:40 -05:00
|
|
|
|
2021-01-15 11:45:49 -05:00
|
|
|
return &model.HostState{
|
2019-12-09 10:45:23 -05:00
|
|
|
CPU: cpuPercent,
|
|
|
|
MemUsed: mv.Used,
|
|
|
|
SwapUsed: ms.Used,
|
2021-02-27 05:56:59 -05:00
|
|
|
DiskUsed: u.Used,
|
2019-12-13 01:51:51 -05:00
|
|
|
NetInTransfer: atomic.LoadUint64(&netInTransfer),
|
|
|
|
NetOutTransfer: atomic.LoadUint64(&netOutTransfer),
|
|
|
|
NetInSpeed: atomic.LoadUint64(&netInSpeed),
|
|
|
|
NetOutSpeed: atomic.LoadUint64(&netOutSpeed),
|
2019-12-09 10:45:23 -05:00
|
|
|
Uptime: hi.Uptime,
|
2019-12-07 05:14:40 -05:00
|
|
|
}
|
2019-12-09 10:45:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TrackNetworkSpeed() {
|
|
|
|
var innerNetInTransfer, innerNetOutTransfer uint64
|
2019-12-11 08:50:49 -05:00
|
|
|
nc, err := net.IOCounters(false)
|
2019-12-07 05:14:40 -05:00
|
|
|
if err == nil {
|
2019-12-11 08:50:49 -05:00
|
|
|
innerNetInTransfer += nc[0].BytesRecv
|
|
|
|
innerNetOutTransfer += nc[0].BytesSent
|
2019-12-13 01:51:51 -05:00
|
|
|
now := uint64(time.Now().Unix())
|
|
|
|
diff := now - atomic.LoadUint64(&lastUpdate)
|
2019-12-09 10:45:23 -05:00
|
|
|
if diff > 0 {
|
2019-12-13 01:51:51 -05:00
|
|
|
atomic.StoreUint64(&netInSpeed, (innerNetInTransfer-atomic.LoadUint64(&netInTransfer))/diff)
|
|
|
|
atomic.StoreUint64(&netOutSpeed, (innerNetOutTransfer-atomic.LoadUint64(&netOutTransfer))/diff)
|
2019-12-09 10:45:23 -05:00
|
|
|
}
|
2019-12-13 01:51:51 -05:00
|
|
|
atomic.StoreUint64(&netInTransfer, innerNetInTransfer)
|
|
|
|
atomic.StoreUint64(&netOutTransfer, innerNetOutTransfer)
|
|
|
|
atomic.StoreUint64(&lastUpdate, now)
|
2019-12-07 05:14:40 -05:00
|
|
|
}
|
|
|
|
}
|