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
2021-08-05 11:22:18 +08:00
if ( ( fp = fopen ( path , " r " ) ) < 0 )
2022-04-10 22:34:13 +08:00
return - 1 ; /* 文件不存在, 则退出. */
2021-08-05 11:22:18 +08:00
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 ;
2021-08-05 11:22:18 +08:00
char buffer [ SIZE ] ;
char temperature [ SIZE ] ;
2021-05-19 09:43:30 +08:00
2021-08-05 11:22:18 +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+ " ) ;
2021-08-05 11:22:18 +08:00
if ( fd = = NULL )
return - 1 ;
2021-05-19 09:43:30 +08:00
2021-08-05 11:22:18 +08:00
// 写入日志文件FD
2022-09-20 14:34:49 +08:00
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 ) ;
2022-09-20 10:26:34 +08:00
if ( _is = = 1 ) {
if ( _alert = = 1 ) {
// 关机活重启时
2022-09-20 14:34:49 +08:00
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 " ,
2022-09-20 10:26:34 +08:00
( float ) c_t ) ;
sprintf ( buffer , " email -r %s -s \" Raspberrypi Temperature \" -t \" %s \" " , recv_mail , temperature ) ;
fp = popen ( buffer , " r " ) ;
pclose ( fp ) ;
fp = NULL ;
} else {
// 正常温度时
2022-09-21 16:12:12 +08:00
if ( tmp_ptr - > tm_min > = 10 & & tmp_ptr - > tm_min < 12 ) {
2022-09-20 14:34:49 +08:00
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 ) ;
2022-09-20 10:26:34 +08:00
sprintf ( buffer , " email -r %s -s \" Raspberrypi Temperature \" -t \" %s \" " , recv_mail , temperature ) ;
fp = popen ( buffer , " r " ) ;
pclose ( fp ) ;
fp = NULL ;
}
}
2021-05-19 09:43:30 +08:00
2022-09-20 10:26:34 +08:00
}
2021-08-05 11:22:18 +08:00
fclose ( fd ) ;
2022-04-10 22:34:13 +08:00
fd = NULL ;
2021-08-05 11:22:18 +08:00
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 ;
2021-08-05 11:22:18 +08:00
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-08-05 11:22:18 +08:00
2021-05-19 09:43:30 +08:00
return ( int ) ( filename - processdir ) ;
}
2021-08-05 11:22:18 +08:00
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 ) ) ;
2021-08-05 11:22:18 +08:00
inifile = strcat ( path , inifile ) ;
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
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 ) ;
}
2022-09-20 10:26:34 +08:00
if ( - 1 = = ( nice ( getinikeyint ( " global " , " nice " , inifile ) ) ) ) // 进程优先级
2021-08-05 11:22:18 +08:00
perror ( " nice " ) ;
2022-04-10 22:34:13 +08:00
2022-09-20 10:26:34 +08:00
while ( 1 ) {
2022-04-10 22:34:13 +08:00
getinikeystring ( " global " , " thermal_zone " , inifile , buffer ) ; // 获取thermal_zone路径
2022-09-20 10:26:34 +08:00
getinikeystring ( " global " , " log_file " , inifile , log_file ) ; // 获取日志文件名
2022-04-10 22:34:13 +08:00
getinikeystring ( " global " , " recv_mail " , inifile , recv_mail ) ; // 获取接收者邮箱
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 ( ) ;
2022-04-10 22:34:13 +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
}
2022-09-20 10:26:34 +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
}
2021-08-05 11:22:18 +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
}
2021-08-05 11:22:18 +08:00
return 0 ;
2021-05-19 09:43:30 +08:00
}