Mod aaa-12 add remote log level configure

RCA:
SOL:
修改人:zhangtao
检视人:
This commit is contained in:
zhangtaohz 2019-08-01 16:44:18 +08:00
parent 452af09016
commit b4b892cee7
8 changed files with 277 additions and 66 deletions

View File

@ -13,7 +13,7 @@
static int copy_file(const char *src, const char *dst)
{
int ret = 1;
int ret = -1;
FILE *src_fp = NULL,*dst_fp = NULL;
src_fp = fopen(src, "r");
if (src_fp == NULL) {
@ -58,7 +58,7 @@ static int __log_conf(const char *mode, const u8 level, const char *conf_path, c
int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg)
{
FILE *fp = NULL;
int ret = 1;
int ret = -1;
u8 exist_backup = 1;
/********** rsyslog configure **********/
@ -97,7 +97,7 @@ END:
}
if (exist_backup == 1) {
if (ret == 1) {
if (ret == -1) {
// 恢复备份配置
if (rename(bak_file, conf_path_file) < 0) {
ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno));
@ -124,10 +124,30 @@ int log_conf_append(const u8 level, const char *conf_path, const char *conf_file
return __log_conf("a", level, conf_path, conf_file, filter_mod, cb_content, arg);
}
int log_level_to_str(const u8 level, char *str, u8 len)
{
u8 i;
char tmp[20];
for (i = 0; i <= level;) {
if (snprintf(tmp, sizeof(tmp), "*.=%s", g_level_array[i].str) < 0) {
ULOG_ERR(g_log, "Setting content of log file configure is failure");
return -1;
}
strcat(str, tmp);
i++;
if (level >= i) {
strcat(str, ";");
}
}
return 0;
}
int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg)
{
int i;
int ret = 1;
int ret = -1;
char line[MAX_LINE_SZ + 100] = {0};
ULOG_DEBUG(g_log, "filter module:%s\n", filter_mod);
if ((filter_mod != NULL) && (strlen(filter_mod) > 0)) {
@ -139,19 +159,11 @@ int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *a
}
}
char tmp[20];
line[0] = '\0'; // 清零
for (i = 0; i <= level;) {
if (snprintf(tmp, sizeof(tmp), "*.=%s", g_level_array[i].str) < 0) {
if (log_level_to_str(level, line, sizeof(line)) != 0) {
ULOG_ERR(g_log, "Setting content of log file configure is failure");
goto END;
}
strcat(line, tmp);
i++;
if (level >= i) {
strcat(line, ";");
}
}
strcat(line, REDIRECT_SEPERATE);
strcat(line, (const char *)arg);
@ -173,7 +185,7 @@ int modify_authorizing(const char *redirect_path)
{
if (chmod(redirect_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) {
ULOG_ERR(g_log, "Authorizing of %s which is modified is failure:%s", redirect_path, strerror(errno));
return 1;
return -1;
}
return 0;
}
@ -182,12 +194,12 @@ int write_conf_content_authorizing(FILE *fp, const u8 level, const c
{
if (write_conf_content(fp, level, filter_mod, arg) != 0) {
ULOG_ERR(g_log, "configure of log conosle which is written is failure");
return 1;
return -1;
}
if (modify_authorizing((const char *)arg) != 0) {
ULOG_ERR(g_log, "Modifying authorizing of %s is failure", (const char *)arg);
return 1;
return -1;
}
return 0;

View File

@ -21,6 +21,8 @@ typedef struct _level_str {
} level_str_t;
extern ulog_t *g_log;
extern FILE *g_conf_fp;
extern char g_conf_file[MAX_PATH_SZ];
static level_str_t g_level_array[] = {
{LOG_EMERG, "emerg"},
@ -39,5 +41,7 @@ int log_conf_append(const u8 level, const char *conf_path, const char *conf_file
int (*cb_content)(FILE *fp, const u8 level, const char *filter_mod, void *arg), void *arg);
int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg);
int write_conf_content_authorizing(FILE *fp, const u8 level, const char *filter_mod, void *arg);
int log_level_to_str(const u8 level, char *str, u8 len);
#endif

View File

@ -15,7 +15,7 @@ static int write_console_content(FILE *fp, const u8 level, const char *filter_mo
if ((dir = opendir(LOG_DEV_DIR)) == NULL) {
ULOG_ERR(g_log, "Open dir:[%s] is failure:%d\n", LOG_DEV_DIR, strerror(errno));
return 1;
return -1;
}
struct dirent *ptr;
@ -36,11 +36,11 @@ static int write_console_content(FILE *fp, const u8 level, const char *filter_mo
ULOG_DEBUG(g_log, "ttyS name:%s", ptr->d_name);
if (snprintf(path, sizeof(path), "%s%s", LOG_DEV_DIR, ptr->d_name) < 0) {
ULOG_ERR(g_log, "Setting %s of log console is failure", ptr->d_name);
return 1;
return -1;
}
if (write_conf_content_authorizing(fp, level, filter_mod, path) != 0) {
ULOG_ERR(g_log, "Writing tty[module:%s] of log is failure", filter_mod);
return 1;
return -1;
}
}
@ -54,7 +54,7 @@ static int config_log_console(const log_console_t *conf)
if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, conf->module_name,
write_console_content, NULL) != 0) {
ULOG_ERR(g_log, "Log's console configure which is written is failure");
return 1;
return -1;
}
return 0;

View File

@ -47,7 +47,7 @@ static level_str_t g_level_array[] = {
static int write_logratate_conf(const log_file_t *conf, const char *log_path)
{
int ret = 1;
int ret = -1;
FILE *fp = NULL;
/********** logrotate **********/
char str_compress[sizeof(STR_COMPRESS)] = {0};
@ -89,11 +89,11 @@ END:
static int conf_log_file(const log_file_t *conf)
{
int ret = 1;
int ret = -1;
u32 max_level = MAX_LOG_LEVEL_VALUE;
if (conf->level > max_level) {
ULOG_WARNING(g_log, "Configure log level:%u more than max value:%u", conf->level, max_level);
return 1;
return -1;
}
char path[sizeof(conf->path)];
@ -107,13 +107,13 @@ static int conf_log_file(const log_file_t *conf)
if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_FILE_NAME, NULL,
write_conf_content, (void *)path) != 0) {
ULOG_ERR(g_log, "Log configure which is written is failure");
return 1;
return -1;
}
/********** logrotate **********/
if (write_logratate_conf(conf, path) != 0) {
ULOG_ERR(g_log, "Logratate configure which is written is failure");
return 1;
return -1;
}

View File

@ -16,7 +16,7 @@ static int write_pty_content(FILE *fp, const u8 level, const char *filter_mod, v
if ((dir = opendir(LOG_DEV_PTY_DIR)) == NULL) {
ULOG_ERR(g_log, "Open dir:[%s] is failure:%d\n", LOG_DEV_PTY_DIR, strerror(errno));
return 1;
return -1;
}
struct dirent *ptr;
@ -37,11 +37,11 @@ static int write_pty_content(FILE *fp, const u8 level, const char *filter_mod, v
ULOG_DEBUG(g_log, "pty name:%s", ptr->d_name);
if (snprintf(path, sizeof(path), "%s%s", LOG_DEV_PTY_DIR, ptr->d_name) < 0) {
ULOG_ERR(g_log, "Setting pty of log[%s] is failure", ptr->d_name);
return 1;
return -1;
}
if (write_conf_content_authorizing(fp, level, filter_mod, path) != 0) {
ULOG_ERR(g_log, "Writing pty[module:%s] of log is failure", filter_mod);
return 1;
return -1;
}
}
@ -54,7 +54,7 @@ static int config_log_pty(const log_pty_t *conf)
if (log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_PTY_FILE_NAME, conf->module_name,
write_pty_content, NULL) != 0) {
ULOG_ERR(g_log, "Log's pty configure which is written is failure");
return 1;
return -1;
}
return 0;

View File

@ -10,6 +10,17 @@
#define LOG_CONF_REMOTE_FILE_NAME "log-remote.conf"
#define RESET_SEEK(fp) fseek(fp, 0, SEEK_SET)
#define LOG_CONF_KEY_REMOTE_LEVEL "remote.level="
typedef enum {
LOG_REMOTE_OP_ADD = 0,
LOG_REMOTE_OP_DEL,
LOG_REMOTE_MAX
} log_op_t;
typedef struct _rfc_key_fmt {
log_rfc_t rfc;
char fmt[20];
@ -55,7 +66,7 @@ static int match_remote_config(const char *line, const void *src)
ULOG_DEBUG(g_log, "The line:%s can't be parsed", line);
}
return 1;
return -1;
}
static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod,
@ -63,7 +74,7 @@ static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod,
int (*final_cb)(FILE *fp, const u8 level, const char *filter_mod, void *arg),
void *arg)
{
int ret = 1;
int ret = -1;
FILE *bak_fp = NULL;
char path[MAX_PATH_SZ];
snprintf(path, sizeof(path), BAK_FILE, LOG_CONF_REMOTE_FILE_NAME);
@ -73,7 +84,7 @@ static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod,
goto FINAL_PHASE;
} else {
ULOG_ERR(g_log, "Opening remote backup file:%s is failure:%s", path, strerror(errno));
return 1;
return -1;
}
}
@ -117,6 +128,67 @@ END:
return ret;
}
static int remote_conf_level_content(FILE *fp, const u8 level)
{
int ret = -1;
FILE *bak_fp = NULL;
char path[MAX_PATH_SZ];
snprintf(path, sizeof(path), BAK_FILE, LOG_CONF_REMOTE_FILE_NAME);
bak_fp = fopen(path, "r");
if (bak_fp == NULL) {
ULOG_ERR(g_log, "Opening remote backup file:%s is failure:%s", path, strerror(errno));
return -1;
}
char *line = NULL;
ssize_t n, n1;
ssize_t n3;
char text_level[MAX_LINE_SZ], old_redirect[MAX_LINE_SZ];
char rewrite_line[MAX_LINE_SZ];
while ((n1 = getline(&line, &n, bak_fp)) != -1) {
n3 = sscanf(line, "%s"REDIRECT_SEPERATE"%s", text_level, old_redirect);
if (errno != 0) {
// 错误发生
ULOG_ERR(g_log, "Parsing remote line is failure:%s", strerror(errno));
goto END;
}
if (n3 == 2) {
// 匹配到
// 改变该行日志级别
memset(rewrite_line, 0, sizeof(rewrite_line));
if (log_level_to_str(level, rewrite_line, sizeof(rewrite_line)) != 0) {
ULOG_ERR(g_log, "Log level converts str is failure");
goto END;
}
strcat(rewrite_line, REDIRECT_SEPERATE);
strcat(rewrite_line, old_redirect);
strcat(rewrite_line, "\n");
n3 = strlen(rewrite_line);
} else {
// 未能识别行
ULOG_DEBUG(g_log, "The line:%s can't be parsed", line);
}
ULOG_DEBUG(g_log, "Recover old line:%s to file:%s", rewrite_line, path);
n1 = fwrite(rewrite_line, 1, n3, fp);
if (n3 != n1) {
ULOG_ERR(g_log, "Recovering old line:%s to file is failure:%s", line, strerror(errno));
goto END;
}
}
ret = 0;
END:
if (line != NULL) {
free(line);
}
if (bak_fp != NULL) {
fclose(bak_fp);
}
return ret;
}
static int add_remote_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *arg)
{
return remote_conf_content(fp, level, filter_mod, match_remote_config, write_conf_content, arg);
@ -127,6 +199,38 @@ static int del_remote_conf_content(FILE *fp, const u8 level, const char *filter_
return remote_conf_content(fp, level, filter_mod, match_remote_config, NULL, arg);
}
static int modify_remote_conf_log_level(FILE *fp, const u8 level, const char *filter_mod, void *arg)
{
return remote_conf_level_content(fp, level);
}
static int get_log_level()
{
if (RESET_SEEK(g_conf_fp) == -1) {
ULOG_ERR(g_log, "[get log level]Seeknig config to begin is faiure:%s", strerror(errno));
return -1;
}
ssize_t n, n1, n2;
char *line;
u8 value;
while ((n1 = getline(&line, &n, g_conf_fp)) != -1) {
n2 = sscanf(line, LOG_CONF_KEY_REMOTE_LEVEL"%u", &value);
if (errno != 0) {
// 错误发生
ULOG_ERR(g_log, "Parsing level from configure is failure:%s", strerror(errno));
return -1;
}
if (n2 == 1) {
return value;
} else {
ULOG_DEBUG(g_log, "Unknown level row:%s", line);
}
}
return LOG_INFO;
}
static int add_remote_host(const log_remote_host_t *conf)
{
ULOG_INFO(g_log, "Adding remote log server[%s:%u], rfc is %s", conf->host, conf->port, rfc_tbl[conf->rfc].fmt);
@ -139,12 +243,18 @@ static int add_remote_host(const log_remote_host_t *conf)
if (snprintf(redirect, sizeof(redirect), "%s@%s:%u:%s",
prefix, conf->host, conf->port, rfc_tbl[conf->rfc].fmt) < 0) {
ULOG_ERR(g_log, "Setting remote redirect[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt);
return 1;
return -1;
}
if (log_conf(7, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL,
int level = get_log_level();
if (level == -1) {
ULOG_ERR(g_log, "Getting log level is failure");
return -1;
}
if (log_conf(level, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL,
add_remote_conf_content, (void *)redirect) != 0) {
ULOG_ERR(g_log, "Adding remote server[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt);
return 1;
return -1;
}
return 0;
@ -154,6 +264,8 @@ static int del_remote_host(const log_remote_host_t *conf)
{
int ret;
char prefix[1] = "";
ULOG_INFO(g_log, "Deleting remote log server[%s:%u], rfc is %s", conf->host, conf->port, rfc_tbl[conf->rfc].fmt);
if (conf->rfc == LOG_RFC_5424) {
strcat(prefix, "@");
};
@ -161,34 +273,69 @@ static int del_remote_host(const log_remote_host_t *conf)
if (snprintf(redirect, sizeof(redirect), "%s@%s:%u:%s",
prefix, conf->host, conf->port, rfc_tbl[conf->rfc].fmt) < 0) {
ULOG_ERR(g_log, "Setting remote redirect[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt);
return 1;
return -1;
}
if (log_conf(7, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL,
del_remote_conf_content, (void *)redirect) != 0) {
ULOG_ERR(g_log, "Adding remote server[%s:%u:%s] is faulure", conf->host, conf->port, rfc_tbl[conf->rfc].fmt);
return 1;
return -1;
}
return ret;
}
static int config_log_remote_host(const log_remote_host_t *conf)
static int config_log_remote_host(const log_op_t op, const log_remote_host_t *conf)
{
if ((sizeof(rfc_tbl) / sizeof(rfc_key_fmt)) < conf->rfc) {
ULOG_WARNING(g_log, "Unknown rfc format key:%u", conf->rfc);
return 1;
return -1;
}
if (conf->op >= LOG_REMOTE_MAX) {
ULOG_WARNING(g_log, "Unknown operation type:%u", conf->op);
return 1;
if (op >= LOG_REMOTE_MAX) {
ULOG_WARNING(g_log, "Unknown operation type:%u", op);
return -1;
}
return remote_funcs[conf->op](conf);
return remote_funcs[op](conf);
}
void rpc_conf_log_remote(rpc_conn *conn, pointer input, int input_len, pointer data)
static int config_log_remote_level(const log_remote_level_t *level)
{
if (truncate(g_conf_file, 0) == -1) {
ULOG_ERR(g_log, "Truncate configure:%s is faiure:%s", g_conf_file, strerror(errno));
return -1;
}
/* 定位到开始位置 */
if (RESET_SEEK(g_conf_fp) == -1) {
ULOG_ERR(g_log, "Seeknig config to begin is faiure:%s", strerror(errno));
return -1;
}
char fmt[MAX_LINE_SZ];
snprintf(fmt, sizeof(fmt), LOG_CONF_KEY_REMOTE_LEVEL"%u", level->level);
ULOG_DEBUG(g_log, "Configure content is %s", fmt);
int len = strlen(fmt);
int n = fwrite(fmt, 1, len, g_conf_fp);
if (len != n) {
ULOG_ERR(g_log, "Writing remote level is failure:%s", strerror(errno));
return -1;
}
fflush(g_conf_fp);
if (log_conf(level->level, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL,
modify_remote_conf_log_level, NULL) != 0) {
ULOG_ERR(g_log, "Modifing log level:%u of remote server [%s:%u:%s] is faulure", level->level);
return -1;
}
return 0;
}
static void rpc_conf_log_remote(const log_op_t op, rpc_conn *conn, pointer input, int input_len, pointer data)
{
if (input == NULL) {
ULOG_WARNING(g_log, "Remote log configure can't null");
return;
}
u32 need_len = sizeof(log_remote_host_t);
if (input_len < need_len) {
ULOG_WARNING(g_log,
@ -197,6 +344,35 @@ void rpc_conf_log_remote(rpc_conn *conn, pointer input, int input_len, pointer d
return;
}
config_log_remote_host((const log_remote_host_t *)input);
config_log_remote_host(op, (const log_remote_host_t *)input);
}
void rpc_conf_log_add_remote(rpc_conn *conn, pointer input, int input_len, pointer data)
{
rpc_conf_log_remote(LOG_REMOTE_OP_ADD, conn, input, input_len, data);
}
void rpc_conf_log_del_remote(rpc_conn *conn, pointer input, int input_len, pointer data)
{
rpc_conf_log_remote(LOG_REMOTE_OP_DEL, conn, input, input_len, data);
}
void rpc_conf_log_remote_level(rpc_conn *conn, pointer input, int input_len, pointer data)
{
if (input == NULL) {
ULOG_WARNING(g_log, "Remote log level can't null");
return;
}
u32 need_len = sizeof(log_remote_level_t);
if (input_len < need_len) {
ULOG_WARNING(g_log,
"The input paramter of rpc log remote level is needed length of %u, but the actual length is %u",
need_len, input_len);
return;
}
config_log_remote_level((const log_remote_level_t *)input);
}

View File

@ -5,24 +5,23 @@
#include "common_types.h"
#include "rpc_common.h"
typedef enum {
LOG_REMOTE_OP_ADD = 0,
LOG_REMOTE_OP_DEL,
LOG_REMOTE_MAX
} log_op_t;
typedef enum {
LOG_RFC_3164 = 0,
LOG_RFC_5424
} log_rfc_t;
typedef struct _log_remote_host {
log_op_t op;
log_rfc_t rfc;
char host[256];
u16 port;
} log_remote_host_t;
void rpc_conf_log_remote(rpc_conn *conn, pointer input, int input_len, pointer data);
typedef struct _log_remote_level {
u8 level;
} log_remote_level_t;
void rpc_conf_log_add_remote(rpc_conn *conn, pointer input, int input_len, pointer data);
void rpc_conf_log_del_remote(rpc_conn *conn, pointer input, int input_len, pointer data);
void rpc_conf_log_remote_level(rpc_conn *conn, pointer input, int input_len, pointer data);
#endif

View File

@ -21,9 +21,13 @@
#define SERVICE_LOG_PTY_NAME "log-pty"
#define SERVICE_LOG_REMOTE_NAME "log-remote"
ulog_t *g_log = NULL;
static sem_t g_sem;
#define DEFAULT_CONFIG_FILE "/etc/log-sched.conf"
ulog_t *g_log = NULL;
FILE *g_conf_fp;
char g_conf_file[MAX_PATH_SZ];
static sem_t g_sem;
void signal_exit()
{
@ -52,7 +56,14 @@ int main(int argc, char **argv)
g_log = ulog_init(LOG_SCHED_MODULE_NAME, !run_daemon);
if (g_log == NULL) {
fprintf(stderr, "Initiating ulog is failure");
return 1;
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;
}
if (run_daemon) {
@ -66,12 +77,12 @@ int main(int argc, char **argv)
if (server == NULL)
{
ULOG_ERR(g_log, "start server error");
return 1;
return -1;
}
if (sem_init(&g_sem, 0, 0) != 0) {
ULOG_ERR(g_log, "Init sem is failure:%s", strerror(errno));
return 1;
return -1;
}
signal(SIGINT, signal_exit);
ULOG_INFO(g_log, "Server of log schedule is started");
@ -80,8 +91,9 @@ int main(int argc, char **argv)
rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, "conf_log_file", rpc_conf_log_file);
rpc_server_regservice(server, SERIVCE_LOG_CONSOLE_NAME, "conf_log_console", rpc_conf_log_console);
rpc_server_regservice(server, SERVICE_LOG_PTY_NAME, "conf_log_pty", rpc_conf_log_pty);
rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, "conf_log_remote", rpc_conf_log_remote);
rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, "conf_log_add_remote", rpc_conf_log_add_remote);
rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, "conf_log_del_remote", rpc_conf_log_del_remote);
rpc_server_regservice(server, SERVICE_LOG_REMOTE_NAME, "conf_log_remote_level", rpc_conf_log_remote_level);
/*
log_file_t log = {0};
@ -104,12 +116,16 @@ int main(int argc, char **argv)
rpc_conf_log_pty(NULL, &tty, sizeof(tty), NULL);
log_remote_host_t remote = {0};
remote.op = LOG_REMOTE_OP_DEL;
strcpy(remote.host, "192.168.3.11");
remote.rfc = LOG_RFC_5424;
remote.port = 514;
rpc_conf_log_remote(NULL, &remote, sizeof(remote), NULL);
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);
*/
if (sem_wait(&g_sem) == -1) {
ULOG_ERR(g_log, "Waitting semaphore is failure:%s", strerror(errno));
}
@ -117,6 +133,10 @@ int main(int argc, char **argv)
ULOG_INFO(g_log, "%s is shutdown", LOG_SCHED_MODULE_NAME);
END:
if (g_conf_fp != NULL) {
fclose(g_conf_fp);
}
ulog_close(g_log);
return 0;
}