secgateway/Platform/user/logging/cmd_remote.c

315 lines
8.6 KiB
C
Raw Normal View History

#include "cmd_remote.h"
#include "logging_common.h"
#include "common_types.h"
#include "log_types.h"
#include "ulog_in.h"
#include "s2j/s2j.h"
#define DEFAULT_REMOTE_HOST_PORT 514
#define MAX_REMOTE_HOST_CNT 8
static int logging_remote_json_parse(pointer input, log_remote_host_x_t *config_buff);
static int logging_remote_json_parse_array(pointer input,
log_remote_host_x_t *config_buff, int *cnt);
static int logging_remote_level_json_parse(pointer input, log_remote_level_t *config_buff);
static log_rfc_t get_rfc_by_name(const char *name)
{
for (int i = 0; i < (sizeof(rfc_tbl) / sizeof(rfc_key_fmt)); i++) {
if (strcasecmp(name, rfc_tbl[i].fmt) == 0) {
return rfc_tbl[i].rfc;
}
}
return LOG_RFC_UNKNOWN;
}
static int conf_remote_by_config_id(cJSON *json_obj, const char *host, const u16 port, const log_rfc_t rfc, uint64 config_id)
{
log_remote_host_x_t remote = {0};
char *json_output = NULL;
int config_type = CM_CONFIG_SET;
if (LOG_CONFIG_REMOTE_GET_HOST == config_id)
{
if (NULL == host || '\0' == host[0])
{
config_type = CM_CONFIG_GET_ALL;
}
else
{
config_type = CM_CONFIG_GET;
strncpy(remote.host, host, sizeof(remote.host));
}
}
else
{
config_type = CM_CONFIG_SET;
strncpy(remote.host, host, sizeof(remote.host));
remote.port = port;
remote.rfc = rfc;
}
s2j_json_set_basic_element(json_obj, &remote, int, port);
s2j_json_set_basic_element(json_obj, &remote, int, rfc);
s2j_json_set_basic_element(json_obj, &remote, string, host);
s2j_json_set_basic_element(json_obj, &remote, int, level);
int ret = set_log_conf(config_type, json_obj, config_id, &json_output);
if (ret != 0) {
ULOG_ERR(g_log, "Host of log which is configured is failure");
} else {
ULOG_DEBUG(g_log, "Host of log whice is configured is success");
}
if (LOG_CONFIG_REMOTE_GET_HOST == config_id)
{
if (CM_CONFIG_GET_ALL == config_type )
{
log_remote_host_x_t remotes_out[MAX_REMOTE_HOST_CNT] = {0};
int cnt = 0;
if (logging_remote_json_parse_array(json_output, remotes_out, &cnt) == 0)
{
for (int i=0; i<cnt; i++)
{
ULOG_DEBUG(g_log, "Log host get: host[%d]: addr=%s, port=%u, log_fmt=%u, level=%u",
i, remotes_out[i].host, remotes_out[i].port, remotes_out[i].rfc, remotes_out[i].level);
}
}
}
else
{
log_remote_host_x_t remote_out = {0};
if (logging_remote_json_parse(json_output, &remote_out) == 0)
{
ULOG_DEBUG(g_log, "Log host get: addr=%s, port=%u, log_fmt=%u, level=%u",
remote_out.host, remote_out.port, remote_out.rfc, remote_out.level);
}
}
}
return ret;
}
int conf_remote(cJSON *json_obj, int argc, char **argv)
{
int ret = -1;
uint64 config_id;
if (argc < 3) {
ULOG_WARNING(g_log, "Parameter too less");
return ret;
}
if (strcasecmp(argv[2], "add") == 0) {
config_id = LOG_CONFIG_REMOTE_ADD_HOST;
} else if (strcasecmp(argv[2], "del") == 0) {
config_id = LOG_CONFIG_REMOTE_DEL_HOST;
} else if (strcasecmp(argv[2], "get") == 0
|| strcasecmp(argv[2], "getall") == 0) {
config_id = LOG_CONFIG_REMOTE_GET_HOST;
} else {
ULOG_WARNING(g_log, "Unknown operation command:%s", argv[2]);
return ret;
}
char *host = NULL;
if (argc >= 4) {
host = argv[3];
}
u16 port = DEFAULT_REMOTE_HOST_PORT;
if (argc >= 5) {
port= atoi(argv[4]);
}
log_rfc_t rfc = LOG_RFC_3164;
if (argc >= 6) {
rfc = get_rfc_by_name(argv[5]);
if (rfc == LOG_RFC_UNKNOWN) {
ULOG_WARNING(g_log, "Unknown rfc standard:%s of log", argv[5]);
return ret;
}
}
return conf_remote_by_config_id(json_obj, host, port, rfc, config_id);
}
int conf_remote_level(cJSON *json_obj, int argc, char **argv)
{
int level;
log_remote_level_t log_level = {0};
log_remote_level_t log_level_out = {0};
uint config_type = CM_CONFIG_SET;
char*json_output = NULL;
if (argc < 3) {
ULOG_WARNING(g_log, "Not input log level");
return -1;
}
if (argc == 3 && strstr(argv[2], "get") != NULL) {
config_type = CM_CONFIG_GET;
}
else {
config_type = CM_CONFIG_SET;
if ((level = log_str_to_level(argv[2])) < 0) {
ULOG_WARNING(g_log, "Unknown log level:%s", argv[2]);
return -1;
}
log_level.level = (u8)level;
}
s2j_json_set_basic_element(json_obj, &log_level, int, level);
int ret = set_log_conf(config_type, json_obj, LOG_CONFIG_REMOTE_LEVEL, &json_output);
if (ret != 0) {
ULOG_ERR(g_log, "Remote level of log which is configured is failure");
} else {
ULOG_DEBUG(g_log, "Remote level of log whice is configured is success");
}
if (CM_CONFIG_GET == config_type) {
logging_remote_level_json_parse(json_output, &log_level_out);
ULOG_DEBUG(g_log, "Log level get: level=%u", log_level_out.level);
}
return ret;
}
static int logging_remote_json_parse(pointer input, log_remote_host_x_t *config_buff)
{
cJSON *json_obj;
if (NULL == config_buff)
{
return -1;
}
json_obj = cJSON_Parse(input);
if(!json_obj)
{
return -1;
}
ULOG_DEBUG(g_log, "remote json parse, json input:%s \n", cJSON_Print(json_obj));
s2j_create_struct_obj(log_host_x, log_remote_host_x_t);
if(log_host_x == NULL)
{
cJSON_Delete(json_obj);
return -1;
}
s2j_struct_get_basic_element(log_host_x, json_obj, int, port);
s2j_struct_get_basic_element(log_host_x, json_obj, int, rfc);
S2J_STRUCT_GET_STRING_ELEMENT_N(log_host_x, json_obj, host, LOG_HOST_SZ);
s2j_struct_get_basic_element(log_host_x, json_obj, int, level);
memcpy(config_buff, log_host_x, sizeof(log_remote_host_x_t));
s2j_delete_struct_obj(json_obj);
cJSON_Delete(json_obj);
return 0;
}
static int logging_remote_json_parse_array(pointer input,
log_remote_host_x_t *config_buff, int *cnt)
{
cJSON *json_obj;
cJSON* p_array;
int json_cnt = 0, i = 0;
int used_cnt = 0;
json_obj = cJSON_Parse(input);
if(!json_obj)
{
return -1;
}
ULOG_DEBUG(g_log, "remote json parse array, json input:%s \n", cJSON_Print(json_obj));
json_cnt = cJSON_GetArraySize(json_obj);
if (json_cnt > MAX_REMOTE_HOST_CNT)
{
used_cnt = MAX_REMOTE_HOST_CNT;
}
else
{
used_cnt = json_cnt;
}
s2j_create_struct_obj(log_host_x, log_remote_host_x_t);
if(log_host_x == NULL)
{
cJSON_Delete(json_obj);
return -1;
}
*cnt = 0;
for (i = 0; i < used_cnt; i++)
{
p_array = cJSON_GetArrayItem(json_obj, i);
if (p_array)
{
memset(log_host_x, 0, sizeof(log_remote_host_x_t));
s2j_struct_get_basic_element(log_host_x, p_array, int, port);
s2j_struct_get_basic_element(log_host_x, p_array, int, rfc);
s2j_struct_get_basic_element(log_host_x, p_array, string, host);
s2j_struct_get_basic_element(log_host_x, p_array, int, level);
memcpy(config_buff+*cnt, log_host_x, sizeof(log_remote_host_x_t));
(*cnt)++;
}
}
s2j_delete_struct_obj(log_host_x);
cJSON_Delete(json_obj);
return 0;
}
static int logging_remote_level_json_parse(pointer input, log_remote_level_t *config_buff)
{
cJSON *json_obj;
if (NULL == config_buff)
{
return -1;
}
json_obj = cJSON_Parse(input);
if (!json_obj)
{
return -1;
}
s2j_create_struct_obj(log_remote_level, log_remote_level_t);
if (log_remote_level == NULL)
{
cJSON_Delete(json_obj);
return -1;
}
s2j_struct_get_basic_element(log_remote_level, json_obj, int, level);
memcpy(config_buff, log_remote_level, sizeof(log_remote_level_t));
s2j_delete_struct_obj(json_obj);
cJSON_Delete(json_obj);
return 0;
}