Add aaa-12 alert ulog remote and alert console redirect

RCA:
SOL:
修改人:zhangtao
检视人:
This commit is contained in:
zhangtaohz 2019-07-30 18:22:02 +08:00
parent bbf0ed8e72
commit 2a079c859d
5 changed files with 109 additions and 10 deletions

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "ulog.h" #include "ulog.h"
@ -8,7 +9,7 @@
#define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n" #define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n"
#define BAK_FILE "/tmp/%s"
static int copy_file(const char *src, const char *dst) static int copy_file(const char *src, const char *dst)
{ {
@ -97,6 +98,10 @@ END:
if (rename(bak_file, conf_path_file) < 0) { if (rename(bak_file, conf_path_file) < 0) {
ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno)); ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno));
} }
} else {
if (unlink(bak_file) < 0) {
ULOG_WARNING(g_log, "Delete back file:%s is failure", bak_file);
}
} }
return ret; return ret;
@ -142,7 +147,7 @@ int write_conf_content(FILE *fp, const u8 level, const char *filter_mod, void *a
strcat(line, ";"); strcat(line, ";");
} }
} }
strcat(line, " "); strcat(line, REDIRECT_SEPERATE);
strcat(line, (const char *)arg); strcat(line, (const char *)arg);
strcat(line, "\n"); strcat(line, "\n");

View File

@ -9,6 +9,10 @@
#define LOG_CONF_PATH "/etc/rsyslog.d/" #define LOG_CONF_PATH "/etc/rsyslog.d/"
#define LOG_DEV_DIR "/dev/" #define LOG_DEV_DIR "/dev/"
#define BAK_FILE "/tmp/%s"
#define REDIRECT_SEPERATE " "
typedef struct _level_str { typedef struct _level_str {

View File

@ -10,16 +10,100 @@
#define LOG_CONF_REMOTE_FILE_NAME "log-remote.conf" #define LOG_CONF_REMOTE_FILE_NAME "log-remote.conf"
static int config_log_remote_host(const log_remote_host_t *conf) typedef struct _rfc_key_fmt {
{ log_rfc_t rfc;
char redirect[MAX_LINE_SZ]; char fmt[20];
} rfc_key_fmt;
if (snprintf(redirect, sizeof(redirect), "@%s:%u", conf->host, conf->port) < 0) { static rfc_key_fmt rfc_tbl[] = {
ULOG_ERR(g_log, "Setting remote redirect[%s:%u] is faulure", conf->host, conf->port); {LOG_RFC_3164, "RFC3164fmt"},
{LOG_RFC_5424, "RFC5424fmt"}
};
static int match_line(const log_remote_host_t *conf)
{
}
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; return 1;
} }
log_conf_append(7, LOG_CONF_PATH, LOG_CONF_REMOTE_FILE_NAME, NULL,
write_conf_content, (void *)redirect); 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, "@");
};
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_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;
}
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);
return 1;
}
return 0; return 0;
} }

View File

@ -10,9 +10,14 @@ typedef enum {
LOG_REMOTE_OP_DEL LOG_REMOTE_OP_DEL
} log_op_t; } log_op_t;
typedef enum {
LOG_RFC_3164 = 0,
LOG_RFC_5424
} log_rfc_t;
typedef struct _log_remote_host { typedef struct _log_remote_host {
log_op_t op; log_op_t op;
u8 rfc; log_rfc_t rfc;
char host[256]; char host[256];
u16 port; u16 port;
} log_remote_host_t; } log_remote_host_t;

View File

@ -83,6 +83,7 @@ int main(int argc, char **argv)
log_remote_host_t remote = {0}; log_remote_host_t remote = {0};
strcpy(remote.host, "192.168.3.1"); strcpy(remote.host, "192.168.3.1");
remote.rfc = LOG_RFC_5424;
remote.port = 514; remote.port = 514;
rpc_conf_log_remote(NULL, &remote, sizeof(remote), NULL); rpc_conf_log_remote(NULL, &remote, sizeof(remote), NULL);
END: END: