int proc_logcheck(void) { struct stat buffer; int status, fd; unsigned long long max_logfile_size; //不加ull,编译会提示整形溢出,因为编译器认为右边是默认的int类型 max_logfile_size = 3ull * 1024 * 1024 * 1024; while(1) { status = stat(para_conf_s->log_path, &buffer); //如果日志文件大于3g就清空 if((unsigned long long)buffer.st_size > max_logfile_size) { fd = open(para_conf_s->log_path, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); close(fd); monitorlog(LOG_INFO,"cut the log file: %s\n", para_conf_s->log_path); } sleep_select(para_conf_s->time_update); } return 0; }
int proc_update(struct proc_info_s *proc_info) { char *cmd_version = alloca(100); char *cmd_update = alloca(1024); int ppid; float version_update; strcpy(cmd_version , "curl --connect-timeout 3 http://119.147.212.248:8000/monitor_version"); sprintf(cmd_update, "rm -rf %s && wget http://119.147.212.248:8000/monitor_agent -O %s && \ chmod 755 %s && echo succ", proc_info->proc_path, proc_info->proc_path, proc_info->proc_path); //monitorlog(LOG_INFO, "the cmd_update is %s====\n", cmd_update); while(1) { ppid = getppid(); version_update = atof( shell(cmd_version) ); if( VERSION < version_update ) { if(strcmp( shell(cmd_update), "succ") == 0 ) { monitorlog(LOG_INFO,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); monitorlog(LOG_INFO,"!!!!!!!!!!!!!!!start update monitor_agent!!!!!!!!!!!!\n"); monitorlog(LOG_INFO,"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); if (ppid != 1) { kill(ppid, SIGKILL); } execl(proc_info->proc_path, proc_info->proc_path, NULL, (char *)0 ); } } else { monitorlog(LOG_INFO,"curr version: %4.2f > to update version: %4.2f, so monitor_agent don't update!\n", VERSION, version_update); } sleep_select(para_conf_s->time_update); } return 0; }
STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) { #if MICROPY_PY_BUILTINS_FLOAT struct timeval tv; mp_float_t val = mp_obj_get_float(arg); double ipart; tv.tv_usec = round(modf(val, &ipart) * 1000000); tv.tv_sec = ipart; int res; while (1) { MP_THREAD_GIL_EXIT(); res = sleep_select(0, NULL, NULL, NULL, &tv); MP_THREAD_GIL_ENTER(); #if MICROPY_SELECT_REMAINING_TIME // TODO: This assumes Linux behavior of modifying tv to the remaining // time. if (res != -1 || errno != EINTR) { break; } if (MP_STATE_VM(mp_pending_exception) != MP_OBJ_NULL) { return mp_const_none; } //printf("select: EINTR: %ld:%ld\n", tv.tv_sec, tv.tv_usec); #else break; #endif } RAISE_ERRNO(res, errno); #else // TODO: Handle EINTR MP_THREAD_GIL_EXIT(); sleep(mp_obj_get_int(arg)); MP_THREAD_GIL_ENTER(); #endif return mp_const_none; }
STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) { #if MICROPY_PY_BUILTINS_FLOAT struct timeval tv; mp_float_t val = mp_obj_get_float(arg); double ipart; tv.tv_usec = round(modf(val, &ipart) * 1000000); tv.tv_sec = ipart; sleep_select(0, NULL, NULL, NULL, &tv); #else sleep(mp_obj_get_int(arg)); #endif return mp_const_none; }
int lock_fd(int fd, int shared) { #if LOCK_FCNTL struct flock l; memset(&l, 0, sizeof(l)); l.l_whence = SEEK_SET; l.l_type = shared ? F_RDLCK : F_WRLCK; while (fcntl(fd, F_SETLKW, &l)) { if (errno != EBUSY) return -1; sleep_select(1, 0); } #endif #if LOCK_FLOCK while (flock(fd, shared ? LOCK_SH : LOCK_EX)) { if (errno != EBUSY) return -1; sleep_select(1, 0); } #endif return 0; }
int proc_restart(struct proc_info_s *proc_info) { int ret; sleep_select(para_conf_s->time_resetart); kill(getppid(), SIGKILL); //ret = system(proc_info->proc_path); execl(proc_info->proc_path, proc_info->proc_path, NULL, (char *)0 ); monitorlog(LOG_INFO, "the proc_restart result is %d===\n", ret); exit(-10); }
int proc_monitor(struct proc_info_s *proc_info) { int ppid, exec, res, physical_mem; ppid = 0; res = 0; exec = 0; //300MB physical_mem = 300 * 1024 * 1024; //physical_mem = 3000000; while(1) { ppid = getppid(); //主进程崩溃时 if(ppid == 1) { monitorlog(LOG_INFO, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); monitorlog(LOG_INFO, "the master proc of monitor_agent is down and monitor proc auto restart itself!\n"); execl(proc_info->proc_path, proc_info->proc_path, NULL, (char *)0 ); } else { //如果主进程正常运行,但其占用物理内存大于300M时,就替换当前进程映象 res = get_proc_res(ppid); if(res > physical_mem) { monitorlog(LOG_INFO, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"); monitorlog(LOG_INFO, "monitor_agent phisical mem res %lld byte > %lld byte, so replace current process!!\n", res, physical_mem); kill(getppid(), SIGKILL); execl(proc_info->proc_path, proc_info->proc_path, NULL, (char *)0 ); } } monitorlog(LOG_INFO, "the master proc of monitor_agent is normal\n"); sleep_select(para_conf_s->time_monitor); } return 0; }