增加进程优先级

增加正确日志输出
This commit is contained in:
aixiao 2021-08-05 11:22:18 +08:00
parent 3235a97987
commit bd585efe7d
6 changed files with 78 additions and 54 deletions

View File

@ -1,7 +1,7 @@
CROSS_COMPILE ?= CROSS_COMPILE ?=
CC := $(CROSS_COMPILE)gcc CC := $(CROSS_COMPILE)gcc
CFLAGS += -g -Wall -L../libini -I../libini -fno-builtin CFLAGS += -g -O2 -Wall -L../libini -I../libini -fno-builtin
LIB += -lini -static -g LIB += -lini -static
BIN = reboot_temperature BIN = reboot_temperature
all: reboot_temperature.o all: reboot_temperature.o

View File

@ -1,7 +1,8 @@
[server] [server]
thermal_zone = "/sys/class/thermal/thermal_zone0/temp"; thermal_zone = "/sys/class/thermal/thermal_zone0/temp";
temperature = 80 temperature = 70
log_file = "reboot.log"; log_file = "log_reboot.log";
second = 10 second = 60
off_power = 0 nice = -20
off_power = 1

Binary file not shown.

View File

@ -3,108 +3,126 @@
int get_temperature(char *path) int get_temperature(char *path)
{ {
char buffer[SIZE]; char buffer[SIZE];
FILE *fp; FILE *fp;
if ((fp = fopen(path, "r")) < 0) {
return 1; /* 文件不存在,则退出. */
}
while (fgets(buffer, SIZE, fp) != NULL) { if ((fp = fopen(path, "r")) < 0)
; return -1; /* 文件不存在,则退出. */
}
while (fgets(buffer, SIZE, fp) != NULL) ;
fclose(fp); fclose(fp);
return atoi(buffer) / 1000; return atoi(buffer) / 1000;
} }
int log(int l_t, int c_t, char *log_file) int error_log(int l_t, int c_t, char *log_file)
{
time_t tmpcal_ptr;
struct tm *tmp_ptr = NULL;
FILE *fp;
char buffer[SIZE];
char temperature[SIZE];
memset(buffer, 0, SIZE);
memset(temperature, 0, SIZE);
time(&tmpcal_ptr);
tmp_ptr = localtime(&tmpcal_ptr);
FILE *fd = fopen(log_file, "a+");
if (fd == NULL)
return -1;
// 写入日志文件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);
// 发送邮件字符串
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);
//printf("%s\n", buffer);
fp = popen(buffer, "r");
pclose(fp);
fclose(fd);
return 0;
}
int correct_log(int l_t, int c_t, char *log_file)
{ {
time_t tmpcal_ptr; time_t tmpcal_ptr;
struct tm *tmp_ptr = NULL; struct tm *tmp_ptr = NULL;
time(&tmpcal_ptr); time(&tmpcal_ptr);
tmp_ptr = localtime(&tmpcal_ptr); tmp_ptr = localtime(&tmpcal_ptr);
FILE *fd = fopen(log_file, "a+"); FILE *fd = fopen(log_file, "a+");
if (fd == NULL) { if (fd == NULL)
return 0; return -1;
}
fprintf(fd, "%d.%d.%d %d:%d:%d %s %d %s %d\n", (1900 + tmp_ptr->tm_year), 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);
(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);
char temperature[270];
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);
// 发送告警邮件
//char *argv[]={"/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", temperature, (char*)0};
//execv("/root/qqMail/qqMail", argv);
char buffer[2700];
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);
//printf("%s\n", buffer);
FILE *fp;
fp = popen(buffer, "r");
pclose(fp);
return 0; return 0;
} }
int get_executable_path(char *processdir, char *processname, int len) int get_executable_path(char *processdir, char *processname, int len)
{ {
char *filename; char *filename;
if (readlink("/proc/self/exe", processdir, len) <= 0) {
if (readlink("/proc/self/exe", processdir, len) <= 0)
return -1; return -1;
}
filename = strrchr(processdir, '/'); filename = strrchr(processdir, '/');
if (filename == NULL) if (filename == NULL)
return -1; return -1;
++filename; ++filename;
strcpy(processname, filename); strcpy(processname, filename);
*filename = '\0'; *filename = '\0';
return (int)(filename - processdir); return (int)(filename - processdir);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[], char **env)
{ {
char buffer[SIZE]; char buffer[SIZE];
char log_file[SIZE]; char log_file[SIZE];
memset(buffer, 0, SIZE);
memset(log_file, 0, SIZE);
char *inifile = "conf/config.ini"; char *inifile = "conf/config.ini";
char path[SIZE] = { 0 }; char path[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(log_file, 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)))) { // 进程优先级
perror("nice");
}
while (1) { while (1) {
getinikeystring("server", "thermal_zone", inifile, buffer); // 获取thermal_zone路径 getinikeystring("server", "thermal_zone", inifile, buffer); // 获取thermal_zone路径
getinikeystring("server", "log_file", inifile, log_file); // 获取日志文件名 getinikeystring("server", "log_file", inifile, log_file); // 获取日志文件名
if (get_temperature(buffer) >= getinikeyint("server", "temperature", inifile)) { if (get_temperature(buffer) >= getinikeyint("server", "temperature", inifile)) { // 达到重启或者关机温度
sync(); sync();
if (getinikeyint("server", "off_power", inifile) == 1) { if (getinikeyint("server", "off_power", inifile) == 1) {
log(get_temperature(buffer), getinikeyint("server", "temperature", inifile), log_file); error_log(get_temperature(buffer), getinikeyint("server", "temperature", inifile), log_file);
return reboot(RB_POWER_OFF); // 关机 return reboot(RB_POWER_OFF); // 关机
} else {
log(get_temperature(buffer), getinikeyint("server", "temperature", inifile), log_file);
} else {
error_log(get_temperature(buffer), getinikeyint("server", "temperature", inifile), log_file);
return reboot(RB_AUTOBOOT); // 重启 return reboot(RB_AUTOBOOT); // 重启
} }
} else { // 未达到重启或者关机温度
correct_log(get_temperature(buffer), getinikeyint("server", "temperature", inifile), log_file);
} }
sleep(getinikeyint("server", "second", inifile)); sleep(getinikeyint("server", "second", inifile));
} }
return 1; return 0;
} }

View File

@ -1,3 +1,6 @@
#ifndef REBOOT_TEMPERATUR_H
#define REBOOT_TEMPERATUR_H
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -7,4 +10,6 @@
#include <sys/reboot.h> #include <sys/reboot.h>
#include "libini.h" #include "libini.h"
#define SIZE 270 #define SIZE 2700
#endif

Binary file not shown.