2019-07-26 03:53:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2019-07-31 09:32:43 +00:00
|
|
|
#include <signal.h>
|
2019-07-26 03:53:33 +00:00
|
|
|
|
|
|
|
#include "rpc_server.h"
|
|
|
|
#include "ulog_api.h"
|
|
|
|
#include "log_file.h"
|
|
|
|
#include "log_console.h"
|
2019-07-31 09:32:43 +00:00
|
|
|
#include "log_pty.h"
|
2019-07-29 10:20:17 +00:00
|
|
|
#include "log_remote.h"
|
2019-07-26 03:53:33 +00:00
|
|
|
#include "rpc_module.h"
|
2019-08-02 09:58:02 +00:00
|
|
|
#include "ulog_in.h"
|
2019-08-15 06:04:17 +00:00
|
|
|
#include "sev_sched.h"
|
2019-07-26 03:53:33 +00:00
|
|
|
|
|
|
|
|
2019-07-29 10:20:17 +00:00
|
|
|
#define LOG_SCHED_MODULE_NAME "log-sched"
|
|
|
|
|
2019-08-01 08:44:18 +00:00
|
|
|
#define DEFAULT_CONFIG_FILE "/etc/log-sched.conf"
|
|
|
|
|
2019-07-26 03:53:33 +00:00
|
|
|
ulog_t *g_log = NULL;
|
2019-08-01 08:44:18 +00:00
|
|
|
FILE *g_conf_fp;
|
|
|
|
char g_conf_file[MAX_PATH_SZ];
|
2019-07-31 09:32:43 +00:00
|
|
|
|
2019-08-28 10:19:24 +00:00
|
|
|
typedef ret_code (*initial_cb)();
|
2019-08-30 02:36:56 +00:00
|
|
|
typedef ret_code (*exit_cb)();
|
|
|
|
typedef struct _log_opt {
|
2019-08-28 10:19:24 +00:00
|
|
|
char type[20];
|
2019-08-30 02:36:56 +00:00
|
|
|
initial_cb init_func;
|
|
|
|
exit_cb exit_func;
|
|
|
|
} log_opt_t;
|
2019-08-28 10:19:24 +00:00
|
|
|
|
2019-08-30 02:36:56 +00:00
|
|
|
log_opt_t g_log_opt[] = {
|
|
|
|
{"console", console_initial, NULL},
|
|
|
|
{"monitor", pty_initial, pty_exit}
|
2019-08-28 10:19:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
void signal_exit()
|
|
|
|
{
|
2019-08-15 06:04:17 +00:00
|
|
|
log_sev_exit();
|
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
ULOG_DEBUG(g_log, "Receive shutdown signal");
|
|
|
|
}
|
2019-07-26 03:53:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int run_daemon = 0;
|
|
|
|
char *options = "d";
|
|
|
|
int opt;
|
|
|
|
|
|
|
|
while ((opt = getopt(argc, argv, options)) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'd':
|
|
|
|
run_daemon = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_log = ulog_init(LOG_SCHED_MODULE_NAME, !run_daemon);
|
|
|
|
if (g_log == NULL) {
|
2019-08-28 08:54:10 +00:00
|
|
|
fprintf(stderr, "Initiating ulog is failure\n");
|
2019-08-01 08:44:18 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(g_conf_file, DEFAULT_CONFIG_FILE);
|
|
|
|
g_conf_fp = fopen(g_conf_file, "a+");
|
|
|
|
if (g_conf_fp == NULL) {
|
|
|
|
ULOG_ERR(g_log, "Opening configure:%s is failure:%s", g_conf_file, strerror(errno));
|
|
|
|
goto END;
|
2019-07-26 03:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (run_daemon) {
|
|
|
|
if (daemon(0, 0) == -1) {
|
|
|
|
ULOG_ERR(g_log, "Setting daemon running is failure:%s", strerror(errno));
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 10:19:24 +00:00
|
|
|
/* 各类配置初始化 */
|
2019-08-30 02:36:56 +00:00
|
|
|
for (int i = 0; i < (sizeof(g_log_opt) / sizeof(log_opt_t)); i++) {
|
|
|
|
if ( NULL != g_log_opt[i].init_func) {
|
|
|
|
g_log_opt[i].init_func();
|
|
|
|
}
|
2019-08-28 10:19:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 02:36:56 +00:00
|
|
|
/* 重启syslog */
|
|
|
|
sev_restart();
|
|
|
|
|
2019-07-26 03:53:33 +00:00
|
|
|
rpc_server *server = rpc_server_create_ex(RPC_MODULE_SYSLOG_NAME);
|
|
|
|
if (server == NULL)
|
|
|
|
{
|
|
|
|
ULOG_ERR(g_log, "start server error");
|
2019-08-15 06:04:17 +00:00
|
|
|
goto END;
|
2019-08-15 09:17:12 +00:00
|
|
|
}
|
2019-08-15 06:04:17 +00:00
|
|
|
|
|
|
|
if (log_sev_init() != 0) {
|
2019-08-15 09:17:12 +00:00
|
|
|
ULOG_INFO(g_log, "Log service which is initited is failure");
|
2019-08-15 06:04:17 +00:00
|
|
|
goto END;
|
|
|
|
}
|
2019-08-15 09:17:12 +00:00
|
|
|
ULOG_INFO(g_log, "Log service which is initited is success");
|
2019-08-15 06:04:17 +00:00
|
|
|
|
2019-08-15 09:17:12 +00:00
|
|
|
signal(SIGINT, signal_exit);
|
2019-07-26 03:53:33 +00:00
|
|
|
ULOG_INFO(g_log, "Server of log schedule is started");
|
|
|
|
|
2019-08-15 06:04:17 +00:00
|
|
|
/* 注册配置处理函数 */
|
2019-08-14 09:17:18 +00:00
|
|
|
rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, CONF_LOG_FILE_FUNC, rpc_conf_log_file);
|
2019-08-02 09:58:02 +00:00
|
|
|
rpc_server_regservice(server, SERIVCE_LOG_CONSOLE_NAME, CONF_LOG_CONSOLE_FUNC, rpc_conf_log_console);
|
2019-08-13 08:25:43 +00:00
|
|
|
rpc_server_regservice(server, SERVICE_LOG_PTY_NAME, CONF_LOG_PTY_FUNC, rpc_conf_log_pty);
|
2019-08-14 09:17:18 +00:00
|
|
|
rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, CONF_LOG_REMOTE_ADD_HOST_FUNC, rpc_conf_log_add_remote);
|
|
|
|
rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, CONF_LOG_REMOTE_DEL_HOST_FUNC, rpc_conf_log_del_remote);
|
|
|
|
rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, CONF_LOG_REMOTE_LEVEL_FUNC, rpc_conf_log_remote_level);
|
2019-07-31 09:32:43 +00:00
|
|
|
/*
|
2019-07-26 03:53:33 +00:00
|
|
|
|
|
|
|
log_file_t log = {0};
|
|
|
|
log.is_compress = LOG_UNCOMPRESS; //LOG_COMPRESS;
|
|
|
|
log.del_over_days = 10;
|
|
|
|
log.level = LOG_DEBUG;
|
|
|
|
rpc_conf_log_file(NULL, &log, sizeof(log), NULL);
|
|
|
|
|
|
|
|
log_console_t console = {0};
|
|
|
|
strcpy(console.module_name, "111");
|
|
|
|
console.level = LOG_DEBUG;
|
|
|
|
console.on = 1;
|
|
|
|
rpc_conf_log_console(NULL, &console, sizeof(console), NULL);
|
2019-07-26 10:22:43 +00:00
|
|
|
|
|
|
|
|
2019-07-31 09:32:43 +00:00
|
|
|
log_pty_t tty = {0};
|
2019-07-29 10:20:17 +00:00
|
|
|
//strcpy(tty.module_name, "111");
|
2019-07-26 10:22:43 +00:00
|
|
|
tty.level = LOG_DEBUG;
|
|
|
|
tty.on = 1;
|
2019-07-31 09:32:43 +00:00
|
|
|
rpc_conf_log_pty(NULL, &tty, sizeof(tty), NULL);
|
2019-07-29 10:20:17 +00:00
|
|
|
|
|
|
|
log_remote_host_t remote = {0};
|
2019-07-31 09:32:43 +00:00
|
|
|
strcpy(remote.host, "192.168.3.11");
|
2019-07-30 10:22:02 +00:00
|
|
|
remote.rfc = LOG_RFC_5424;
|
2019-07-29 10:20:17 +00:00
|
|
|
remote.port = 514;
|
2019-08-01 08:44:18 +00:00
|
|
|
rpc_conf_log_add_remote(NULL, &remote, sizeof(remote), NULL);
|
|
|
|
|
|
|
|
log_remote_level_t level;
|
|
|
|
level.level = 5;
|
|
|
|
rpc_conf_log_remote_level(NULL, &level, sizeof(level), NULL);
|
2019-07-31 09:32:43 +00:00
|
|
|
*/
|
2019-08-15 09:17:12 +00:00
|
|
|
|
|
|
|
sev_loop();
|
2019-07-31 09:32:43 +00:00
|
|
|
|
|
|
|
ULOG_INFO(g_log, "%s is shutdown", LOG_SCHED_MODULE_NAME);
|
2019-07-26 03:53:33 +00:00
|
|
|
END:
|
2019-08-30 02:36:56 +00:00
|
|
|
|
|
|
|
/* 各类配置退出前的收尾 */
|
|
|
|
for (int i = 0; i < (sizeof(g_log_opt) / sizeof(log_opt_t)); i++) {
|
|
|
|
if ( NULL != g_log_opt[i].exit_func) {
|
|
|
|
g_log_opt[i].exit_func();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 08:44:18 +00:00
|
|
|
if (g_conf_fp != NULL) {
|
|
|
|
fclose(g_conf_fp);
|
|
|
|
}
|
|
|
|
|
2019-07-26 03:53:33 +00:00
|
|
|
ulog_close(g_log);
|
|
|
|
return 0;
|
|
|
|
}
|