Package: taskp5
Work with various DBMS in Python.Module 2: Python. Task 5.PrefaceThis project contains a solution to one of the tasks of the EPAM DevOps Initial Internal Training Course #7 in 2023.
Detailed information about the course, as well as reports on each of the completed tasks (including this one) can be foundhere.This project builds upontask #4with some modifications specific to this task condition. Please review thedocumentationto familiarize yourself with these changes. Below you will find additional information on the new features in task #5.Table of ContentsConditionsImplementationStructureInstallationUsageLibraryCLIPipes and filesShowcaseGeneral ProvisionsConditionsPlease reviewmain conditionsfrom the task #4.Additional ones:Use a database to store the log.Install databases MySQL, PostgreSQL, Mongo DB.Import log data from a file into the database.Export log data from the database to a file in log format.Implement 10 requests to the database (subtasks from task 4).ImplementationLog_Parser2is a Python package that could be added to your global or virtual environment by preferable package manager pip, pipenv, poetry, etc.
The project itself was managed and built using thePoetry library,
so if you intend to clone this repo and make some changes for your own purposes, please installPoetryor migrate to your preferred package management library.Based on the need to build and the possibility of using both the library and the CLI, the code was split into a library for importing and a script for execution
via the command line. Additionally, the package contains a showcase that demonstrates all use cases when run through the command line.To enhance the command line's functionality and expand showcase capabilities, theQuestionarylibrary is used and will be installed through a dependency link upon package installation.Structuretask5/
├── README.md (You are here now)
├── pyproject.toml # Poetry package management file
└── log_parser2/ 
    ├──__init__.py # library entry point
    ├──__main__.py # CLI entry point
    ├──__version__.py
    ├── db_provider.py # implementation of work with various databases
    ├── logging.py # logging implementation
    ├── log_parser2.py # library implementation
    ├── cli/
    │   ├──__init__.py
    │   ├──__main__.py
    │   └── cli.py # CLI code implementation
    └── showcase/
            ├──__init__.py 
            ├──__main__.py # showcase entry point when using python -m log_parser2.showcase
            ├── access.log # a sample CSV file
            └── showcase.py # showcase implementationInstallationUse your preferred installation method via different package installation managers to install Log_Parser2.PipTo install Log_Parser2 packet to your environment using pip manager invokepip install taskp5.$pipinstalltaskp5
