This commit is contained in:
yanweidong 2024-03-01 17:34:57 +08:00
commit 27da26336f
6 changed files with 253 additions and 0 deletions

153
cmd/ps.go Normal file
View File

@ -0,0 +1,153 @@
package cmd
import (
"encoding/json"
"fmt"
"os/exec"
"strings"
"git.apinb.com/bsm-sdk/engine/env"
"git.apinb.com/bsm-sdk/engine/utils"
"git.apinb.com/bsm-sdk/engine/vars"
"github.com/modood/table"
"github.com/spf13/cobra"
"gorm.io/gorm"
)
type ActionsReleases struct {
gorm.Model
Identity string
Origin string
ServiceKey string
Version string
FullName string
OssUrl string
EtcPath string
FilePath string
ImagePath string
}
type PsTable struct {
ServiceKey string
Origin string
Status string
Performance string
CurrentVersion string
RegisterVersion string
UpdatedAt string
}
var psCmd = &cobra.Command{
Use: "ps",
Short: "list restiry and local installed microservice.",
Run: func(cmd *cobra.Command, args []string) {
psExecute()
},
}
func psExecute() {
pt := make([]PsTable, 0)
body, err := utils.HttpGet(registryUrl)
checkError(err)
var ms []ActionsReleases
err = json.Unmarshal(body, &ms)
checkError(err)
for _, v := range ms {
srvLine := getSrvStatus(v.ServiceKey)
srvLine.ServiceKey = v.ServiceKey
srvLine.Origin = v.Origin
srvLine.RegisterVersion = v.Version
srvLine.UpdatedAt = v.UpdatedAt.Format(vars.YYYY_MM_DD_HH_MM_SS)
pt = append(pt, srvLine)
}
// Output to stdout
table.Output(pt)
}
func getSrvStatus(srv string) PsTable {
isRun := checkProcessRunning(srv)
var status string = " - "
if isRun {
status = "Running"
}
return PsTable{
Status: status,
CurrentVersion: getCurrentVersion(srv),
Performance: getProcessInfo(srv),
}
}
func getPerformance(srv string) string {
}
func getCurrentVersion(srv string) string {
var data map[string]string
cmd := exec.Command(env.MeshEnv.Prefix+srv, "--json") // 替换为适合操作系统的命令
output, err := cmd.Output()
if err != nil {
return " - "
}
err = json.Unmarshal(output, &data)
if err != nil {
return " - "
}
if val, ok := data["version"]; ok {
return val
}
return " - "
}
func getProcessInfo(processName string) string {
var info string = " - "
cmd := exec.Command("ps", "-eo", "pid,%cpu,%mem") // 使用ps命令查询进程信息
output, err := cmd.Output() // 获取命令输出结果
if err != nil {
return info
}
// 将输出按换行符分隔成多行字符串数组
lines := strings.Split(string(output), "\n")[1:]
for _, line := range lines {
fields := strings.Fields(line) // 将每行按空格分隔为字段数组
if len(fields) >= 3 && fields[2] == processName {
pid := fields[0] // PID进程标识
cpuUsage := fields[1] // CPU使用情况
memoryUsage := fields[2] // 内存使用情况
info = fmt.Sprintf("PID:%s / CPU:%s / MEM:%s", pid, cpuUsage, memoryUsage)
return info
}
}
return info
}
func checkProcessRunning(processName string) bool {
cmd := exec.Command("ps", "-ef") // 替换为适合操作系统的命令
output, err := cmd.Output()
if err != nil {
return false
}
// 将输出按行分割成切片
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, processName) {
return true
}
}
return false
}

48
cmd/root.go Normal file
View File

@ -0,0 +1,48 @@
package cmd
import (
"fmt"
"os"
"git.apinb.com/bsm-sdk/engine/env"
"github.com/spf13/cobra"
)
var (
registryUrl string
)
func init() {
// get os env.
psCmd.Flags().StringVarP(&registryUrl, "registry", "r", "", "registry url.")
if registryUrl == "" {
registryUrl = env.GetEnvDefault("BlocksMesh_Registry", "http://registry.apinb.com")
}
rootCmd.AddCommand(psCmd)
}
var rootCmd = &cobra.Command{
Use: "bsm",
Short: "bsm is a Blocks Service Mesh cli.",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("usage: bsm <command>")
},
}
func checkError(err error) {
if err != nil {
panic(err)
os.Exit(1)
}
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

1
cmd/types.go Normal file
View File

@ -0,0 +1 @@
package cmd

20
go.mod Normal file
View File

@ -0,0 +1,20 @@
module git.apinb.com/bsm-tools/bsm
go 1.22.0
require git.apinb.com/bsm-sdk/engine v1.0.9
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
)
require (
github.com/google/uuid v1.6.0 // indirect
github.com/jaevor/go-nanoid v1.3.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/modood/table v0.0.0-20220527013332-8d47e76dad33
github.com/spf13/cobra v1.8.0
gorm.io/gorm v1.25.7 // indirect
)

24
go.sum Normal file
View File

@ -0,0 +1,24 @@
git.apinb.com/bsm-sdk/engine v1.0.9 h1:3zPYp8wtBVi7Aeg3H3/q7ZSVDRsGiXxQXwNzyMxfhao=
git.apinb.com/bsm-sdk/engine v1.0.9/go.mod h1:zCJfxj6RHHVHQmDn+Z2qa5s1qU7a3rTL9AZqEja9vIc=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jaevor/go-nanoid v1.3.0 h1:nD+iepesZS6pr3uOVf20vR9GdGgJW1HPaR46gtrxzkg=
github.com/jaevor/go-nanoid v1.3.0/go.mod h1:SI+jFaPuddYkqkVQoNGHs81navCtH388TcrH0RqFKgY=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/modood/table v0.0.0-20220527013332-8d47e76dad33 h1:T5IbS9C1G2zeHb6eBy6OfIvj5tfQB23kGFpewCJuGDg=
github.com/modood/table v0.0.0-20220527013332-8d47e76dad33/go.mod h1:41qyXVI5QH9/ObyPj27CGCVau5v/njfc3Gjj7yzr0HQ=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/gorm v1.25.7 h1:VsD6acwRjz2zFxGO50gPO6AkNs7KKnvfzUjHQhZDz/A=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=

7
main.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "git.apinb.com/bsm-tools/bsm/cmd"
func main() {
cmd.Execute()
}