Difference between revisions of "OPS435 Assignment 2 for Section C"

From CDOT Wiki
Jump to: navigation, search
(Detail Algorithm Document)
(Daily Report From Online)
Line 277: Line 277:
==== Daily Report From Online ====
==== Daily Report From Online ====
Running the script with "online" as a file argument should call a subprocess.Popen object and run the command <code>last -Fiw</code>.
Running the script with <B>no filename</b> as a file argument should call a subprocess.Popen object and run the command <code>last -Fiw</code>.
[eric@mtrx-node06pd ~]$ ./a2.py -l user online
[eric@mtrx-node06pd ~]$ ./a2.py -l user
Line 305: Line 305:
[eric@mtrx-node06pd ~]$ ./a2.py -u adas20 -t daily online
[eric@mtrx-node06pd ~]$ ./a2.py -u adas20 -t daily

Revision as of 23:35, 24 March 2021

Assignment 2 - Usage Report

Weight: 15% of the overall grade

Due Date: Please follow the three stages of submission schedule:

  • Complete the requirements for the first milestone and push to GitHub by April 4, 2021 by 11:59 PM,
  • Complete the your Python script and push to GitHub by April 18, 2021 at 11:59 PM, and
  • Copy your Python script into a Word document and submit to Blackboard by April 18, 2021 at 11:59 PM.


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 > usage_data_file
$ cat usage_data_file
rchan    pts/9     Tue Feb 13 16:53:42 2018 - Tue Feb 13 16:57:02 2018  (00:03)    
cwsmith  pts/10    Wed Feb 14 23:09:12 2018 - Thu Feb 15 02:11:23 2018  (03:02)
rchan    pts/2     Tue Feb 13 16:22:00 2018 - Tue Feb 13 16:45:00 2018  (00:23)    
rchan    pts/5     Tue Feb 15 16:22:00 2018 - Tue Feb 15 16:55:00 2018  (00:33)    
asmith   pts/2    Tue Feb 13 16:19:29 2018 - Tue Feb 13 16:22:00 2018  (00:02)    
tsliu2   pts/4    Tue Feb 13 16:17:21 2018 - Tue Feb 13 16:30:10 2018  (00:12)    
cwsmith  pts/13     Tue Mar 13 18:08:52 2018 - Tue Mar 13 18:46:52 2018  (00:38)    
asmith   pts/11    Tue Feb 13 14:07:43 2018 - Tue Feb 13 16:07:43 2018  (02:00)

It is always desirable to have a daily, or monthly usage reports by user or by remote host based on the above information.

Tasks for this assignment

In this assignment, your should preform the following activities:

Allowed Python Modules


Accept the Assignment #2 via the link on Blackboard, and clone the Github repository on a Linux machine of your choosing. Your code should only be located in the file "assignment2.py".

Program Name and valid command line arguments

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:

