Difference between revisions of "OPS435 Assignment 1 A"

From CDOT Wiki
Jump to: navigation, search
(Header)
 
(63 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:OPS435-Python]][[Category:rchan]]
 
[[Category:OPS435-Python]][[Category:rchan]]
<font color='red'>'''DRAFT - Under Construction'''</font>
+
=Assignment 1 - Usage Report=
=Assignment 1 - Users' Usage Report=
+
'''Weight:''' 15% of the overall grade
'''Weight:''' 20% of the overall grade
 
  
 
'''Due Date:''' Ask your professor for exact date
 
'''Due Date:''' Ask your professor for exact date
Line 9: Line 8:
  
 
==Overview==
 
==Overview==
Most system administrators would like to know the utilization of their systems by their users. On a Linux system, the users' 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 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. The following listing is a sample output of running the 'last' command:
+
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 record 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 listing is a sample output of the 'last' command with the '-F' flag on:
 
<pre>
 
<pre>
 
$ last -F
 
$ last -F
rchan   pts/0       :0              Mon Jan  8 22:40:02 2018 - Tue Jan  9 17:31:50 2018  (18:51)     
+
avhuo   pts/9       10.43.222.35    Tue Feb 13 16:53:42 2018 - Tue Feb 13 16:57:02 2018  (00:03)     
rchan    pts/0       :0              Mon Jan  8 21:47:32 2018 - Mon Jan  8 21:47:38 2018  (00:00)     
+
clee18  pts/6       10.40.43.94      Tue Feb 13 16:51:47 2018 - Tue Feb 13 16:56:13 2018  (00:04)     
rchan    :0          :0              Mon Jan  8 21:46:36 2018 - Tue Jan  9 17:36:18 2018  (19:49)     
+
clee18  pts/6        10.40.43.94      Tue Feb 13 16:50:20 2018 - Tue Feb 13 16:51:27 2018  (00:01)     
(unknown :0          :0              Mon Jan  8 20:25:13 2018 - Mon Jan  8 21:46:36 2018  (01:21)     
+
shana    pts/2        toroon4512w-lp13 Tue Feb 13 16:47:48 2018 - Tue Feb 13 16:58:31 2018  (00:10)     
reboot  system boot  3.10.0-514.10.2. Mon Jan  8 15:25:07 2018 - Fri Jan 12 01:31:18 2018 (3+10:06)  
+
hfang171 pts/4        24.114.50.50    Tue Feb 13 16:31:38 2018 - Tue Feb 13 17:48:39 2018 (01:17)  
rchan   :0          :0              Mon Jan  8 17:19:04 2018 - Mon Jan  8 17:34:29 2018  (00:15)     
+
bigia   pts/8       10.247.6.126    Tue Feb 13 16:28:43 2018 - Tue Feb 13 17:28:31 2018  (00:59)     
(unknown :0          :0              Mon Jan 8 16:48:02 2018 - Mon Jan  8 17:19:03 2018  (00:31)     
+
ckim16  pts/2        10.40.91.236    Tue Feb 13 16:22:36 2018 - Tue Feb 13 16:45:10 2018 (00:22)   
reboot   system boot  3.10.0-514.10.2. Mon Jan  8 11:47:56 2018 - Mon Jan  8 17:34:30 2018  (05:46)     
+
ddchan  pts/2        10.43.115.162    Tue Feb 13 16:19:29 2018 - Tue Feb 13 16:22:00 2018  (00:02)     
rchan   :0          :0              Mon Jan  8 13:55:51 2018 - Mon Jan  8 14:01:26 2018  (00:05)     
+
tsliu2   pts/4        10.40.105.130    Tue Feb 13 16:17:21 2018 - Tue Feb 13 16:30:10 2018  (00:12)     
(unknown :0          :0              Mon Jan  8 13:55:37 2018 - Mon Jan  8 13:55:51 2018  (00:00)     
+
shana   pts/13      10.40.91.247    Tue Feb 13 16:07:52 2018 - Tue Feb 13 16:45:52 2018  (00:38)     
 +
tchiu2  pts/11      10.40.105.130    Tue Feb 13 16:07:43 2018 - Tue Feb 13 16:17:12 2018  (00:09)  
 +
    
 
...
 
...
 
</pre>
 
</pre>
  
In this assignment, your task is to create a python program with well designed functions to produce daily, weekly, and monthly usage report by user (and/or by other criteria) based on the information obtained from the 'last' command.
+
In this assignment, your task is to create a python program to produce 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.
  
 +
== Resource for useful modules ==
 +
* the <b>argparse</b> module
 +
** [https://docs.python.org/3/howto/argparse.html Argparse Tutorial] - should read this first.
 +
** [https://docs.python.org/3/library/argparse.html Argparse API reference information page]
 +
* the <b>time</b> module
 +
** [https://www.tutorialspoint.com/python3/python_date_time.htm Time module Tutorial]
 +
** [https://docs.python.org/3/library/time.html Time module API reference page]
 +
 
 
== Instructions ==
 
== Instructions ==
  
 
=== Program Name and valid command line arguments ===
 
=== Program Name and valid command line arguments ===
Name your Python3 program as <code>usage_report.py</code>. The program should accept the following command line parameters:
+
Name your Python3 program as <code>ur.py</code>. The program should accept one or more "file name" as its command line parameters and other optional parameters as shown below. Your ur.py script should produce the following usage when run with the --help option:
* <b><code>--user</code></b> or <b><code>-u</code></b> as an optional first argument, followed by:
+
<pre>
* <b><code>filename</code></b>, or
+
[rchan@centos7 a1]$ python3 ./ur.py -h
* <b><code>filename1 filename2 </code></b> etc ... - any number of filenames from 1 to as many as the command-line supports.
+
usage: ur.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
 +
</pre>
 +
Replace the last line with your own full name
  
If the first argument is <b><code>--user</code></b> or <b><code>-u</code></b>, then all the usage report to be generated should be based on user only.
 
  
If there is no filename provided at the command line, your program should get the login records from the 'last' command with the appropriate flags.
+
If one of the file name given is "last", your python program should get the login records from the 'last' command with the appropriate flags.
  
If there is more than one filename provided, merged 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.
+
If there is one file name provided at the command line, read the login 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'.
 +
 
 +
If there is more than one file name provided, merges 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 ===
 
=== Header ===
  
All your Python codes for this program must be placed in a single source file. Please include the following declaration by you as comment in your Python source code file (replace "Student Name" with your own name):
+
All your Python codes for this program must be placed in a <font color='red'><b><u>single source file</u></b></font>. Please include the following declaration by <b><u>you</u></b> as comment in your Python source code file (replace "Student Name" with your own name):
  
<source>OPS435 Assignment 1 - Winter 2018
+
<source>OPS435 Assignment 1 - Summer 2018
Program: usage_report.py
+
Program: ur.py
 
Author: "Student Name"
 
Author: "Student Name"
The python code in this file (usage_report.py) is original work written by
+
The python code in this file (ur.py) is original work written by
 
"Student Name". No code in this file is copied from any other source  
 
"Student Name". No code in this file is copied from any other source  
 
including any person, textbook, or on-line resource. I have not shared
 
including any person, textbook, or on-line resource. I have not shared
Line 56: Line 89:
 
</source>
 
</source>
  
=== Main Menu ===
 
  
=== Daily Usage Menu ===
+
==== Daily Usage Report by User ====
 +
The following is a sample Daily Usage Report for user1 on host.cty.senecacollege.ca:
 +
<pre>
 +
Daily Usage Report for user1
 +
============================
 +
Date          Usage in seconds
 +
2018 02 14          60
 +
2018 02 07          45
 +
2018 02 01          5
 +
 
 +
Total              110
 +
</pre>
 +
 
 +
==== 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:
 +
 
 +
<pre>
 +
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
 +
</pre>
 +
 
 +
==== Weekly Usage Report by User ====
 +
The following is a sample Weekly Usage Report for user1 on host.cty.senecacollege.ca:
 +
 
 +
<pre>
 +
Weekly Usage Report for user1
 +
=============================
 +
Week #        Usage in Seconds
 +
2017 01            160
 +
2017 02            45
 +
2017 03              5
 +
2017 04            20
 +
 
 +
Total              220
 +
</pre>
 +
 
 +
==== 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:
 +
 
 +
<pre>
 +
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
 +
</pre>
 +
 
 +
==== Monthly Usage Report by User ====
 +
The following is a sample Monthly Usage Report for user1 on host.cty.senecacollege.ca:
 +
<pre>
 +
Monthly Usage Report for user1
 +
==============================
 +
Month        Usage in Seconds
 +
2017 01            160
 +
2017 02            45
 +
 
 +
Total              205
 +
</pre>
 +
 
 +
==== Monthly Usage Report by Remote Host ====
 +
The following is a sample Monthly Usage Report for 10.94.10.25 on host.cty.senecacollege.ca:
 +
 
 +
<pre>
 +
Monthly Usage Report from 10.94.10.25
 +
=====================================
 +
Month        Usage in Seconds
 +
2017 01            30
 +
2017 02            41
 +
 
 +
Total              71
 +
</pre>
 +
 
 +
== Program Development and Testing ==
 +
You should create python functions to handle 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 or by remote host
 +
* generate weekly usage reports by user or by remote host
 +
* generate monthly usage reports by user or by remote host
 +
To  help you with this assignment, [[OPS435 A1 Usage Report Template|here is a template]] you can use as a starting point in developing your Python Usage Report program.
 +
 
 +
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.
 +
 
 +
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 program produces the correct results before submitting your python program on Blackboard.
 +
 
 +
== Sample login records file and sample run ==
 +
* [https://scs.senecac.on.ca/~raymond.chan/ops435/a1/ Sample test date files and sample runs]
  
=== Weekly Usage Menu ===
+
== Rubric ==
  
=== Monthly Usage Menu ===
+
{| class="wikitable" border="1"
 +
! 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 ||
  
== Sample login records file ==
+
|}
  
 
== Submission ==
 
== Submission ==
After fully testing your program, submit the .py file on Blackboard
+
After fully testing your program, submit your  ur.py file on Blackboard

Latest revision as of 06:46, 7 June 2018

Assignment 1 - Usage Report

Weight: 15% of the overall grade

Due Date: Ask your professor for exact date

Late Penalty: 10% 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 record 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 listing is a sample output of the 'last' command with the '-F' flag on:

$ last -F
avhuo    pts/9        10.43.222.35     Tue Feb 13 16:53:42 2018 - Tue Feb 13 16:57:02 2018  (00:03)    
clee18   pts/6        10.40.43.94      Tue Feb 13 16:51:47 2018 - Tue Feb 13 16:56:13 2018  (00:04)    
clee18   pts/6        10.40.43.94      Tue Feb 13 16:50:20 2018 - Tue Feb 13 16:51:27 2018  (00:01)    
shana    pts/2        toroon4512w-lp13 Tue Feb 13 16:47:48 2018 - Tue Feb 13 16:58:31 2018  (00:10)    
hfang171 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        10.247.6.126     Tue Feb 13 16:28:43 2018 - Tue Feb 13 17:28:31 2018  (00:59)    
ckim16   pts/2        10.40.91.236     Tue Feb 13 16:22:36 2018 - Tue Feb 13 16:45:10 2018  (00:22)    
ddchan   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)    
shana    pts/13       10.40.91.247     Tue Feb 13 16:07:52 2018 - Tue Feb 13 16:45:52 2018  (00:38)    
tchiu2   pts/11       10.40.105.130    Tue Feb 13 16:07:43 2018 - Tue Feb 13 16:17:12 2018  (00:09)    
   
...

In this assignment, your task is to create a python program to produce 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.

Resource for useful modules

Instructions

Program Name and valid command line arguments

Name your Python3 program as ur.py. The program should accept one or more "file name" as its command line parameters and other optional parameters as shown below. Your ur.py script should produce the following usage when run with the --help option:

[rchan@centos7 a1]$ python3 ./ur.py -h
usage: ur.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 one of the file name given is "last", your python program should get the login records from the 'last' command with the appropriate flags.

If there is one file name provided at the command line, read the login 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'.

If there is more than one file name provided, merges 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 comment in your Python source code file (replace "Student Name" with your own name):

OPS435 Assignment 1 - Summer 2018
Program: ur.py
Author: "Student Name"
The python code in this file (ur.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. I have not shared
this python program 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.


Daily Usage Report by User

The following is a sample Daily Usage Report for user1 on host.cty.senecacollege.ca:

Daily Usage Report for user1
============================
Date          Usage in seconds
2018 02 14          60
2018 02 07          45
2018 02 01           5

Total              110

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

Program Development and Testing

You should create python functions to handle 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 or by remote host
  • generate weekly usage reports by user or by remote host
  • generate monthly usage reports by user or by remote host

To help you with this assignment, here is a template you can use as a starting point in developing your Python Usage Report program.

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.

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 program 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 ur.py file on Blackboard