OPS435 Python Assignment 2
Contents
- 1 Assignment 2 - Usage Report
Assignment 2 - Usage Report
Weight: 15% of the overall grade
Due Date: Ask your professor for exact date
Late Penalty: 20% per school day, and note that this assignment must be completed satisfactorily in order to pass the course even if you get zero mark for this assignment.
Overview
Most system administrators would like to know the utilization of their systems by their users. On a Linux system, each user's login records are normally stored in the binary file /var/log/wtmp. The login records in this binary file can not be viewed or edited directly using normal Linux text commands like 'less', 'cat', etc. The 'last' command is often used to display the login records stored in this file in a human readable form. Please check the man page of the 'last' command for available options. The following is the contents of the file named "usage_data_file", which is a sample output of the 'last' command with the '-Fiw' flag on:
$ last -Fiw rchan pts/9 10.40.91.236 Tue Feb 13 16:53:42 2018 - Tue Feb 13 16:57:02 2018 (00:03) cwsmith pts/6 10.40.43.94 Tue Feb 13 16:51:47 2018 - Tue Feb 13 16:56:13 2018 (00:04) mlee18 pts/6 10.40.43.94 Tue Feb 13 16:50:20 2018 - Tue Feb 13 16:51:27 2018 (00:01) hfang pts/4 24.114.50.50 Tue Feb 13 16:31:38 2018 - Tue Feb 13 17:48:39 2018 (01:17) bigia pts/8 24.114.50.50 Tue Feb 13 19:28:43 2018 - Tue Feb 13 20:28:31 2018 (00:59) rchan pts/2 10.40.91.236 Tue Feb 13 16:22:00 2018 - Tue Feb 13 16:45:00 2018 (00:23) asmith pts/2 10.43.115.162 Tue Feb 13 16:19:29 2018 - Tue Feb 13 16:22:00 2018 (00:02) tsliu2 pts/4 10.40.105.130 Tue Feb 13 16:17:21 2018 - Tue Feb 13 16:30:10 2018 (00:12) mshana pts/13 10.40.91.247 Tue Feb 13 16:07:52 2018 - Tue Feb 13 16:45:52 2018 (00:38) asmith pts/11 10.40.105.130 Tue Feb 13 14:07:43 2018 - Tue Feb 13 16:07:43 2018 (02:00)
In this assignment, your duty is to
- work out an algorithm for producing daily, weekly, and monthly usage reports by user or by remote host based on the information obtained from the 'last' command or given files which contain similar information as the 'last' command.
- Once you believe you have a workable algorithm, you should then identify the different tasks in your algorithm and any repeated sub-tasks,
- implement your solution using a python script with the proper use of functions, and finally
- review your working python code to see whether you can improve the interface of each function and facilitate better core re-use (this process is called refactoring).
Modules you can use in your script for this assignment
- the os, sys modules
- the argparse module
- Argparse Tutorial - should read this first.
- Argparse API reference information page
- the time module
Instructions
Program Name and valid command line arguments
Name your Python3 script as ur_[student_id].py
. Create a symbolic link to your script as ur.py (e.g. use the command ln -s ur_rchan.py ur.py to create the line) so that you can refer to your script as ur.py. Your script must accept one or more "file name" as its command line parameters and other optional parameters as shown below. Your python script should produce the following usage text when run with the --help option:
[rchan@centos7 a1]$ python3 ./ur.py -h usage: ur_rchan.py [-h] [-l {user,host}] [-r RHOST] [-t {daily,weekly,monthly}] [-u USER] [-v] F [F ...] Usage Report based on the last command positional arguments: F list of files to be processed optional arguments: -h, --help show this help message and exit -l {user,host}, --list {user,host} generate user name or remote host IP from the given files -r RHOST, --rhost RHOST usage report for the given remote host IP -t {daily,weekly,monthly}, --type {daily,weekly,monthly} type of report: daily, weekly, and monthly -u USER, --user USER usage report for the given user name -v, --verbose tune on output verbosity Copyright 2018 - Raymond Chan
Replace the last line with your own full name
If there is one file name provided at the command line, read the login/logout records from the contents of the given file. The format of each line in the file should be the same as the output of 'last -Fiw'. Filter out incomplete login/logout record (check for the number of fields in each record).
If there is more than one file name provided, merge all the files together with the first one at the top and the last one at the bottom. Read and process the file contents in that order in your program.
Header
All your Python codes for this program must be placed in a single source file. Please include the following declaration by you as the script level docstring in your Python source code file (replace [Student_id] with your Seneca email user name, and "Student Name" with your own name):
OPS435 Assignment 2 - Fall 2018
Program: ur_[Stduent_id].py
Author: "Student Name"
The python code in this file ur_[Student_id].py is original work written by
"Student Name". No code in this file is copied from any other source
including any person, textbook, or on-line resource except those provided
by the course instructor. I have not shared this python file with anyone
or anything except for submission for grading.
I understand that the Academic Honesty Policy will be enforced and violators
will be reported and appropriate action will be taken.
Sample outputs
The following are the reports generated by the usage report script (ur.py) with the "usage_data_file" mentioned in the overview section.
Daily Usage Report by User
The following is a Daily Usage Report created by the following command:
[rchan@centos7 a2]$ ./ur.py -u rchan -t daily usage_data_file
Daily Usage Report for rchan ============================ Date Usage in Seconds 2018 02 13 1580 Total 1580
Daily Usage Report by Remote Host
The following is a sample Daily Usage Report from Remote Host 10.94.10.25 on host.cty.senecacollege.ca:
Daily Usage Report from 10.94.10.25 =================================== Date Usage in seconds 2018 02 14 30 2018 02 07 41 2018 02 01 9 Total 80
Weekly Usage Report by User
The following is a sample Weekly Usage Report for user1 on host.cty.senecacollege.ca:
Weekly Usage Report for user1 ============================= Week # Usage in Seconds 2017 01 160 2017 02 45 2017 03 5 2017 04 20 Total 220
Weekly Usage Report by Remote Host
The following is a sample Weekly Usage Report from the remote host 10.94.10.25 on host.cty.senecacollege.ca:
Weekly Usage Report from 10.94.10.25 ==================================== Week # Usage in Seconds 2017 01 30 2017 03 41 2017 05 9 2017 06 11 Total 91
Monthly Usage Report by User
The following is a sample Monthly Usage Report for user1 on host.cty.senecacollege.ca:
Monthly Usage Report for user1 ============================== Month Usage in Seconds 2017 01 160 2017 02 45 Total 205
Monthly Usage Report by Remote Host
The following is a sample Monthly Usage Report for 10.94.10.25 on host.cty.senecacollege.ca:
Monthly Usage Report from 10.94.10.25 ===================================== Month Usage in Seconds 2017 01 30 2017 02 41 Total 71
Suggested Script Development and Testing
The following python functions (to be created) are useful in handling the following sub-tasks:
- gets login records from the output of "last" command and filters out unwanted records
- reads login records from files and filters out unwanted records
- generate daily usage reports by user and/or by remote host
- generate weekly usage reports by user and/or by remote host
- generate monthly usage reports by user and/or by remote host
To help you with this assignment, here is a template you can use as a starting point in developing your own Python Usage Report script.
Once you have finished coding a function, you should start a Python3 interactive shell, import your functions and manually call the function you want to test under interactive mode.
Once you have all the individual function tested and that each is working properly, perform the final test with test data provided by your professor and verify that your script produces the correct results before submitting your python program on Blackboard.
Sample login records file and sample run
Rubric
Task | Maximum mark | Actual mark |
---|---|---|
Program Authorship Declaration | 1 | |
Program usage | 2 | |
Program Options | 2 | |
Daily Usage Report by User | 2 | |
Daily Usage Report by Remote Host | 2 | |
Weekly Usage Report by User | 2 | |
Weekly Usage Report by Remote Host | 2 | |
Monthly Usage Report by User | 2 | |
Monthly Usage Report by Remote Host | 2 | |
Get Login Records from 'last' | 2 | |
Get Login Records from files | 2 | |
Generate user name list | 2 | |
Generate remote host IP list | 2 | |
Total | 25 |
Submission
After fully testing your program, submit your python file on Blackboard