#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
#include <sys/time.h>
#include <time.h>
FILE* fp = NULL;
void get_cpu_temp()
{
FILE *fd;
int temp1, temp2, temp3, temp4, temp5, temp6;
char buff[256];
fd = fopen("/sys/class/thermal/thermal_zone0/temp","r");
fgets(buff,sizeof(buff),fd);
sscanf(buff, "%d", &temp1);
fclose(fd);
printf("cpu1 temp: %d\n", temp1 / 1000);
fd = fopen("/sys/class/thermal/thermal_zone1/temp","r");
fgets(buff,sizeof(buff),fd);
sscanf(buff, "%d", &temp2);
fclose(fd);
printf("cpu2 temp: %d\n", temp2 / 1000);
fd = fopen("/sys/class/thermal/thermal_zone2/temp","r");
fgets(buff,sizeof(buff),fd);
sscanf(buff, "%d", &temp3);
fclose(fd);
printf("cpu3 temp: %d\n", temp3 / 1000);
fd = fopen("/sys/class/thermal/thermal_zone3/temp","r");
fgets(buff,sizeof(buff),fd);
sscanf(buff, "%d", &temp4);
fclose(fd);
printf("cpu4 temp: %d\n", temp4 / 1000);
fd = fopen("/sys/class/thermal/thermal_zone4/temp","r");
fgets(buff,sizeof(buff),fd);
sscanf(buff, "%d", &temp5);
fclose(fd);
printf("cpu5 temp: %d\n", temp5 / 1000);
fd = fopen("/sys/class/thermal/thermal_zone5/temp","r");
fgets(buff,sizeof(buff),fd);
sscanf(buff, "%d", &temp6);
fclose(fd);
printf("cpu6 temp: %d\n", temp6 / 1000);
printf("-------------------------\n");
char time[64] = { 0 };
struct timeval tv;
int off;
gettimeofday(&tv, NULL);
off = strftime(time, sizeof(time), "%H_%M_%S", localtime(&tv.tv_sec));
snprintf(time + off, sizeof(time) - off, "_%03d", (int)tv.tv_usec / 1000);
fprintf(fp, "%s,%u,%u,%u,%u,%u,%u,", time, temp1 / 1000, temp2 / 1000, temp3 / 1000, temp4 / 1000, temp5 / 1000, temp6 / 1000);
fprintf(fp, "\n");
fflush(fp);
}
int main()
{
fp = fopen("temp.csv", "w");
if (!fp)
{
printf("Failed to open file temp.csv, error\n");
return -1;
}
int ret = fprintf(fp, "time," "cpu1," "cpu2," "cpu3," "cpu4," "cpu5," "cpu6,");
if (ret < 0)
{
printf("Write header to log file failed! error:\n");
return -1;
}
fprintf(fp, "\n");
while(1) {
get_cpu_temp();
sleep(1);
}
fclose(fp);
return 0;
}
linux 读取cpu温度记录到csv文件
最新推荐文章于 2024-05-21 14:37:05 发布