[eric@centos7 a1]$ python3 ./assignment2.py -h
usage: new_template.py [-h] [-l {user,host}] [-r RHOST] [-t {daily,monthly}]
                       [-u USER] [-s] [-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
  -r RHOST, --rhost RHOST
                        usage report for the given remote host IP
  -t {daily,monthly}, --type {daily,monthly}
                        type of report: daily or monthly
  -u USER, --user USER  usage report for the given user name
  -s, --seconds         return times in seconds
  -v, --verbose         turn on output verbosity

Copyright 2021 - Eric Brauer

Replace the last line with your own full name.

Compare the usage output you have now with the one above. There is one option missing, you will need to change the argparse function to implement it.

You will that there is an 'args' object in assignment2.py. Once the parse_command_args() function is called, it will return an args object. The command line arguments will be stored as attributes of that object. Do not use sys.argv to parse arguments.

If there is only one file name provided at the command line, read the login/logout records from the contents of the given file. If the file name is "online", get the record on the system your script is being execute using the Linux command "last -iwF". The format of each line in the file should be the same as the output of 'last -Fiw'. Filter out incomplete login/logout record (hints: 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.


All your Python codes for this assignment 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 - Winter 2021
Program: assignment2.py
Author: "Student Name"
The python code in this file assignment2.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.

Use of Github

You will once again be graded partly on correct use of version control, that is use of numerous commits with sensible commit messages. In professional practice, this is critically important for the timely delivery of code. You will be expected to use:

  1. git add *.py
  2. git commit -m "a message that describes the change"
  3. git push after completing each step. There is no penalty for "too many commits", there is no such thing!

    Suggested Process

    1. Read the rest of this document, try and understand what is expected.
    2. Use the invite link posted to Blackboard to accept the assignment, and clone the repo to a Linux machine.
    3. Run the script itself. Investigate argparse. In the main block, print(args). Experiment with the various options.
    4. Read the usage output in the docs, what option must you implement? Go ahead and implement it. Commit the change.
    5. Investigate the parse_for_user() function, with the usage_data_file. In main, call `parse_for_user()` with `output` as the argument. Investigate what's returned.
    6. parse_for_user() should take the list of lines from the file, and instead return a list of usernames. In main, print the title header and the output. Commit the change.
    7. Once you have `output` --> `parse_for_user()` --> correct output being printed, use if conditions to print only when `-l user` is in the command line arguments.
    8. Continue committing these changes as your proceed.
    9. Implement the same things as parse_for_user but for parse_for_hosts. Use the user function to guide you.
    10. Compare your output with the output below.
    11. Write the `parse_for_daily()` function using the pseudocode given. This should be taking the list of lines from your file, and output a dictionary with start dates in DD/MM/YYYY format as the key and usage in seconds as the value.
    12. {'01/01/1980': 1200, '02/01/1980': 2400, '03/01/1980': 2200}
    13. Once your `parse_for_daily()` function works, call it with the argparse options, and display the contents.
    14. Write (or modify) a function to do the same for remote hosts.
    15. Implement the outputting of the duration in HH:MM:SS instead of seconds. It's recommended you write a function to take in seconds and return a string. Call this when the `-s` option is absent. Make sure this is working with remote hosts as well. You should now have x of y tests passing.
    16. Finally, implement the `--monthly` option. Create a new function and get it working. start with seconds, then duration and make sure it works with remote as well.
    17. Perform last checks and document your code. Write **why** your code is doing what it does, rather than **what** it's doing. You should have 100% of tests succeeding.

    Output Format

    The format of your log tables should be identical to the sample output below, in order to minimize test check error. The horizontal banner between title and data should be composed of equal signs (=), and be the length of the title string. List tables should need no extra formatting. For daily/montly tables with two columns, The first column should be 10 characters long and be left-aligned. The second column should be 15 characters long and be right-aligned.

    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. You can download the file here to test your ur.py script.

    User List

    The following is the user list extracted from the usage_data_file created by the command:

    [eric@centos7 a2]$ ./assignment2.py -l user usage_data_file
    User list for usage_data_file

    Remote Host List

    The following is the remote host list extracted from the usage_file_file created by the command:

    [eric@centos7 a2]$ ./assignment2.py -l host usage_data_file
    Host list for usage_data_file

    The Verbose Option

    Either of the following two tests can be modified with the --verbose option. You shouldn't have to do anything to get this working:

    [eric@centos7 a2]$ ./assignment2.py -l host -v usage_data_file
    Files to be processed: usage_data_file
    Type of args for files <class 'str'>
    Host list for usage_data_file

    List For Specific Day

    Specifying a --date in YYYY-MM-DD format should list all users or hosts that were logged in at some point during that date, even if their start time or end time is different. For example, user cwsmith logged in on Feb 14 and logged off on Feb 15, but they show up when the following command is run:

    [eric@centos7 a2]$ ./assignment2.py -l user -d 2018-02-14 usage_data_file
    User list for usage_data_file

    This should work for hosts as well:

    [eric@centos7 a2]$ ./assignment2.py -l host -d 2018-02-14 usage_data_file
    Host list for usage_data_file

    If the user types in an invalid date, the script should halt and print the following error message:

    [eric@centos7 a2]$ ./assignment2.py -l host -d 2018-02-xx usage_data_file
    Date not recognized. Use YYYY-MM-DD format.

    Daily Usage Report by User

    The following are Daily Usage Report is created for user rchan.

    [eric@centos a2]$ ./a2.py -u rchan -t daily usage_data_file
    Daily Usage Report for rchan
    Date                Usage
    13/02/2018        0:26:00
    15/02/2018        0:33:00
    Total             0:59:20

    Be also sure to test with the --verbose

    Daily Usage Report by Remote Host

    The following is a Daily Usage Report created for the Remote Host by the command:

    [eric@centos7 a2]$ ./a2.py -r -t daily usage_data_file
    Daily Usage Report for
    Date                Usage
    14/02/2018        3:02:11
    13/02/2018        2:12:49
    Total             5:15:00

    Weekly Usage Report by User

    The following is a Weekly Usage Report created for user cwsmith by the command:

    [eric@centos7 a2]$ ./assignment2.py -u cwsmith -t weekly usage_data_file 
    Weekly Usage Report for cwsmith
    Date                Usage
    2018 06           3:02:11
    2018 10           0:38:00
    Total             3:40:11

    Weekly Usage Report by Remote Host

    The following is a Weekly Usage Report created for the remote host by the command:

    [eric@centos7 a2]$ ./assignment2.py -r -t weekly usage_data_file
    Weekly Usage Report for
    Date                Usage
    2018 06           0:02:31
    Total             0:02:31

    Daily Report From Online

    Running the script with no filename as a file argument should call a subprocess.Popen object and run the command last -Fiw.

    [eric@mtrx-node06pd ~]$ ./a2.py -l user

    (Example Output from Matrix):

    User list for online
    [eric@mtrx-node06pd ~]$ ./a2.py -u adas20 -t daily
    Daily Usage Report for abholay
    Date                Usage
    16/07/2020       00:13:09
    17/07/2020       00:08:59
    Total            00:22:08

    First Milestone


    Python script coding and debugging

    For each function, identify what type of objects should be passed to the function, and what type of objects should be returned to the caller. Once you have finished coding a function, you should start a Python3 interactive shell, import your functions and manually test each function and verify its correctness.

    Final Test

    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. Upload all the files for this assignment 2 to your vm in myvmlab and perform the final test.


    Task Maximum mark Actual mark
    Algorithm Submission 10
    Check Script Results 30
    Additional Check: 'online' 5
    GitHub Use 15
    List Functions 5
    Daily/Monthly Functions 10
    Output Functions 5
    Other Functions 5
    Overall Design/Coherence 10
    Documentation 5
    Total 100


    • Stage 1: Complete the first milestone on GitHub by April 4, 2021.
    • Stage 2: Use commits to push your python script for this assignment to Github.com. The final state of your repository will be looked at on April 18, 2021 at 11:59 PM.
    • Stage 3: Copy your python script into a Word document and submit to Blackboard by April 18, 2021 at 11:59 PM.