网络编程
位置:首页>> 网络编程>> Go语言>> golang通过node_exporter监控GPU及cpu频率、温度的代码

golang通过node_exporter监控GPU及cpu频率、温度的代码

作者:爷来辣  发布时间:2024-02-04 14:53:22 

标签:golang,监控,GPU

导语:通过node_exporter监控GPU以及cpu频率、温度,不想用一个node_exporter再加一个dcgm,分开监控。我这里监控的是热区的温度。如果需要监控各个cpu核心的温度需要修改一下代码。

结合了https://gitee.com/kevinliu_CQ/node_exporter监控GPU的代码。

加入了cpu的2项自定义监控https://gitee.com/jiaminxu/self_node_exporter

安装一下go

wget https://dl.google.com/go/go1.16.5.linux-amd64.tar.gz
tar -xvf go1.16.5.linux-amd64.tar.gz  
cp -r ./go /usr/local
cd /usr/local/go/bin
cp -r * /usr/bin
mkdir -p $HOME/go
echo "export GOROOT=$HOME/go" >> /etc/profile
echo "export GOPATH=$HOME/gopath" >> /etc/profile
echo "export PATH=$PATH:$GOROOT/bin:$GOPATH/bin" >> /etc/profile
echo "export env -w GO111MODULE=on" >> /etc/profile
echo "export env -w GOPROXY=https://goproxy.io,direct" >> /etc/profile
source /etc/profile
cd /srv/
git clone https://gitee.com/kevinliu_CQ/node_exporter.git
cd node_exporter/
# cp自己的go文件到collector/下
mkdir -p /usr/local/cuda/include
cp -p nvml.h /usr/local/cuda/include
export GOPROXY=https://goproxy.cn
go build

编译完直接使用./node_exporter二进制文件即可。

gitee中已经编译好了amd64的可执行文件。

这个版本添加了Nvidia GPU信息的抓取,所以编译的时候需要nvml.h 复制到/usr/local/cuda/include 目录里面
1.  复制依赖
```bash
mkdir -p /usr/local/cuda/include
cp -p nvml.h /usr/local/cuda/include
```
2.  定义GOPROXY变量
```bash
export GOPROXY=https://goproxy.cn
```
3. 编译项目
```bash
cd /root/gitee_node_exporter
go build
如果报错kit相关需要替换"github.com/go-kit/log"为"github.com/go-kit/kit/log"
```
4. 在如果一切正常在目录中会生成node_exporter的可执行二进制文件
5. 运行即可
```bash
./node_exporter --web.listen-address=":19200"
```
6. 打包arm64架构的方法
 * apt install gcc-aarch64-linux-gnu
 * env CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC_FOR_TARGET=gcc-aarch64-linux-gnu CC=aarch64-linux-gnu-gcc go build

CPU 温度(最热的核心)由 x86_pkg_temp 给出。

$ cat /sys/class/thermal/thermal_zone10/type
x86_pkg_temp

然后/sys/class/thermal/thermal_zone10/temp是应该在 i3 状态栏中使用的文件。

附带说明一下,每个内核的温度都可以temp*_input/sys/devices/platform/coretemp.0/hwmon/hwmon*/. 关联temp*_label显示哪个文件与哪个内核相关(在我的例子中是 4 个内核):

.../hwmon*/$  grep "" temp*_label
temp1_label:Physical id 0
temp2_label:Core 0
temp3_label:Core 1
temp4_label:Core 2
temp5_label:Core 3

该文件temp1_input对应于内核的最热值。

如果要获取对应每个cpu的温度 需要

cat /sys/devices/platform/coretemp.0/hwmon/hwmon2/temp1_input

golang通过node_exporter监控GPU及cpu频率、温度的代码

参考

https://qa.1r1g.cn/unix/ask/21339181/

https://cloud.tencent.com/developer/article/1820706

来源:https://blog.csdn.net/xujiamin0022016/article/details/124648395

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com