Compare commits

..

15 Commits

Author SHA1 Message Date
shy
9b0046fa51 opt some 2025-03-10 18:57:52 +08:00
shy
862000e2ce add some 2025-03-05 09:36:08 +08:00
shy
10f5cc7099 opt some 2025-03-03 22:21:33 +08:00
shy
6a3f0d8bb9 opt some 2025-03-03 18:52:35 +08:00
shy
9bcc69bb48 add some 2025-03-03 07:53:15 +08:00
shy
20f7311c13 opt some 2025-03-02 22:30:42 +08:00
shy
d28e211359 add some 2025-03-01 17:44:57 +08:00
shy
62cc9bdefa add some 2025-03-01 14:23:09 +08:00
shy
c8562f0272 add some 2025-03-01 11:36:56 +08:00
shy
c63ebf3304 add some 2025-03-01 08:40:43 +08:00
shy
e1010d87ff add some 2025-02-27 18:14:05 +08:00
shy
0e72e916e0 add some 2025-02-27 16:27:38 +08:00
shy
645c7ac277 add some 2025-02-27 14:38:04 +08:00
shy
ab835b0a69 opt some 2025-02-26 22:37:36 +08:00
shy
796ea6b301 add some 2025-02-23 19:49:33 +08:00
10 changed files with 190 additions and 40 deletions

View File

@ -5,8 +5,8 @@ flags = -ldflags "-w -s" -v
all: def
@date +"%Y-%m-%d %H:%M:%S"
go build ${flags} -o ${binarys} src/main.go ${version} ${binpack} && ./${binarys} forever restart &>/dev/null
go build ${flags} -o ${binarys} src/main.go src/option.go ${version} ${binpack} && ./${binarys} forever restart &>/dev/null
def:
@[ -f ${version} ] || echo "package main">${version}
@[ -f ${binpack} ] || echo "package main">${binpack}
@[ -f ${binpack} ] || echo "package main">${binpack}

View File

