2019-12-07 05:14:40 -05:00
|
|
|
|
package monitor
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2021-04-26 07:13:02 -04:00
|
|
|
|
"regexp"
|
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-04-26 07:13:02 -04:00
|
|
|
|
var expectDiskFsTypes = []string{
|
2021-05-03 22:22:38 -04:00
|
|
|
|
"apfs", "ext4", "ext3", "ext2", "f2fs", "reiserfs", "jfs", "btrfs", "fuseblk", "zfs", "simfs", "ntfs", "fat32", "exfat", "xfs",
|
2021-04-26 07:13:02 -04:00
|
|
|
|
}
|
|
|
|
|
var excludeNetInterfaces = []string{
|
|
|
|
|
"lo", "tun", "docker", "veth", "br-", "vmbr", "vnet", "kube",
|
|
|
|
|
}
|
|
|
|
|
var getMacDiskNo = regexp.MustCompile(`\/dev\/disk(\d)s.*`)
|
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 != "" {
|
2021-04-25 11:46:15 -04:00
|
|
|
|
cpuType = "Vrtual"
|
2021-02-27 06:24:19 -05:00
|
|
|
|
} 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-04-25 11:46:15 -04:00
|
|
|
|
diskTotal, _ := getDiskTotalAndUsed()
|
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,
|
|
|
|
|
SwapTotal: ms.Total,
|
2021-04-25 11:46:15 -04:00
|
|
|
|
DiskTotal: diskTotal,
|
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
|
|
|
|
mv, _ := mem.VirtualMemory()
|
|
|
|
|
ms, _ := mem.SwapMemory()
|
2019-12-09 10:45:23 -05:00
|
|
|
|
var cpuPercent float64
|
|
|
|
|
cp, err := cpu.Percent(time.Second*time.Duration(delay), false)
|
|
|
|
|
if err == nil {
|
|
|
|
|
cpuPercent = cp[0]
|
|
|
|
|
}
|
2021-04-25 11:46:15 -04:00
|
|
|
|
_, diskUsed := getDiskTotalAndUsed()
|
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-04-25 11:46:15 -04:00
|
|
|
|
DiskUsed: diskUsed,
|
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
|
2021-04-25 11:46:15 -04:00
|
|
|
|
nc, err := net.IOCounters(true)
|
2019-12-07 05:14:40 -05:00
|
|
|
|
if err == nil {
|
2021-04-25 11:46:15 -04:00
|
|
|
|
for _, v := range nc {
|
2021-04-26 07:13:02 -04:00
|
|
|
|
if isListContainsStr(excludeNetInterfaces, v.Name) {
|
2021-04-25 11:46:15 -04:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
innerNetInTransfer += v.BytesRecv
|
|
|
|
|
innerNetOutTransfer += v.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
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-25 11:46:15 -04:00
|
|
|
|
|
|
|
|
|
func getDiskTotalAndUsed() (total uint64, used uint64) {
|
|
|
|
|
diskList, _ := disk.Partitions(false)
|
2021-04-26 07:13:02 -04:00
|
|
|
|
devices := make(map[string]string)
|
|
|
|
|
countedDiskForMac := make(map[string]struct{})
|
2021-04-25 11:46:15 -04:00
|
|
|
|
for _, d := range diskList {
|
|
|
|
|
fsType := strings.ToLower(d.Fstype)
|
2021-04-26 07:13:02 -04:00
|
|
|
|
// 不统计 K8s 的虚拟挂载点:https://github.com/shirou/gopsutil/issues/1007
|
|
|
|
|
if devices[d.Device] == "" && isListContainsStr(expectDiskFsTypes, fsType) && !strings.Contains(d.Mountpoint, "/var/lib/kubelet") {
|
|
|
|
|
devices[d.Device] = d.Mountpoint
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for device, mountPath := range devices {
|
|
|
|
|
diskUsageOf, _ := disk.Usage(mountPath)
|
|
|
|
|
// 这里是针对 Mac 机器的处理,https://github.com/giampaolo/psutil/issues/1509
|
|
|
|
|
matches := getMacDiskNo.FindStringSubmatch(device)
|
|
|
|
|
if len(matches) == 2 {
|
|
|
|
|
if _, has := countedDiskForMac[matches[1]]; !has {
|
|
|
|
|
countedDiskForMac[matches[1]] = struct{}{}
|
2021-04-25 11:46:15 -04:00
|
|
|
|
total += diskUsageOf.Total
|
|
|
|
|
}
|
2021-04-26 07:13:02 -04:00
|
|
|
|
} else {
|
|
|
|
|
total += diskUsageOf.Total
|
2021-04-25 11:46:15 -04:00
|
|
|
|
}
|
2021-04-26 07:13:02 -04:00
|
|
|
|
used += diskUsageOf.Used
|
2021-04-25 11:46:15 -04:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-04-26 07:13:02 -04:00
|
|
|
|
|
|
|
|
|
func isListContainsStr(list []string, str string) bool {
|
|
|
|
|
for i := 0; i < len(list); i++ {
|
|
|
|
|
if strings.Contains(list[i], str) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|