Commit d9cbd940 authored by Your Name's avatar Your Name

add win mac

parent e4a1e692
Windows IP 配置
主机名 . . . . . . . . . . . . . : WUBAN-AI-SERVER
主 DNS 后缀 . . . . . . . . . . . :
节点类型 . . . . . . . . . . . . : 混合
IP 路由已启用 . . . . . . . . . . : 否
WINS 代理已启用 . . . . . . . . . : 否
未知适配器 Clash:
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : Clash Tunnel
物理地址. . . . . . . . . . . . . :
DHCP 已启用 . . . . . . . . . . . : 否
自动配置已启用. . . . . . . . . . : 是
IPv4 地址 . . . . . . . . . . . . : 198.18.0.1(首选)
子网掩码 . . . . . . . . . . . . : 255.255.0.0
默认网关. . . . . . . . . . . . . :
DNS 服务器 . . . . . . . . . . . : 198.18.0.2
TCPIP 上的 NetBIOS . . . . . . . : 已启用
以太网适配器 Onboard LAN 2:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : Intel(R) I350 Gigabit Network Connection
物理地址. . . . . . . . . . . . . : AC-1F-6B-80-CC-47
DHCP 已启用 . . . . . . . . . . . : 是
自动配置已启用. . . . . . . . . . : 是
以太网适配器 Onboard LAN 1:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : Intel(R) I350 Gigabit Network Connection #2
物理地址. . . . . . . . . . . . . : AC-1F-6B-80-CC-46
DHCP 已启用 . . . . . . . . . . . : 是
自动配置已启用. . . . . . . . . . : 是
以太网适配器 vEthernet (Default Switch):
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter
物理地址. . . . . . . . . . . . . : 00-15-5D-B8-01-19
DHCP 已启用 . . . . . . . . . . . : 否
自动配置已启用. . . . . . . . . . : 是
本地链接 IPv6 地址. . . . . . . . : fe80::4d75:233c:5637:565c%52(首选)
IPv4 地址 . . . . . . . . . . . . : 172.19.224.1(首选)
子网掩码 . . . . . . . . . . . . : 255.255.240.0
默认网关. . . . . . . . . . . . . :
DHCPv6 IAID . . . . . . . . . . . : 872420701
DHCPv6 客户端 DUID . . . . . . . : 00-01-00-01-2C-84-96-8D-AC-1F-6B-80-CC-47
DNS 服务器 . . . . . . . . . . . : fec0:0:0:ffff::1%1
fec0:0:0:ffff::2%1
fec0:0:0:ffff::3%1
TCPIP 上的 NetBIOS . . . . . . . : 已启用
以太网适配器 vEthernet (VertexNet):
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #2
物理地址. . . . . . . . . . . . . : C8-3A-35-A4-6F-45
DHCP 已启用 . . . . . . . . . . . : 是
自动配置已启用. . . . . . . . . . : 是
本地链接 IPv6 地址. . . . . . . . : fe80::7626:8981:5013:175b%23(首选)
IPv4 地址 . . . . . . . . . . . . : 192.168.1.118(首选)
子网掩码 . . . . . . . . . . . . : 255.255.255.0
获得租约的时间 . . . . . . . . . : 2024年4月28日 8:52:38
租约过期的时间 . . . . . . . . . : 2024年4月29日 16:52:59
默认网关. . . . . . . . . . . . . : 192.168.1.1
DHCP 服务器 . . . . . . . . . . . : 192.168.1.1
DHCPv6 IAID . . . . . . . . . . . : 902314549
DHCPv6 客户端 DUID . . . . . . . : 00-01-00-01-2C-84-96-8D-AC-1F-6B-80-CC-47
DNS 服务器 . . . . . . . . . . . : fec0:0:0:ffff::1%1
fec0:0:0:ffff::2%1
fec0:0:0:ffff::3%1
192.168.1.1
TCPIP 上的 NetBIOS . . . . . . . : 已启用
未知适配器 本地连接:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : TeamViewer VPN Adapter
物理地址. . . . . . . . . . . . . : 00-FF-36-FC-A3-1E
DHCP 已启用 . . . . . . . . . . . : 是
自动配置已启用. . . . . . . . . . : 是
无线局域网适配器 本地连接* 11:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter #3
物理地址. . . . . . . . . . . . . : CA-3A-35-A4-6F-45
DHCP 已启用 . . . . . . . . . . . : 是
自动配置已启用. . . . . . . . . . : 是
无线局域网适配器 本地连接* 12:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter #4
物理地址. . . . . . . . . . . . . : EA-3A-35-A4-6F-45
DHCP 已启用 . . . . . . . . . . . : 是
自动配置已启用. . . . . . . . . . : 是
以太网适配器 vEthernet (WSL):
连接特定的 DNS 后缀 . . . . . . . :
描述. . . . . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #3
物理地址. . . . . . . . . . . . . : 00-15-5D-56-16-74
DHCP 已启用 . . . . . . . . . . . : 否
自动配置已启用. . . . . . . . . . : 是
本地链接 IPv6 地址. . . . . . . . : fe80::7054:fc39:cd14:3e91%68(首选)
IPv4 地址 . . . . . . . . . . . . : 172.22.16.1(首选)
子网掩码 . . . . . . . . . . . . : 255.255.240.0
默认网关. . . . . . . . . . . . . :
DHCPv6 IAID . . . . . . . . . . . : 1140856157
DHCPv6 客户端 DUID . . . . . . . : 00-01-00-01-2C-84-96-8D-AC-1F-6B-80-CC-47
DNS 服务器 . . . . . . . . . . . : fec0:0:0:ffff::1%1
fec0:0:0:ffff::2%1
fec0:0:0:ffff::3%1
TCPIP 上的 NetBIOS . . . . . . . : 已启用
Windows IP Configuration
Host Name . . . . . . . . . . . . : WUBAN-AI-SERVER
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Hybrid
IP Routing Enabled. . . . . . . . : No
WINS Proxy Enabled. . . . . . . . : No
Unknown adapter Clash:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Clash Tunnel
Physical Address. . . . . . . . . :
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
IPv4 Address. . . . . . . . . . . : 198.18.0.1(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.0.0
Default Gateway . . . . . . . . . :
DNS Servers . . . . . . . . . . . : 198.18.0.2
NetBIOS over Tcpip. . . . . . . . : Enabled
Ethernet adapter Onboard LAN 2:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Intel(R) I350 Gigabit Network Connection
Physical Address. . . . . . . . . : AC-1F-6B-80-CC-47
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Ethernet adapter Onboard LAN 1:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Intel(R) I350 Gigabit Network Connection #2
Physical Address. . . . . . . . . : AC-1F-6B-80-CC-46
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Ethernet adapter vEthernet (Default Switch):
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter
Physical Address. . . . . . . . . : 00-15-5D-B8-01-19
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : fe80::4d75:233c:5637:565c%52(Preferred)
IPv4 Address. . . . . . . . . . . : 172.19.224.1(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.240.0
Default Gateway . . . . . . . . . :
DHCPv6 IAID . . . . . . . . . . . : 872420701
DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-2C-84-96-8D-AC-1F-6B-80-CC-47
DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
fec0:0:0:ffff::2%1
fec0:0:0:ffff::3%1
NetBIOS over Tcpip. . . . . . . . : Enabled
Ethernet adapter vEthernet (VertexNet):
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #2
Physical Address. . . . . . . . . : C8-3A-35-A4-6F-45
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : fe80::7626:8981:5013:175b%23(Preferred)
IPv4 Address. . . . . . . . . . . : 192.168.1.118(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Lease Obtained. . . . . . . . . . : Sunday, April 28, 2024 8:52:38 AM
Lease Expires . . . . . . . . . . : Monday, April 29, 2024 6:49:44 PM
Default Gateway . . . . . . . . . : 192.168.1.1
DHCP Server . . . . . . . . . . . : 192.168.1.1
DHCPv6 IAID . . . . . . . . . . . : 902314549
DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-2C-84-96-8D-AC-1F-6B-80-CC-47
DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
fec0:0:0:ffff::2%1
fec0:0:0:ffff::3%1
192.168.1.1
NetBIOS over Tcpip. . . . . . . . : Enabled
Unknown adapter 本地连接:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : TeamViewer VPN Adapter
Physical Address. . . . . . . . . : 00-FF-36-FC-A3-1E
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Wireless LAN adapter 本地连接* 11:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter #3
Physical Address. . . . . . . . . : CA-3A-35-A4-6F-45
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Wireless LAN adapter 本地连接* 12:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Microsoft Wi-Fi Direct Virtual Adapter #4
Physical Address. . . . . . . . . : EA-3A-35-A4-6F-45
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
Ethernet adapter vEthernet (WSL):
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Hyper-V Virtual Ethernet Adapter #3
Physical Address. . . . . . . . . : 00-15-5D-56-16-74
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : fe80::7054:fc39:cd14:3e91%68(Preferred)
IPv4 Address. . . . . . . . . . . : 172.22.16.1(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.240.0
Default Gateway . . . . . . . . . :
DHCPv6 IAID . . . . . . . . . . . : 1140856157
DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-2C-84-96-8D-AC-1F-6B-80-CC-47
DNS Servers . . . . . . . . . . . : fec0:0:0:ffff::1%1
fec0:0:0:ffff::2%1
fec0:0:0:ffff::3%1
NetBIOS over Tcpip. . . . . . . . : Enabled
package main
import "testing"
// package main
func TestMemUtil(t *testing.T) {
// import (
// "fmt"
// "io/ioutil"
// "log"
// "golang.org/x/text/encoding/simplifiedchinese"
// "golang.org/x/text/transform"
// )
cli, err := NewProCli("http://192.168.1.21:9090")
// func main() {
// // 读取文件
// content, err := ioutil.ReadFile("your_file.txt")
// if err != nil {
// log.Fatal(err)
// }
if err != nil {
t.Fatal(err)
}
// // 将字节转换为字符串输出
// decoded, _, err := transform.Bytes(simplifiedchinese.GBK.NewDecoder(), content)
// if err != nil {
// log.Fatal(err)
// }
gpus, err := cli.MemUtil()
// fmt.Println(string(decoded))
// }
for k, v := range gpus {
// func TestMemUtil(t *testing.T) {
t.Log("k", k, "v", v)
}
// cli, err := NewProCli("http://192.168.1.21:9090")
}
// if err != nil {
// t.Fatal(err)
// }
//MemInfo
// gpus, err := cli.MemUtil()
func TestMemInfo(t *testing.T) {
// for k, v := range gpus {
cli, err := NewProCli("http://192.168.1.21:9090")
// t.Log("k", k, "v", v)
// }
if err != nil {
t.Fatal(err)
}
// }
gpus, err := cli.MemInfo()
// //MemInfo
for k, v := range gpus {
// func TestMemInfo(t *testing.T) {
t.Log("k", k, "v", v)
}
// cli, err := NewProCli("http://192.168.1.21:9090")
}
// if err != nil {
// t.Fatal(err)
// }
// gpus, err := cli.MemInfo()
// for k, v := range gpus {
// t.Log("k", k, "v", v)
// }
// }
package main
import (
"bufio"
"bytes"
"context"
"fmt"
"io/ioutil"
"sort"
"strings"
"time"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
type Network struct {
......@@ -15,6 +21,7 @@ type Network struct {
Speed int64 `json:"speed"`
Send int64 `json:"send_rate"`
Recevice int64 `json:"recv_rate"`
Mac string `json:"mac"`
}
func (c *ProApi) Networks() ([]Network, error) {
......@@ -37,6 +44,12 @@ func (c *ProApi) Networks() ([]Network, error) {
return nil, err
}
networkInfo, err := c.NetworkInfo()
if err != nil {
return nil, err
}
res := make([]Network, 0, len(speed))
for k, v := range speed {
......@@ -53,9 +66,26 @@ func (c *ProApi) Networks() ([]Network, error) {
if r, ok := receive[k]; ok {
n.Recevice = r
}
if m, ok := networkInfo[k]; ok {
n.Mac = m
}
res = append(res, n)
}
winRes, err := GetNics("ipconfig")
if err != nil {
return nil, err
}
for _, v := range winRes {
res = append(res, Network{
Device: v.Device,
Mac: v.MAC,
})
}
// 定义排序函数
sortByDevice := func(i, j int) bool {
return res[i].Device < res[j].Device
......@@ -70,6 +100,58 @@ func (c *ProApi) Networks() ([]Network, error) {
//node_network_speed_bytes/1024/1024/1024
//node_network_info
func (c *ProApi) NetworkInfo() (map[string]string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
queryStr := `node_network_info`
result, warnings, err := c.API.Query(ctx, queryStr, time.Now(), v1.WithTimeout(5*time.Second))
if err != nil {
return nil, err
}
if len(warnings) > 0 {
fmt.Printf("Warnings: %v\n", warnings)
}
// fmt.Printf("Result:\n%v \nstring %v \n", result.Type(), result.String())
res := make(map[string]string, 8)
switch {
case result.Type() == model.ValVector:
vectorVal := result.(model.Vector)
for _, elem := range vectorVal {
// for k, v := range elem.Metric {
// fmt.Println("k", k, "v", v)
// }
deviceNameStr := ""
if deviceName, ok := elem.Metric["device"]; ok {
deviceNameStr = string(deviceName)
} else {
continue
}
if addressName, ok := elem.Metric["address"]; ok {
res[deviceNameStr] = string(addressName)
//deviceNameStr = string(addressName)
} else {
continue
}
}
}
return res, nil
}
func (c *ProApi) NetworkSpeed() (map[string]int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
......@@ -203,3 +285,136 @@ func (c *ProApi) NetworkTransmit() (map[string]int64, error) {
return res, nil
}
func readIpconfigFile(file string) ([][]string, error) {
// content, err := ioutil.ReadFile("ipconfig")
content, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
// 将字节转换为字符串输出
decoded, _, err := transform.Bytes(simplifiedchinese.GBK.NewDecoder(), content)
if err != nil {
return nil, err
}
reader := bytes.NewReader(decoded)
scanner := bufio.NewScanner(reader)
begin := false
// // 逐行扫描文件内容
type network []string
networks := make([][]string, 0, 8)
count := 0
var net network
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, " ") && len(line) != 0 {
begin = true
net = append(net, line)
}
if begin == true {
if len(line) == 0 {
count = count + 1
if count == 2 {
networks = append(networks, net)
net = make(network, 0, 8)
begin = false
count = 0
continue
}
}
if strings.HasPrefix(line, " ") {
net = append(net, line)
}
}
}
networks = append(networks, net)
// 检查是否有错误发生在Scanner的过程中
if err := scanner.Err(); err != nil {
return nil, err
}
return networks, nil
}
func GetNics(file string) ([]NIC, error) {
nets, err := readIpconfigFile(file)
if err != nil {
return nil, err
}
res := make([]NIC, 0, len(nets))
for _, v := range nets {
nic := NIC{
Device: v[0],
}
for _, item := range v {
if strings.Contains(item, "描述") || strings.Contains(item, "Description") {
input := item
parts := strings.Split(input, ":")
if len(parts) > 1 {
result := strings.TrimSpace(parts[len(parts)-1]) // 获取冒号后的字符串并去除前后空格
nic.Desc = result
}
}
//物理地址
if strings.Contains(item, "物理地址") || strings.Contains(item, "Physical Address") {
input := item
parts := strings.Split(input, ":")
if len(parts) > 1 {
result := strings.TrimSpace(parts[len(parts)-1]) // 获取冒号后的字符串并去除前后空格
//fmt.Println(result) // 输出结果
nic.MAC = result
}
}
//IPv4 地址
if strings.Contains(item, "IPv4 地址") || strings.Contains(item, "IPv4 Address") {
input := item
parts := strings.Split(input, ":")
if len(parts) > 1 {
result := strings.TrimSpace(parts[len(parts)-1]) // 获取冒号后的字符串并去除前后空格
//fmt.Println(result) // 输出结果
nic.IPV4 = result
}
}
}
res = append(res, nic)
}
return res, nil
}
type NIC struct {
Device string `json:"device"`
Desc string `json:"desc"`
MAC string `json:"mac"`
IPV4 string `json:"ipv4"`
}
package main
import "testing"
import (
"encoding/json"
"fmt"
"testing"
)
func TestNetworkSpeed(t *testing.T) {
......@@ -51,3 +55,20 @@ func TestNetworkTransmit(t *testing.T) {
t.Log("k", k, "v", v)
}
}
func TestIpconfig(t *testing.T) {
res, err := GetNics("ipconfig")
if err != nil {
t.Fatal(err)
}
jsonAsStr, err := json.Marshal(res)
if err != nil {
t.Fatal(err)
}
fmt.Println(string(jsonAsStr))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment