2019-08-12 08:56:52 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "cmd_console.h"
|
2019-08-12 10:12:53 +00:00
|
|
|
#include "cmd_monitor.h"
|
2019-08-13 08:25:43 +00:00
|
|
|
#include "cmd_remote.h"
|
2019-08-14 09:17:18 +00:00
|
|
|
#include "cmd_file.h"
|
2019-08-12 08:56:52 +00:00
|
|
|
#include "ulog_api.h"
|
|
|
|
#include "s2j/s2j.h"
|
|
|
|
|
|
|
|
#define LOGGING_MODULE_NAME "logging"
|
|
|
|
|
|
|
|
typedef int (*cmd_cb)(cJSON *, int, char **);
|
|
|
|
|
|
|
|
typedef struct _log_cmd {
|
|
|
|
char cmd[20];
|
|
|
|
cmd_cb cb;
|
|
|
|
} log_cmd_t;
|
|
|
|
|
|
|
|
ulog_t *g_log;
|
2019-08-13 08:25:43 +00:00
|
|
|
log_cmd_t g_log_cmd[] = {
|
2019-08-12 10:12:53 +00:00
|
|
|
{"console", conf_console},
|
2019-08-13 08:25:43 +00:00
|
|
|
{"monitor", conf_monitor},
|
2019-08-14 09:17:18 +00:00
|
|
|
{"host", conf_remote},
|
|
|
|
{"trap", conf_remote_level},
|
|
|
|
{"file", conf_file}
|
2019-08-12 08:56:52 +00:00
|
|
|
};
|
|
|
|
|
2019-08-13 08:25:43 +00:00
|
|
|
log_cmd_t *get_cb_by_cmd(const char *cmd)
|
2019-08-12 08:56:52 +00:00
|
|
|
{
|
2019-08-13 08:25:43 +00:00
|
|
|
for (int i = 0; i < (sizeof(g_log_cmd) / sizeof(log_cmd_t)); i++) {
|
|
|
|
if (strcasecmp(cmd, g_log_cmd[i].cmd) == 0) {
|
|
|
|
return &g_log_cmd[i];
|
2019-08-12 08:56:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-29 07:12:30 +00:00
|
|
|
static void logging_usage()
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
char tmp_str[1024] = "";
|
|
|
|
fprintf(stdout, "-----------------------------------------------------------------------------------------\n");
|
|
|
|
fprintf(stdout, "usage of %s:", LOGGING_MODULE_NAME);
|
|
|
|
for (int i = 0; i < (sizeof(g_log_cmd) / sizeof(log_cmd_t)); i++) {
|
|
|
|
strcat(tmp_str, g_log_cmd[i].cmd);
|
|
|
|
if (i < (sizeof(g_log_cmd) / sizeof(log_cmd_t))-1) {
|
|
|
|
strcat(tmp_str, "|");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stdout, " %s %s\n", LOGGING_MODULE_NAME, tmp_str);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
fprintf(stdout, "1. usage of %s %s:\n", LOGGING_MODULE_NAME, g_log_cmd[0].cmd);
|
|
|
|
fprintf(stdout, " %s %s <level> [<module_name>]\n", LOGGING_MODULE_NAME, g_log_cmd[0].cmd);
|
|
|
|
fprintf(stdout, " %s %s off\n", LOGGING_MODULE_NAME, g_log_cmd[0].cmd);
|
|
|
|
fprintf(stdout, " %s %s get [<module_name>]\n", LOGGING_MODULE_NAME, g_log_cmd[0].cmd);
|
|
|
|
fprintf(stdout, " <level> = emerg | alert | crit | err | warn | notice | info | debug\n");
|
|
|
|
fprintf(stdout, " <module_name> the filtering module_name, only recode this module's log while set\n");
|
|
|
|
fprintf(stdout, " eg: %s %s info moudle1\n", LOGGING_MODULE_NAME, g_log_cmd[0].cmd);
|
|
|
|
fprintf(stdout, " %s %s off\n", LOGGING_MODULE_NAME, g_log_cmd[0].cmd);
|
|
|
|
fprintf(stdout, " %s %s get\n", LOGGING_MODULE_NAME, g_log_cmd[0].cmd);
|
|
|
|
fprintf(stdout, " %s %s get module1\n", LOGGING_MODULE_NAME, g_log_cmd[0].cmd);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
fprintf(stdout, "2. usage of %s %s is simple as %s\n", LOGGING_MODULE_NAME, g_log_cmd[1].cmd, g_log_cmd[0].cmd);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
fprintf(stdout, "3. usage of %s %s:\n", LOGGING_MODULE_NAME, g_log_cmd[2].cmd);
|
|
|
|
fprintf(stdout, " %s %s add <ip_addr> [<port>] [<RFCfmt>]\n", LOGGING_MODULE_NAME, g_log_cmd[2].cmd);
|
|
|
|
fprintf(stdout, " %s %s del <ip_addr> [<port>] [<RFCfmt>]\n", LOGGING_MODULE_NAME, g_log_cmd[2].cmd);
|
|
|
|
fprintf(stdout, " %s %s get <ip_addr>\n", LOGGING_MODULE_NAME, g_log_cmd[2].cmd);
|
|
|
|
fprintf(stdout, " %s %s getall\n", LOGGING_MODULE_NAME, g_log_cmd[2].cmd);
|
|
|
|
fprintf(stdout, " <ip_addr> is the ip address of remote syslog server\n");
|
|
|
|
fprintf(stdout, " <port> is the port of remote syslog server\n");
|
|
|
|
fprintf(stdout, " <RFCfmt> = RFC3164fmt|RFC5424fmt, is the version of syslog format\n");
|
|
|
|
fprintf(stdout, " eg: %s %s add 192.168.100.1 514 RFC3164fmt\n", LOGGING_MODULE_NAME, g_log_cmd[2].cmd);
|
|
|
|
fprintf(stdout, " %s %s del 192.168.100.1 514 RFC3164fmt\n", LOGGING_MODULE_NAME, g_log_cmd[2].cmd);
|
|
|
|
fprintf(stdout, " %s %s get 192.168.100.1\n", LOGGING_MODULE_NAME, g_log_cmd[2].cmd);
|
|
|
|
fprintf(stdout, " %s %s getall\n", LOGGING_MODULE_NAME, g_log_cmd[2].cmd);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
fprintf(stdout, "4. usage of %s %s:\n", LOGGING_MODULE_NAME, g_log_cmd[3].cmd);
|
|
|
|
fprintf(stdout, " %s %s <level>\n", LOGGING_MODULE_NAME, g_log_cmd[3].cmd);
|
|
|
|
fprintf(stdout, " %s %s get\n", LOGGING_MODULE_NAME, g_log_cmd[3].cmd);
|
|
|
|
fprintf(stdout, " <level> = emerg | alert | crit | err | warn | notice | info | debug \n");
|
2019-08-29 07:41:04 +00:00
|
|
|
fprintf(stdout, " eg: %s %s info\n", LOGGING_MODULE_NAME, g_log_cmd[3].cmd);
|
|
|
|
fprintf(stdout, " %s %s get\n", LOGGING_MODULE_NAME, g_log_cmd[3].cmd);
|
2019-08-29 07:12:30 +00:00
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
fprintf(stdout, "5. usage of %s %s:\n", LOGGING_MODULE_NAME, g_log_cmd[4].cmd);
|
|
|
|
fprintf(stdout, " %s %s <level> [<file_path>] [<is_compress>] [<del_over_days>] [<del_over_size>]\n", LOGGING_MODULE_NAME, g_log_cmd[4].cmd);
|
|
|
|
fprintf(stdout, " %s %s get\n", LOGGING_MODULE_NAME, g_log_cmd[4].cmd);
|
|
|
|
fprintf(stdout, " <level> = emerg | alert | crit | err | warn | notice | info | debug\n");
|
|
|
|
fprintf(stdout, " <file_path> is the path of syslog files saved\n");
|
|
|
|
fprintf(stdout, " <is_compress> = 0 | 1 , which 0 is syslog files is not compress while 1 is compress\n");
|
|
|
|
fprintf(stdout, " <del_over_days> is the syslog files life-time (days)\n");
|
|
|
|
fprintf(stdout, " <del_over_size> is the syslog files life-size (bytes)\n");
|
2019-08-29 07:41:04 +00:00
|
|
|
fprintf(stdout, " eg: %s %s info /var/log/syslog-files 0 100 1024000\n", LOGGING_MODULE_NAME, g_log_cmd[4].cmd);
|
|
|
|
fprintf(stdout, " %s %s get\n", LOGGING_MODULE_NAME, g_log_cmd[4].cmd);
|
2019-08-29 07:12:30 +00:00
|
|
|
|
|
|
|
fprintf(stdout, "-----------------------------------------------------------------------------------------\n");
|
|
|
|
}
|
|
|
|
|
2019-08-12 08:56:52 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2019-08-13 08:25:43 +00:00
|
|
|
int ret = -1;
|
2019-08-12 08:56:52 +00:00
|
|
|
if (argc < 2) {
|
2019-08-27 00:49:47 +00:00
|
|
|
fprintf(stdout, "Parameter too few\n");
|
2019-08-29 07:12:30 +00:00
|
|
|
logging_usage();
|
2019-08-13 08:25:43 +00:00
|
|
|
return ret;
|
2019-08-29 07:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[1], "-h")==0 || strcmp(argv[1], "--help")==0) {
|
|
|
|
logging_usage();
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-12 08:56:52 +00:00
|
|
|
|
|
|
|
g_log = ulog_init(LOGGING_MODULE_NAME, 1);
|
|
|
|
if (g_log == NULL) {
|
2019-08-27 00:49:47 +00:00
|
|
|
fprintf(stderr, "Initiating ulog is failure\n");
|
2019-08-13 08:25:43 +00:00
|
|
|
return ret;
|
2019-08-12 08:56:52 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 08:25:43 +00:00
|
|
|
log_cmd_t *log_cmd = get_cb_by_cmd(argv[1]);
|
|
|
|
if (log_cmd == NULL) {
|
2019-08-14 09:23:08 +00:00
|
|
|
ulog_close(g_log);
|
2019-08-12 08:56:52 +00:00
|
|
|
ULOG_WARNING(g_log, "Not find logging cmd:%s", argv[1]);
|
2019-08-14 09:23:08 +00:00
|
|
|
return -1;
|
2019-08-12 08:56:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s2j_create_json_obj(json_obj);
|
|
|
|
if(json_obj == NULL)
|
|
|
|
{
|
|
|
|
ULOG_ERR(g_log, "Creating json object is failure");
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
2019-08-13 08:25:43 +00:00
|
|
|
if (log_cmd->cb(json_obj, argc, argv) != 0) {
|
|
|
|
ULOG_ERR(g_log, "Configuring %s is failure", log_cmd->cmd);
|
|
|
|
goto END;
|
|
|
|
}
|
2019-08-12 08:56:52 +00:00
|
|
|
|
2019-08-13 08:25:43 +00:00
|
|
|
ret = 0;
|
2019-08-12 08:56:52 +00:00
|
|
|
END:
|
|
|
|
if(json_obj != NULL) {
|
|
|
|
cJSON_Delete(json_obj);
|
|
|
|
}
|
|
|
|
ulog_close(g_log);
|
2019-08-13 08:25:43 +00:00
|
|
|
return ret;
|
2019-08-12 08:56:52 +00:00
|
|
|
}
|