Add files via upload
This commit is contained in:
27
go.mod
Normal file
27
go.mod
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
module examp/s
|
||||||
|
|
||||||
|
go 1.20
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/labstack/echo/v4 v4.11.1
|
||||||
|
github.com/labstack/gommon v0.4.0
|
||||||
|
github.com/shirou/gopsutil v3.21.11+incompatible
|
||||||
|
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||||
|
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
|
||||||
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
|
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||||
|
github.com/tklauser/go-sysconf v0.3.12 // indirect
|
||||||
|
github.com/tklauser/numcpus v0.6.1 // indirect
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0 // indirect
|
||||||
|
github.com/valyala/fasttemplate v1.2.2 // indirect
|
||||||
|
github.com/yusufpapurcu/wmi v1.2.3 // indirect
|
||||||
|
golang.org/x/crypto v0.11.0 // indirect
|
||||||
|
golang.org/x/net v0.12.0 // indirect
|
||||||
|
golang.org/x/sys v0.11.0 // indirect
|
||||||
|
golang.org/x/text v0.11.0 // indirect
|
||||||
|
golang.org/x/time v0.3.0 // indirect
|
||||||
|
)
|
||||||
59
go.sum
Normal file
59
go.sum
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
|
||||||
|
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
||||||
|
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
|
||||||
|
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
|
||||||
|
github.com/labstack/echo/v4 v4.11.1 h1:dEpLU2FLg4UVmvCGPuk/APjlH6GDpbEPti61srUUUs4=
|
||||||
|
github.com/labstack/echo/v4 v4.11.1/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
|
||||||
|
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
|
||||||
|
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
|
||||||
|
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
|
||||||
|
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||||
|
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||||
|
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||||
|
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||||
|
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
|
||||||
|
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/shirou/gopsutil v3.21.11+incompatible h1:+1+c1VGhc88SSonWP6foOcLhvnKlUeu/erjjvaPEYiI=
|
||||||
|
github.com/shirou/gopsutil v3.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
|
||||||
|
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
|
||||||
|
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
|
||||||
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
|
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||||
|
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
|
||||||
|
github.com/tklauser/go-sysconf v0.3.12/go.mod h1:Ho14jnntGE1fpdOqQEEaiKRpvIavV0hSfmBq8nJbHYI=
|
||||||
|
github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+Fk=
|
||||||
|
github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY=
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
|
||||||
|
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||||
|
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||||
|
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
|
||||||
|
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
|
||||||
|
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
|
||||||
|
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||||
|
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
|
||||||
|
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
|
||||||
|
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
|
||||||
|
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
|
||||||
|
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
|
||||||
|
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
|
||||||
|
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
|
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
|
||||||
|
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
258
main.go
Normal file
258
main.go
Normal file
@@ -0,0 +1,258 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/shirou/gopsutil/cpu"
|
||||||
|
"github.com/shirou/gopsutil/disk"
|
||||||
|
"github.com/shirou/gopsutil/host"
|
||||||
|
"github.com/shirou/gopsutil/load"
|
||||||
|
"github.com/shirou/gopsutil/mem"
|
||||||
|
"github.com/shirou/gopsutil/net"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/labstack/echo/v4"
|
||||||
|
"github.com/labstack/echo/v4/middleware"
|
||||||
|
"github.com/skip2/go-qrcode"
|
||||||
|
"os"
|
||||||
|
"encoding/base64"
|
||||||
|
"io/ioutil"
|
||||||
|
"github.com/labstack/gommon/log"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
type Status struct {
|
||||||
|
T time.Time `json:"-"`
|
||||||
|
Cpu float64 `json:"cpu"`
|
||||||
|
Mem struct {
|
||||||
|
Current uint64 `json:"current"`
|
||||||
|
Total uint64 `json:"total"`
|
||||||
|
} `json:"mem"`
|
||||||
|
Swap struct {
|
||||||
|
Current uint64 `json:"current"`
|
||||||
|
Total uint64 `json:"total"`
|
||||||
|
} `json:"swap"`
|
||||||
|
Disk struct {
|
||||||
|
Current uint64 `json:"current"`
|
||||||
|
Total uint64 `json:"total"`
|
||||||
|
} `json:"disk"`
|
||||||
|
Uptime uint64 `json:"uptime"`
|
||||||
|
Loads []float64 `json:"loads"`
|
||||||
|
TcpCount int `json:"tcpCount"`
|
||||||
|
UdpCount int `json:"udpCount"`
|
||||||
|
NetIO struct {
|
||||||
|
Up uint64 `json:"up"`
|
||||||
|
Down uint64 `json:"down"`
|
||||||
|
} `json:"netIO"`
|
||||||
|
NetTraffic struct {
|
||||||
|
Sent uint64 `json:"sent"`
|
||||||
|
Recv uint64 `json:"recv"`
|
||||||
|
SentM uint64 `json:"sentM"`
|
||||||
|
RecvM uint64 `json:"recvM"`
|
||||||
|
} `json:"netTraffic"`
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func show_qrcode(){
|
||||||
|
|
||||||
|
fmt.Println("欢迎使用服务器监控脚本")
|
||||||
|
fmt.Println("------------------稍等片刻-----------------")
|
||||||
|
fmt.Println("获取公网IP")
|
||||||
|
|
||||||
|
|
||||||
|
resp, err := http.Get("https://api4.ipify.org/?format=text")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("无法获取IP地址:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("无法读取响应:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
local_ip := string(body)
|
||||||
|
|
||||||
|
|
||||||
|
fmt.Println("你的外部IP地址是:", local_ip)
|
||||||
|
fmt.Println("服务器端口号", 1323)
|
||||||
|
|
||||||
|
url:= "http://"+local_ip+":1323/"
|
||||||
|
|
||||||
|
data := []byte(url)
|
||||||
|
// 进行Base64编码
|
||||||
|
encoded := base64.StdEncoding.EncodeToString(data)
|
||||||
|
fmt.Println("绑定ID", encoded)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 生成二维码
|
||||||
|
qr, err := qrcode.New(encoded, qrcode.Medium)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("生成二维码时出错:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fmt.Println("\n")
|
||||||
|
|
||||||
|
fmt.Println("请使用微信小程序 矢光小屋 扫描下方二维码 添加服务器")
|
||||||
|
fmt.Println("\n")
|
||||||
|
|
||||||
|
// 在终端中显示二维码
|
||||||
|
fmt.Println(qr.ToSmallString(false))
|
||||||
|
}
|
||||||
|
|
||||||
|
func customLogger() echo.MiddlewareFunc {
|
||||||
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
|
return func(c echo.Context) error {
|
||||||
|
// 获取Echo的默认日志记录器
|
||||||
|
logger := c.Echo().Logger
|
||||||
|
// 设置日志级别为致命错误
|
||||||
|
logger.SetLevel(log.ERROR)
|
||||||
|
// 执行下一个中间件或处理程序
|
||||||
|
return next(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func GetStatus(lastStatus *Status) *Status {
|
||||||
|
now := time.Now()
|
||||||
|
status := &Status{
|
||||||
|
T: now,
|
||||||
|
}
|
||||||
|
|
||||||
|
percents , err := cpu.Percent(0, false)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("get cpu percent failed:", err)
|
||||||
|
} else {
|
||||||
|
status.Cpu = percents[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
upTime , err := host.Uptime()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("get uptime failed:", err)
|
||||||
|
} else {
|
||||||
|
status.Uptime = upTime
|
||||||
|
}
|
||||||
|
|
||||||
|
memInfo, err := mem.VirtualMemory()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("get virtual memory failed:",err)
|
||||||
|
} else {
|
||||||
|
status.Mem.Current = memInfo.Used
|
||||||
|
status.Mem.Total = memInfo.Total
|
||||||
|
}
|
||||||
|
|
||||||
|
swapInfo, err := mem.SwapMemory()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("get swap memory failed:", err)
|
||||||
|
} else {
|
||||||
|
status.Swap.Current = swapInfo.Used
|
||||||
|
status.Swap.Total = swapInfo.Total
|
||||||
|
}
|
||||||
|
|
||||||
|
distInfo,err := disk.Usage("/")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("get dist usage failed:", err)
|
||||||
|
} else {
|
||||||
|
status.Disk.Current = distInfo.Used/1024/1024
|
||||||
|
status.Disk.Total = distInfo.Total/1024/1024
|
||||||
|
}
|
||||||
|
|
||||||
|
avgState, err := load.Avg()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("get load avg failed:",err)
|
||||||
|
} else {
|
||||||
|
status.Loads = []float64{avgState.Load1, avgState.Load5, avgState.Load15}
|
||||||
|
}
|
||||||
|
|
||||||
|
ioStats, err := net.IOCounters(false)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("get io counters failed:", err)
|
||||||
|
} else if len(ioStats) > 0 {
|
||||||
|
ioStat := ioStats[0]
|
||||||
|
status.NetTraffic.Sent = ioStat.BytesSent
|
||||||
|
status.NetTraffic.Recv = ioStat.BytesRecv
|
||||||
|
status.NetTraffic.SentM = ioStat.BytesSent/1024/1024
|
||||||
|
status.NetTraffic.RecvM = ioStat.BytesRecv/1024/1024
|
||||||
|
|
||||||
|
if lastStatus != nil {
|
||||||
|
duration := now.Sub(lastStatus.T)
|
||||||
|
seconds := float64(duration) / float64(time.Second)
|
||||||
|
up := uint64(float64(status.NetTraffic.Sent-lastStatus.NetTraffic.Sent) / seconds)
|
||||||
|
down := uint64(float64(status.NetTraffic.Recv-lastStatus.NetTraffic.Recv) / seconds)
|
||||||
|
status.NetIO.Up = up
|
||||||
|
status.NetIO.Down = down
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Println("can not find io counters")
|
||||||
|
}
|
||||||
|
|
||||||
|
status.TcpCount, err = 0,nil // GetTCPCount()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("get tcp connections failed:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
status.UdpCount, err = 0,nil // GetUDPCount()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("get udp connections failed:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return status
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var S *Status
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
|
||||||
|
// Echo instance
|
||||||
|
e := echo.New()
|
||||||
|
|
||||||
|
// Middleware
|
||||||
|
//e.Use(middleware.Logger())
|
||||||
|
e.Use(middleware.Recover())
|
||||||
|
e.Use(customLogger())
|
||||||
|
|
||||||
|
// Routes
|
||||||
|
e.GET("/", hello)
|
||||||
|
e.GET("/", state)
|
||||||
|
|
||||||
|
show_qrcode()
|
||||||
|
|
||||||
|
// Start server
|
||||||
|
e.Logger.Fatal(e.Start(":1323"))
|
||||||
|
|
||||||
|
e.Logger.SetOutput(ioutil.Discard)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler
|
||||||
|
func hello(c echo.Context) error {
|
||||||
|
return c.String(http.StatusOK, "Hello, World!")
|
||||||
|
}
|
||||||
|
|
||||||
|
func state(c echo.Context) error {
|
||||||
|
|
||||||
|
|
||||||
|
S = GetStatus(S);
|
||||||
|
|
||||||
|
bytes, err := json.Marshal(S)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return c.String(http.StatusOK, string(bytes))
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user