[golang]-prometheus获取物理cpu热区的温度

2023-10-18 00:20

本文主要是介绍[golang]-prometheus获取物理cpu热区的温度,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

导语:golang,通过prometheus获取物理cpu热区的温度

package collectorimport ("bytes""encoding/json""fmt""os/exec""strconv""strings""github.com/go-kit/log""github.com/prometheus/client_golang/prometheus""github.com/shirou/gopsutil/cpu"
)const (// 定义自定义数据指标的子系统名称// 这里可能改成temp_metrics好点tempMetricsSubsystem = "temp_metrics"
)// 定义 tempMetricsCollector 结构体
type tempMetricsCollector struct {logger log.Logger//...
}type TempStat struct {CPU int32 `json:"cpu"`// Temp       float64 `json:"temp"`PhysicalID string `json:"physicalId"`
}func init() {// 在该函数中调用 registerCollector() 函数,注册自定义 tempMetricsCollectorregisterCollector("temp_metrics", defaultEnabled, NewTempMetricsCollector)
}// 定义 tempMetricsCollector 的工厂函数,后续传入 registerCollector() 函数中,以便创建 tempMetricsCollector 对象
func NewTempMetricsCollector(logger log.Logger) (Collector, error) {return &tempMetricsCollector{logger: logger,}, nil
}// 实现 Update() 函数,以便在处理请求时被 Collector.Collect() 调用
func (c *tempMetricsCollector) Update(ch chan<- prometheus.Metric) error {var metricType prometheus.ValueTypemetricType = prometheus.CounterValuem := make(map[string]float64)infos, err := cpu.Info()if err != nil {return fmt.Errorf("couldn't get cpunfo: %s", err)}// 把切片里的有效数据放到map中for _, info := range infos {data, _ := json.MarshalIndent(info, "", " ")var p InfoStaterr := json.Unmarshal(data, &p)if err != nil {fmt.Println("temp json err:", err)}// cpu_no := string(p.CPU)// 获取cpu温度//执行/sys/class/thermal/thermal_zone$cpu_id/temp 并输出返回文本cmd_line1 := fmt.Sprintf("%s%d%s", "cat /sys/class/thermal/thermal_zone", p.CPU, "/temp")cmd1 := exec.Command("sh", "-c", string(cmd_line1))fmt.Println("cmd1 is ", cmd1)// cmd1 := exec.Command("sh", "-c", "fuser -v ", req, " |   grep '[0-9]*[1-9][0-9]'")stdout1 := &bytes.Buffer{}cmd1.Stdout = stdout1cmd1.Run()s2 := stdout1.String()s2 = strings.Replace(s2, " ", "", -1)s2 = strings.Replace(s2, "\n", "", -1)// fmt.Println("s2", s2)fmt.Printf("%#v\n", s2)f1, err1 := strconv.ParseFloat(s2, 64)if err1 != nil {return fmt.Errorf("couldn't change s2 to f1: %s", err)}fmt.Println("f1", f1)s1 := fmt.Sprintf("%s%d", "cpu_temp_", p.CPU)m[s1] = f1// m[s1] = f1 * 0.001fmt.Println("temp map m:", m)for k, v := range m {// `prometheus.MustNewConstMetric()` 返回 `prometheus.constMetric` 对象,由描述信息,指标类型,指标值构成.如下指标// # HELP go_info Information about the Go environment.// # TYPE go_info gauge// go_info{version="go1.14.4"} 1ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(// 描述信息包括 数据指标名称(由 `BuildFQName()`函数组合而成),帮助信息,变量标签,常量标签prometheus.BuildFQName(namespace, tempMetricsSubsystem, k),fmt.Sprintf("Temp metrics field cpu temp %s.", k),nil, nil,),// 返回v为float64类型metricType, v,)}}return nil
}

结果看情况是通过grafana处理还是在代码里直接*0.01

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

参考

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

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

