reboot_temperature/reboot_temperature.c

151 lines
4.7 KiB
C
Raw Normal View History

2021-05-19 09:43:30 +08:00
#include "reboot_temperature.h"
int get_temperature(char *path)
{
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);
return atoi(buffer) / 1000;
}
2022-04-10 22:34:13 +08:00
int error_log(int l_t, int c_t, char *log_file, char *send_mail, char *mail_key, char *recv_mail)
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;
char buffer[SIZE];
char temperature[SIZE];
2021-05-19 09:43:30 +08:00
memset(buffer, 0, SIZE);
memset(temperature, 0, SIZE);
2021-05-19 09:43:30 +08:00
time(&tmpcal_ptr);
tmp_ptr = localtime(&tmpcal_ptr);
2022-04-10 22:34:13 +08:00
fd = fopen(log_file, "a+");
if (fd == NULL)
return -1;
2021-05-19 09:43:30 +08:00
// 写入日志文件FD
fprintf(fd, "%d.%d.%d %d:%d:%d %s %d %s %d\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, "树莓CPU温度", l_t, "达到可重启温度", c_t);
2021-05-19 09:43:30 +08:00
// 发送邮件字符串
sprintf(temperature, "%d.%d.%d %d:%d:%d %s %d %s %d\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, "树莓CPU温度", l_t, "达到可重启温度", c_t);
2021-05-19 09:43:30 +08:00
2022-04-10 22:34:13 +08:00
sprintf(buffer, "qqMail -l smtp.qq.com -p 25 -f %s -e %s -q NIUYULING -r %s@QQ.COM -n NIUYULING -s \"Raspberrypi Temperature\" -t \"%s\"", send_mail, mail_key, recv_mail, temperature);
2021-05-19 09:43:30 +08:00
//printf("%s\n", buffer);
2021-05-19 09:43:30 +08:00
fp = popen(buffer, "r");
pclose(fp);
fclose(fd);
2022-04-10 22:34:13 +08:00
fp = NULL;
fd = NULL;
return 0;
}
int correct_log(int l_t, int c_t, char *log_file)
{
2022-04-10 22:34:13 +08:00
time_t tmpcal_ptr = 0;
struct tm *tmp_ptr = NULL;
2022-04-10 22:34:13 +08:00
FILE *fd = NULL;
time(&tmpcal_ptr);
tmp_ptr = localtime(&tmpcal_ptr);
2022-04-10 22:34:13 +08:00
fd = fopen(log_file, "a+");
if (fd == NULL)
return -1;
fprintf(fd, "%d.%d.%d %d:%d:%d %s %d %s %d\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, "树莓派CPU温度", l_t, "未达到重启或者关机温度", c_t);
fclose(fd);
2022-04-10 22:34:13 +08:00
fd = NULL;
2021-05-19 09:43:30 +08:00
return 0;
}
int get_executable_path(char *processdir, char *processname, int len)
{
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-04-10 22:34:13 +08:00
char send_mail[SIZE] = {0};
char mail_key[SIZE] = {0};
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);
memset(buffer, 0, SIZE);
memset(log_file, 0, SIZE);
2022-04-10 22:34:13 +08:00
memset(send_mail, 0 , SIZE);
memset(mail_key, 0, SIZE);
memset(recv_mail, 0, SIZE);
2021-05-19 09:43:30 +08:00
2022-04-10 22:34:13 +08:00
if (daemon(1, 1) == -1)
{
2021-05-19 09:43:30 +08:00
perror("daemon");
exit(1);
}
2022-04-10 22:34:13 +08:00
if (-1 == (nice(getinikeyint("global", "nice", inifile)))) // 进程优先级
{
perror("nice");
}
2022-04-10 22:34:13 +08:00
while (1)
{
getinikeystring("global", "thermal_zone", inifile, buffer); // 获取thermal_zone路径
getinikeystring("global", "log_file", inifile, log_file); // 获取日志文件名
getinikeystring("global", "send_mail", inifile, send_mail); // 获取发送者QQ号
getinikeystring("global", "mail_key", inifile, mail_key); // 获取QQ邮箱授权码
getinikeystring("global", "recv_mail", inifile, recv_mail); // 获取接收者邮箱
if (get_temperature(buffer) >= getinikeyint("global", "temperature", inifile)) // 达到重启或者关机温度
{
2021-05-19 09:43:30 +08:00
sync();
2022-04-10 22:34:13 +08:00
if (getinikeyint("global", "off_power", inifile) == 1) {
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, send_mail, mail_key, recv_mail);
return reboot(RB_POWER_OFF); // 关机
2022-04-10 22:34:13 +08:00
}
else
{
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, send_mail, mail_key, recv_mail);
return reboot(RB_AUTOBOOT); // 重启
2021-05-19 09:43:30 +08:00
}
2022-04-10 22:34:13 +08:00
}
else // 未达到重启或者关机温度
{
correct_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file);
2021-05-19 09:43:30 +08:00
}
2022-04-10 22:34:13 +08:00
sleep(getinikeyint("global", "second", inifile));
2021-05-19 09:43:30 +08:00
}
return 0;
2021-05-19 09:43:30 +08:00
}