Skip to content

Commit

Permalink
feat: support openwrt getip (hootrhino#108)
Browse files Browse the repository at this point in the history
* feat: support openwrt getip

* feat: support get os dist

* feat: support osDist api
  • Loading branch information
lion-brave authored Jun 3, 2023
1 parent 26540aa commit de1b1ab
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 7 deletions.
18 changes: 11 additions & 7 deletions plugin/http_server/system_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,17 @@ func System(c *gin.Context, hh *HttpApiServer, e typex.RuleX) {
runtime.ReadMemStats(&m)
ip, err0 := utils.HostNameI()
hardWareInfo := map[string]interface{}{
"version": e.Version().Version,
"diskInfo": calculateDiskInfo(diskInfo),
"systemMem": bToMb(m.Sys),
"allocMem": bToMb(m.Alloc),
"totalMem": bToMb(m.TotalAlloc),
"cpuPercent": calculateCpuPercent(cpuPercent),
"osArch": runtime.GOOS + "-" + runtime.GOARCH,
"version": e.Version().Version,
"diskInfo": calculateDiskInfo(diskInfo),
"systemMem": bToMb(m.Sys),
"allocMem": bToMb(m.Alloc),
"totalMem": bToMb(m.TotalAlloc),
"cpuPercent": calculateCpuPercent(cpuPercent),
"osArch": runtime.GOOS + "-" + runtime.GOARCH,
"osDist": func() string {
v, _ := utils.GetOSDistribution()
return v
},
"startedTime": StartedTime,
"ip": func() []string {
if err0 != nil {
Expand Down
59 changes: 59 additions & 0 deletions utils/os_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package utils
import (
"fmt"
"os"
"os/exec"
"runtime"
"strings"

"github.com/hootrhino/rulex/glogger"
)
Expand Down Expand Up @@ -56,3 +58,60 @@ func BtoMB(bytes uint64) float64 {
func BToMb(b uint64) uint64 {
return b / 1024 / 1024
}

/*
*
* 获取操作系统发行版版本
runtime.GOARCH:
386: 32-bit Intel/AMD x86 architecture
amd64: 64-bit Intel/AMD x86 architecture
arm: ARM architecture (32-bit)
arm64: ARM architecture (64-bit)
ppc64: 64-bit PowerPC architecture
ppc64le: 64-bit little-endian PowerPC architecture
mips: MIPS architecture (32-bit)
mips64: MIPS architecture (64-bit)
s390x: IBM System z architecture (64-bit)
wasm: WebAssembly architecture
runtime.GOOS:
darwin: macOS
freebsd: FreeBSD
linux: Linux
windows: Windows
netbsd: NetBSD
openbsd: OpenBSD
plan9: Plan 9
dragonfly: DragonFly BSD
*
*/
func GetOSDistribution() (string, error) {
if runtime.GOOS == "windows" {
return runtime.GOOS, nil
}
// Linux 有很多发行版, 目前特别要识别一下Openwrt
if runtime.GOOS == "linux" {
cmd := exec.Command("cat", "/etc/issue")
output, err := cmd.Output()
if err != nil {
return runtime.GOOS, err
}
osIssue := strings.ToLower(string(output))
if strings.Contains((osIssue), "openwrt") {
return "openwrt", nil
}
if strings.Contains((osIssue), "ubuntu") {
return "ubuntu", nil
}
if strings.Contains((osIssue), "debian") {
return "debian", nil
}
if strings.Contains((osIssue), "armbian") {
return "armbian", nil
}
}
return runtime.GOOS, nil
}
17 changes: 17 additions & 0 deletions utils/os_util_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ import (
*
*/
func HostNameI() ([]string, error) {
dist, _ := GetOSDistribution()
if dist == "openwrt" {
line := `ip addr show | awk '/inet / {print $2}' | awk 'BEGIN{FS="/"} {split($0, arr, "/"); print arr[1]}'`
cmd := exec.Command("sh", "-c", line)
output, err := cmd.Output()
if err != nil {
return []string{}, err
}
result := strings.TrimSpace(string(output))
ips := []string{}
for _, v := range strings.Split(result, "\n") {
if v != "127.0.0.1" {
ips = append(ips, v)
}
}
return ips, nil
}
cmd := exec.Command("hostname", "-I")
data, err1 := cmd.Output()
if err1 != nil {
Expand Down

0 comments on commit de1b1ab

Please sign in to comment.