Changes

Jump to: navigation, search

OPS435 Assignment 2 for Section C

2,075 bytes added, 16:04, 14 April 2021
Weekly Usage Report by Remote Host
== Tasks for this assignment ==
In this assignment, your you will create a script that can generate usage reports based off of output from the last command or from a file in a similar format. You will use usage_data_file for testing but the script should preform the following activities:also be tested on some other Linux machines and on Matrix.
Depending on the options selected, your script should list users or remote IP addresses, either overall or limited by a specific date. It should also generate daily usage reports for specific users/remote hosts, or weekly usage reports as well.
== Allowed Python Modules ==
* the <b>os, sys</b> modules
* the <b>argparse</b> module
* The <b>datetime</b> module
* The <b>subprocess</b> module
** === Argparse ===Argparse is a much, much better way of dealing with command line arguments. It not only handles positional arguments, but options as well and will generate usage messages.It's <i>very highly recommended</i> that you spend at least a few minutes reading through the [https://docs.python.org/3/howto/argparse.html Argparse Tutorial] . === Datetime ===Since Python is a <i>batteries</i> included language, it's important to get accustomed with using some of the modules in the standard library. Since we are dealing with dates and times, you are required to work with the datetime module. The full docs can be found [https://docs.python.org/3/library/datetime.html here]. Datetime objects can be initialized from strings that match a particular format. One example is provided for you in the codebase: [https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime the strptime] function. Once you have created datetime objects, you can do useful things with them: * <code> d1 > d2 </code> will return True is d1 is <b>later</b> than d2.* <code> d2 - should read d1 </code> will return a <i>timedelta</i> object, which is an amount of time between d2 and d1. More interesting methods are in the [https://docs.python.org/3/library/datetime.html#datetime.datetime.fold table] in the docs. === Timedelta ===When performing some operations with datetime objects, we may see timedelta objects being created. In math, <b>delta</b> refers to a difference. So a timedelta object is essentially an object that represents a duration. You can do useful things with timedelta objects:* <code> delta1 + delta2 </code> will add up two durations. For example, if delta1 is two hours and delta2 is three hours, then this firstoperation will return five hours.** <code> str(delta1) </code> will represent the timedelta in a friendly format: H:MM:SS if the duration is less than 24 hours.  More interesting methods are in the [https://docs.python.org/3/library/argparsedatetime.html Argparse API reference information page#datetime.datetime.fold table]in the docs.
== Instructions ==
You will that there is an 'args' object in assignment2.py. Once the <code>parse_command_args()</code> function is called, it will return an args object. The command line arguments will be stored as attributes of that object. <b>Do not use sys.argv to parse arguments.</b>
If there is only one a file name provided at the command line, read the login/logout records from the contents of the given file. If the there is not 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.
=== Header ===
You will once again be graded partly on <b>correct use of version control</b>, 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:
<ol>
<li><code>git add *assignment2.py</code>
<li><code>git commit -m "a message that describes the change"</code>
<li><code>git push</code>
<li> Run the script itself. Investigate argparse. <b>In the main block, print(args).</b> Experiment with the various options.
<li> Read the usage output in the docs, what option must you implement? Go ahead and implement it. <b>Commit the change.</b>
<li> Use the check script to check your work: <code>./checkA2.py -f -v TestHelp</code>. It should succeed.<li> Investigate the <code>parse_for_user()</code> function, with the <code>usage_data_file</code>. <b>In main, call `parse_for_user()` with `output` as the argument. Investigate what's returned.</b>
<li> <code>parse_for_user()</code> should take the list of lines from the file, and instead return a list of usernames. <b>In main, print the title header and the output. Commit the change.</b>
<li> <b>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.</b>
<li> Test using <bcode>Continue committing these changes as your proceed./checkA2.py -f -v TestList</bcode><li> Implement . You should see some tests succeeding, but some failing. Use the check script to start implementing the same things as parse_for_user but functions needed for <codeb>parse_for_hosts-l host</codeb>. Use the user function to guide you. <li> Compare <b>Continue committing these changes as your output with proceed.</b> Your script should now be passing the output belowTestList tests.<li> Write Now implement the `parse_for_daily()` function using the pseudocode given-d <date> option. This should be taking the will filter your user list of lines from your file, and output a dictionary with start dates in DD/MM/YYYY format as based on the key and usage in seconds as date provided by the valueuser.<li> Use <code> {'01./01/1980': 1200, '02/01/1980': 2400, '03/01/1980': 2200} checkA2.py -f -v TestDate</code>to check your work. <lib> Once your `parse_for_daily()` function works, call it with You have completed the argparse options, and display the contents.first milestone!</b><li> Write (or modify) a function The next stage will be to do implement the same for remote hostsdaily/weekly reports.Use <code>TestDaily<li/code> 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.<licode>TestWeekly</code> Finally, implement with the `--monthly` option. Create a new function and get it working. start with seconds, then duration and make sure it works with remote as wellcheck script.
<li> 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.
</ol>
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.  <pre>Daily Usage Report for rchan < same number of characters============================ <Date Usage < right justified13/02/2018 0:26:00 <15/02/2018 0:33:00Total 0:59:20llllllllllrrrrrrrrrrrrrrr < left column is 10 chars, right column is 15^left just.</pre>
=== 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 [https://scs.senecac.on.ca/~raymond.chan/ops435/a2/usage_data_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:
<pre>
[eric@centos a2]$ ./a2assignment2.py -u rchan -t daily usage_data_file
</pre>
The following is a Daily Usage Report created for the Remote Host 10.40.105.103 by the command:
<pre>
[eric@centos7 a2]$ ./a2assignment2.py -r 10.40.105.130 -t daily usage_data_file
</pre>
<pre>
Weekly Usage Report for 10.4340.115105.162130
=====================================
Date Usage
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>.
<pre>
[eric@mtrx-node06pd ~]$ ./a2assignment2.py -l user
</pre>
<pre>
[eric@mtrx-node06pd ~]$ ./a2assignment2.py -u adas20 -t daily
</pre>
=== First Milestone ===
TODOBy April 4, you should have your <code>TestHelp</code>, <code>TestList</code> and <code>TestDate</code> tests all passing. Make sure that the code is in your GitHub repository. I will use a pull request comment to give feedback, suggest changes or get you unstuck.
=== Python script coding and debugging ===
! Task !! Maximum mark !! Actual mark
|-
| Algorithm Submission First Milestone || 10 ||
|-
| Check Script Results || 30 ||
| List Functions || 5 ||
|-
| Daily/Monthly Weekly Functions || 10 ||
|-
| Output Date Functions || 5 ||
|-
| Other Output/other Functions || 5 ||
|-
| Overall Design/Coherence || 10 ||

Navigation menu