@ -1,8 +1,8 @@
# ContextOS
ContextOS 通过集群化、模块化、自动化的方式,只用一个 20M 大小的程序文件,就可以在各种设备上,一键启动完整的云计算服务与云研发环境。
## 源码安装
### 克隆编译
## 安装系统
### 下载编译
```sh
git clone https://shylinux.com/x/ContextOS
cd ContextOS; source etc/miss.sh
@ -13,7 +13,5 @@ cd ContextOS; source etc/miss.sh
ish_miss_serve
```
### 访问网页
```sh
open http://localhost:9020
```
### 管理后台
打开管理后台 [http://localhost:9020](http://localhost:9020)

8
go.mod
View File

@ -2,12 +2,12 @@ module 2025-dev.shylinux.com/x/20250213-machine
go 1.13
require 2025-dev.shylinux.com/x/20250215-cluster v0.0.2
require 2025-dev.shylinux.com/x/20250215-cluster v0.0.7
require (
shylinux.com/x/ice v1.5.62
shylinux.com/x/icebergs v1.9.64
shylinux.com/x/toolkits v1.0.17
shylinux.com/x/ice v1.5.73
shylinux.com/x/icebergs v1.9.75
shylinux.com/x/toolkits v1.0.19
)
require github.com/shirou/gopsutil/v4 v4.25.1

16
go.sum
View File

@ -1,5 +1,5 @@
2025-dev.shylinux.com/x/20250215-cluster v0.0.2 h1:pplNlwzJSFl46+eUFp4iHW+iKq7i0yliOAuffN1IVmE=
2025-dev.shylinux.com/x/20250215-cluster v0.0.2/go.mod h1:/fdcvbXiqFjbuG/Ctyl+oGLxzWADflMZHSuQNMgO8oo=
2025-dev.shylinux.com/x/20250215-cluster v0.0.7 h1:GhQImaF6RbliJ6467gdeB4AVzCNyz5LwmA9AeCphEvo=
2025-dev.shylinux.com/x/20250215-cluster v0.0.7/go.mod h1:rhNacwQogLrmiNkmoptqpKY9ieRXAhVHenHwZRcdBB8=
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=
@ -49,12 +49,12 @@ shylinux.com/x/go-git/v5 v5.6.7 h1:WD5QSco7m3QooPCgdvQ6/GyGIFPun8C+hex5N41LYlk=
shylinux.com/x/go-git/v5 v5.6.7/go.mod h1:Qb0lA+uIrofZg8NQerhYcJHgGWixFqvS6p3aJ/L5Nlk=
shylinux.com/x/go-qrcode v0.0.3 h1:RMo+Vidbgq3HatLBj7DDXcTbTLFUwzis5K7TqBkD38U=
shylinux.com/x/go-qrcode v0.0.3/go.mod h1:KAbtU+KwiiABMZ/CJ0zh9PI2AX82Uf9rRYcQ4ODm4po=
shylinux.com/x/ice v1.5.62 h1:BUrb8hbQ+heePk9c+n7/7nLuofiuFyUmKD47MDvtc20=
shylinux.com/x/ice v1.5.62/go.mod h1:tI/fdpFpkdp2FyBxXCefcfrOFFZgoT6fnxUrhwOFivs=
shylinux.com/x/icebergs v1.9.64 h1:EkqZNWrLxAyfnobYdYCzIa5GJfwptIcoHwNSLYkaj3g=
shylinux.com/x/icebergs v1.9.64/go.mod h1:EZTbCFZGyL3hl+2wF70unet/S7y0F18hnLEq9+5qM70=
shylinux.com/x/ice v1.5.73 h1:wp+KeQ14hLOdxxSou/Y0zHi48Jm4nLx1rFOwGz8ix04=
shylinux.com/x/ice v1.5.73/go.mod h1:uGI73gYkk+FiJt5qmy/aYhoKRJ4wobcJk3WrPGh8Waw=
shylinux.com/x/icebergs v1.9.75 h1:9zAm0ppwUWl0As/vqfQG/QR/vwXa5zmfdIGE/gmvErQ=
shylinux.com/x/icebergs v1.9.75/go.mod h1:3Bdp3tjzw+hUKJF+kR8pfsrbjAf72DVZmCaE8/MPFtk=
shylinux.com/x/toolkits v0.7.10/go.mod h1:CHDJarGlDkg60kVsvMLYL/a5hAnRLEOShiEsMOuEp0Q=
shylinux.com/x/toolkits v1.0.17 h1:Y8QRUl0f36fgLfq+YPe7b5HxVlyY104PPVNGfLptb2I=
shylinux.com/x/toolkits v1.0.17/go.mod h1:CHDJarGlDkg60kVsvMLYL/a5hAnRLEOShiEsMOuEp0Q=
shylinux.com/x/toolkits v1.0.19 h1:Nrx0xYRc5ph1WS66EZ1hJUCe+2FdSWQ4QP6tBlguikQ=
shylinux.com/x/toolkits v1.0.19/go.mod h1:CHDJarGlDkg60kVsvMLYL/a5hAnRLEOShiEsMOuEp0Q=
shylinux.com/x/websocket v0.0.4 h1:AJpwblePoOpiE6C8NrvgNYpKTotXMLrDDX2chTvx44Q=
shylinux.com/x/websocket v0.0.4/go.mod h1:3UGWkjTu3ie5NAZen7J+uLPBrO7DFeKloj6Jxo13Oiw=

11
src/document/index.shy Normal file
View File

@ -0,0 +1,11 @@
navmenu `
"{{ .Title }}" /
_
"下 载" download/
"文 档" started/
"开 发" development/
_
"应 用" "https://web.shylinux.com/s/20240903-operation/c/web.team.operation.portal"
"源 码" "https://git.shylinux.com/dev/machine"
"社 区" "https://bbs.shylinux.com"
`

View File

@ -1,6 +1,11 @@
package main
import "shylinux.com/x/ice"
import _ "2025-dev.shylinux.com/x/20250213-machine/src/monitor"
import (
"shylinux.com/x/ice"
func main() { print(ice.Run()) }
_ "2025-dev.shylinux.com/x/20250213-machine/src/monitor"
)
func main() { print(ice.Run()) }
func init() { ice.Info.CodeMain = "src/monitor/monitor.go" }

View File

@ -1,4 +1,12 @@
title "ContextOS-Machine"
refer `
官网 https://dev.shylinux.com
源码 https://git.shylinux.com/dev/machine
后台 https://dev.shylinux.com/s/20250213-machine
`
field web.chat.dev.machine.monitor
field web.chat.dev.machine.process
section "gopsutil"
refer `
源码 https://github.com/shirou/gopsutil

View File

@ -4,6 +4,7 @@ import (
"time"
"shylinux.com/x/ice"
"shylinux.com/x/icebergs/base/aaa"
"shylinux.com/x/icebergs/base/cli"
"shylinux.com/x/icebergs/base/mdb"
"shylinux.com/x/icebergs/base/nfs"
@ -20,13 +21,44 @@ import (
"github.com/shirou/gopsutil/v4/net"
)
type monitor struct {
type Monitor struct {
travel.Travel
fields string `data:"Total,Free,Available,Used,UsedPercent,ModelName,Mhz,Core,Thread,CorePercent,ThreadPercent,DiskTotal,DiskFree,DiskUsed,DiskUsedPercent,BytesRecv,BytesSent,PacketsRecv,PacketsSent,version,time"`
fields string `data:"ModelName,Mhz,Core,Thread,CorePercent,ThreadPercent,Total,Free,Available,Used,UsedPercent,DiskTotal,DiskFree,DiskUsed,DiskUsedPercent,BytesRecv,BytesSent,PacketsRecv,PacketsSent,ip,hostname,version,time"`
script string `name:"script dev nodename port" help:"脚本"`
list string `name:"list space auto" help:"设备监控"`
}
func (s monitor) List(m *ice.Message, arg ...string) {
func (s Monitor) Inputs(m *ice.Message, arg ...string) {
switch arg[0] {
case ice.DEV:
m.SpaceListServer().Table(func(value ice.Maps) {
m.Push(arg[0], value[mdb.TEXT])
})
case web.SPACE:
m.Cmd("", s.Foreach).Table(func(value ice.Maps) {
m.Push(arg[0], value[web.SPACE])
})
}
}
func (s Monitor) Foreach(m *ice.Message, arg ...string) {
s.Travel.Foreach(m, arg...)
stats := map[string]int{}
m.Table(func(value ice.Maps) {
kit.For([]string{"Thread", "Used", "Available", "DiskUsed", "DiskFree"}, func(key string) { stats[key] += kit.Int(value[key]) })
})
list := []string{}
kit.For(stats, func(key string, value int) { list = append(list, key, kit.FmtSize(value)) })
m.StatusTimeCount(list)
}
func (s Monitor) Script(m *ice.Message, arg ...string) {
m.Option(cli.CTX_ARG, kit.JoinCmdArgs(
ice.DEV, m.OptionDefault(ice.DEV, m.Option(ice.MSG_USERHOST)),
tcp.NODENAME, m.OptionDefault(tcp.NODENAME, "$(hostname)-machine"),
tcp.PORT, m.OptionDefault(tcp.PORT, "9031"),
))
m.Cmdy(code.PUBLISH, nfs.CONTEXTS, nfs.BINARY)
}
func (s Monitor) List(m *ice.Message, arg ...string) {
if len(arg) == 0 {
c, _ := cpu.Info()
v, _ := mem.VirtualMemory()
@ -79,6 +111,10 @@ func (s monitor) List(m *ice.Message, arg ...string) {
m.Push(field, kit.FmtSize(n[0].PacketsRecv))
case "PacketsSent":
m.Push(field, kit.FmtSize(n[0].PacketsSent))
case aaa.IP:
m.Push(field, m.Cmd(tcp.HOST).Append(aaa.IP))
case tcp.HOSTNAME:
m.Push(field, ice.Info.NodeName)
case nfs.VERSION:
m.Push(field, ice.Info.Make.Version)
case mdb.TIME:
@ -87,22 +123,10 @@ func (s monitor) List(m *ice.Message, arg ...string) {
m.Push(field, "")
}
})
m.Action(s.Foreach)
m.Option(cli.CTX_ARG, kit.JoinCmdArgs(tcp.NODENAME, m.Option(ice.MSG_USERPOD)))
m.Cmdy(code.PUBLISH, nfs.CONTEXTS, ice.APP)
m.Action(s.Foreach, s.Script)
} else {
m.Cmdy(web.SPACE, arg[0], m.PrefixKey())
}
}
func (s monitor) Foreach(m *ice.Message, arg ...string) {
s.Travel.Foreach(m, arg...)
stats := map[string]int{}
m.Table(func(value ice.Maps) {
kit.For([]string{"Thread", "Used", "Available", "DiskUsed", "DiskFree"}, func(key string) { stats[key] += kit.Int(value[key]) })
})
list := []string{}
kit.For(stats, func(key string, value int) { list = append(list, key, kit.FmtSize(value)) })
m.StatusTimeCount(list)
}
func init() { ice.Cmd("web.chat.dev.machine.monitor", monitor{}) }
func init() { ice.Cmd("web.chat.dev.machine.monitor", Monitor{}) }

103
src/monitor/process.go Normal file
View File

@ -0,0 +1,103 @@
package monitor
import (
"os"
"strings"
"syscall"
"time"
"shylinux.com/x/ice"
kit "shylinux.com/x/toolkits"
"github.com/shirou/gopsutil/v4/mem"
"github.com/shirou/gopsutil/v4/process"
)
type Process struct {
Monitor
list string `name:"list name auto" help:"进程监控"`
kill string `name:"kill signal" help:"结束进程" icon:"bi bi-stop-circle"`
}
func (s Process) Weight(m *ice.Message, arg ...string) {
stat := map[string]int{}
v, _ := mem.VirtualMemory()
s.getList(func(p *process.Process, cmd string) {
name, _ := p.Name()
mem, _ := p.MemoryInfo()
stat[name] += int(mem.RSS)
})
kit.For(stat, func(key string, value int) {
m.Push("name", key).Push("rss", kit.FmtSize(value))
})
m.DisplayStoryWeight().SortIntR("rss").Action(s.Weight)
m.Push("name", "rest").Push("rss", kit.FmtSize(v.Available))
}
func (s Process) List(m *ice.Message, arg ...string) {
stat := map[string]int{}
s.getList(func(p *process.Process, cmd string) {
if len(arg) > 0 && !strings.Contains(cmd, arg[0]) {
return
}
t, _ := p.CreateTime()
ts, _ := p.NumThreads()
cpu, _ := p.CPUPercent()
mem, _ := p.MemoryInfo()
conn, _ := p.Connections()
fd, _ := p.NumFDs()
user, _ := p.Username()
name, _ := p.Name()
m.Push("time", strings.TrimSuffix(time.Unix(t/1000, t%1000).Format(ice.MOD_TIME), ".000"))
m.Push("pid", p.Pid)
m.Push("ts", ts)
m.Push("cpu", kit.Format("%.2f", cpu))
m.Push("rss", kit.FmtSize(mem.RSS))
m.Push("vms", kit.FmtSize(mem.VMS))
m.Push("cs", len(conn))
m.Push("fs", fd)
m.Push("user", user)
m.Push("name", name)
m.Push("cmd", kit.Select(name, cmd))
stat["ts"] += int(ts)
stat["cs"] += len(conn)
stat["fs"] += int(fd)
stat["rss"] += int(mem.RSS)
if strings.Contains(cmd, "bin/ice.bin") {
stat["ice.bin"] += int(mem.RSS)
stat["ice.num"]++
if strings.Contains(cmd, "-bash -c") {
return
}
kit.If(strings.Contains(cmd, "bin/ice.bin space"), func() { stat["ice.space"]++ })
kit.If(strings.Contains(cmd, "bin/ice.bin serve"), func() { stat["ice.serve"]++ })
}
})
stats := map[string]string{}
kit.For(stat, func(key string, value int) {
if kit.IsIn(key, "rss", "ice.bin") {
stats[key] = kit.FmtSize(value)
} else {
stats[key] = kit.Format(value)
}
})
m.PushAction(s.Kill).Action(s.Foreach, s.Weight)
m.StatusTimeCount(stats).SortIntR("rss")
}
func (s Process) Kill(m *ice.Message, arg ...string) {
if p, e := os.FindProcess(kit.Int(m.Option("pid"))); e == nil {
p.Signal(syscall.Signal(kit.Int(kit.Select("9", m.Option("signal")))))
}
}
func init() { ice.Cmd("web.chat.dev.machine.process", Process{}) }
func (s Process) getList(cb func(p *process.Process, cmd string)) {
ps, _ := process.Processes()
for _, p := range ps {
cmd, _ := p.Cmdline()
if cmd == "" {
continue
}
cb(p, cmd)
}
}

View File

@ -0,0 +1 @@
export ctx_dev={{.Option "domain"}}{{.Option "ctx_env"}}; {{.Option "ctx_cli"}} binary {{.Option "ctx_arg"}}