[+] add new command /info
Some checks failed
Build and Push Docker Images / docker (push) Failing after 44s
Some checks failed
Build and Push Docker Images / docker (push) Failing after 44s
Signed-off-by: sunyz <i@sunyz.net>
This commit is contained in:
parent
5f713c4a2f
commit
8f10713277
16
go.mod
16
go.mod
@ -2,4 +2,18 @@ module github.com/realSunyz/lucky-tgbot
|
||||
|
||||
go 1.23.5
|
||||
|
||||
require gopkg.in/telebot.v3 v3.3.8
|
||||
require (
|
||||
github.com/shirou/gopsutil/v4 v4.24.12
|
||||
gopkg.in/telebot.v3 v3.3.8
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/ebitengine/purego v0.8.1 // indirect
|
||||
github.com/go-ole/go-ole v1.2.6 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.12 // indirect
|
||||
github.com/tklauser/numcpus v0.6.1 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||
golang.org/x/sys v0.28.0 // indirect
|
||||
)
|
||||
|
11
main.go
11
main.go
@ -1,15 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/realSunyz/lucky-tgbot/plugin/reborn"
|
||||
"github.com/realSunyz/lucky-tgbot/plugin/slash"
|
||||
"github.com/realSunyz/lucky-tgbot/plugin/torf"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/realSunyz/lucky-tgbot/plugin/info"
|
||||
"github.com/realSunyz/lucky-tgbot/plugin/reborn"
|
||||
"github.com/realSunyz/lucky-tgbot/plugin/slash"
|
||||
"github.com/realSunyz/lucky-tgbot/plugin/torf"
|
||||
tele "gopkg.in/telebot.v3"
|
||||
)
|
||||
|
||||
@ -33,12 +34,12 @@ func main() {
|
||||
source := rand.NewSource(time.Now().UnixNano())
|
||||
r := rand.New(source)
|
||||
|
||||
b.Handle("/info", info.Execute)
|
||||
|
||||
b.Handle("/reborn", func(c tele.Context) error {
|
||||
return reborn.Execute(c, r, rebornData)
|
||||
})
|
||||
|
||||
// b.Handle("/info", info.Execute)
|
||||
|
||||
b.Handle(tele.OnText, func(c tele.Context) error {
|
||||
inputText := c.Text()
|
||||
|
||||
|
108
plugin/info/main.go
Normal file
108
plugin/info/main.go
Normal file
@ -0,0 +1,108 @@
|
||||
package info
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/shirou/gopsutil/v4/cpu"
|
||||
"github.com/shirou/gopsutil/v4/docker"
|
||||
"github.com/shirou/gopsutil/v4/host"
|
||||
"github.com/shirou/gopsutil/v4/mem"
|
||||
tele "gopkg.in/telebot.v3"
|
||||
"log"
|
||||
)
|
||||
|
||||
func getCPUInfo() string {
|
||||
// Get CPU model name
|
||||
cpuName := func() string {
|
||||
cpuInfo, err := cpu.Info()
|
||||
if err != nil || len(cpuInfo) == 0 {
|
||||
log.Printf("Error fetching CPU info: %s", err)
|
||||
return "N/A"
|
||||
}
|
||||
return cpuInfo[0].ModelName
|
||||
}
|
||||
|
||||
// Get CPU usage percentage
|
||||
cpuPercent := func() string {
|
||||
cpuUsage, err := cpu.Percent(0, false)
|
||||
if err != nil {
|
||||
log.Printf("Error fetching CPU usage: %s", err)
|
||||
return "N/A"
|
||||
}
|
||||
return fmt.Sprintf("%.2f%%", cpuUsage[0])
|
||||
}
|
||||
|
||||
cpuInfo := fmt.Sprintf(
|
||||
"CPU Model: `%s`\nCPU Usage: `%s`\n", cpuName(), cpuPercent())
|
||||
|
||||
return cpuInfo
|
||||
}
|
||||
|
||||
func getRAMInfo() string {
|
||||
vmInfo := func() (string, string, string) {
|
||||
vmStat, err := mem.VirtualMemory()
|
||||
if err != nil || vmStat == nil {
|
||||
log.Printf("Error fetching memory info: %s", err)
|
||||
return "N/A", "N/A", "N/A"
|
||||
}
|
||||
vmPercent := fmt.Sprintf("%.2f%%", vmStat.UsedPercent)
|
||||
vmTotal := fmt.Sprintf("%.2f", float64(vmStat.Total)/1e9)
|
||||
vmUsed := fmt.Sprintf("%.2f", float64(vmStat.Used)/1e9)
|
||||
return vmPercent, vmUsed, vmTotal
|
||||
}
|
||||
|
||||
vmPercent, vmUsed, vmTotal := vmInfo()
|
||||
|
||||
ramInfo := fmt.Sprintf(
|
||||
"RAM Usage: `%s` (`%sGB` / `%sGB`)\n", vmPercent, vmUsed, vmTotal)
|
||||
|
||||
return ramInfo
|
||||
}
|
||||
|
||||
func getHostInfo() string {
|
||||
hostName := func() string {
|
||||
hostInfo, err := host.Info()
|
||||
if err != nil || hostInfo == nil {
|
||||
log.Printf("Error fetching host info: %s", err)
|
||||
return "N/A"
|
||||
}
|
||||
return hostInfo.Hostname
|
||||
}
|
||||
|
||||
hostInfo := fmt.Sprintf(
|
||||
"Hostname: `%s`\n", hostName())
|
||||
|
||||
return hostInfo
|
||||
}
|
||||
|
||||
func getDocker() string {
|
||||
checkDocker := func() string {
|
||||
dockerStat, err := docker.GetDockerStat()
|
||||
if err == nil && len(dockerStat) > 0 {
|
||||
return "Yes"
|
||||
} else if errors.Is(err, docker.ErrDockerNotAvailable) {
|
||||
return "No"
|
||||
} else {
|
||||
log.Printf("Error fetching container info: %s", err)
|
||||
return "N/A"
|
||||
}
|
||||
}
|
||||
|
||||
dockerInfo := fmt.Sprintf("Running inside a container: `%s`\n", checkDocker())
|
||||
|
||||
return dockerInfo
|
||||
}
|
||||
|
||||
func Execute(c tele.Context) error {
|
||||
cpuInfo := getCPUInfo()
|
||||
ramInfo := getRAMInfo()
|
||||
hostInfo := getHostInfo()
|
||||
dockerInfo := getDocker()
|
||||
|
||||
outputText := fmt.Sprintf("%s%s%s%s", hostInfo, cpuInfo, ramInfo, dockerInfo)
|
||||
|
||||
return c.Reply(outputText, &tele.SendOptions{
|
||||
ParseMode: "Markdown",
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user