From 56e4a01af0b22d7e629fd4db419ae5d6a3a5be50 Mon Sep 17 00:00:00 2001 From: OpenGnSys Support Team Date: Thu, 23 Nov 2023 16:51:51 +0100 Subject: rest: revisit logging Skip logging for the following REST API invocations: - GET /scopes - GET /clients - POST /clients (for legacy web console only) The web front-end generated very frequent requests for this. Update logging format to: 127.0.0.1:54637 POST /wol clients=192.168.2.130 to include client IP address. --- src/rest.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/rest.h | 53 +++++++++++++++++++++++ 2 files changed, 190 insertions(+), 4 deletions(-) diff --git a/src/rest.c b/src/rest.c index b58b468..5cd6791 100644 --- a/src/rest.c +++ b/src/rest.c @@ -6388,6 +6388,135 @@ static int og_client_ok(struct og_client *cli, char *buf_reply) return 0; } +static const char *og_method_str[] = { + [OG_METHOD_GET] = "GET", + [OG_METHOD_POST] = "POST", + [OG_METHOD_DELETE] = "DELETE", + [OG_METHOD_NO_HTTP] = "NOHTTP", +}; + +static const char *og_method(enum og_rest_method method) +{ + if (method > OG_METHOD_NO_HTTP) + return "UNKNOWN"; + + return og_method_str[method]; +} + +struct { + const char *uri; +} og_uri_handler[] = { + [OG_URI_UNKNOWN] = { "unknown", }, + [OG_URI_CLIENTS] = { "clients", }, + [OG_URI_CLIENT_REPO] = { "client/repo", }, + [OG_URI_CLIENT_SETUP] = { "client/setup", }, + [OG_URI_CLIENT_SERVER] = { "client/server", }, + [OG_URI_CLIENT_INFO] = { "client/info", }, + [OG_URI_CLIENT_ADD] = { "client/add", }, + [OG_URI_CLIENT_UPDATE] = { "client/update", }, + [OG_URI_CLIENT_DELETE] = { "client/delete", }, + [OG_URI_WOL] = { "wol", }, + [OG_URI_SHELL_RUN] = { "shell/run", }, + [OG_URI_SHELL_OUTPUT] = { "shell/output", }, + [OG_URI_SESSION] = { "session", }, + [OG_URI_SCOPES] = { "scopes", }, + [OG_URI_POWEROFF] = { "poweroff", }, + [OG_URI_REBOOT] = { "reboot", }, + [OG_URI_BOOT_MODE] = { "mode", }, + [OG_URI_STOP] = { "stop", }, + [OG_URI_REFRESH] = { "refresh", }, + [OG_URI_HARDWARE] = { "hardware", }, + [OG_URI_SOFTWARE] = { "software", }, + [OG_URI_REPO] = { "repositories", }, + [OG_URI_REPO_ADD] = { "repository/add", }, + [OG_URI_REPO_DELETE] = { "repository/delete", }, + [OG_URI_IMAGES] = { "images", }, + [OG_URI_IMAGE_CREATE] = { "image/create" }, + [OG_URI_IMAGE_RESTORE] = { "image/restore", }, + [OG_URI_IMAGE_DELETE] = { "image/delete", }, + [OG_URI_PART_SETUP] = { "setup", }, + [OG_URI_RUN_SCHEDULE] = { "run/schedule", }, + [OG_URI_TASK_RUN] = { "task/run", }, + [OG_URI_SCHEDULE_CREATE] = { "schedule/create", }, + [OG_URI_SCHEDULE_DELETE] = { "schedule/delete", }, + [OG_URI_SCHEDULE_UPDATE] = { "schedule/update", }, + [OG_URI_SCHEDULE_GET] = { "schedule/get", }, + [OG_URI_OGLIVE_LIST] = { "oglive/list", }, + [OG_URI_OGLIVE_SET] = { "oglive/set", }, + [OG_URI_CENTER_ADD] = { "center/add", }, + [OG_URI_CENTER_DELETE] = { "center/delete", }, + [OG_URI_ROOM_ADD] = { "room/add", }, + [OG_URI_ROOM_DELETE] = { "room/delete", }, + [OG_URI_PROC_ADD] = { "procedure/add", }, + [OG_URI_PROC_UPDATE] = { "procedure/update", }, + [OG_URI_PROC_RUN] = { "procedure/run", }, + [OG_URI_SCHEDULE_RUN] = { "schedule/command", }, + [OG_URI_PROC_DEL] = { "procedure/delete", }, + [OG_URI_TASK_ADD] = { "task/add", }, + [OG_URI_SERVER] = { "server", }, + [OG_URI_STATS] = { "stats", }, +}; + +static const char *og_uri(enum og_rest_uri uri) +{ + if (uri >= OG_URI_MAX) + return "unknown"; + + return og_uri_handler[uri].uri; +} + +static enum og_rest_uri og_str_to_uri(const char *uri) +{ + int i; + + for (i = 0; i < OG_URI_MAX; i++) { + if (!strncmp(og_uri_handler[i].uri, uri, + strlen(og_uri_handler[i].uri))) + return i; + } + + return OG_URI_UNKNOWN; +} + +static void og_rest_log(const struct og_client *cli, enum og_rest_method method, + enum og_rest_uri uri, const struct og_msg_params *params) +{ + char log_buf[(16 * OG_CLIENTS_MAX) + 4096] = {}; + int i, ret; + + switch (uri) { + case OG_URI_SCOPES: + case OG_URI_CLIENTS: + /* very spammy, do not log these. */ + return; + default: + break; + } + + ret = snprintf(log_buf, sizeof(log_buf), "%s:%hu %s /%s ", + inet_ntoa(cli->addr.sin_addr), ntohs(cli->addr.sin_port), + og_method(method), og_uri(uri)); + + if (params->ips_array_len > 0) { + ret += snprintf(&log_buf[ret], sizeof(log_buf) - ret, "clients="); + if (ret > sizeof(log_buf)) + return; + + for (i = 0; i < params->ips_array_len - 1; i++) { + ret += snprintf(&log_buf[ret], sizeof(log_buf) - ret, + "%s,", params->ips_array[i]); + if (ret > sizeof(log_buf)) + return; + } + + ret += snprintf(&log_buf[ret], sizeof(log_buf) - ret, "%s", params->ips_array[i]); + if (ret > sizeof(log_buf)) + return; + } + + syslog(LOG_INFO, "%s", log_buf); +} + int og_client_state_process_payload_rest(struct og_client *cli) { char buf_reply[OG_MSG_RESPONSE_MAXLEN] = {}; @@ -6395,13 +6524,10 @@ int og_client_state_process_payload_rest(struct og_client *cli) enum og_rest_method method; const char *cmd, *body; json_error_t json_err; + enum og_rest_uri uri; json_t *root = NULL; int err = 0; - syslog(LOG_DEBUG, "%s:%hu %.32s ...\n", - inet_ntoa(cli->addr.sin_addr), - ntohs(cli->addr.sin_port), cli->buf); - if (!strncmp(cli->buf, "GET", strlen("GET"))) { method = OG_METHOD_GET; cmd = cli->buf + strlen("GET") + 2; @@ -6430,6 +6556,8 @@ int og_client_state_process_payload_rest(struct og_client *cli) } } + uri = og_str_to_uri(cmd); + if (!strncmp(cmd, "clients", strlen("clients"))) { if (method != OG_METHOD_POST && method != OG_METHOD_GET) { @@ -7065,6 +7193,8 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_client_not_found(cli); } + og_rest_log(cli, method, uri, ¶ms); + json_decref(root); if (err < 0) @@ -7073,6 +7203,9 @@ int og_client_state_process_payload_rest(struct og_client *cli) return og_client_ok(cli, buf_reply); err_process_rest_payload: + syslog(LOG_ERR, "%s:%hu %.32s ...\n", + inet_ntoa(cli->addr.sin_addr), + ntohs(cli->addr.sin_port), cli->buf); json_decref(root); return err; diff --git a/src/rest.h b/src/rest.h index 6120a4e..5e21b39 100644 --- a/src/rest.h +++ b/src/rest.h @@ -84,6 +84,59 @@ static inline int og_client_socket(const struct og_client *cli) int og_client_state_process_payload_rest(struct og_client *cli); +enum og_rest_uri { + OG_URI_UNKNOWN = 0, + OG_URI_CLIENTS, + OG_URI_CLIENT_REPO, + OG_URI_CLIENT_SETUP, + OG_URI_CLIENT_SERVER, + OG_URI_CLIENT_INFO, + OG_URI_CLIENT_ADD, + OG_URI_CLIENT_UPDATE, + OG_URI_CLIENT_DELETE, + OG_URI_WOL, + OG_URI_SHELL_RUN, + OG_URI_SHELL_OUTPUT, + OG_URI_SESSION, + OG_URI_SCOPES, + OG_URI_POWEROFF, + OG_URI_REBOOT, + OG_URI_BOOT_MODE, + OG_URI_STOP, + OG_URI_REFRESH, + OG_URI_HARDWARE, + OG_URI_SOFTWARE, + OG_URI_REPO, + OG_URI_REPO_ADD, + OG_URI_REPO_DELETE, + OG_URI_IMAGES, + OG_URI_IMAGE_CREATE, + OG_URI_IMAGE_RESTORE, + OG_URI_IMAGE_DELETE, + OG_URI_PART_SETUP, + OG_URI_RUN_SCHEDULE, + OG_URI_TASK_RUN, + OG_URI_SCHEDULE_CREATE, + OG_URI_SCHEDULE_DELETE, + OG_URI_SCHEDULE_UPDATE, + OG_URI_SCHEDULE_GET, + OG_URI_OGLIVE_LIST, + OG_URI_OGLIVE_SET, + OG_URI_CENTER_ADD, + OG_URI_CENTER_DELETE, + OG_URI_ROOM_ADD, + OG_URI_ROOM_DELETE, + OG_URI_PROC_ADD, + OG_URI_PROC_UPDATE, + OG_URI_PROC_RUN, + OG_URI_SCHEDULE_RUN, + OG_URI_PROC_DEL, + OG_URI_TASK_ADD, + OG_URI_SERVER, + OG_URI_STATS, + OG_URI_MAX +}; + enum og_rest_method { OG_METHOD_GET = 0, OG_METHOD_POST, -- cgit v1.2.3-18-g5258