secgateway/Platform/user/ulog/log-sched/log_remote.c

203 lines
6.2 KiB
C
Raw Normal View History

#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <syslog.h>
#include "log_remote.h"
#include "log_common.h"
#define LOG_CONF_REMOTE_FILE_NAME "log-remote.conf"
typedef struct _rfc_key_fmt {
log_rfc_t rfc;
char fmt[20];
} rfc_key_fmt;
static rfc_key_fmt rfc_tbl[] = {
{LOG_RFC_3164, "RFC3164fmt"},
{LOG_RFC_5424, "RFC5424fmt"}
};
typedef int (*op_func)(const log_remote_host_t *conf);
static int add_remote_host(const log_remote_host_t *conf);
static int del_remote_host(const log_remote_host_t *conf);
static op_func remote_funcs[] = {
add_remote_host,
del_remote_host
};
static int match_remote_config(const char *line, const void *src)
{
char text_level[MAX_LINE_SZ], old_redirect[MAX_LINE_SZ];
int n;
ULOG_DEBUG(g_log, "The line:%s will be matched", line);
n = 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));
return -1;
}
if (n == 2) {
// 匹配到
// 是否相同配置判读
ULOG_DEBUG(g_log, "%s will compare with %s", old_redirect, (const char *)src);
if (strcmp(old_redirect, src) == 0) {
return 0;
}
} else {
// 未能识别行
ULOG_DEBUG(g_log, "The line:%s can't be parsed", line);
}
return 1;
}
static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod,
int (*match_cb)(const char *line, const void *src),
int (*final_cb)(FILE *fp, const u8 level, const char *filter_mod, void *arg),
void *arg)
{
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) {
if (errno == ENOENT) {
goto FINAL_PHASE;
} else {
ULOG_ERR(g_log, "Opening remote backup file:%s is failure:%s", path, strerror(errno));
return 1;
}
}
char *line = NULL;
ssize_t n, n1;
char text_level[MAX_LINE_SZ], old_redirect[MAX_LINE_SZ];
while ((n = getline(&line, &n, bak_fp)) != -1) {
int match = match_cb(line, arg);
if (match == -1) {
ULOG_ERR(g_log, "Configure which is matched is failure");
} else if (match == 0) {
ULOG_DEBUG(g_log, "Be matched");
continue;
}
ULOG_DEBUG(g_log, "Recover old line:%s to file:%s", line, path);
n1 = fwrite(line, 1, n, fp);
if (n != n1) {
ULOG_ERR(g_log, "Recovering old line:%s to file is failure:%s", line, strerror(errno));
goto END;
}
}
FINAL_PHASE:
if (final_cb != NULL) {
if (final_cb(fp, level, filter_mod, arg) != 0) {
ULOG_ERR(g_log, "Executing final callback is failure");
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);
}
static int del_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, NULL, arg);
}
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);
char prefix[1] = "";
if (conf->rfc == LOG_RFC_5424) {
strcat(prefix, "@");
};
char redirect[MAX_LINE_SZ];
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;
}
if (log_conf(7, 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 0;
}
static int del_remote_host(const log_remote_host_t *conf)
{
int ret;
char prefix[1] = "";
if (conf->rfc == LOG_RFC_5424) {
strcat(prefix, "@");
};
char redirect[MAX_LINE_SZ];
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;
}
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 ret;
}
static int config_log_remote_host(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;
}
if (conf->op >= LOG_REMOTE_MAX) {
ULOG_WARNING(g_log, "Unknown operation type:%u", conf->op);
return 1;
}
return remote_funcs[conf->op](conf);
}
void rpc_conf_log_remote(rpc_conn *conn, pointer input, int input_len, pointer data)
{
u32 need_len = sizeof(log_remote_host_t);
if (input_len < need_len) {
ULOG_WARNING(g_log,
"The input paramter of rpc log remote host is needed length of %u, but the actual length is %u",
need_len, input_len);
return;
}
config_log_remote_host((const log_remote_host_t *)input);
}