Module 7: Final Project

Spread the love

INTRODUCTION – Final Project

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? 

  • /csv_to_html.py error_message.csv /var/www/html/<errors.html>.html
  • /csv_convert_html.py error_message.csv /var/www/html/<errors>.html
  • ./csv_to_html.py error_message.csv /var/www/html/<errors>.html (CORRECT)
  • /html_to_csv.py error_message.csv /var/www/html/<errors>.html

Correct

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))
  •  [('apples', 5), ('bananas', 7), ('oranges', 3), (peaches, 2)]
  • sorted fruit = {"oranges": 3, "apples": 5, "bananas": 7, "peaches": 2}
  • [(peaches, 2), ('oranges', 3), ('apples', 5), ('bananas', 7)] (CORRECT)
  • [('bananas', 7), ('apples', 5), ('oranges', 3), (peaches, 2)]

Correct

13. Which of the following is a potential pitfall of automation in Python? 

  • It increases the amount of manual work required
  • It makes the system more prone to errors
  • It improves the overall efficiency of the system
  • It limits the system’s ability to adapt to changes (CORRECT)

Correct

14. Why are regular expressions useful? 

  • They allow us to search and manipulate text based on patterns (CORRECT)
  • They allow us to create graphical user interfaces
  • They allow us to perform mathematical operations
  • They allow us to connect to databases

Correct

15. What is the method used to match a string stored in a line variable by defining a pattern? 

  • re.search() (CORRECT)
  • re.match()
  • re.find()
  • re.string()

Correct

16. What is the primary purpose of using regular expressions in log analysis? 

  • To encrypt log data for enhanced security
  • To compress log files and save storage space
  • To extract specific patterns and information from unstructured log data (CORRECT)
  • To format log messages for easier readability

Correct

17. Which of the following is true about using regular expressions? 

  • They reduce the need of error checking in code
  • They don’t allow for flexible pattern matching in strings
  • They can simplify complex string processing tasks (CORRECT)
  • They can be used only in Python

Correct

18. Why do you need to know how to write automation scripts that process a system log and generate reports from the log files? 

 
  • To increase job complexity without adding tangible value to the organization
  • To eliminate the need for manual log analysis, saving time, and improving efficiency (CORRECT)
  • To showcase proficiency in scripting languages without practical applications
  • To impress colleagues with technical skills and programming expertise

Correct

19. What would you expect the command grep “ERROR” syslog.log to return?

  • All ERROR logs in syslog.log (CORRECT)
  • The ERROR messages in syslog.log 
  • All logs in syslog.log
  • All ERROR logs in syslog.log that have no corresponding error message

Correct

20. Which argument can be used with the sorted() function to sort a dictionary’s items based on their values in descending order? 

  • sorted(dictionary, reverse=True)
  • sorted(dictionary, key=lambda x: x[1], reverse=True)
  • sorted(dictionary.values(), reverse=True)
  • sorted(dictionary.items(), key=lambda x: x[1], reverse=True) (CORRECT)

Correct

21. If there is no csv file named user_emails.csv, what will the command nano user_emails.csv return? 

  • A new csv file named user_emails.csv (CORRECT)
  • A new csv file named nano user_emails.csv 
  • An error message
  • A new csv file named user_emails.csv populated withusers emails

Correct

22. What will the following command return?

grep "ERROR" syslog.log
  • A duplicate file of syslog with the errors removed
  • All the ERROR logs in syslog (CORRECT)
  • All the INFO logs in syslog
  • All the usernames in syslog

Correct

23. When sorting this dictionary:

fruit = {"oranges": 3, "apples": 5, "bananas": 4, "pears": 2}

What will the following line of code return?

sorted(fruit.items(), key=operator.itemgetter(1))
  •  [('bananas', 4), ('apples', 5), ('oranges', 3), ('pears', 2)]
  • sorted fruit = {"oranges": 3, "apples": 5, "bananas": 4, "pears": 2}
  • [('apples', 5), ('bananas', 4), ('oranges', 3), ('pears', 2)]
  • [('pears', 2), ('oranges', 3), ('bananas', 4), ('apples', 5)] (CORRECT)

Correct

24. When sorting this dictionary:

fruit = {"oranges": 3, "apples": 5, "bananas": 7, "pears": 2}

What will the following line of code return?

sorted(fruit.items(), key=operator.itemgetter(1))
  • [('bananas', 7), ('apples', 5), ('oranges', 3), ('pears', 2)]
  • [('pears', 2), ('oranges', 3), ('apples', 5), ('bananas', 7)] (CORRECT)
  • [('apples', 5), ('bananas', 7), ('oranges', 3), ('pears', 2)]
  • sorted fruit = {"oranges": 3, "apples": 5, "bananas": 7, "pears": 2}

Correct

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.

Leave a Comment