Collectingtaskp5Usingcachedtaskp5-2.1.N-py3-none-any.whl(62kB)Collectingquestionary<2.0.0,>=2.0.0(fromtaskp5)Downloadingquestionary-2.0.0-py3-none-any.whl(31kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━31.1/31.1kBN.NMB/seta0:00:00
Collectingprompt_toolkit<4.0,>=2.0(fromquestionary<2.0.0,>=1.10.0->taskp5)Downloadingprompt_toolkit-3.0.39-py3-none-any.whl(385kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━385.2/385.2kBN.NMB/seta0:00:00
Collectingwcwidth(fromprompt_toolkit<4.0,>=2.0->questionary<2.0.0,>=1.10.0->taskp5)Downloadingwcwidth-0.2.6-py2.py3-none-any.whl(29kB)━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━29.2/29.2kBN.NMB/seta0:00:00
Installingcollectedpackages:wcwidth,prompt_toolkit,questionary,taskp5
Successfullyinstalledprompt_toolkit-3.0.39questionary-1.10.0taskp5-2.1.Nwcwidth-0.2.6To uninstall Log Parser from your environment invokepip uninstall taskp5.
It's important to note that the pip manager does not uninstall dependent packages. Therefore, if you wish to remove them, you'll need to take
the initiative and perform the task yourself. You can do this by using the commandspip uninstall questionary prompt-toolkit wcwidth.PoetryTo install Log Parser packet to your environment using poetry manager invokepoetry add taskp5.$poetryaddtaskp5
Usingversion^2.1.Nfortaskp5

Updatingdependencies
Resolvingdependencies...

Packageoperations:6installs,0updates,0removals•Installingwcwidth(0.2.6)•Installingprompt-toolkit(3.0.39)•Installingquestionary(1.10.0)WritinglockfileBy taking this action, a new dependency line will be added to your pyproject.toml file.[tool.poetry.dependencies]taskp5="^2.1.N"To uninstall Log Parser from your environment invokepoetry remove taskp5.
One of the benefits of utilizing Poetry is that it allows for the removal of all dependent packages with a single command.UsageThere are various ways to use this library, as mentioned earlier.Utilize it like a library you can just import it into your .py file and use LogHelper class within your code.Utilize CLI via the command shell, either as a Python module or as a standalone command.Utilize CLI command in a pipe by passing stdout of other commands to the stdin of the log_parser2 command, writing stdout and stderr to files, or passing them to following commands.The library also contains rich showcase command that allows you to test all the use cases and even perform them in batches.LibraryBelow is a code snippet that demonstrates how to be able to use the log_parser2 library in your code.fromlog_parser2.log_parser2importlogger,LogHandlerimportrewithopen('access.log','r')asf:db_config=dict(host='3.12.77.55',user='awesome_user',password='*********',database='some_database_name')dbase=DBTypes.MYSQL.value(db_config,**provider_param)handler=LogHandler(log=f,extractor=re.compile(extractor,flags=re.I|re.X),selection=selection,aggregation=aggregation,filter_str=filter_str,database=dbase)print(handler.output)dbase.close()CLIThe CLI interface has a single command called "log_parser2". It can be invoked using two methods:python3 -m log_parser2or simplylog_parser2.
Log_parser2 accepts various arguments, which are described below.log_parser2 [-h] [--version] -d  -c  [-C] -e  [-s] [-a] [-f] [-r [INDEXES ...]] [-v] [file_name]

positional arguments:
  file_name             An input filename

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -d , --database-type  Specify a database type that you intend to use (default: MYSQL). 
                        Possible values could be: MYSQL, PostgresSQL, MongoDB
  -c , --credentials    Specify a file with credentials used to connect to the database.
  -C, --create-db       Whether create a new database to store parsed log data or not.
  -e , --extractor      Specify a regular expression for extracting certain data from each log line. 
                        The following expression patterns can be used to make      
                        writing an expression easier and reduce its size. 
                        For instance, "IP4\ \((?P<IP4_LIST>(?:(?:IP4|.*?),\ )*)(?:IP4|.*?)\)\ -\ -\
                        \[DATE_TIME_SEC\]\ \"(?:REQUEST|-)\"" 
                        
                        IP4_OCTET, MASK_OCTET, IP4, IP4_CIDR, MASK, IP6, IP6_CIDR, MAC_PART, MAC, VERSION, AGENT_AGENT,   
                        AGENT_OS, AGENT_DEVICE, AGENT_BROWSER, USER_AGENT, DOMAIN, EMAIL_NAME_CHARSET, EMAIL, URI, URL, 
                        REQUEST_TYPES, REQUEST, DATE_TIME_SEC, UUID
  -s , --selection      Specify a regular expression template to output selected data. To add previously extracted data
                        via regular expression groups use template group naming like \g<group_name>. 
                        It is allowed to use various transformation functions for substitution, extracting and even  
                        splitting data.
                        For instance, 
                        "TO_MIN(\g<DATE_TIME>) \g<URL>" or "SPLIT(RE('(?<=\().+?(?=\))', \g<IP4>), ',\ ')" 
                        
                        The list of possible functions: MIN, MAX, SPLIT, SUB, INTERVAL, SUM, COUNT, RE
  -a , --aggregation    The aggregate to be calculated during log parsing. It is used for sorting or summation.
                        For instance, "MAX(\g<STATUS>,5)" or ..... 
                        
                        The list of possible functions: MIN, MAX, SPLIT, SUB, INTERVAL, SUM, COUNT, RE
  -f , --filter         Specify a regular expression for filtering each line. It is supposed to used RE functions
                        and group naming like \g<group_name>. If the result of using regular expression return None
                        the line will be omitted. 
                        
                        For instance, 
                        "RE('50\d', \g<STATUS>)" or "RE((?:/+[a-z\d\-._~%&\'()*+,;=:@{\'}{\'}]+){2}, \g<REQUEST_PATH>)"
  -r [INDEXES ...], --rows [INDEXES ...]
                        The row range from the log file to be parsed. You can pass values in the following formats: particular indexes: index1 index2 ...       
                        indexN range of indexes: index1-index2 from the beginning up to index: -index from index to the end: index-
  -v                    Increase verbosity level (add more v)It's mandatory to create a file with credential information to connect to a database and pass it via the -c flag.
Also, it's allowed to choose database type from MYSQL, PostgreSQL, and MongoDB.To handle the arguments, theargparsemodule is used. If you are already acquainted with it, you will have no difficulty in passing the arguments along with their values and comprehending their behavior.The examples in a more convenient form you could find in theshowcase.Pipes and filesThe log_parser2 command could be used inside the pipe of the BASH commands. It can be used in various ways within a pipeline:receiving inputdirect output to a filedirect logging also to a fileThis command isn't intended for use in pipe sequence due to the table output format but is allowed.ShowcaseTo showcase the behavior of the log_parser2 library, an interactive command called "log_parser2_showcase" has been created.
This command utilizes both the log_parser2 CLI and the log_parser2 library. It's an interactive command you can invoke vialog_parser2_showcaseorpython3 -m log_parser2.showcase.
It has an optional flag that allows you to view all use cases at once without any interaction.
You can use the commandlog_parser2_showcase --allto activate this feature.
There are also a ready-made Apache Tomcat log file presents -access.log.
You can use the showcase to test your own regular expressions and regular expression templates.General provisionsAll materials provided and/or made available contain EPAM’s proprietary and confidential information and must not to be copied,
reproduced or disclosed to any third party or to any other person, other than those persons who have a bona fide need to review it
for the purpose of participation in the online courses being provided by EPAM.
The intellectual property rights in all materials (including any trademarks) are owned by EPAM Systems Inc or its associated companies,
and a limited license, terminable at the discretion of EPAM without notice, is hereby granted to you solely for the purpose of participating
in the online courses being provided by EPAM. Neither you nor any other party shall acquire any intellectual property rights of any kind
in such materials.