提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
linux服务器获取cpu温度信息:通过sys文件节点读取(其他软件查询方式应该类似)
c代码示例,可以运行,仅供参考
提示:以下是本篇文章正文内容,下面案例代码仅供参考
一、sys文件读取?
linux内核版本:5.10
sys文件节点目录:
/sys/class/hwmon/hwmonX
temp1_input:当前核心温度 (数值/1000 = 温度)
temp1_crit:最坏的温度
name: 这个hwmon的名称
不同的机器可能节点还有tempX_lable等。
二、代码示例
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
struct sensor_temp {
char file_name[64];
int val;
};
struct sensor_hwmon {
char hw_name[64];
struct sensor_temp *input;
int input_num;
struct sensor_temp *crit;
int crit_num;
};
int hw_num;
char hw_name[20][32];
struct sensor_hwmon g_hw[20];
// 匹配 tempX_input 或 tempX_crit
int match_filename(char * file_name, int len, int file)
{
int ret = 0;
switch(file)
{
case 1:
if(strncmp(file_name, "temp", 4) == 0)
{
if(strncmp(file_name+ len - 5, "input", 5) == 0)
{
return 1;
}
}
break;
case 2:
if(strncmp(file_name, "temp", 4) == 0)
{
if(strncmp(file_name + len - 4, "crit", 4) == 0)
{
return 1;
}
}
break;
default:
break;
}
return 0;
}
int main() {
int i = 0;
int j = 0;
DIR *dir;
FILE *file;
char read_buf[256] = {0};
char path_dir[256] = {0};
char file_dir[256] = {0};
struct dirent *ent;
int tm_input = 0;
int tm_crit = 0;
if((dir = opendir("/sys/class/hwmon/")) == NULL) {
perror("Could not open directory");
return -1;
}
hw_num = 0;
while((ent = readdir(dir)) != NULL) {
// 这里只打印出非目录的文件
//if(ent->d_type == DT_DIR) {
printf("%s\n", ent->d_name);
//}
if(strncmp(ent->d_name,"hwmon", strnlen("hwmon", 32)) == 0)
{
memcpy( hw_name[hw_num],ent->d_name, strnlen(ent->d_name, 32));
hw_num++;
}
} // 判断/sys/class/hwmon/hwmonX 目录有几个
printf("hw_num = %d\n", hw_num);
closedir(dir);
for(i = 0; i < hw_num; i++)
{
memset(path_dir,0 , 256);
snprintf(path_dir, 256,"/sys/class/hwmon/%s", hw_name[i]);
printf("%s\n", path_dir);
if((dir = opendir(path_dir)) == NULL) {
printf("Could not open directory %s", path_dir);
continue;
}
while((ent = readdir(dir)) != NULL) {
// 这里只打印出非目录的文件
if(ent->d_type != DT_DIR) {
printf("%s\n", ent->d_name);
if(match_filename(ent->d_name, strnlen(ent->d_name, 64), 1) == 1)
{
g_hw[i].input_num++; // 确定hwmonX目录下 tempX_input文件个数
}
if(match_filename(ent->d_name, strnlen(ent->d_name, 64), 2) == 1)
{
g_hw[i].crit_num++; // 确定hwmonX目录下 tempX_crit文件个数
}
}
}
closedir(dir);
}
for(i = 0; i < hw_num; i++)
{
printf("g_hw[i].input_num = %d, g_hw[i].crit_num = %d\n", g_hw[i].input_num, g_hw[i].crit_num);
g_hw[i].input = (struct sensor_temp *)malloc(g_hw[i].input_num * sizeof(struct sensor_temp)); // 申请内存
g_hw[i].crit = (struct sensor_temp *)malloc(g_hw[i].crit_num * sizeof(struct sensor_temp));
memset(path_dir,0 , 256);
snprintf(path_dir, 256,"/sys/class/hwmon/%s", hw_name[i]);
printf("%s\n", path_dir);
if((dir = opendir(path_dir)) == NULL) {
printf("Could not open directory %s\n", path_dir);
continue;
}
tm_input = 0;
tm_crit = 0;
while((ent = readdir(dir)) != NULL) {
memset(file_dir, 0 , 256);
memset(read_buf, 0 , 256);
// 这里只打印出非目录的文件
if(ent->d_type != DT_DIR) {
if(strncmp(ent->d_name,"name", strnlen("name", 32)) == 0)
{
strncpy(file_dir, path_dir, 256);
strncat(file_dir,"/name" , 6);
file = fopen(file_dir, "r");
if (file != NULL) {
fscanf(file, "%64s", read_buf);
printf("file name = %s, read_buf = %s\n", file_dir,read_buf);
strncpy( g_hw[i].hw_name, read_buf, 64);
fclose(file);
}else
{
printf("无法打开文件。\n");
}
}
if(match_filename(ent->d_name, strnlen(ent->d_name, 64), 1) == 1) // 匹配文件写结构体
{
snprintf(file_dir, 256,"%s/%s", path_dir, ent->d_name);
printf("file_dir = %s\n",file_dir);
file = fopen(file_dir, "r");
if (file != NULL) {
fscanf(file, "%64d", &(g_hw[i].input[tm_input].val));
printf("g_hw[i].input[%d].val = %d\n", tm_input, g_hw[i].input[tm_input].val);
strncpy( g_hw[i].input[tm_input].file_name, ent->d_name, 64);
tm_input++;
fclose(file);
}else
{
printf("无法打开文件。\n");
}
}
if(match_filename(ent->d_name, strnlen(ent->d_name, 64), 2) == 1)
{
snprintf(file_dir, 256,"%s/%s", path_dir, ent->d_name);
printf("file_dir = %s\n",file_dir);
file = fopen(file_dir, "r");
if (file != NULL) {
fscanf(file, "%64d", &(g_hw[i].crit[tm_crit].val));
printf("g_hw[i].input[%d].val = %d\n", tm_crit, g_hw[i].crit[tm_crit].val);
strncpy( g_hw[i].input[tm_crit].file_name, ent->d_name, 64);
tm_crit++;
fclose(file);
}else
{
printf("无法打开文件。\n");
}
}
}
}
closedir(dir);
}
for(i = 0; i < hw_num; i++)
{
if(g_hw[i].input)
{
free(g_hw[i].input);
}
if(g_hw[i].crit)
{
free(g_hw[i].crit);
}
}
return 0;
}
总结
sys节点获取cpu温度,代码随意仅供参考。