这篇关于[golang]-prometheus获取物理cpu热区的温度的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/228924

相关文章

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR

Prometheus与Grafana在DevOps中的应用与最佳实践

Prometheus 与 Grafana 在 DevOps 中的应用与最佳实践 随着 DevOps 文化和实践的普及,监控和可视化工具已成为 DevOps 工具链中不可或缺的部分。Prometheus 和 Grafana 是其中最受欢迎的开源监控解决方案之一,它们的结合能够为系统和应用程序提供全面的监控、告警和可视化展示。本篇文章将详细探讨 Prometheus 和 Grafana 在 DevO

JS和jQuery获取节点的兄弟,父级,子级元素

原文转自http://blog.csdn.net/duanshuyong/article/details/7562423 先说一下JS的获取方法,其要比JQUERY的方法麻烦很多,后面以JQUERY的方法作对比。 JS的方法会比JQUERY麻烦很多,主要则是因为FF浏览器,FF浏览器会把你的换行也当最DOM元素。 <div id="test"><div></div><div></div

vcpkg子包路径批量获取

获取vcpkg 子包的路径,并拼接为set(CMAKE_PREFIX_PATH “拼接路径” ) import osdef find_directories_with_subdirs(root_dir):# 构建根目录下的 "packages" 文件夹路径root_packages_dir = os.path.join(root_dir, "packages")# 如果 "packages"

Golang进程权限调度包runtime

关于 runtime 包几个方法: Gosched:让当前线程让出 cpu 以让其它线程运行,它不会挂起当前线程,因此当前线程未来会继续执行GOMAXPROCS:设置最大的可同时使用的 CPU 核数Goexit:退出当前 goroutine(但是defer语句会照常执行)NumGoroutine:返回正在执行和排队的任务总数GOOS:目标操作系统NumCPU:返回当前系统的 CPU 核数量 p

Golang 网络爬虫框架gocolly/colly(五)

gcocolly+goquery可以非常好地抓取HTML页面中的数据,但碰到页面是由Javascript动态生成时,用goquery就显得捉襟见肘了。解决方法有很多种: 一,最笨拙但有效的方法是字符串处理,go语言string底层对应字节数组,复制任何长度的字符串的开销都很低廉,搜索性能比较高; 二,利用正则表达式,要提取的数据往往有明显的特征,所以正则表达式写起来比较简单,不必非常严谨; 三,使

Golang网络爬虫框架gocolly/colly(四)

爬虫靠演技,表演得越像浏览器,抓取数据越容易,这是我多年爬虫经验的感悟。回顾下个人的爬虫经历,共分三个阶段:第一阶段,09年左右开始接触爬虫,那时由于项目需要,要访问各大国际社交网站,Facebook,myspace,filcker,youtube等等,国际上叫得上名字的社交网站都爬过,大部分网站提供restful api,有些功能没有api,就只能用http抓包工具分析协议,自己爬;国内的优酷、

Golang网络爬虫框架gocolly/colly(三)

熟悉了《Golang 网络爬虫框架gocolly/colly 一》和《Golang 网络爬虫框架gocolly/colly 二》之后就可以在网络上爬取大部分数据了。本文接下来将爬取中证指数有限公司提供的行业市盈率。(http://www.csindex.com.cn/zh-CN/downloads/industry-price-earnings-ratio) 定义数据结构体: type Zhj

Golang支持平滑升级的HTTP服务

前段时间用Golang在做一个HTTP的接口,因编译型语言的特性,修改了代码需要重新编译可执行文件,关闭正在运行的老程序,并启动新程序。对于访问量较大的面向用户的产品,关闭、重启的过程中势必会出现无法访问的情况,从而影响用户体验。 使用Golang的系统包开发HTTP服务,是无法支持平滑升级(优雅重启)的,本文将探讨如何解决该问题。 一、平滑升级(优雅重启)的一般思路 一般情况下,要实现平滑