Python_FTP_Server_and_Client : Distributed Log File Automated Archive System
This week, we will introduce the automated compute cluster nodes' log automated archive system project and FTP Communication utility library module.
Program Design Purpose: This project aims to create an FTP server & client lib program for file transfer and a files management system for log data synchronization. We will develop an automated log file archive system that regularly collects newly generated log files from multiple nodes in a computer cluster and saves them on a central log file historian server. Additionally, a web interface will be provided on the log file historian server to allow users to access and review the archived logs.
To check the system usage demo, please refer to this video:
# Created: 2024/07/27
# Version: v_0.1.2
# Copyright: Copyright (c) 2024 LiuYuancheng
# License: MIT License
Introduction
Often, there is a need to continuously collect program log files from numerous servers for big data analysis, system operation monitoring, or threat detection projects. This project will create an automated log file archiver, which can synchronize specific log storage on nodes with a central log storage server. Additionally, it provides a web interface for users to check and download log files from the web archive server. The project includes three components:
The system work flow diagram is shown below:
System Design
In this section, we will introduce the main features, detailed design of each module and the function of sub-modules based on the workflow diagram shown in the introduction section.
Design of FTP Comm Library
The FTP Communication Library includes both server and client modules, each capable of running in parallel with the main thread to allow for seamless integration into user programs.
The FTP-server module includes the following features:
We use pyftpdlib to implement the FTP server module, reference link: https://pypi.org/project/pyftpdlib/
The FTP-client module includes the following features:
We use the python built-in lib ftplib to implement the client module, ref-link: https://docs.python.org/3/library/ftplib.html
Design of Log Archive Agent
The Log Archive Agent continuously monitors the node's log storage folder. When a new log file is created, it uploads the log files to the corresponding folder on the server. The main features include:
The Log Archive Agent will keep a JSON file recording the files that have been transferred to the server. It follows these steps to archive new log files:
Design of Log Archive Server
All agents will connect to the server to upload log files. The server manages agent access and provides a web interface for users to search and download log files. The main features include:
In the server side user permission config please follow below permission char setting table:
System Setup
领英推荐
Development Environment
Additional Lib/Software Need
Hardware Needed : None
Program Files List
System Usage
Usage of FTP Comm Lib
To use the FTO communication lib in your program, please follow the test case program ftpTestcaseClient.py and ftpTestcaseServer.py
To integrate the Log archive service in your program and init:
dir = os.path.join(dirpath, 'ftpServer_data')
server = ftpComm.ftpServer(dir, port=8081, threadFlg=True)
server.addUser('client1', '123456')
server.startServer()
To integrate the log archive client to in your program and init:
client = ftpComm.ftpClient('127.0.0.1', 8081, 'client1', '123456')
client.connectToServer()
Usage of Log Archive Server
Change the ServerConfig_template.txt to ServerConfig.txt then follow the comments in it to set the parameters as shown the example below:
# This is the config file template for the module <logArchiveServer.py>
# Setup the parameter with below format (every line follows <key>:<val> format, the
# key can not be changed):
#-----------------------------------------------------------------------------
# Init the FTP server parameters
FTP_SER_PORT:8081
# default max speed for client download: 300 Kb/sec (30 * 1024)
MAX_UPLOAD_SPEED:307200
# default max speed for client upload: 300 Kb/sec (30 * 1024)
MAX_DOWNLOAD_SPEED:307200
#-----------------------------------------------------------------------------
# User record json file, need to put in same folder with the logArchiveServer.py
USER_RCD:userRecord.json
#-----------------------------------------------------------------------------
# FTP storage root folder
LOG_DIR:ServerLogSorage
LOG_PF:.txt
#-----------------------------------------------------------------------------
# Init the Flask app parameters
FLASK_SER_PORT:5000
FLASK_DEBUG_MD:False
FLASK_MULTI_TH:True
Then add a valid user in the userRcd.json file and set the permission (refer to the design of Log Archive server side permission config), as shown the example below:
{
"admin": {
"passwd": "******",
"perm": "elradfmwM"
},
"agent": {
"passwd": "******",
"perm": "elr"
}
}
Run the server program:
python3 logArchiveServer.py
Usage of Log Archive Agent
Change the AgentConfig_template.txt to AgentConfig.txt then follow the comments in it to set the parameters as shown the example below:
# This is the config file template for the module <logArchiveAgent.py>
# Setup the parameter with below format (every line follows <key>:<val> format, the
# key can not be changed):
#-----------------------------------------------------------------------------
# Unique Agent ID, all the log file will be saved in the server's home/<AGENT_ID>/ folder
AGENT_ID:Test
#-----------------------------------------------------------------------------
# FTP server info and login credentials, don't uplaod the credentials to the Github
FTP_SER_IP:127.0.0.1
FTP_SER_PORT:8081
USER_NAME:agent
USER_PWD:P@ssw0rd
#-----------------------------------------------------------------------------
# Uploaded files record json file, need to be at same folder of <logArchiveAgent.py>
# if not exist, the agent will create a empty one.
RCD_JSON:uploadRcd.json
#-----------------------------------------------------------------------------
# local folder save the log files.
LOG_DIR:AgentLogFolder
# the file extension of the log files such as : .txt, .log, .csv, etc.
LOG_PF:.txt
#-----------------------------------------------------------------------------
# Time interval between 2 uplaod processes, in seconds.
UPLOAD_INV:10
Run the log archive agent program:
python logAchiveAgent.py
View Logs Contents From Web UI
To view the archived log file in the server, type in the URL https://<webhost_IPaddress>:5000 to view the web interface and download the log files:
Reference : To check the project detail, please refer to this link: https://github.com/LiuYuancheng/Python_FTP_Server_and_Client
Thanks for spending time to check the article detail, if you have any question and suggestion or find any program bug, please feel free to message me. Many thanks if you can give some comments and share any of the improvement advice so we can make our work better ~
Senior Consultant @ EY | Cyber Security, Smart Metering| IT-OT | AUDITING | IT- INFRA | Cloud Computing-Micro-Soft Azure-104 | CEH | Cyber Incident Response.
3 个月Great