This commit is contained in:
Serafim 2024-12-12 23:29:58 +03:00
parent 3cd23b0956
commit dca2789086
6 changed files with 262 additions and 80 deletions

BIN
log/log

Binary file not shown.

View File

@ -1,16 +1,48 @@
#include <iostream>
#include <string>
#include <sys/stat.h>
#include <cstring>
#include <time.h> #include <time.h>
#include <fstream>
#include <ctime> #include <ctime>
#include <pthread.h> #include <iostream>
#include <fstream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cstring>
#include <csignal>
const char* LOG_FIFO_NAME = "/pipe/log";
std::string LOG_DIR = "/log_data"; std::string LOG_DIR = "/log_data";
void log(std::string msg) { int pipeFd;
void handle_signal(int sig) {
if (sig == SIGTERM || sig == SIGINT) {
close(pipeFd);
unlink(LOG_FIFO_NAME);
}
}
int main() {
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
mkdir(LOG_DIR.c_str(), 0755); mkdir(LOG_DIR.c_str(), 0755);
// Create or open the named pipe
mkfifo(LOG_FIFO_NAME, 0666);
// Open the pipe for reading
pipeFd = open(LOG_FIFO_NAME, O_RDONLY);
if (pipeFd == -1) {
std::cout << "Error opening pipe!" << std::endl;
return 1;
}
// Read log messages from the pipe and write them to the log file
char buffer[256];
while (true) {
ssize_t bytesRead = read(pipeFd, buffer, sizeof(buffer) - 1);
if (bytesRead > 0) {
buffer[bytesRead] = '\0';
time_t now = time(0); time_t now = time(0);
struct tm tstruct; struct tm tstruct;
tstruct = *localtime(&now); tstruct = *localtime(&now);
@ -20,14 +52,13 @@ void log(std::string msg) {
log_file.open(LOG_DIR + "/" + filename + ".log", std::ios_base::app); log_file.open(LOG_DIR + "/" + filename + ".log", std::ios_base::app);
char timestamp[32]; char timestamp[32];
std::strftime(timestamp, 32, "%Y/%m/%d %H:%M:%S ", &tstruct); std::strftime(timestamp, 32, "%Y/%m/%d %H:%M:%S ", &tstruct);
log_file << timestamp << msg << std::endl; log_file << timestamp << buffer << std::endl;
log_file.close(); log_file.close();
} }
}
int main() { // Clean up
log("uwu"); close(pipeFd);
log("owo");
log("yay");
return 0; return 0;
} }

View File

@ -1,6 +1,151 @@
2024/12/12 00:24:34 uwu 2024/12/12 19:56:03 Creating socket...
2024/12/12 00:24:34 owo 2024/12/12 19:56:03 Socket created
2024/12/12 00:24:34 yay 2024/12/12 19:56:03 Listening for connections...
2024/12/12 00:24:50 uwu 2024/12/12 19:56:10 New connection accepted
2024/12/12 00:24:50 owo 2024/12/12 19:56:10 New thread created for socket 5
2024/12/12 00:24:50 yay 2024/12/12 19:56:12 Got new message from client 5:
2024/12/12 19:56:12 Sent new info to client 5
2024/12/12 19:56:14 Got new message from client 5:
2024/12/12 19:56:15 Got new message from client 5:
2024/12/12 19:56:16 Got new message from client 5:
2024/12/12 19:56:16 Sent new info to client 5
2024/12/12 19:56:18 Got new message from client 5:
2024/12/12 19:56:18 Client 5 info is not up to dateSent new info to client 5
2024/12/12 19:56:19 Got new message from client 5:
2024/12/12 19:56:19 Actual info already on client 5
2024/12/12 19:56:20 Got new message from client 5:
2024/12/12 19:56:20 Sent new info to client 5
2024/12/12 19:56:21 Got new message from client 5:
2024/12/12 19:56:21 Sent new info to client 5
2024/12/12 19:56:22 Got new message from client 5:
2024/12/12 19:56:23 Got new message from client 5:
2024/12/12 19:56:23 Client 5 info is not up to dateSent new info to client 5
2024/12/12 19:56:24 Got new message from client 5:
2024/12/12 19:56:24 Sent new info to client 5
2024/12/12 19:56:25 Got new message from client 5:
2024/12/12 19:56:26 Got new message from client 5:
2024/12/12 19:56:26 Client 5 info is not up to date
2024/12/12 19:56:26 Sent new info to client 5
2024/12/12 19:56:27 Got new message from client 5:
2024/12/12 19:56:27 Sent new info to client 5
2024/12/12 19:56:28 Got new message from client 5:
2024/12/12 19:56:29 Got new message from client 5:
2024/12/12 19:56:29 Sent new info to client 5
2024/12/12 19:56:30 Got new message from client 5:
2024/12/12 19:56:31 Got new message from client 5:
2024/12/12 19:56:32 Got new message from client 5:
2024/12/12 19:56:32 Client 5 info is not up to dateSent new info to client 5
2024/12/12 19:56:33 Got new message from client 5:
2024/12/12 19:56:33 Sent new info to client 5
2024/12/12 19:56:34 Got new message from client 5:
2024/12/12 19:56:35 Got new message from client 5:
2024/12/12 19:56:35 Sent new info to client 5
2024/12/12 19:56:36 Got new message from client 5:
2024/12/12 19:56:36 Client 5 info is not up to date
2024/12/12 19:56:36 Sent new info to client 5
2024/12/12 19:56:37 Got new message from client 5:
2024/12/12 19:56:38 Got new message from client 5:
2024/12/12 19:56:38 Sent new info to client 5
2024/12/12 19:56:39 Got new message from client 5:
2024/12/12 19:56:39 Client 5 info is not up to date
2024/12/12 19:56:39 Sent new info to client 5
2024/12/12 19:56:40 Got new message from client 5:
2024/12/12 19:56:40 Actual info already on client 5
2024/12/12 19:56:40 Connection with client 5 closed
2024/12/12 19:57:04 Creating socket...
2024/12/12 19:57:04 Socket created
2024/12/12 19:57:04 Listening for connections...
2024/12/12 19:57:10 New connection accepted
2024/12/12 19:57:10 New thread created for socket 5
2024/12/12 19:57:12 Got new message from client 5:
2024/12/12 19:57:12 Client 5 info is not up to date
2024/12/12 19:57:12 Sent new info to client 5
2024/12/12 19:57:13 Got new message from client 5:
2024/12/12 19:57:13 Sent new info to client 5
2024/12/12 19:57:14 Got new message from client 5:
2024/12/12 19:57:15 Got new message from client 5:
2024/12/12 19:57:15 Client 5 info is not up to date
2024/12/12 19:57:15 Sent new info to client 5
2024/12/12 19:57:16 Got new message from client 5:
2024/12/12 19:57:16 Sent new info to client 5
2024/12/12 19:57:17 Got new message from client 5:
2024/12/12 19:57:18 Got new message from client 5:
2024/12/12 19:57:18 Sent new info to client 5
2024/12/12 19:57:19 Got new message from client 5:
2024/12/12 19:57:19 Client 5 info is not up to date
2024/12/12 19:57:19 Sent new info to client 5
2024/12/12 19:57:20 Got new message from client 5:
2024/12/12 19:57:20 Actual info already on client 5
2024/12/12 19:57:21 Got new message from client 5:
2024/12/12 19:57:21 Sent new info to client 5
2024/12/12 19:57:22 Got new message from client 5:
2024/12/12 19:57:23 Got new message from client 5:
2024/12/12 19:57:24 Got new message from client 5:
2024/12/12 19:57:25 Got new message from client 5:
2024/12/12 19:57:26 Got new message from client 5:
2024/12/12 19:57:27 Got new message from client 5:
2024/12/12 19:57:27 Client 5 info is not up to dateSent new info to client 5
2024/12/12 19:57:28 Got new message from client 5:
2024/12/12 19:57:28 Sent new info to client 5
2024/12/12 19:57:29 Got new message from client 5:
2024/12/12 19:57:29 Actual info already on client 5
2024/12/12 19:57:30 Got new message from client 5:
2024/12/12 19:57:30 Client 5 info is not up to date
2024/12/12 19:57:30 Sent new info to client 5
2024/12/12 19:57:31 Got new message from client 5:
2024/12/12 19:57:31 Client 5 info is not up to dateSent new info to client 5
2024/12/12 19:57:32 Got new message from client 5:
2024/12/12 19:57:33 Got new message from client 5:
2024/12/12 19:57:33 Client 5 info is not up to date
2024/12/12 19:57:33 Sent new info to client 5
2024/12/12 19:57:34 Got new message from client 5:
2024/12/12 19:57:34 Client 5 info is not up to dateSent new info to client 5
2024/12/12 19:57:35 Got new message from client 5:
2024/12/12 19:57:36 Got new message from client 5:
2024/12/12 19:57:36 Sent new info to client 5
2024/12/12 19:57:37 Got new message from client 5:
2024/12/12 19:57:37 Sent new info to client 5
2024/12/12 19:57:38 Got new message from client 5:
2024/12/12 19:57:38 Actual info already on client 5
2024/12/12 19:57:39 Got new message from client 5:
2024/12/12 19:57:39 Sent new info to client 5
2024/12/12 19:57:40 Got new message from client 5:
2024/12/12 19:57:40 Sent new info to client 5
2024/12/12 19:57:41 Got new message from client 5:
2024/12/12 19:57:41 Actual info already on client 5
2024/12/12 19:57:42 Got new message from client 5:
2024/12/12 19:57:42 Sent new info to client 5
2024/12/12 19:57:42 Connection with client 5 closed
2024/12/12 20:00:42 Socket created
2024/12/12 20:00:49 New thread created for socket 5
2024/12/12 20:00:55 Got new message from client 5
2024/12/12 20:00:55 Sent new info to client
2024/12/12 20:00:58 Got new message from client 5
2024/12/12 20:00:58 Sent new info to client
2024/12/12 20:01:00 Got new message from client 5
2024/12/12 20:01:03 Got new message from client 5
2024/12/12 20:01:03 Sent new info to client
2024/12/12 20:01:04 Got new message from client 5
2024/12/12 20:01:04 Sent new info to client
2024/12/12 20:01:05 Got new message from client 5
2024/12/12 20:01:06 Got new message from client 5
2024/12/12 20:01:07 Got new message from client 5
2024/12/12 20:01:07 Sent new info to client
2024/12/12 20:01:08 Got new message from client 5
2024/12/12 20:01:08 Actual info already on client
2024/12/12 20:01:09 Got new message from client 5
2024/12/12 20:01:09 Sent new info to client
2024/12/12 20:01:10 Got new message from client 5
2024/12/12 20:01:10 Sent new info to client
2024/12/12 20:01:11 Got new message from client 5
2024/12/12 20:01:11 Actual info already on client
2024/12/12 20:01:12 Got new message from client 5
2024/12/12 20:01:13 Got new message from client 5
2024/12/12 20:01:14 Got new message from client 5
2024/12/12 20:01:14 Actual info already on client
2024/12/12 20:01:21 Connection with client 5 closed
2024/12/12 20:14:22 Socket created
2024/12/12 20:14:31 New thread created for socket 4
2024/12/12 20:14:49 Got new message from client 4
2024/12/12 20:14:54 Connection with client 4 closed
2024/12/12 20:22:52 New thread created for socket 4

Binary file not shown.

View File

@ -24,7 +24,7 @@
/* Configuration */ /* Configuration */
// Name of the pipe used to transfer logs to the log server // Name of the pipe used to transfer logs to the log server
std::string LOG_FIFO_NAME = "./tmp/log"; std::string LOG_FIFO_NAME = "./../tmp/log";
// Type of the server: server1 - last error & cursor position, server2 - mem % & vmem % // Type of the server: server1 - last error & cursor position, server2 - mem % & vmem %
std::string SERVER_TYPE = "server1"; std::string SERVER_TYPE = "server1";
@ -53,6 +53,8 @@ int active_connections = 0;
// Total amount of handled requests // Total amount of handled requests
int handndled_requests = 0; int handndled_requests = 0;
// Log pipe file descriptor
int is_log_ok = false;
// Initializes ncurses lib // Initializes ncurses lib
void init_ncurses() { void init_ncurses() {
@ -191,23 +193,24 @@ void log(std::string msg) {
std::strftime(timestamp, 32, "%H:%M:%S ", &tstruct); std::strftime(timestamp, 32, "%H:%M:%S ", &tstruct);
last_log_messages[0] = timestamp + std::string(" ") + msg; last_log_messages[0] = timestamp + std::string(" ") + msg;
// Write message to the logging pipe TODO // Write message to the logging pipe
// int fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY); int log_fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY);
// if(fd != -1) { if(log_fd != -1) {
// write(fd, msg.c_str(), strlen(msg.c_str()) + 1); is_log_ok = true;
// close(fd); write(log_fd, msg.c_str(), msg.length());
// } close(log_fd);
} else {
is_log_ok = false;
}
} }
// Opens file descriptor for logging pipe TODO // // Opens file descriptor for logging pipe TODO
void init_log_pipe() { // void init_log_pipe() {
// mkfifo(LOG_FIFO_NAME.c_str(), 0666); // log_fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY);
} // }
// Returns file descriptor for server socket // Returns file descriptor for server socket
int start_socket() { int start_socket() {
log("Creating socket...");
int server_socket = socket(AF_INET, SOCK_STREAM, 0); int server_socket = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in server_address; sockaddr_in server_address;
server_address.sin_family = AF_INET; server_address.sin_family = AF_INET;
@ -221,8 +224,6 @@ int start_socket() {
return -1; return -1;
} }
log("Socket created");
int status = listen(server_socket, MAX_CONNECTIONS); int status = listen(server_socket, MAX_CONNECTIONS);
if (status != 0) { if (status != 0) {
@ -230,8 +231,6 @@ int start_socket() {
return -1; return -1;
} }
log("Listening for connections...");
return server_socket; return server_socket;
} }
@ -243,7 +242,7 @@ void* handle_connection(void* arg) {
std::string info = ""; std::string info = "";
log("New thread created for socket " + std::to_string(client_socket)); log("New thread created for socket " + std::to_string(client_socket) + '\0');
while (true) { while (true) {
// Recieve message from client // Recieve message from client
@ -258,16 +257,15 @@ void* handle_connection(void* arg) {
} }
if (bytes_recieved == -1) { if (bytes_recieved == -1) {
log("Error while recieving message: " + std::to_string(errno) + " - " + std::strerror(errno)); log("Error while recieving message: " + std::to_string(errno) + " - " + std::strerror(errno) + '\0');
break; break;
} }
log("Got new message from client " + std::to_string(client_socket) + ": " + buffer); log("Got new message from client " + std::to_string(client_socket) + '\0');
std::string new_info = get_sysinfo(); std::string new_info = get_sysinfo();
if (info != new_info) { if (info != new_info) {
log("Client " + std::to_string(client_socket) + " info is not up to date");
info = new_info; info = new_info;
time_t now = time(0); time_t now = time(0);
struct tm tstruct; struct tm tstruct;
@ -276,10 +274,10 @@ void* handle_connection(void* arg) {
std::strftime(timestamp, 32, "%Y/%m/%d %H:%M:%S ", &tstruct); std::strftime(timestamp, 32, "%Y/%m/%d %H:%M:%S ", &tstruct);
std::string msg = timestamp + info; std::string msg = timestamp + info;
send(client_socket, msg.c_str(), strlen(msg.c_str()) + 1, 0); send(client_socket, msg.c_str(), strlen(msg.c_str()) + 1, 0);
log("Sent new info to client " + std::to_string(client_socket)); log("Sent new info to client\0");
handndled_requests++; handndled_requests++;
} else { } else {
log("Actual info already on client " + std::to_string(client_socket)); log("Actual info already on client\0");
} }
} }
@ -302,6 +300,12 @@ void* draw(void* arg) {
LOG_FIFO_NAME.c_str(), SERVER_TYPE.c_str(), PORT, MAX_CONNECTIONS, CMD_LOG_BUFFER, CMD_REFRESH_TIME LOG_FIFO_NAME.c_str(), SERVER_TYPE.c_str(), PORT, MAX_CONNECTIONS, CMD_LOG_BUFFER, CMD_REFRESH_TIME
); );
if (is_log_ok) {
printw("Log server connected\n\n");
} else {
printw("Log server is not connected\n\n");
}
// Print statistics // Print statistics
printw("Active connections: %i\nHandled messages: %i\n", active_connections, handndled_requests); printw("Active connections: %i\nHandled messages: %i\n", active_connections, handndled_requests);
@ -337,7 +341,6 @@ void* commands(void* arg) {
int main() { int main() {
init_ncurses(); init_ncurses();
init_log_pipe();
// Create thread to redraw console output // Create thread to redraw console output
pthread_t tid; pthread_t tid;
@ -350,6 +353,8 @@ int main() {
int server_socket = start_socket(); int server_socket = start_socket();
log("Socket created\0");
// Create thread to read commands // Create thread to read commands
pthread_t tcid; pthread_t tcid;
int* server_socket_copy = new int(server_socket); int* server_socket_copy = new int(server_socket);
@ -367,14 +372,12 @@ int main() {
continue; continue;
} }
log("New connection accepted");
active_connections++; active_connections++;
// Create thread for each connection // Create thread for each connection
int* client_socket_copy = new int(client_socket); int* client_socket_copy = new int(client_socket);
pthread_t tid; pthread_t tid;
if (pthread_create(&tid, nullptr, &handle_connection, client_socket_copy) != 0) { if (pthread_create(&tid, nullptr, &handle_connection, client_socket_copy) != 0) {
log("Failed to create thread for the connection");
delete client_socket_copy; delete client_socket_copy;
} }
pthread_detach(tid); pthread_detach(tid);

View File

@ -24,7 +24,7 @@
/* Configuration */ /* Configuration */
// Name of the pipe used to transfer logs to the log server // Name of the pipe used to transfer logs to the log server
std::string LOG_FIFO_NAME = "./tmp/log"; std::string LOG_FIFO_NAME = "./../tmp/log";
// Type of the server: server1 - last error & cursor position, server2 - mem % & vmem % // Type of the server: server1 - last error & cursor position, server2 - mem % & vmem %
std::string SERVER_TYPE = "server2"; std::string SERVER_TYPE = "server2";
@ -53,6 +53,8 @@ int active_connections = 0;
// Total amount of handled requests // Total amount of handled requests
int handndled_requests = 0; int handndled_requests = 0;
// Log pipe file descriptor
int is_log_ok = false;
// Initializes ncurses lib // Initializes ncurses lib
void init_ncurses() { void init_ncurses() {
@ -191,23 +193,24 @@ void log(std::string msg) {
std::strftime(timestamp, 32, "%H:%M:%S ", &tstruct); std::strftime(timestamp, 32, "%H:%M:%S ", &tstruct);
last_log_messages[0] = timestamp + std::string(" ") + msg; last_log_messages[0] = timestamp + std::string(" ") + msg;
// Write message to the logging pipe TODO // Write message to the logging pipe
// int fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY); int log_fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY);
// if(fd != -1) { if(log_fd != -1) {
// write(fd, msg.c_str(), strlen(msg.c_str()) + 1); is_log_ok = true;
// close(fd); write(log_fd, msg.c_str(), msg.length());
// } close(log_fd);
} else {
is_log_ok = false;
}
} }
// Opens file descriptor for logging pipe TODO // // Opens file descriptor for logging pipe TODO
void init_log_pipe() { // void init_log_pipe() {
// mkfifo(LOG_FIFO_NAME.c_str(), 0666); // log_fd = open(LOG_FIFO_NAME.c_str(), O_WRONLY);
} // }
// Returns file descriptor for server socket // Returns file descriptor for server socket
int start_socket() { int start_socket() {
log("Creating socket...");
int server_socket = socket(AF_INET, SOCK_STREAM, 0); int server_socket = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in server_address; sockaddr_in server_address;
server_address.sin_family = AF_INET; server_address.sin_family = AF_INET;
@ -221,8 +224,6 @@ int start_socket() {
return -1; return -1;
} }
log("Socket created");
int status = listen(server_socket, MAX_CONNECTIONS); int status = listen(server_socket, MAX_CONNECTIONS);
if (status != 0) { if (status != 0) {
@ -230,8 +231,6 @@ int start_socket() {
return -1; return -1;
} }
log("Listening for connections...");
return server_socket; return server_socket;
} }
@ -243,7 +242,7 @@ void* handle_connection(void* arg) {
std::string info = ""; std::string info = "";
log("New thread created for socket " + std::to_string(client_socket)); log("New thread created for socket " + std::to_string(client_socket) + '\0');
while (true) { while (true) {
// Recieve message from client // Recieve message from client
@ -258,16 +257,15 @@ void* handle_connection(void* arg) {
} }
if (bytes_recieved == -1) { if (bytes_recieved == -1) {
log("Error while recieving message: " + std::to_string(errno) + " - " + std::strerror(errno)); log("Error while recieving message: " + std::to_string(errno) + " - " + std::strerror(errno) + '\0');
break; break;
} }
log("Got new message from client " + std::to_string(client_socket) + ": " + buffer); log("Got new message from client " + std::to_string(client_socket) + '\0');
std::string new_info = get_sysinfo(); std::string new_info = get_sysinfo();
if (info != new_info) { if (info != new_info) {
log("Client " + std::to_string(client_socket) + " info is not up to date");
info = new_info; info = new_info;
time_t now = time(0); time_t now = time(0);
struct tm tstruct; struct tm tstruct;
@ -276,10 +274,10 @@ void* handle_connection(void* arg) {
std::strftime(timestamp, 32, "%Y/%m/%d %H:%M:%S ", &tstruct); std::strftime(timestamp, 32, "%Y/%m/%d %H:%M:%S ", &tstruct);
std::string msg = timestamp + info; std::string msg = timestamp + info;
send(client_socket, msg.c_str(), strlen(msg.c_str()) + 1, 0); send(client_socket, msg.c_str(), strlen(msg.c_str()) + 1, 0);
log("Sent new info to client " + std::to_string(client_socket)); log("Sent new info to client\0");
handndled_requests++; handndled_requests++;
} else { } else {
log("Actual info already on client " + std::to_string(client_socket)); log("Actual info already on client\0");
} }
} }
@ -302,6 +300,12 @@ void* draw(void* arg) {
LOG_FIFO_NAME.c_str(), SERVER_TYPE.c_str(), PORT, MAX_CONNECTIONS, CMD_LOG_BUFFER, CMD_REFRESH_TIME LOG_FIFO_NAME.c_str(), SERVER_TYPE.c_str(), PORT, MAX_CONNECTIONS, CMD_LOG_BUFFER, CMD_REFRESH_TIME
); );
if (is_log_ok) {
printw("Log server connected\n\n");
} else {
printw("Log server is not connected\n\n");
}
// Print statistics // Print statistics
printw("Active connections: %i\nHandled messages: %i\n", active_connections, handndled_requests); printw("Active connections: %i\nHandled messages: %i\n", active_connections, handndled_requests);
@ -337,7 +341,6 @@ void* commands(void* arg) {
int main() { int main() {
init_ncurses(); init_ncurses();
init_log_pipe();
// Create thread to redraw console output // Create thread to redraw console output
pthread_t tid; pthread_t tid;
@ -350,6 +353,8 @@ int main() {
int server_socket = start_socket(); int server_socket = start_socket();
log("Socket created\0");
// Create thread to read commands // Create thread to read commands
pthread_t tcid; pthread_t tcid;
int* server_socket_copy = new int(server_socket); int* server_socket_copy = new int(server_socket);
@ -367,14 +372,12 @@ int main() {
continue; continue;
} }
log("New connection accepted");
active_connections++; active_connections++;
// Create thread for each connection // Create thread for each connection
int* client_socket_copy = new int(client_socket); int* client_socket_copy = new int(client_socket);
pthread_t tid; pthread_t tid;
if (pthread_create(&tid, nullptr, &handle_connection, client_socket_copy) != 0) { if (pthread_create(&tid, nullptr, &handle_connection, client_socket_copy) != 0) {
log("Failed to create thread for the connection");
delete client_socket_copy; delete client_socket_copy;
} }
pthread_detach(tid); pthread_detach(tid);