reboot_temperature/reboot_temperature.c

159 lines
5.1 KiB
C
Raw Normal View History

2021-05-19 09:43:30 +08:00
#include "reboot_temperature.h"
2022-09-20 10:26:34 +08:00
static float get_temperature(char *path)
2021-05-19 09:43:30 +08:00
{
char buffer[SIZE];
2022-04-10 22:34:13 +08:00
FILE *fp = NULL;
2021-05-19 09:43:30 +08:00
if ((fp = fopen(path, "r")) < 0)
2022-04-10 22:34:13 +08:00
return -1; /* 文件不存在, 则退出. */
while (fgets(buffer, SIZE, fp) != NULL) ;
2021-05-19 09:43:30 +08:00
fclose(fp);
2022-09-20 10:26:34 +08:00
return atof(buffer) / (float)1000;
2021-05-19 09:43:30 +08:00
}
2022-09-20 10:26:34 +08:00
static int error_log(float l_t, int c_t, char *log_file, char *recv_mail, int _is, int _alert)
2021-05-19 09:43:30 +08:00
{
2022-04-10 22:34:13 +08:00
time_t tmpcal_ptr = 0;
2021-05-19 09:43:30 +08:00
struct tm *tmp_ptr = NULL;
2022-04-10 22:34:13 +08:00
FILE *fp = NULL;
FILE *fd = NULL;
2023-04-06 16:04:45 +08:00
char buffer[SIZE+1024+270];
char temperature[SIZE+1024];
char hostname[SIZE];
memset(buffer, 0, SIZE+1024+270);
memset(temperature, 0, SIZE+1024);
memset(hostname, 0, SIZE);
2021-05-19 09:43:30 +08:00
time(&tmpcal_ptr);
tmp_ptr = localtime(&tmpcal_ptr);
2023-04-06 16:04:45 +08:00
if (gethostname(hostname, SIZE) != 0)
{
perror("gethostname");
}
2022-04-10 22:34:13 +08:00
fd = fopen(log_file, "a+");
if (fd == NULL)
return -1;
2023-04-06 16:04:45 +08:00
// 写入日志文件FD
2023-04-06 16:04:45 +08:00
fprintf(fd, "[%d.%d.%d %d:%d:%d] %s %s %.3f℃, %s %.3f℃\n", (1900 + tmp_ptr->tm_year), (1 + tmp_ptr->tm_mon), tmp_ptr->tm_mday, tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, hostname, "CPU temperature", l_t, "Restartable or shutdown temperature", (float)c_t);
2022-09-20 10:26:34 +08:00
2023-04-06 16:04:45 +08:00
if (_is == 1)
{
if (_alert == 1)
{
// 关机重启时
snprintf(temperature, SIZE+1024, "[%d.%d.%d %d:%d:%d] %s %s %.3f℃, %s %.3f℃\n", (1900 + tmp_ptr->tm_year), (1 + tmp_ptr->tm_mon), tmp_ptr->tm_mday, tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, hostname, "CPU temperature", l_t, "Restartable or shutdown temperature",
2022-09-20 10:26:34 +08:00
(float)c_t);
2023-04-06 16:04:45 +08:00
snprintf(buffer, SIZE+1024+270, "gomail -r %s -s \"Raspberrypi Temperature\" -t \"%s\"", recv_mail, temperature);
2022-09-20 10:26:34 +08:00
fp = popen(buffer, "r");
pclose(fp);
2023-04-06 16:04:45 +08:00
}
else
{
2022-09-20 10:26:34 +08:00
// 正常温度时
2023-04-06 16:04:45 +08:00
if (tmp_ptr->tm_min >= 10 && tmp_ptr->tm_min < 12)
{
snprintf(temperature, SIZE+1024, "[%d.%d.%d %d:%d:%d] %s %s:%.3f℃\n", (1900 + tmp_ptr->tm_year), (1 + tmp_ptr->tm_mon), tmp_ptr->tm_mday, tmp_ptr->tm_hour, tmp_ptr->tm_min, tmp_ptr->tm_sec, hostname, "CPU temperature", l_t);
snprintf(buffer, SIZE+1024+270, "gomail -r %s -s \"Raspberrypi Temperature\" -t \"%s\"", recv_mail, temperature);
2022-09-20 10:26:34 +08:00
fp = popen(buffer, "r");
2023-04-06 16:04:45 +08:00
while (fgets(buffer, SIZE, fp) != NULL) {
fprintf(stdout, "%s", buffer);
}
2022-09-20 10:26:34 +08:00
pclose(fp);
}
}
2021-05-19 09:43:30 +08:00
2022-09-20 10:26:34 +08:00
}
fclose(fd);
2022-04-10 22:34:13 +08:00
fd = NULL;
2021-05-19 09:43:30 +08:00
return 0;
}
2022-09-20 10:26:34 +08:00
static int get_executable_path(char *processdir, char *processname, int len)
2021-05-19 09:43:30 +08:00
{
char *filename;
if (readlink("/proc/self/exe", processdir, len) <= 0)
2021-05-19 09:43:30 +08:00
return -1;
filename = strrchr(processdir, '/');
if (filename == NULL)
return -1;
++filename;
strcpy(processname, filename);
*filename = '\0';
2021-05-19 09:43:30 +08:00
return (int)(filename - processdir);
}
int main(int argc, char *argv[], char **env)
2021-05-19 09:43:30 +08:00
{
char buffer[SIZE];
char log_file[SIZE];
char *inifile = "conf/config.ini";
char path[SIZE] = { 0 };
2022-09-20 10:26:34 +08:00
char recv_mail[SIZE] = { 0 };
2021-05-19 09:43:30 +08:00
char executable_filename[SIZE] = { 0 };
(void)get_executable_path(path, executable_filename, sizeof(path));
inifile = strcat(path, inifile);
2023-04-06 16:04:45 +08:00
if (-1 == access(inifile, F_OK)) { // 如果配置不存在
inifile="/etc/reboot_temperature.ini";
}
memset(buffer, 0, SIZE);
memset(log_file, 0, SIZE);
2022-04-10 22:34:13 +08:00
memset(recv_mail, 0, SIZE);
2021-05-19 09:43:30 +08:00
2023-04-06 16:04:45 +08:00
2022-09-20 10:26:34 +08:00
if (daemon(1, 1) == -1) {
2021-05-19 09:43:30 +08:00
perror("daemon");
exit(1);
}
2023-04-06 16:04:45 +08:00
if (-1 == (nice(getinikeyint("global", "nice", inifile)))) // 进程优先级
perror("nice");
2022-04-10 22:34:13 +08:00
2023-04-06 16:04:45 +08:00
2022-09-20 10:26:34 +08:00
while (1) {
2023-04-06 16:04:45 +08:00
getinikeystring("global", "thermal_zone", inifile, buffer); // 获取thermal_zone路径
getinikeystring("global", "log_file", inifile, log_file); // 获取日志文件名
getinikeystring("global", "recv_mail", inifile, recv_mail); // 获取接收者邮箱
2022-04-10 22:34:13 +08:00
2022-09-20 10:26:34 +08:00
if (get_temperature(buffer) >= getinikeyint("global", "temperature", inifile)) // 达到重启或者关机温度
2022-04-10 22:34:13 +08:00
{
2021-05-19 09:43:30 +08:00
sync();
2023-04-06 16:04:45 +08:00
if (getinikeyint("global", "off_power", inifile) == 1)
{
2022-09-20 10:26:34 +08:00
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 1);
return reboot(RB_POWER_OFF); // 关机
} else {
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 1);
return reboot(RB_AUTOBOOT); // 重启
2022-04-10 22:34:13 +08:00
}
2023-04-06 16:04:45 +08:00
}
else // 未达到重启或者关机温度
2022-04-10 22:34:13 +08:00
{
2022-09-20 10:26:34 +08:00
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 0);
2021-05-19 09:43:30 +08:00
}
2023-04-06 16:04:45 +08:00
2022-09-20 10:26:34 +08:00
// 等待
2022-04-10 22:34:13 +08:00
sleep(getinikeyint("global", "second", inifile));
2021-05-19 09:43:30 +08:00
}
2023-04-06 16:04:45 +08:00
return 0;
2021-05-19 09:43:30 +08:00
}