mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-18 02:29:22 +08:00
feat: 添加对Darwin和FreeBSD的GPU及操作系统名称获取功能
This commit is contained in:
18
.github/workflows/release.yml
vendored
18
.github/workflows/release.yml
vendored
@@ -1,4 +1,4 @@
|
||||
name: Build and Attach Binaries to Release
|
||||
name: Release Binaries
|
||||
|
||||
on:
|
||||
release:
|
||||
@@ -13,8 +13,15 @@ jobs:
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
goos: [windows, linux]
|
||||
goarch: [amd64, arm64]
|
||||
exclude:
|
||||
- goos: windows
|
||||
goarch: arm
|
||||
- goos: darwin
|
||||
goarch: 386
|
||||
- goos: darwin
|
||||
goarch: arm
|
||||
goos: [windows, linux, darwin, freebsd]
|
||||
goarch: [amd64, arm64, 386, arm]
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
@@ -35,6 +42,9 @@ jobs:
|
||||
BINARY_NAME=komari-agent-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||
if [ "${{ matrix.goos }}" = "windows" ]; then
|
||||
BINARY_NAME=${BINARY_NAME}.exe
|
||||
elif [ "${{ matrix.goos }}" = "darwin" ]; then
|
||||
# macOS binaries typically do not have a file extension
|
||||
true
|
||||
fi
|
||||
go build -trimpath -ldflags="-s -w -X github.com/komari-monitor/komari-agent/update.CurrentVersion=${VERSION}" -o $BINARY_NAME
|
||||
|
||||
@@ -45,6 +55,8 @@ jobs:
|
||||
BINARY_NAME=komari-agent-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||
if [ "${{ matrix.goos }}" = "windows" ]; then
|
||||
BINARY_NAME=${BINARY_NAME}.exe
|
||||
elif [ "${{ matrix.goos }}" = "darwin" ]; then
|
||||
true
|
||||
fi
|
||||
gh release upload ${{ github.event.release.tag_name }} $BINARY_NAME --repo ${{ github.repository }}
|
||||
shell: bash
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@ agent.json
|
||||
komari-agent.exe
|
||||
komari-agent
|
||||
.komari-agent.exe.old
|
||||
build/
|
62
build_all.sh
Normal file
62
build_all.sh
Normal file
@@ -0,0 +1,62 @@
|
||||
#!/bin/bash
|
||||
|
||||
# 定义颜色
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
WHITE='\033[0;37m'
|
||||
NC='\033[0m' # 无颜色
|
||||
|
||||
# 定义操作系统和架构列表
|
||||
OS_LIST=("windows" "linux" "darwin" "freebsd")
|
||||
ARCH_LIST=("amd64" "arm64" "386" "arm")
|
||||
|
||||
# 创建构建目录
|
||||
mkdir -p ./build
|
||||
|
||||
# 获取当前版本
|
||||
VERSION=$(git describe --tags --abbrev=0 2>/dev/null || echo "dev")
|
||||
|
||||
# 初始化失败列表
|
||||
FAILED_BUILDS=()
|
||||
|
||||
# 遍历操作系统和架构组合
|
||||
for GOOS in "${OS_LIST[@]}"; do
|
||||
for GOARCH in "${ARCH_LIST[@]}"; do
|
||||
# 排除 windows/arm, darwin/386 和 darwin/arm
|
||||
if { [ "$GOOS" = "windows" ] && [ "$GOARCH" = "arm" ]; } || \
|
||||
{ [ "$GOOS" = "darwin" ] && { [ "$GOARCH" = "386" ] || [ "$GOARCH" = "arm" ]; }; }; then
|
||||
continue
|
||||
fi
|
||||
|
||||
echo -e "Building for $GOOS/$GOARCH..."
|
||||
|
||||
# 设置输出二进制文件名
|
||||
BINARY_NAME="komari-agent-${GOOS}-${GOARCH}"
|
||||
if [ "$GOOS" = "windows" ]; then
|
||||
BINARY_NAME="${BINARY_NAME}.exe"
|
||||
fi
|
||||
|
||||
# 构建二进制文件
|
||||
env GOOS=$GOOS GOARCH=$GOARCH CGO_ENABLED=0 go build -trimpath -ldflags="-s -w -X github.com/komari-monitor/komari-agent/update.CurrentVersion=${VERSION}" -o "./build/$BINARY_NAME"
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo -e "${RED}Failed to build for $GOOS/$GOARCH${NC}"
|
||||
FAILED_BUILDS+=("$GOOS/$GOARCH")
|
||||
else
|
||||
echo -e "${GREEN}Successfully built $BINARY_NAME${NC}"
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
# 输出失败的构建
|
||||
if [ ${#FAILED_BUILDS[@]} -ne 0 ]; then
|
||||
echo -e "\n${RED}The following builds failed:${NC}"
|
||||
for BUILD in "${FAILED_BUILDS[@]}"; do
|
||||
echo -e "${RED}- $BUILD${NC}"
|
||||
done
|
||||
else
|
||||
echo -e "\n${GREEN}All builds completed successfully.${NC}"
|
||||
fi
|
||||
|
||||
# 提示构建完成
|
||||
echo -e "\nBinaries are in the ./build directory."
|
28
monitoring/unit/gpu_darwin.go
Normal file
28
monitoring/unit/gpu_darwin.go
Normal file
@@ -0,0 +1,28 @@
|
||||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GpuName returns the name of the GPU on Darwin (macOS)
|
||||
func GpuName() string {
|
||||
cmd := exec.Command("system_profiler", "SPDisplaysDataType")
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "Unknown"
|
||||
}
|
||||
|
||||
lines := strings.Split(string(output), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.HasPrefix(line, "Chipset Model:") {
|
||||
return strings.TrimSpace(strings.TrimPrefix(line, "Chipset Model:"))
|
||||
}
|
||||
}
|
||||
|
||||
return "Unknown"
|
||||
}
|
28
monitoring/unit/gpu_freebsd.go
Normal file
28
monitoring/unit/gpu_freebsd.go
Normal file
@@ -0,0 +1,28 @@
|
||||
//go:build freebsd
|
||||
// +build freebsd
|
||||
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// GpuName returns the name of the GPU on FreeBSD
|
||||
func GpuName() string {
|
||||
cmd := exec.Command("pciconf", "-lv")
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "Unknown"
|
||||
}
|
||||
|
||||
lines := strings.Split(string(output), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.Contains(line, "VGA") || strings.Contains(line, "Display") {
|
||||
return line
|
||||
}
|
||||
}
|
||||
|
||||
return "Unknown"
|
||||
}
|
21
monitoring/unit/os_darwin.go
Normal file
21
monitoring/unit/os_darwin.go
Normal file
@@ -0,0 +1,21 @@
|
||||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// OSName returns the name of the operating system on Darwin (macOS)
|
||||
func OSName() string {
|
||||
cmd := exec.Command("sw_vers", "-productName")
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "macOS"
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(string(output))
|
||||
return name
|
||||
}
|
21
monitoring/unit/os_freebsd.go
Normal file
21
monitoring/unit/os_freebsd.go
Normal file
@@ -0,0 +1,21 @@
|
||||
//go:build freebsd
|
||||
// +build freebsd
|
||||
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// OSName returns the name of the operating system on FreeBSD
|
||||
func OSName() string {
|
||||
cmd := exec.Command("uname", "-sr")
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "FreeBSD"
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(string(output))
|
||||
return name
|
||||
}
|
27
monitoring/unit/process_darwin.go
Normal file
27
monitoring/unit/process_darwin.go
Normal file
@@ -0,0 +1,27 @@
|
||||
//go:build darwin
|
||||
// +build darwin
|
||||
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ProcessCount returns the number of running processes on Darwin (macOS)
|
||||
func ProcessCount() (count int) {
|
||||
return processCountDarwin()
|
||||
}
|
||||
|
||||
// processCountDarwin counts processes using the `ps` command
|
||||
func processCountDarwin() (count int) {
|
||||
cmd := exec.Command("ps", "-A")
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Count the number of lines in the output, excluding the header line
|
||||
lines := strings.Split(string(output), "\n")
|
||||
return len(lines) - 1
|
||||
}
|
27
monitoring/unit/process_freebsd.go
Normal file
27
monitoring/unit/process_freebsd.go
Normal file
@@ -0,0 +1,27 @@
|
||||
//go:build freebsd
|
||||
// +build freebsd
|
||||
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ProcessCount returns the number of running processes on FreeBSD
|
||||
func ProcessCount() (count int) {
|
||||
return processCountFreeBSD()
|
||||
}
|
||||
|
||||
// processCountFreeBSD counts processes using the `ps` command
|
||||
func processCountFreeBSD() (count int) {
|
||||
cmd := exec.Command("ps", "-ax")
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
// Count the number of lines in the output, excluding the header line
|
||||
lines := strings.Split(string(output), "\n")
|
||||
return len(lines) - 1
|
||||
}
|
Reference in New Issue
Block a user