优化日志输出区分主机
This commit is contained in:
parent
7133de2456
commit
1eed11b4d4
4
Makefile
4
Makefile
@ -1,7 +1,7 @@
|
|||||||
CROSS_COMPILE ?=
|
CROSS_COMPILE ?=
|
||||||
CC := $(CROSS_COMPILE)gcc
|
CC := $(CROSS_COMPILE)gcc
|
||||||
CFLAGS += -g -Os -Wall -L../libini -I../libini -fno-builtin
|
CFLAGS += -Wall -Os -g -L../libini -I../libini -fno-builtin
|
||||||
LIB += -lini -static
|
LIB += -Wl,-rpath,/root/libini/ /root/libini/libini.so
|
||||||
BIN = reboot_temperature
|
BIN = reboot_temperature
|
||||||
|
|
||||||
all: reboot_temperature.o
|
all: reboot_temperature.o
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[global]
|
[global]
|
||||||
thermal_zone = "/sys/class/thermal/thermal_zone0/temp";
|
thermal_zone = "/sys/class/thermal/thermal_zone0/temp";
|
||||||
temperature = 90
|
temperature = 85
|
||||||
log_file = "log_reboot.log";
|
log_file = "log_reboot.log";
|
||||||
second = 60
|
second = 60
|
||||||
nice = -20
|
nice = -20
|
||||||
|
@ -20,38 +20,54 @@ static int error_log(float l_t, int c_t, char *log_file, char *recv_mail, int _i
|
|||||||
struct tm *tmp_ptr = NULL;
|
struct tm *tmp_ptr = NULL;
|
||||||
FILE *fp = NULL;
|
FILE *fp = NULL;
|
||||||
FILE *fd = NULL;
|
FILE *fd = NULL;
|
||||||
char buffer[SIZE];
|
char buffer[SIZE+1024+270];
|
||||||
char temperature[SIZE];
|
char temperature[SIZE+1024];
|
||||||
|
char hostname[SIZE];
|
||||||
|
|
||||||
|
memset(buffer, 0, SIZE+1024+270);
|
||||||
|
memset(temperature, 0, SIZE+1024);
|
||||||
|
memset(hostname, 0, SIZE);
|
||||||
|
|
||||||
memset(buffer, 0, SIZE);
|
|
||||||
memset(temperature, 0, SIZE);
|
|
||||||
time(&tmpcal_ptr);
|
time(&tmpcal_ptr);
|
||||||
tmp_ptr = localtime(&tmpcal_ptr);
|
tmp_ptr = localtime(&tmpcal_ptr);
|
||||||
|
|
||||||
|
if (gethostname(hostname, SIZE) != 0)
|
||||||
|
{
|
||||||
|
perror("gethostname");
|
||||||
|
}
|
||||||
|
|
||||||
fd = fopen(log_file, "a+");
|
fd = fopen(log_file, "a+");
|
||||||
if (fd == NULL)
|
if (fd == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
// 写入日志文件FD
|
// 写入日志文件FD
|
||||||
fprintf(fd, "%d.%d.%d %d:%d:%d %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, "Raspberry CPU temperature", l_t, "Restartable or shutdown temperature", (float)c_t);
|
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);
|
||||||
|
|
||||||
if (_is == 1) {
|
if (_is == 1)
|
||||||
if (_alert == 1) {
|
{
|
||||||
// 关机活重启时
|
|
||||||
sprintf(temperature, "%d.%d.%d %d:%d:%d %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, "Raspberry CPU temperature", l_t, "Restartable or shutdown temperature",
|
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",
|
||||||
(float)c_t);
|
(float)c_t);
|
||||||
sprintf(buffer, "gomail -r %s -s \"Raspberrypi Temperature\" -t \"%s\"", recv_mail, temperature);
|
snprintf(buffer, SIZE+1024+270, "gomail -r %s -s \"Raspberrypi Temperature\" -t \"%s\"", recv_mail, temperature);
|
||||||
fp = popen(buffer, "r");
|
fp = popen(buffer, "r");
|
||||||
pclose(fp);
|
pclose(fp);
|
||||||
fp = NULL;
|
}
|
||||||
} else {
|
else
|
||||||
|
{
|
||||||
// 正常温度时
|
// 正常温度时
|
||||||
if (tmp_ptr->tm_min >= 10 && tmp_ptr->tm_min < 12) {
|
if (tmp_ptr->tm_min >= 10 && tmp_ptr->tm_min < 12)
|
||||||
sprintf(temperature, "%d.%d.%d %d:%d:%d %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, "Raspberry CPU temperature", l_t);
|
{
|
||||||
sprintf(buffer, "gomail -r %s -s \"Raspberrypi Temperature\" -t \"%s\"", recv_mail, temperature);
|
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);
|
||||||
fp = popen(buffer, "r");
|
fp = popen(buffer, "r");
|
||||||
|
while (fgets(buffer, SIZE, fp) != NULL) {
|
||||||
|
fprintf(stdout, "%s", buffer);
|
||||||
|
}
|
||||||
|
|
||||||
pclose(fp);
|
pclose(fp);
|
||||||
fp = NULL;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,10 +107,16 @@ int main(int argc, char *argv[], char **env)
|
|||||||
(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);
|
||||||
|
|
||||||
|
|
||||||
|
if (-1 == access(inifile, F_OK)) { // 如果配置不存在
|
||||||
|
inifile="/etc/reboot_temperature.ini";
|
||||||
|
}
|
||||||
|
|
||||||
memset(buffer, 0, SIZE);
|
memset(buffer, 0, SIZE);
|
||||||
memset(log_file, 0, SIZE);
|
memset(log_file, 0, SIZE);
|
||||||
memset(recv_mail, 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);
|
||||||
@ -103,6 +125,7 @@ int main(int argc, char *argv[], char **env)
|
|||||||
if (-1 == (nice(getinikeyint("global", "nice", inifile)))) // 进程优先级
|
if (-1 == (nice(getinikeyint("global", "nice", inifile)))) // 进程优先级
|
||||||
perror("nice");
|
perror("nice");
|
||||||
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
getinikeystring("global", "thermal_zone", inifile, buffer); // 获取thermal_zone路径
|
getinikeystring("global", "thermal_zone", inifile, buffer); // 获取thermal_zone路径
|
||||||
getinikeystring("global", "log_file", inifile, log_file); // 获取日志文件名
|
getinikeystring("global", "log_file", inifile, log_file); // 获取日志文件名
|
||||||
@ -111,21 +134,25 @@ int main(int argc, char *argv[], char **env)
|
|||||||
if (get_temperature(buffer) >= getinikeyint("global", "temperature", inifile)) // 达到重启或者关机温度
|
if (get_temperature(buffer) >= getinikeyint("global", "temperature", inifile)) // 达到重启或者关机温度
|
||||||
{
|
{
|
||||||
sync();
|
sync();
|
||||||
if (getinikeyint("global", "off_power", inifile) == 1) {
|
if (getinikeyint("global", "off_power", inifile) == 1)
|
||||||
|
{
|
||||||
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 1);
|
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 1);
|
||||||
return reboot(RB_POWER_OFF); // 关机
|
return reboot(RB_POWER_OFF); // 关机
|
||||||
} else {
|
} else {
|
||||||
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 1);
|
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 1);
|
||||||
return reboot(RB_AUTOBOOT); // 重启
|
return reboot(RB_AUTOBOOT); // 重启
|
||||||
}
|
}
|
||||||
} else // 未达到重启或者关机温度
|
}
|
||||||
|
else // 未达到重启或者关机温度
|
||||||
{
|
{
|
||||||
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 0);
|
error_log(get_temperature(buffer), getinikeyint("global", "temperature", inifile), log_file, recv_mail, getinikeyint("global", "is_alert", inifile), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 等待
|
// 等待
|
||||||
sleep(getinikeyint("global", "second", inifile));
|
sleep(getinikeyint("global", "second", inifile));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
#include <sys/reboot.h>
|
#include <sys/reboot.h>
|
||||||
#include "libini.h"
|
#include "libini.h"
|
||||||
|
|
||||||
#define SIZE 2700
|
|
||||||
|
#define SIZE 1024
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user