Add human-readable data size formatting for status messages

- Added format_bytes() function to display data sizes in B, kB, MB, or GB with 2 decimal places
- Updated status logging in both wssshc.c and wssshd2/websocket.c to use formatted data sizes for total data, sent/received bytes, and transfer rates
parent b114d0b3
...@@ -46,6 +46,18 @@ static const char *TUNNEL_REQUEST_MSG = "{\"type\":\"tunnel_request\",\"request_ ...@@ -46,6 +46,18 @@ static const char *TUNNEL_REQUEST_MSG = "{\"type\":\"tunnel_request\",\"request_
static const char *TUNNEL_ACK_MSG = "{\"type\":\"tunnel_ack\",\"request_id\":\"%s\"}"; static const char *TUNNEL_ACK_MSG = "{\"type\":\"tunnel_ack\",\"request_id\":\"%s\"}";
static const char *TUNNEL_ERROR_MSG = "{\"type\":\"tunnel_error\",\"request_id\":\"%s\",\"error\":\"%s\"}"; static const char *TUNNEL_ERROR_MSG = "{\"type\":\"tunnel_error\",\"request_id\":\"%s\",\"error\":\"%s\"}";
void format_bytes(unsigned long long bytes, char *buffer, size_t buffer_size) {
if (bytes < 1024) {
snprintf(buffer, buffer_size, "%llu B", bytes);
} else if (bytes < 1024 * 1024) {
snprintf(buffer, buffer_size, "%.2f kB", (double)bytes / 1024.0);
} else if (bytes < 1024 * 1024 * 1024) {
snprintf(buffer, buffer_size, "%.2f MB", (double)bytes / (1024.0 * 1024.0));
} else {
snprintf(buffer, buffer_size, "%.2f GB", (double)bytes / (1024.0 * 1024.0 * 1024.0));
}
}
void log_message(const char *level, const char *format, ...) { void log_message(const char *level, const char *format, ...) {
time_t now = time(NULL); time_t now = time(NULL);
char time_str[20]; char time_str[20];
...@@ -1503,16 +1515,23 @@ void *status_thread(void *arg) { ...@@ -1503,16 +1515,23 @@ void *status_thread(void *arg) {
double avg_rate = tunnel_uptime > 0 ? (double)total_data / tunnel_uptime : 0.0; double avg_rate = tunnel_uptime > 0 ? (double)total_data / tunnel_uptime : 0.0;
double last_rate = tunnel->bytes_last_period / 30.0; double last_rate = tunnel->bytes_last_period / 30.0;
log_message("STATUS", " Tunnel ID: %s | Client: %s | Service: %s | Uptime: %ld s | Data: %llu B (%llu sent, %llu recv) | Rate: %.2f B/s avg, %.2f B/s last\n", char total_data_str[32], sent_data_str[32], recv_data_str[32], avg_rate_str[32], last_rate_str[32];
format_bytes(total_data, total_data_str, sizeof(total_data_str));
format_bytes(tunnel->total_bytes_sent, sent_data_str, sizeof(sent_data_str));
format_bytes(tunnel->total_bytes_received, recv_data_str, sizeof(recv_data_str));
format_bytes((unsigned long long)avg_rate, avg_rate_str, sizeof(avg_rate_str));
format_bytes((unsigned long long)last_rate, last_rate_str, sizeof(last_rate_str));
log_message("STATUS", " Tunnel ID: %s | Client: %s | Service: %s | Uptime: %ld s | Data: %s (%s sent, %s recv) | Rate: %s/s avg, %s/s last\n",
tunnel->request_id, tunnel->request_id,
strlen(tunnel->client_id) > 0 ? tunnel->client_id : "unknown", strlen(tunnel->client_id) > 0 ? tunnel->client_id : "unknown",
strlen(tunnel->service) > 0 ? tunnel->service : "ssh", strlen(tunnel->service) > 0 ? tunnel->service : "ssh",
tunnel_uptime, tunnel_uptime,
total_data, total_data_str,
tunnel->total_bytes_sent, sent_data_str,
tunnel->total_bytes_received, recv_data_str,
avg_rate, avg_rate_str,
last_rate); last_rate_str);
} }
} }
......
...@@ -50,6 +50,18 @@ volatile sig_atomic_t sigint_count = 0; ...@@ -50,6 +50,18 @@ volatile sig_atomic_t sigint_count = 0;
volatile sig_atomic_t graceful_shutdown = 0; volatile sig_atomic_t graceful_shutdown = 0;
volatile sig_atomic_t reload_config = 0; volatile sig_atomic_t reload_config = 0;
void format_bytes(unsigned long long bytes, char *buffer, size_t buffer_size) {
if (bytes < 1024) {
snprintf(buffer, buffer_size, "%llu B", bytes);
} else if (bytes < 1024 * 1024) {
snprintf(buffer, buffer_size, "%.2f kB", (double)bytes / 1024.0);
} else if (bytes < 1024 * 1024 * 1024) {
snprintf(buffer, buffer_size, "%.2f MB", (double)bytes / (1024.0 * 1024.0));
} else {
snprintf(buffer, buffer_size, "%.2f GB", (double)bytes / (1024.0 * 1024.0 * 1024.0));
}
}
void log_message(const char *level, const char *format, ...) { void log_message(const char *level, const char *format, ...) {
time_t now = time(NULL); time_t now = time(NULL);
char time_str[20]; char time_str[20];
...@@ -103,16 +115,23 @@ void print_status() { ...@@ -103,16 +115,23 @@ void print_status() {
double avg_rate = tunnel_uptime > 0 ? (double)total_data / tunnel_uptime : 0.0; double avg_rate = tunnel_uptime > 0 ? (double)total_data / tunnel_uptime : 0.0;
double last_rate = tunnel->bytes_last_period / 30.0; double last_rate = tunnel->bytes_last_period / 30.0;
log_message("STATUS", " Tunnel ID: %s | Client: %s | Service: %s | Uptime: %ld s | Data: %llu B (%llu sent, %llu recv) | Rate: %.2f B/s avg, %.2f B/s last\n", char total_data_str[32], sent_data_str[32], recv_data_str[32], avg_rate_str[32], last_rate_str[32];
format_bytes(total_data, total_data_str, sizeof(total_data_str));
format_bytes(tunnel->total_bytes_sent, sent_data_str, sizeof(sent_data_str));
format_bytes(tunnel->total_bytes_received, recv_data_str, sizeof(recv_data_str));
format_bytes((unsigned long long)avg_rate, avg_rate_str, sizeof(avg_rate_str));
format_bytes((unsigned long long)last_rate, last_rate_str, sizeof(last_rate_str));
log_message("STATUS", " Tunnel ID: %s | Client: %s | Service: %s | Uptime: %ld s | Data: %s (%s sent, %s recv) | Rate: %s/s avg, %s/s last\n",
tunnel->request_id, tunnel->request_id,
strlen(tunnel->client_id) > 0 ? tunnel->client_id : "local", strlen(tunnel->client_id) > 0 ? tunnel->client_id : "local",
strlen(tunnel->service) > 0 ? tunnel->service : "ssh", strlen(tunnel->service) > 0 ? tunnel->service : "ssh",
tunnel_uptime, tunnel_uptime,
total_data, total_data_str,
tunnel->total_bytes_sent, sent_data_str,
tunnel->total_bytes_received, recv_data_str,
avg_rate, avg_rate_str,
last_rate); last_rate_str);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment