Difference between revisions of "Python-for-Automation/C3/Log-Monitor/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " <div style="margin-left:1.27cm;margin-right:0cm;"></div> {| border="1" |- || '''Visual Cue''' || '''Narration''' |- |- style="border:1pt solid #000000;padding-top:0cm;paddin...")
 
 
Line 1: Line 1:
  
 
<div style="margin-left:1.27cm;margin-right:0cm;"></div>
 
 
{| border="1"
 
{| border="1"
 
|-
 
|-
 
|| '''Visual Cue'''
 
|| '''Visual Cue'''
 
|| '''Narration'''
 
|| '''Narration'''
|-
+
 
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide:  
+
|| '''Show slide''':  
  
 
'''Welcome'''
 
'''Welcome'''
|| Welcome to the Spoken Tutorial on '''"Log Monitoring ".'''
+
|| Welcome to the Spoken Tutorial on '''"Log Monitoring "'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide:  
+
|| '''Show slide''':  
  
 
'''Learning Objectives'''
 
'''Learning Objectives'''
 
|| In this tutorial, we will learn about
 
|| In this tutorial, we will learn about
* <div style="margin-left:1.27cm;margin-right:0cm;">System logs and</div>
+
* System logs and
* <div style="margin-left:1.27cm;margin-right:0cm;">How to monitor logs</div>
+
* How to monitor logs
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide:  
+
|| '''Show slide''':  
 
|| To record this tutorial, I am using
 
|| To record this tutorial, I am using
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Ubuntu Linux os version 22.04'''</div>
+
* '''Ubuntu Linux OS version 22.04'''
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Python version 3.12.3'''</div>
+
* '''Python version 3.12.3'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide:  
+
|| '''Show slide''':  
  
 
'''Prerequisite'''
 
'''Prerequisite'''
Line 33: Line 31:
 
[https://www.spoken-tutorial.org/ https://spoken-tutorial.org]
 
[https://www.spoken-tutorial.org/ https://spoken-tutorial.org]
 
|| To follow this tutorial  
 
|| To follow this tutorial  
* <div style="margin-left:1.27cm;margin-right:0cm;">You must have basic knowledge of using '''Linux Terminal '''and''' Python.'''</div>
+
* You must have basic knowledge of using '''Linux Terminal '''and ''' Python.'''
* <div style="margin-left:1.27cm;margin-right:0cm;">For pre-requisite '''Linux''' and '''Python '''Tutorials, please visit this website.</div>
+
* For pre-requisite '''Linux''' and '''Python '''Tutorials, please visit this website.
* <div style="margin-left:1.27cm;margin-right:0cm;">Python libraries required for automation must be installed</div>
+
* Python libraries required for automation must be installed.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide:
+
|| '''Show slide''':  
  
 
'''Code files'''
 
'''Code files'''
 
||  
 
||  
* <div style="margin-left:1.27cm;margin-right:0cm;">The files used in this tutorial are provided in the '''Code files''' link.</div>
+
* The files used in this tutorial are provided in the '''Code files''' link.
* <div style="margin-left:1.27cm;margin-right:0cm;">Please download and extract the files.</div>
+
* Please download and extract the files.
* <div style="margin-left:1.27cm;margin-right:0cm;">Make a copy and then use them while practicing.</div>
+
* Make a copy and then use them while practicing.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide:  
+
|| '''Show slide''':  
  
 
'''Log monitoring'''
 
'''Log monitoring'''
|| Log Monitoring in automation :
+
|| Log Monitoring in automation:
* <div style="margin-left:1.27cm;margin-right:0cm;">Collects and analyzes log data for insights.</div>
+
* Collects and analyzes log data for insights.
* <div style="margin-left:1.27cm;margin-right:0cm;">Record events, errors, and system messages.</div>
+
* Record events, errors, and system messages.
* <div style="margin-left:1.27cm;margin-right:0cm;">Helps to detect issues before they become critical.</div>
+
* Helps to detect issues before they become critical.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide: '''Libraries used'''
+
|| '''Show slide''':  
 +
 
 +
'''Libraries used'''
 
||
 
||
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Logging''' library is used to log events during the execution of the program.</div>
+
* '''Logging''' library is used to log events during the execution of the program.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''collections.Counter''' is used for counting elements in an iterable and for summarizing log data.</div>
+
* '''collections.Counter''' is used for counting elements in an iterable and for summarizing log data.
 +
 
 +
|-
 +
||  '''Show slide''':
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
'''Types of logs'''
|| Show slide:
+
 
|| There are different types of log files.
 
|| There are different types of log files.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Application Logs '''will''' '''record events within a running application.</div>
+
* '''Application Logs '''will record events within a running application.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''System Logs''' captures the operating system events.</div>
+
* '''System Logs''' captures the operating system events.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Security Logs '''will''' '''track access control and authorization events.</div>
+
* '''Security Logs '''will track access control and authorization events.
* <div style="margin-left:1.27cm;margin-right:0cm;">'''Audit Logs '''will analyze what is happening on your system in great detail.</div>
+
* '''Audit Logs ''' will analyze what is happening on your system in great detail.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Only narration:
 
|| Only narration:
|| In this tutorial we’ll see how to monitor syslog files using '''Python''' code.
+
|| In this tutorial we will see how to monitor syslog files using '''Python''' code.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Open terminal'''(Ctrl + Alt + T)'''
 
|| Open terminal'''(Ctrl + Alt + T)'''
 
|| Open the terminal by pressing '''Control + Alt + T '''keys simultaneously.
 
|| Open the terminal by pressing '''Control + Alt + T '''keys simultaneously.
  
 
Let us see how to access the log file in our system.
 
Let us see how to access the log file in our system.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Type '''cd /var/log'''
 
|| Type '''cd /var/log'''
 
|| Type '''cd space forward slash var forward slash log'''.
 
|| Type '''cd space forward slash var forward slash log'''.
  
Press enter.
+
Press Enter.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Type '''ls'''
 
|| Type '''ls'''
|| Let us list the files in this directory.Type '''ls''' and press enter.
+
|| Let us list the files in this directory.Type '''ls''' and press Enter.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight the output
 
|| Highlight the output
 
|| We can see that all the log files stored in the system are displayed here.
 
|| We can see that all the log files stored in the system are displayed here.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Only narration:
 
|| Only narration:
 
|| For demonstration purpose we will work with the '''Syslog''' files.
 
|| For demonstration purpose we will work with the '''Syslog''' files.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Open '''syslog''':Type '''nano syslog'''
 
|| Open '''syslog''':Type '''nano syslog'''
 
|| Type '''nano syslog '''to open the syslog file.
 
|| Type '''nano syslog '''to open the syslog file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show '''Syslog'''
 
|| Show '''Syslog'''
 
|| The '''syslog''' is a central log file in Ubuntu.
 
|| The '''syslog''' is a central log file in Ubuntu.
  
 
It records system messages, user activities, and errors.
 
It records system messages, user activities, and errors.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight: '''Timestamp'''
 
|| Highlight: '''Timestamp'''
 
|| Each log entry starts with a '''timestamp''' of when the event occurred.
 
|| Each log entry starts with a '''timestamp''' of when the event occurred.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:''' Host'''
 
|| Highlight:''' Host'''
|| Next to the '''timestamp''' is the '''Host name '''of the machine where the event was recorded.
+
|| Next to the '''timestamp''' is the '''Host name ''' of the machine where the event was recorded.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight: '''Service/Program'''
 
|| Highlight: '''Service/Program'''
 
|| After the '''Host''' , is the '''Service/Program name''' of the system service generating the log.
 
|| After the '''Host''' , is the '''Service/Program name''' of the system service generating the log.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:''' Message'''
 
|| Highlight:''' Message'''
 
|| Finally we get the '''message''' - which is the details of the event or error.
 
|| Finally we get the '''message''' - which is the details of the event or error.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Only narration
 
|| Only narration
|| In this tutorial, we will read this file and identify any abnormalities in the file.We will generate a report based on the analysis.
+
|| In this tutorial, we will read this file and identify any abnormalities in the file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
 
 +
We will generate a report based on the analysis.
 +
|-  
 
|| Press '''ctrl + x '''
 
|| Press '''ctrl + x '''
 
|| Press '''ctrl + x '''to exit the file.
 
|| Press '''ctrl + x '''to exit the file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Download '''logMonitor.py'''
 
|| Download '''logMonitor.py'''
 
|| Let us now look into the code file.  
 
|| Let us now look into the code file.  
  
Download '''logMonitor.py '''from '''code files'''.
+
Download '''logMonitor.py '''from '''Code files'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Open '''logMonitor.py'''
 
|| Open '''logMonitor.py'''
 
|| Open '''logMonitor.py''' file using text editor.
 
|| Open '''logMonitor.py''' file using text editor.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight
 
|| Highlight
 
|| First we import necessary libraries.
 
|| First we import necessary libraries.
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | Highlight:
+
|| Highlight:
  
 
'''logger = logging.getLogger(__name__)'''
 
'''logger = logging.getLogger(__name__)'''
  
 
'''logger.setLevel(logging.DEBUG)'''
 
'''logger.setLevel(logging.DEBUG)'''
| style="border:0.75pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.176cm;padding-right:0.176cm;" | A logger instance is created using '''logging.getLogger'''.  
+
|| A logger instance is created using '''logging.getLogger'''.  
  
The log level is set to '''DEBUG.'''This is''' '''to capture all messages, including lower-severity ones.
+
The log level is set to '''DEBUG.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
 
 +
This is''' '''to capture all messages, including lower-severity ones.
 +
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''log_file = '/var/log/syslog''''
 
'''log_file = '/var/log/syslog''''
|| Then we define the path to the log file.Here I set it as the location of my syslog file. You can change it according to your file path.
+
|| Then we define the path to the log file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
 
 +
Here I set it as the location of my syslog file.  
 +
 
 +
You can change it according to your file path.
 +
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 149: Line 159:
  
 
This handler writes log messages to the log file.
 
This handler writes log messages to the log file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 155: Line 165:
 
|| Next we set the file handler to capture '''DEBUG''' level messages and above.
 
|| Next we set the file handler to capture '''DEBUG''' level messages and above.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 164: Line 174:
  
 
|-
 
|-
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;" | Highlight:
+
|| Highlight:
  
 
'''logger.addHandler(file_handler)'''
 
'''logger.addHandler(file_handler)'''
| style="border:0.75pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.176cm;padding-right:0.176cm;" | Finally, the handler is added to the logger using '''logger.addHandler.'''  
+
|| Finally, the handler is added to the logger using '''logger.addHandler.'''  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
 
 
|| The different logging levels are demonstrated here.
 
|| The different logging levels are demonstrated here.
  
Line 179: Line 188:
 
'''logger.warning()''', '''logger.error()''', and '''logger.critical()''' log issues with increasing severity.
 
'''logger.warning()''', '''logger.error()''', and '''logger.critical()''' log issues with increasing severity.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
 
|| Now define a function to check for '''errors''' or '''critical''' messages in the log.
 
|| Now define a function to check for '''errors''' or '''critical''' messages in the log.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''abnormalities = []'''
 
'''abnormalities = []'''
 
|| Initialize an empty list to store abnormal log lines.
 
|| Initialize an empty list to store abnormal log lines.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''with open(log_file, 'r') as file:'''
 
'''with open(log_file, 'r') as file:'''
 
|| Open the log file in read mode to analyze its content.
 
|| Open the log file in read mode to analyze its content.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| Point to ''for '''loop.
|| Using a '''for '''loop, iterate through each line.Check if the line contains ''''ERROR'''' or''' 'CRITICAL'''' log levels.
+
|| Using a '''for ''' loop, iterate through each line.
 +
 
 +
Check if the line contains ''''ERROR'''' or ''' 'CRITICAL'''' log levels.
  
 
These lines are considered abnormalities and are stored in a list.
 
These lines are considered abnormalities and are stored in a list.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''return abnormalities'''
 
'''return abnormalities'''
|| It returns the list of abnormal log entries
+
|| It returns a list of abnormal log entries
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''def summarize_log_file(log_file):'''
 
'''def summarize_log_file(log_file):'''
 
|| Next we define a function to summarize log messages by their levels.
 
|| Next we define a function to summarize log messages by their levels.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
 
|| Initialize a dictionary to count occurrences of each log level.
 
|| Initialize a dictionary to count occurrences of each log level.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight :
 
|| Highlight :
  
 
'''with open(log_file, 'r') as file''':
 
'''with open(log_file, 'r') as file''':
 
|| Open the log file in read mode for processing.
 
|| Open the log file in read mode for processing.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:''' '''
 
|| Highlight:''' '''
|| Using a '''for '''loop, iterate through each line and count the occurrences of each log level.
+
|| Using a '''for ''' loop, iterate through each line and count the occurrences of each log level.
  
 
The results are stored in a dictionary.
 
The results are stored in a dictionary.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''return log_data'''
 
'''return log_data'''
 
|| Finally we return the dictionary summarizing log level counts.
 
|| Finally we return the dictionary summarizing log level counts.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| Highlight: entity causing the most abnormalities.
 
|| Now we define a function to find the entity causing the most abnormalities.
 
|| Now we define a function to find the entity causing the most abnormalities.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| Highlight corresponding lines
 
|| Extract and store the entity name from each abnormal log entry.
 
|| Extract and store the entity name from each abnormal log entry.
  
Line 236: Line 247:
 
Next we will find the entity that appears the most frequently in abnormalities.
 
Next we will find the entity that appears the most frequently in abnormalities.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''return most_common_entity'''
 
'''return most_common_entity'''
 
|| Finally return the most frequent abnormal entity and its count.
 
|| Finally return the most frequent abnormal entity and its count.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| Highlight:
 +
 
 +
Abnormalities_period.
 
|| Next we define a function to find the period with the most abnormalities.
 
|| Next we define a function to find the period with the most abnormalities.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| Highlight:
 +
 
 +
Months
 
|| Extract the date from each abnormality entry and get only the year and month from the dates.
 
|| Extract the date from each abnormality entry and get only the year and month from the dates.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''month_counts = Counter(months)'''
 
'''month_counts = Counter(months)'''
 
|| Using '''Counter''' we count occurrences of each month.
 
|| Using '''Counter''' we count occurrences of each month.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Then find the month with the most abnormalities using the '''most_common''' function.
 
|| Then find the month with the most abnormalities using the '''most_common''' function.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| Cursor near the function.
 
|| Next we define a function to plot the log level data.
 
|| Next we define a function to plot the log level data.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| Cursor near the function.
 
|| In this function we extract the log levels as a list from the log data dictionary.
 
|| In this function we extract the log levels as a list from the log data dictionary.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Extract the counts for each log level.
 
|| Extract the counts for each log level.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| Cursor on '''plt.figure '''
|| Then using '''plt.figure '''create a figure for the plot with a specified size.
+
|| Then using '''plt.figure ''' create a figure for the plot with a specified size.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| Point to the  color palette.
|| We plot a bar chart for the log levels with corresponding counts.You can specify your own color palette here.
+
|| We plot a bar chart for the log levels with corresponding counts.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
 
||
+
You can specify your own color palette here.
 +
|-  
 +
|| Point to  x-axis label to 'Log Level' and y-axis label.
 
|| Set the x-axis label to 'Log Level' and y-axis label to 'Count'
 
|| Set the x-axis label to 'Log Level' and y-axis label to 'Count'
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''plt.title('Log Level Distribution')'''
 
'''plt.title('Log Level Distribution')'''
 
|| Also set the title of the plot as 'Log Level Distribution'.
 
|| Also set the title of the plot as 'Log Level Distribution'.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''plt.show()'''
 
'''plt.show()'''
 
|| Then we display the plot using '''plt.show()''' .
 
|| Then we display the plot using '''plt.show()''' .
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''abnormalities = check_abnormalities(log_file)'''
 
'''abnormalities = check_abnormalities(log_file)'''
 
|| Next call the '''check_abnormalities''' function to find abnormal log entries.
 
|| Next call the '''check_abnormalities''' function to find abnormal log entries.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
Line 296: Line 313:
  
 
This is done by finding the length of the abnormalities dictionary.
 
This is done by finding the length of the abnormalities dictionary.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Finally, print the total number of abnormalities found in the log file.
 
|| Finally, print the total number of abnormalities found in the log file.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| Next we call '''summarize_log_file''' to summarize the log data by log levels.
 
|| Next we call '''summarize_log_file''' to summarize the log data by log levels.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
||  
+
|| Point to for loop.
 
|| Using a for loop, iterate through the log summary data.
 
|| Using a for loop, iterate through the log summary data.
  
 
Print the count for each log level.
 
Print the count for each log level.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
||  
 
||  
 
|| We now call '''find_most_abnormal_entity '''to identify the most abnormal entity and print it.
 
|| We now call '''find_most_abnormal_entity '''to identify the most abnormal entity and print it.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:''' '''
 
|| Highlight:''' '''
 
|| Then call the '''find_max_abnormalities_period '''to identify the period with the most abnormalities and print it.
 
|| Then call the '''find_max_abnormalities_period '''to identify the period with the most abnormalities and print it.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
  
 
'''plot_log_data(log_summary)'''
 
'''plot_log_data(log_summary)'''
 
|| Finally we call''' plot_log_data '''to plot the summarized log data.
 
|| Finally we call''' plot_log_data '''to plot the summarized log data.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Only narration
 
|| Only narration
 
|| Now let us execute the code.
 
|| Now let us execute the code.
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Only narration
 
|| Only narration
 
|| Save the code as '''logMonitor.py '''in the '''Downloads '''folder.
 
|| Save the code as '''logMonitor.py '''in the '''Downloads '''folder.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Open terminal'''(Ctrl + Alt + T)'''
 
|| Open terminal'''(Ctrl + Alt + T)'''
 
|| Open the '''terminal''' by pressing '''Control + Alt + T '''keys simultaneously.
 
|| Open the '''terminal''' by pressing '''Control + Alt + T '''keys simultaneously.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| '''>'''Type '''source Automation/bin/activate'''
 
|| '''>'''Type '''source Automation/bin/activate'''
  
 
Then press enter.
 
Then press enter.
|| We’ll open the virtual environment for the '''Automation''' series.Type '''source space Automation forward slash bin forward slash activate.'''
+
|| We’ll open the virtual environment for the '''Automation''' series.
  
Then press enter.
+
Type '''source space Automation forward slash bin forward slash activate.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|| '''>'''Type <span style="background-color:#ffffff;">'''cd Downloads'''</span>
+
|| <span style="background-color:#ffffff;">Now type, </span><span style="background-color:#ffffff;">'''cd Downloads'''</span><span style="background-color:#ffffff;">.</span>
+
  
<span style="background-color:#ffffff;">Press </span><span style="background-color:#ffffff;">'''Enter.'''</span>
+
Then press Enter.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| '''>'''Type '''sudo <span style="background-color:#ffffff;">python3 logMonitor.py'''</span>
+
|| '''>'''Type '''cd Downloads'''
 +
|| Now type, '''cd Downloads'''.
 +
 
 +
Press '''Enter.'''
 +
|-  
 +
|| '''>'''Type '''sudo python3 logMonitor.py'''
 
|| Let us run the code.
 
|| Let us run the code.
  
 
Type '''sudo space python3 space logMonitor dot py '''
 
Type '''sudo space python3 space logMonitor dot py '''
 
Press '''Enter.'''
 
Press '''Enter.'''
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight :'''Total abnormalities found in log file: 179'''
 
|| Highlight :'''Total abnormalities found in log file: 179'''
 
|| We can see that the count of total abnormalities found in my log file is shown.
 
|| We can see that the count of total abnormalities found in my log file is shown.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight:
 
|| Highlight:
 
|| Next the log summary is also printed which gives me the count of each log level.
 
|| Next the log summary is also printed which gives me the count of each log level.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight :''' '''
 
|| Highlight :''' '''
 
|| The entity or activity that causes the most abnormalities is printed.
 
|| The entity or activity that causes the most abnormalities is printed.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Highlight :
 
|| Highlight :
 
|| Followed by the period with the maximum amount of abnormalities is also printed.
 
|| Followed by the period with the maximum amount of abnormalities is also printed.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| Show the plot:
 
|| Show the plot:
 
|| Here we can see the boxplot of my syslog summary.
 
|| Here we can see the boxplot of my syslog summary.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
 
|| >Type: '''deactivate'''
 
|| >Type: '''deactivate'''
 
|| Type '''deactivate '''to exit the virtual environment.
 
|| Type '''deactivate '''to exit the virtual environment.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide:  
+
|| '''Show slide''':  
  
 
'''Summary'''
 
'''Summary'''
|| This brings us to the end of the tutorial. Let us summarize.
+
|| This brings us to the end of the tutorial.
 +
 
 +
Let us summarize.
  
 
In this tutorial, we have learnt about
 
In this tutorial, we have learnt about
* <div style="margin-left:1.27cm;margin-right:0cm;">System logs</div>
+
* System logs
* <div style="margin-left:1.27cm;margin-right:0cm;">How to monitor logs</div>
+
* How to monitor logs
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide:  
+
|| '''Show slide''':  
  
 
'''Assignment'''
 
'''Assignment'''
 
|| As an assignment, please do the following:
 
|| As an assignment, please do the following:
* <div style="margin-left:1.27cm;margin-right:0cm;">Scan the log file for occurrences of the word "ERROR" and "CRITICAL" .</div>
+
* Scan the log file for occurrences of the word "ERROR" and "CRITICAL" .
* <div style="margin-left:1.27cm;margin-right:0cm;">Extract the timestamp of each occurrence.</div>
+
* Extract the timestamp of each occurrence.
* <div style="margin-left:1.27cm;margin-right:0cm;"><span style="background-color:#ffffff;">Plot the frequency of errors by time using </span><span style="background-color:#ffffff;">'''matplotlib.'''</span></div>
+
* Plot the frequency of errors by time using '''matplotlib.'''
  
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide:  
+
|| '''Show slide''':  
  
 
'''About the Spoken Tutorial Project'''
 
'''About the Spoken Tutorial Project'''
|| <span style="background-color:#ffffff;">The video at the following link summarizes the </span><span style="background-color:#ffffff;">'''Spoken Tutorial project'''</span><span style="background-color:#ffffff;">.</span>
+
|| The video at the following link summarizes the '''Spoken Tutorial project'''.
  
 
Please download and watch it.
 
Please download and watch it.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show Slide:
+
|| '''Show slide''':  
  
<span style="background-color:#ffffff;">'''Spoken Tutorial </span><span style="background-color:#ffffff;">Workshops'''</span>
+
'''Spoken Tutorial Workshops'''  
|| <span style="background-color:#ffffff;">The </span><span style="background-color:#ffffff;">'''Spoken Tutorial Project'''</span><span style="background-color:#ffffff;"> team conducts workshops and gives certificates.</span>
+
|| The '''Spoken Tutorial Project''' team conducts workshops and gives certificates.
  
 
For more details, please write to us.
 
For more details, please write to us.
 
|-
 
|-
| style="border:0.75pt solid #808080;padding:0.176cm;" | Show Slide:
+
|| '''Show slide''':  
  
 
'''Answers for THIS Spoken Tutorial'''
 
'''Answers for THIS Spoken Tutorial'''
| style="border:0.75pt solid #808080;padding:0.176cm;" | Please post your timed queries in this forum.
+
|| Please post your timed queries in this forum.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show Slide:
+
|| '''Show slide''':  
  
 
'''FOSSEE Forum'''
 
'''FOSSEE Forum'''
|| <span style="background-color:#ffffff;">For any general or technical questions on </span>'''Python for'''
+
|| For any general or technical questions on '''Python for''' '''Automation''',
  
'''Automation'''<span style="background-color:#ffffff;">, visit the</span><span style="background-color:#ffffff;">''' FOSSEE forum'''</span><span style="background-color:#ffffff;"> and post your question.</span>
+
visit the''' FOSSEE forum''' and post your question.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide
+
|| '''Show slide''':
  
 
'''Acknowledgement'''
 
'''Acknowledgement'''
 
|| '''Spoken Tutorial''' Project was established by the '''Ministry of Education, Government of India'''.
 
|| '''Spoken Tutorial''' Project was established by the '''Ministry of Education, Government of India'''.
|- style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"
+
|-  
|| Show slide:
+
|| '''Show slide''':  
  
 
'''Thank You'''
 
'''Thank You'''
|| <span style="background-color:#ffffff;">This is </span><span style="background-color:#ffffff;">'''Dhaarani Pushpam S, '''</span><span style="background-color:#ffffff;">a FOSSEE Semester long intern 2024, IIT Bombay signing off.</span>
+
|| This is '''Dhaarani Pushpam S, '''a FOSSEE Semester long intern 2024, IIT Bombay signing off.
  
  

Latest revision as of 17:27, 3 December 2024

Visual Cue Narration
Show slide:

Welcome

Welcome to the Spoken Tutorial on "Log Monitoring ".
Show slide:

Learning Objectives

In this tutorial, we will learn about
  • System logs and
  • How to monitor logs
Show slide: To record this tutorial, I am using
  • Ubuntu Linux OS version 22.04
  • Python version 3.12.3
Show slide:

Prerequisite

https://spoken-tutorial.org

To follow this tutorial
  • You must have basic knowledge of using Linux Terminal and Python.
  • For pre-requisite Linux and Python Tutorials, please visit this website.
  • Python libraries required for automation must be installed.
Show slide:

Code files

  • The files used in this tutorial are provided in the Code files link.
  • Please download and extract the files.
  • Make a copy and then use them while practicing.
Show slide:

Log monitoring

Log Monitoring in automation:
  • Collects and analyzes log data for insights.
  • Record events, errors, and system messages.
  • Helps to detect issues before they become critical.
Show slide:

Libraries used

  • Logging library is used to log events during the execution of the program.
  • collections.Counter is used for counting elements in an iterable and for summarizing log data.
Show slide:

Types of logs

There are different types of log files.
  • Application Logs will record events within a running application.
  • System Logs captures the operating system events.
  • Security Logs will track access control and authorization events.
  • Audit Logs will analyze what is happening on your system in great detail.
Only narration: In this tutorial we will see how to monitor syslog files using Python code.
Open terminal(Ctrl + Alt + T) Open the terminal by pressing Control + Alt + T keys simultaneously.

Let us see how to access the log file in our system.

Type cd /var/log Type cd space forward slash var forward slash log.

Press Enter.

Type ls Let us list the files in this directory.Type ls and press Enter.
Highlight the output We can see that all the log files stored in the system are displayed here.
Only narration: For demonstration purpose we will work with the Syslog files.
Open syslog:Type nano syslog Type nano syslog to open the syslog file.
Show Syslog The syslog is a central log file in Ubuntu.

It records system messages, user activities, and errors.

Highlight: Timestamp Each log entry starts with a timestamp of when the event occurred.
Highlight: Host Next to the timestamp is the Host name of the machine where the event was recorded.
Highlight: Service/Program After the Host , is the Service/Program name of the system service generating the log.
Highlight: Message Finally we get the message - which is the details of the event or error.
Only narration In this tutorial, we will read this file and identify any abnormalities in the file.

We will generate a report based on the analysis.

Press ctrl + x Press ctrl + x to exit the file.
Download logMonitor.py Let us now look into the code file.

Download logMonitor.py from Code files.

Open logMonitor.py Open logMonitor.py file using text editor.
Highlight First we import necessary libraries.
Highlight:

logger = logging.getLogger(__name__)

logger.setLevel(logging.DEBUG)

A logger instance is created using logging.getLogger.

The log level is set to DEBUG.

This is to capture all messages, including lower-severity ones.

Highlight:

log_file = '/var/log/syslog'

Then we define the path to the log file.

Here I set it as the location of my syslog file.

You can change it according to your file path.

Highlight:

file_handler = logging.FileHandler(log_file)

A file handler is created using logging.FileHandler.

This handler writes log messages to the log file.

Highlight:

file_handler.setLevel(logging.DEBUG)

Next we set the file handler to capture DEBUG level messages and above.
Highlight:

file_handler.setFormatter(formatter)

We then define a format for log messages.

Each message includes time, the logger’s name, log level, and message itself.

Highlight:

logger.addHandler(file_handler)

Finally, the handler is added to the logger using logger.addHandler.
Highlight: The different logging levels are demonstrated here.

logger.debug() logs detailed info for debugging purposes.

logger.info() logs general operational events in the program.

logger.warning(), logger.error(), and logger.critical() log issues with increasing severity.

Highlight: Now define a function to check for errors or critical messages in the log.
Highlight:

abnormalities = []

Initialize an empty list to store abnormal log lines.
Highlight:

with open(log_file, 'r') as file:

Open the log file in read mode to analyze its content.
Point to for 'loop. Using a for loop, iterate through each line.

Check if the line contains 'ERROR' or 'CRITICAL' log levels.

These lines are considered abnormalities and are stored in a list.

Highlight:

return abnormalities

It returns a list of abnormal log entries
Highlight:

def summarize_log_file(log_file):

Next we define a function to summarize log messages by their levels.
Highlight: Initialize a dictionary to count occurrences of each log level.
Highlight :

with open(log_file, 'r') as file:

Open the log file in read mode for processing.
Highlight: Using a for loop, iterate through each line and count the occurrences of each log level.

The results are stored in a dictionary.

Highlight:

return log_data

Finally we return the dictionary summarizing log level counts.
Highlight: entity causing the most abnormalities. Now we define a function to find the entity causing the most abnormalities.
Highlight corresponding lines Extract and store the entity name from each abnormal log entry.

Counter library is used to count the occurrences of each entity.

Next we will find the entity that appears the most frequently in abnormalities.

Highlight:

return most_common_entity

Finally return the most frequent abnormal entity and its count.
Highlight:

Abnormalities_period.

Next we define a function to find the period with the most abnormalities.
Highlight:

Months

Extract the date from each abnormality entry and get only the year and month from the dates.
Highlight:

month_counts = Counter(months)

Using Counter we count occurrences of each month.
Then find the month with the most abnormalities using the most_common function.
Cursor near the function. Next we define a function to plot the log level data.
Cursor near the function. In this function we extract the log levels as a list from the log data dictionary.
Extract the counts for each log level.
Cursor on plt.figure Then using plt.figure create a figure for the plot with a specified size.
Point to the color palette. We plot a bar chart for the log levels with corresponding counts.

You can specify your own color palette here.

Point to x-axis label to 'Log Level' and y-axis label. Set the x-axis label to 'Log Level' and y-axis label to 'Count'
Highlight:

plt.title('Log Level Distribution')

Also set the title of the plot as 'Log Level Distribution'.
Highlight:

plt.show()

Then we display the plot using plt.show() .
Highlight:

abnormalities = check_abnormalities(log_file)

Next call the check_abnormalities function to find abnormal log entries.
Highlight:

abnormality_count = len(abnormalities)

Then calculate the total number of abnormalities found.

This is done by finding the length of the abnormalities dictionary.

Finally, print the total number of abnormalities found in the log file.
Next we call summarize_log_file to summarize the log data by log levels.
Point to for loop. Using a for loop, iterate through the log summary data.

Print the count for each log level.

We now call find_most_abnormal_entity to identify the most abnormal entity and print it.
Highlight: Then call the find_max_abnormalities_period to identify the period with the most abnormalities and print it.
Highlight:

plot_log_data(log_summary)

Finally we call plot_log_data to plot the summarized log data.
Only narration Now let us execute the code.
Only narration Save the code as logMonitor.py in the Downloads folder.
Open terminal(Ctrl + Alt + T) Open the terminal by pressing Control + Alt + T keys simultaneously.
>Type source Automation/bin/activate

Then press enter.

We’ll open the virtual environment for the Automation series.

Type source space Automation forward slash bin forward slash activate.

Then press Enter.

>Type cd Downloads Now type, cd Downloads.

Press Enter.

>Type sudo python3 logMonitor.py Let us run the code.

Type sudo space python3 space logMonitor dot py Press Enter.

Highlight :Total abnormalities found in log file: 179 We can see that the count of total abnormalities found in my log file is shown.
Highlight: Next the log summary is also printed which gives me the count of each log level.
Highlight : The entity or activity that causes the most abnormalities is printed.
Highlight : Followed by the period with the maximum amount of abnormalities is also printed.
Show the plot: Here we can see the boxplot of my syslog summary.
>Type: deactivate Type deactivate to exit the virtual environment.
Show slide:

Summary

This brings us to the end of the tutorial.

Let us summarize.

In this tutorial, we have learnt about

  • System logs
  • How to monitor logs
Show slide:

Assignment

As an assignment, please do the following:
  • Scan the log file for occurrences of the word "ERROR" and "CRITICAL" .
  • Extract the timestamp of each occurrence.
  • Plot the frequency of errors by time using matplotlib.
Show slide:

About the Spoken Tutorial Project

The video at the following link summarizes the Spoken Tutorial project.

Please download and watch it.

Show slide:

Spoken Tutorial Workshops

The Spoken Tutorial Project team conducts workshops and gives certificates.

For more details, please write to us.

Show slide:

Answers for THIS Spoken Tutorial

Please post your timed queries in this forum.
Show slide:

FOSSEE Forum

For any general or technical questions on Python for Automation,

visit the FOSSEE forum and post your question.

Show slide:

Acknowledgement

Spoken Tutorial Project was established by the Ministry of Education, Government of India.
Show slide:

Thank You

This is Dhaarani Pushpam S, a FOSSEE Semester long intern 2024, IIT Bombay signing off.


Thanks for joining.

Contributors and Content Editors

Madhurig, Nirmala Venkat