2019-07-29 10:20:17 +00:00
|
|
|
#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"
|
|
|
|
|
2019-07-30 10:22:02 +00:00
|
|
|
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"}
|
|
|
|
};
|
|
|
|
|
|
|
|
static int match_line(const log_remote_host_t *conf)
|
2019-07-29 10:20:17 +00:00
|
|
|
{
|
2019-07-30 10:22:02 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int remote_conf_content(FILE *fp, const u8 level, const char *filter_mod, 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(bak_fp, "r");
|
|
|
|
if (bak_fp == NULL) {
|
|
|
|
ULOG_ERR(g_log, "Opening remote back file:%s is failure:%s", path, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *line = NULL;
|
|
|
|
ssize_t n, n1;
|
|
|
|
char *pad, *host, *rfc_fmt;
|
|
|
|
u16 port;
|
|
|
|
while ((n = getline(&line, &n, bak_fp)) != -1) {
|
|
|
|
n1 = sscanf(line, "%s"REDIRECT_SEPERATE"%s:%u:%s", &pad, &host, &port, &rfc_fmt);
|
|
|
|
if (n1 == 4) {
|
|
|
|
// 匹配到
|
|
|
|
} if (errno != 0) {
|
|
|
|
// 错误发生
|
|
|
|
} else {
|
|
|
|
// 未知行,跳过
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write_conf_content(fp, level, filter_mod, arg) != 0) {
|
|
|
|
ULOG_ERR(g_log, "Write remote configure 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_host(const log_remote_host_t *conf)
|
|
|
|
{
|
|
|
|
char prefix[1] = "";
|
|
|
|
if (conf->rfc == LOG_RFC_5424) {
|
|
|
|
strcat(prefix, "@");
|
|
|
|
};
|
|
|
|
|
2019-07-29 10:20:17 +00:00
|
|
|
char redirect[MAX_LINE_SZ];
|
2019-07-30 10:22:02 +00:00
|
|
|
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_append(7, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL,
|
|
|
|
write_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;
|
|
|
|
}
|
2019-07-29 10:20:17 +00:00
|
|
|
|
2019-07-30 10:22:02 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int del_remote_host(const log_remote_host_t *conf)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-07-29 10:20:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2019-07-30 10:22:02 +00:00
|
|
|
|
2019-07-29 10:20:17 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|