配置文件自定义邮箱信息

This commit is contained in:
aixiao 2022-04-10 22:34:13 +08:00
parent 7d2928b585
commit f881a600d3
3 changed files with 70 additions and 38 deletions

View File

@ -1,13 +1,18 @@
# reboot_temperature # reboot_temperature
树莓派温度上限时重启或者关机, 保护硬件 树莓派温度上限时重启或者关机, 第一时间保护硬件, 不处在温度上线过长时间
搭配qqMail(https://git.aixiao.me/aixiao/qqMail)实现QQ邮件发送告警 搭配qqMail(https://git.aixiao.me/aixiao/qqMail)实现QQ邮件发送告警
## Help Information ## Help Information
thermal_zone = "/sys/class/thermal/thermal_zone0/temp"; // 树莓派温度 [global]
temperature = 70 // 温度上限 thermal_zone = "/sys/class/thermal/thermal_zone0/temp"; // 树莓派温度
log_file = "log_reboot.log"; // 日志文件 temperature = 90 // 温度上限
second = 60 // 循环秒 log_file = "log_reboot.log"; // 日志文件
nice = -20 // 进程进程优先级 +19 - -20 second = 60 // 循环秒
off_power = 1 // 1关机 0重启 nice = -20 // 进程进程优先级 +19 - -20
off_power = 0 // 1关机 0重启
send_mail = "1605227279"; // 发送者QQ
mail_key = "dqqpbbbxoaziba"; // 发送者QQ密钥
recv_mail = "1605227279"; // 接收者QQ

View File

@ -1,8 +1,13 @@
[server] [global]
thermal_zone = "/sys/class/thermal/thermal_zone0/temp"; thermal_zone = "/sys/class/thermal/thermal_zone0/temp";
temperature = 70 temperature = 90
log_file = "log_reboot.log"; log_file = "log_reboot.log";
second = 60 second = 60
nice = -20 nice = -20
off_power = 1 off_power = 0
send_mail = "1605227279";
mail_key = "dqqpbbbxoazibafd";
recv_mail = "1605227279";

View File

@ -3,10 +3,10 @@
int get_temperature(char *path) int get_temperature(char *path)
{ {
char buffer[SIZE]; char buffer[SIZE];
FILE *fp; FILE *fp = NULL;
if ((fp = fopen(path, "r")) < 0) if ((fp = fopen(path, "r")) < 0)
return -1; /* 文件不存在,则退出. */ return -1; /* 文件不存在, 则退出. */
while (fgets(buffer, SIZE, fp) != NULL) ; while (fgets(buffer, SIZE, fp) != NULL) ;
@ -14,11 +14,12 @@ int get_temperature(char *path)
return atoi(buffer) / 1000; return atoi(buffer) / 1000;
} }
int error_log(int l_t, int c_t, char *log_file) int error_log(int l_t, int c_t, char *log_file, char *send_mail, char *mail_key, char *recv_mail)
{ {
time_t tmpcal_ptr; time_t tmpcal_ptr = 0;
struct tm *tmp_ptr = NULL; struct tm *tmp_ptr = NULL;
FILE *fp; FILE *fp = NULL;
FILE *fd = NULL;
char buffer[SIZE]; char buffer[SIZE];
char temperature[SIZE]; char temperature[SIZE];
@ -27,7 +28,7 @@ int error_log(int l_t, int c_t, char *log_file)
time(&tmpcal_ptr); time(&tmpcal_ptr);
tmp_ptr = localtime(&tmpcal_ptr); tmp_ptr = localtime(&tmpcal_ptr);
FILE *fd = fopen(log_file, "a+"); fd = fopen(log_file, "a+");
if (fd == NULL) if (fd == NULL)
return -1; return -1;
@ -37,30 +38,35 @@ int error_log(int l_t, int c_t, char *log_file)
// 发送邮件字符串 // 发送邮件字符串
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); 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);
sprintf(buffer, "/root/qqMail/qqMail -l smtp.qq.com -p 25 -f 1605227279 -e dqqpbbbxoazibafd -q NIUYULING -r 1605227279@QQ.COM -n NIUYULING -s \"Raspberrypi Temperature\" -t \"%s\"", temperature); 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);
//printf("%s\n", buffer); //printf("%s\n", buffer);
fp = popen(buffer, "r"); fp = popen(buffer, "r");
pclose(fp); pclose(fp);
fclose(fd); fclose(fd);
fp = NULL;
fd = NULL;
return 0; return 0;
} }
int correct_log(int l_t, int c_t, char *log_file) int correct_log(int l_t, int c_t, char *log_file)
{ {
time_t tmpcal_ptr; time_t tmpcal_ptr = 0;
struct tm *tmp_ptr = NULL; struct tm *tmp_ptr = NULL;
FILE *fd = NULL;
time(&tmpcal_ptr); time(&tmpcal_ptr);
tmp_ptr = localtime(&tmpcal_ptr); tmp_ptr = localtime(&tmpcal_ptr);
FILE *fd = fopen(log_file, "a+"); fd = fopen(log_file, "a+");
if (fd == NULL) if (fd == NULL)
return -1; 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); 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); fclose(fd);
fd = NULL;
return 0; return 0;
} }
@ -86,42 +92,58 @@ int main(int argc, char *argv[], char **env)
char log_file[SIZE]; char log_file[SIZE];
char *inifile = "conf/config.ini"; char *inifile = "conf/config.ini";
char path[SIZE] = { 0 }; char path[SIZE] = { 0 };
char send_mail[SIZE] = {0};
char mail_key[SIZE] = {0};
char recv_mail[SIZE] ={0};
char executable_filename[SIZE] = { 0 }; char executable_filename[SIZE] = { 0 };
(void)get_executable_path(path, executable_filename, sizeof(path)); (void)get_executable_path(path, executable_filename, sizeof(path));
inifile = strcat(path, inifile); inifile = strcat(path, inifile);
memset(buffer, 0, SIZE); memset(buffer, 0, SIZE);
memset(log_file, 0, SIZE); memset(log_file, 0, SIZE);
memset(send_mail, 0 , SIZE);
memset(mail_key, 0, SIZE);
memset(recv_mail, 0, SIZE);
if (daemon(1, 1) == -1) { if (daemon(1, 1) == -1)
{
perror("daemon"); perror("daemon");
exit(1); exit(1);
} }
if (-1 == (nice(getinikeyint("server", "nice", inifile)))) { // 进程优先级 if (-1 == (nice(getinikeyint("global", "nice", inifile)))) // 进程优先级
{
perror("nice"); perror("nice");
} }
while (1) { while (1)
getinikeystring("server", "thermal_zone", inifile, buffer); // 获取thermal_zone路径 {
getinikeystring("server", "log_file", inifile, log_file); // 获取日志文件名 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("server", "temperature", inifile)) { // 达到重启或者关机温度 if (get_temperature(buffer) >= getinikeyint("global", "temperature", inifile)) // 达到重启或者关机温度
{
sync(); sync();
if (getinikeyint("server", "off_power", inifile) == 1) { if (getinikeyint("global", "off_power", inifile) == 1) {
error_log(get_temperature(buffer), getinikeyint("server", "temperature", inifile), log_file); error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, send_mail, mail_key, recv_mail);
return reboot(RB_POWER_OFF); // 关机 return reboot(RB_POWER_OFF); // 关机
} else {
error_log(get_temperature(buffer), getinikeyint("server", "temperature", inifile), log_file);
return reboot(RB_AUTOBOOT); // 重启
} }
} else { // 未达到重启或者关机温度 else
correct_log(get_temperature(buffer), getinikeyint("server", "temperature", inifile), log_file); {
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, send_mail, mail_key, recv_mail);
return reboot(RB_AUTOBOOT); // 重启
}
}
else // 未达到重启或者关机温度
{
correct_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file);
} }
sleep(getinikeyint("server", "second", inifile)); sleep(getinikeyint("global", "second", inifile));
} }
return 0; return 0;