nezha/service/monitor/monitor.go

109 lines
2.8 KiB
Go
Raw Normal View History

2019-12-07 05:14:40 -05:00
package monitor
import (
2019-12-09 03:02:49 -05:00
"encoding/json"
2019-12-07 05:14:40 -05:00
"fmt"
2019-12-09 03:02:49 -05:00
"io/ioutil"
"net/http"
"strings"
2019-12-13 01:51:51 -05:00
"sync/atomic"
2019-12-07 05:14:40 -05:00
"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"
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 03:02:49 -05:00
type ipDotSbGeoIP struct {
2019-12-11 01:10:48 -05:00
CountryCode string `json:"country_code,omitempty"`
IP string `json:"ip,omitempty"`
2019-12-09 03:02:49 -05:00
}
2019-12-09 10:45:23 -05:00
var netInSpeed, netOutSpeed, netInTransfer, netOutTransfer, lastUpdate uint64
2019-12-07 05:14:40 -05:00
// GetHost ..
func GetHost() *model.Host {
hi, _ := host.Info()
var cpus []string
ci, _ := cpu.Info()
for i := 0; i < len(ci); i++ {
cpus = append(cpus, fmt.Sprintf("%v-%vC%vT", ci[i].ModelName, ci[i].Cores, ci[i].Stepping))
}
2019-12-11 08:50:49 -05:00
mv, _ := mem.VirtualMemory()
ms, _ := mem.SwapMemory()
2019-12-13 01:51:51 -05:00
u, _ := disk.Usage("/")
2019-12-11 01:10:48 -05:00
var ip ipDotSbGeoIP
2019-12-09 03:02:49 -05:00
resp, err := http.Get("https://api.ip.sb/geoip")
if err == nil {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &ip)
}
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,
2019-12-13 01:51:51 -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,
2019-12-09 03:02:49 -05:00
IP: ip.IP,
CountryCode: strings.ToLower(ip.CountryCode),
2019-12-09 10:45:23 -05:00
Version: dao.Version,
2019-12-07 05:14:40 -05:00
}
}
// GetState ..
2019-12-09 10:45:23 -05:00
func GetState(delay int64) *model.State {
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
2019-12-13 01:51:51 -05:00
u, _ := disk.Usage("/")
2019-12-07 05:14:40 -05:00
2019-12-09 10:45:23 -05:00
return &model.State{
CPU: cpuPercent,
MemUsed: mv.Used,
SwapUsed: ms.Used,
2019-12-13 01:51:51 -05:00
DiskUsed: u.Used,
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
}
// TrackNetworkSpeed ..
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
}
}