添加 gopsutil 获取系统信息

This commit is contained in:
Shikong 2023-07-09 23:04:17 +08:00
parent ab855e3ba3
commit 035149ac9a
16 changed files with 436 additions and 2 deletions

View File

@ -12,7 +12,7 @@ const routes = [
] ]
const Router = VueRouter.createRouter({ const Router = VueRouter.createRouter({
history: VueRouter.createWebHistory(), history: VueRouter.createWebHashHistory(),
routes routes
}) })
export default Router export default Router

View File

@ -65,7 +65,6 @@ function getAllEnv(){
console.log(allEnv) console.log(allEnv)
}) })
} }
</script> </script>
<template> <template>

View File

@ -0,0 +1,157 @@
export namespace cpu {
export class InfoStat {
cpu: number;
vendorId: string;
family: string;
model: string;
stepping: number;
physicalId: string;
coreId: string;
cores: number;
modelName: string;
mhz: number;
cacheSize: number;
flags: string[];
microcode: string;
static createFrom(source: any = {}) {
return new InfoStat(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.cpu = source["cpu"];
this.vendorId = source["vendorId"];
this.family = source["family"];
this.model = source["model"];
this.stepping = source["stepping"];
this.physicalId = source["physicalId"];
this.coreId = source["coreId"];
this.cores = source["cores"];
this.modelName = source["modelName"];
this.mhz = source["mhz"];
this.cacheSize = source["cacheSize"];
this.flags = source["flags"];
this.microcode = source["microcode"];
}
}
}
export namespace disk {
export class PartitionStat {
device: string;
mountpoint: string;
fstype: string;
opts: string;
static createFrom(source: any = {}) {
return new PartitionStat(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.device = source["device"];
this.mountpoint = source["mountpoint"];
this.fstype = source["fstype"];
this.opts = source["opts"];
}
}
export class UsageStat {
path: string;
fstype: string;
total: number;
free: number;
used: number;
usedPercent: number;
inodesTotal: number;
inodesUsed: number;
inodesFree: number;
inodesUsedPercent: number;
static createFrom(source: any = {}) {
return new UsageStat(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.path = source["path"];
this.fstype = source["fstype"];
this.total = source["total"];
this.free = source["free"];
this.used = source["used"];
this.usedPercent = source["usedPercent"];
this.inodesTotal = source["inodesTotal"];
this.inodesUsed = source["inodesUsed"];
this.inodesFree = source["inodesFree"];
this.inodesUsedPercent = source["inodesUsedPercent"];
}
}
}
export namespace net {
export class Addr {
ip: string;
port: number;
static createFrom(source: any = {}) {
return new Addr(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.ip = source["ip"];
this.port = source["port"];
}
}
export class ConnectionStat {
fd: number;
family: number;
type: number;
localaddr: Addr;
remoteaddr: Addr;
status: string;
uids: number[];
pid: number;
static createFrom(source: any = {}) {
return new ConnectionStat(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.fd = source["fd"];
this.family = source["family"];
this.type = source["type"];
this.localaddr = this.convertValues(source["localaddr"], Addr);
this.remoteaddr = this.convertValues(source["remoteaddr"], Addr);
this.status = source["status"];
this.uids = source["uids"];
this.pid = source["pid"];
}
convertValues(a: any, classs: any, asMap: boolean = false): any {
if (!a) {
return a;
}
if (a.slice) {
return (a as any[]).map(elem => this.convertValues(elem, classs));
} else if ("object" === typeof a) {
if (asMap) {
for (const key of Object.keys(a)) {
a[key] = new classs(a[key]);
}
return a;
}
return new classs(a);
}
return a;
}
}
}

View File

@ -0,0 +1,16 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {cpu} from '../models';
import {disk} from '../models';
import {net} from '../models';
import {system} from '../models';
export function GetCpuInfo():Promise<Array<cpu.InfoStat>>;
export function GetDiskPartitions():Promise<Array<disk.PartitionStat>>;
export function GetDiskUsage(arg1:string):Promise<disk.UsageStat>;
export function GetNetWorkConnection():Promise<Array<net.ConnectionStat>>;
export function ScanDir(arg1:string):Promise<Array<system.FileEntry>>;

View File

@ -0,0 +1,23 @@
// @ts-check
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export function GetCpuInfo() {
return window['go']['system']['InfoUtils']['GetCpuInfo']();
}
export function GetDiskPartitions() {
return window['go']['system']['InfoUtils']['GetDiskPartitions']();
}
export function GetDiskUsage(arg1) {
return window['go']['system']['InfoUtils']['GetDiskUsage'](arg1);
}
export function GetNetWorkConnection() {
return window['go']['system']['InfoUtils']['GetNetWorkConnection']();
}
export function ScanDir(arg1) {
return window['go']['system']['InfoUtils']['ScanDir'](arg1);
}

View File

@ -5,6 +5,7 @@ go 1.18
require ( require (
github.com/gen2brain/beeep v0.0.0-20230602101333-f384c29b62dd github.com/gen2brain/beeep v0.0.0-20230602101333-f384c29b62dd
github.com/mutecomm/go-sqlcipher/v4 v4.4.2 github.com/mutecomm/go-sqlcipher/v4 v4.4.2
github.com/shirou/gopsutil v2.21.11+incompatible
github.com/wailsapp/wails/v2 v2.5.1 github.com/wailsapp/wails/v2 v2.5.1
xorm.io/xorm v1.3.2 xorm.io/xorm v1.3.2
) )
@ -34,10 +35,13 @@ require (
github.com/samber/lo v1.27.1 // indirect github.com/samber/lo v1.27.1 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/tkrajina/go-reflector v0.5.5 // indirect github.com/tkrajina/go-reflector v0.5.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect github.com/valyala/fasttemplate v1.2.1 // indirect
github.com/wailsapp/mimetype v1.4.1 // indirect github.com/wailsapp/mimetype v1.4.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
golang.org/x/crypto v0.1.0 // indirect golang.org/x/crypto v0.1.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/net v0.7.0 // indirect golang.org/x/net v0.7.0 // indirect

View File

@ -347,6 +347,8 @@ github.com/samber/lo v1.27.1/go.mod h1:it33p9UtPMS7z72fP4gw/EIfQB2eI8ke7GR2wc6+R
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shirou/gopsutil v2.21.11+incompatible h1:lOGOyCG67a5dv2hq5Z1BLDUqqKp3HkbjPcz5j6XMS0U=
github.com/shirou/gopsutil v2.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4=
github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shopspring/decimal v0.0.0-20200227202807-02e2044944cc/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
@ -377,6 +379,10 @@ github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpP
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk= github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af h1:6yITBqGTE2lEeTPG04SN9W+iWHCRyHqlVYILiSXziwk=
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o= github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af/go.mod h1:4F09kP5F+am0jAwlQLddpoMDM+iewkxxt6nxUQ5nq5o=
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M= github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
github.com/tklauser/go-sysconf v0.3.11 h1:89WgdJhk5SNwJfu+GKyYveZ4IaJ7xAkecBo+KdJV0CM=
github.com/tklauser/go-sysconf v0.3.11/go.mod h1:GqXfhXY3kiPa0nAXPDIQIWzJbMCB7AmcWpGR8lSZfqI=
github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYms=
github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ5UVIcaL4=
github.com/tkrajina/go-reflector v0.5.5 h1:gwoQFNye30Kk7NrExj8zm3zFtrGPqOkzFMLuQZg1DtQ= github.com/tkrajina/go-reflector v0.5.5 h1:gwoQFNye30Kk7NrExj8zm3zFtrGPqOkzFMLuQZg1DtQ=
github.com/tkrajina/go-reflector v0.5.5/go.mod h1:ECbqLgccecY5kPmPmXg1MrHW585yMcDkVl6IvJe64T4= github.com/tkrajina/go-reflector v0.5.5/go.mod h1:ECbqLgccecY5kPmPmXg1MrHW585yMcDkVl6IvJe64T4=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
@ -392,6 +398,8 @@ github.com/wailsapp/wails/v2 v2.5.1 h1:mfG+2kWqQXYOwdgI43HEILjOZDXbk5woPYI3jP2b+
github.com/wailsapp/wails/v2 v2.5.1/go.mod h1:jbOZbcr/zm79PxXxAjP8UoVlDd9wLW3uDs+isIthDfs= github.com/wailsapp/wails/v2 v2.5.1/go.mod h1:jbOZbcr/zm79PxXxAjP8UoVlDd9wLW3uDs+isIthDfs=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw=
github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0= github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
@ -503,6 +511,7 @@ golang.org/x/sys v0.0.0-20210902050250-f475640dd07b/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/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-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/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-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@ -0,0 +1,40 @@
package sqlite
import (
"changeme/lib/utils"
"fmt"
"log"
"path/filepath"
"xorm.io/xorm"
)
var u = utils.Utils{}
func init() {
dbPath := filepath.Join(u.CurrentDir(), "test.db")
fmt.Printf("dbPath => %v\n", dbPath)
// 加密
//dataSourceName := fmt.Sprintf("file:%s?_pragma_key=%s", dbPath, "shikong")
// 不加密
dataSourceName := fmt.Sprintf("file:%s?", dbPath)
db, err := xorm.NewEngine("sqlite3", dataSourceName)
if err != nil {
log.Fatal(err)
return
}
type SqliteMaster struct {
Type string `xorm:"type"`
Name string `xorm:"name"`
TableName string `xorm:"tbl_name"`
RootPage int `xorm:"rootpage"`
Sql string `xorm:"sql"`
}
all := make([]SqliteMaster, 0)
_ = db.Table("sqlite_master").Find(&all)
for _, item := range all {
fmt.Printf("%+v\n", item)
}
}

View File

@ -0,0 +1,10 @@
package system
import (
"github.com/shirou/gopsutil/cpu"
)
func GetCpuInfo() []cpu.InfoStat {
infoStats, _ := cpu.Info()
return infoStats
}

View File

@ -0,0 +1,56 @@
package system
import (
"github.com/shirou/gopsutil/disk"
"os"
"path/filepath"
"time"
)
const TypeDisk = "disk"
func GetDiskPartitions() []disk.PartitionStat {
partitions, _ := disk.Partitions(true)
return partitions
}
func GetDiskUsage(diskPath string) *disk.UsageStat {
usageStat, _ := disk.Usage(diskPath)
return usageStat
}
func GetIOCounters() map[string]disk.IOCountersStat {
info, _ := disk.IOCounters()
return info
}
type FileEntry struct {
Path string
Name string
IsDir bool
IsSymlink bool
ModTime time.Time
Size int64
}
func ScanDir(path string) ([]*FileEntry, error) {
entries, err := os.ReadDir(path)
data := make([]*FileEntry, 0, len(entries))
for _, entry := range entries {
info, _ := entry.Info()
_path := filepath.Join(path, entry.Name())
info.Size()
info.ModTime()
data = append(data, &FileEntry{
Path: _path,
Name: entry.Name(),
IsDir: entry.IsDir(),
IsSymlink: entry.Type() == os.ModeSymlink,
Size: info.Size(),
ModTime: info.ModTime(),
})
}
return data, err
}

View File

@ -0,0 +1,13 @@
package system
import "github.com/shirou/gopsutil/mem"
func GetVirtualMemory() *mem.VirtualMemoryStat {
stat, _ := mem.VirtualMemory()
return stat
}
func GetSwapMemory() *mem.SwapMemoryStat {
stat, _ := mem.SwapMemory()
return stat
}

View File

@ -0,0 +1,8 @@
package system
import "github.com/shirou/gopsutil/net"
func GetNetWorkConnection() []net.ConnectionStat {
info, _ := net.Connections("all")
return info
}

View File

@ -0,0 +1,13 @@
package system
import "github.com/shirou/gopsutil/process"
func GetAllProcessPid() []int32 {
pidList, _ := process.Pids()
return pidList
}
func GetProcessInfo(pid int32) *process.Process {
info, _ := process.NewProcess(pid)
return info
}

View File

@ -0,0 +1,30 @@
package system
import (
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/net"
)
type InfoUtils struct {
}
func (i *InfoUtils) GetCpuInfo() []cpu.InfoStat {
return GetCpuInfo()
}
func (i *InfoUtils) GetDiskPartitions() []disk.PartitionStat {
return GetDiskPartitions()
}
func (i *InfoUtils) GetDiskUsage(diskPath string) *disk.UsageStat {
return GetDiskUsage(diskPath)
}
func (i *InfoUtils) ScanDir(path string) ([]*FileEntry, error) {
return ScanDir(path)
}
func (i *InfoUtils) GetNetWorkConnection() []net.ConnectionStat {
return GetNetWorkConnection()
}

View File

@ -0,0 +1,54 @@
package system
import (
"changeme/backend/utils"
"fmt"
"path/filepath"
"testing"
)
func TestCpu(t *testing.T) {
fmt.Println(GetCpuInfo())
}
func TestMemory(t *testing.T) {
fmt.Println(GetVirtualMemory())
fmt.Println(GetSwapMemory())
}
func TestDisk(t *testing.T) {
partitions := GetDiskPartitions()
fmt.Println(partitions)
for _, partition := range partitions {
fmt.Println(GetDiskUsage(partition.Device))
}
fmt.Println(GetIOCounters())
}
func TestNetwork(t *testing.T) {
fmt.Println(GetNetWorkConnection())
}
func TestProcess(t *testing.T) {
processList := GetAllProcessPid()
for _, pid := range processList {
fmt.Println(GetProcessInfo(pid))
}
}
func TestScanDir(t *testing.T) {
fmt.Println(filepath.IsAbs("E:"), filepath.IsAbs("E:/"))
path, err := filepath.Abs("E:/")
if err != nil {
fmt.Println(err)
}
data, err := ScanDir(path)
if err != nil {
fmt.Println(err)
}
for _, entry := range data {
fmt.Printf("%+v\n", utils.Json(entry))
}
}

View File

@ -2,6 +2,7 @@ package main
import ( import (
"changeme/lib/env" "changeme/lib/env"
"changeme/lib/system"
"changeme/lib/utils" "changeme/lib/utils"
"embed" "embed"
@ -31,6 +32,7 @@ func main() {
app, app,
&env.Env{}, &env.Env{},
&utils.Utils{}, &utils.Utils{},
&system.InfoUtils{},
}, },
Debug: options.Debug{ Debug: options.Debug{
OpenInspectorOnStartup: true, OpenInspectorOnStartup: true,