In this module, you are going to apply all things you have learned thus far by way of a rather challenging final assignment: Writing a script that scans log files for a particular error. The first step is formulating a problem statement that clarifies the problem; then follow up by researching available options to explore. Then you will plan on how to approach solving the problem before proceeding to write the actual code for your solution.
Learning Objectives:
To use frameworks and best practices in problem approach
Construct a problem statement to better understand the inputs and outputs for a script
Prepare a structured plan for the problem
Develop a lengthy script that addresses the problem.
LOG ANALYSIS USING REGULAR EXPRESSIONS
1. What will the following command return?
grep "ERROR Tried to add information to closed ticket" syslog.log
A duplicate file of syslog with the “Tried to add information to closed ticket” errors removed
All the closed tickets in syslog
All the ERROR logs in which the system tried to add information to closed ticket (CORRECT)
All the ERROR logs in syslog
Correct
2. You can reverse the order of the sort using the reverse parameter. What type of argument does the reverse parameter take?
Operator
Sort
Boolean (CORRECT)
Values
Correct
3. What is the primary advantage of using regular expressions when writing automation scripts to process a system log and generate reports from log files?
Flexible pattern matching for extracting specific data from log entries (CORRECT)
Simplify the process of creating log files
Automate the installation of log analysis software
Enhance the visual presentation of log data in reports
Correct
4. While you were working with the log file named syslog.log, what command did you use to view the file?
cat file syslog.log
grep syslog.log
cat syslog.log (CORRECT)
search syslog.log
Correct
5. What would you expect the command grep “ERROR Ticket doesn’t exist” syslog.log to return?
All ERROR logs in syslog.log with the error message “Ticket doesn’t exist” (CORRECT)
All ERROR logs in syslog.log
All logs in syslog.log except the ones with the error message “Ticket doesn’t exist”
All logs in syslog.log that do not have an existing ticket
Correct
6. What is the Python module used to perform similar tasks to the Unix command grep for filtering log data?
logfilter module
re (Regular Expression) module (CORRECT)
logsearch module
grep module
Correct
7. Evaluate the following problem statement: “I want to create a script to sort files.” What’s missing?
The problem statement does not specify what the script is supposed to do.
The problem statement does not specify the programming language.
The problem statement is complete.
The problem statement does not specify what files to sort. (CORRECT)
Correct
8. Which of the following commands would convert a csv file named error_message.csv into HTML file named errors.html?
9. Once you’ve understood the problem statement, what should be the second step for your coding project?
Planning
Writing the code
Researching available tools (CORRECT)
Writing a design document
Right on! The first thing to do would be to find the best way to approach the problem using tools such as the Python Standard Library which itself will yield functions and modules for the efficient performance of tasks like file reading, pattern matching, and error logging.
10. Which task can you accomplish by using regular expressions in log analysis?
Parsing log entries to extract specific fields (CORRECT)
Sorting log entries based on timestamps
Counting the total number of log entries in a file
Converting log data into graphical charts
Correct
11. Complete the sentence for the following Python regular expression: To match a string stored in a line variable, we use the search() method by defining a_____.
span
line
pattern (CORRECT)
log
Correct
12. When sorting this dictionary:
fruit = {"oranges": 3, "apples": 5, "bananas": 7, "peaches": 2}
What will the following line of code return?
sorted(fruit.items(), key=operator.itemgetter(1))
25. What syntax would you use to enlist all the ERROR messages of a specific kind?
grep ERROR [file-name] [message]
grep [file-name] [message] ERROR
grep ERROR [message] [file-name] (CORRECT)
grep [file-name] ERROR [message]
Correct
26. In Python, regular expressions are typically handled using which module?
sys
math
re (CORRECT)
os
Correct
27. How does the sorted() function sort items in a Python dictionary?
Sorts dictionary keys in descending order and returns a list of keys
Sorts dictionary keys in ascending order and returns a list of keys (CORRECT)
Sorts dictionary items based on their keys in ascending order and returns a list of items
Sorts dictionary items based on their values in ascending order and returns a list of items
Correct
28. If there is no python script named ticky_check.py, what will the command nano ticky_check.py return?
A new python script named nano ticky_check.py
An error message
A new csv file named ticky_check.py
A new python script named ticky_check.py (CORRECT)
Correct
CONCLUSION – Final Project
This is an empowering part of your learning because it will provide you with an opportunity to transform your learning into practice by undertaking a culminating project at the end. You will use your scripting skills in the challenge of developing a script that scans log files for specific errors.
First, you will draft a precise problem definition to understand the requirement and then investigate thoroughly to find possible avenues of action. Having formulated a strategy, you will embark on solving the problem with the final implementation through coding solutions.