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 10.40.91.236 Tue Feb 13 16:53:42 2018 - Tue Feb 13 16:57:02 2018 (00:03) cwsmith pts/10 10.40.105.130 Wed Feb 14 23:09:12 2018 - Thu Feb 15 02:11:23 2018 (03:02) rchan pts/2 10.40.91.236 Tue Feb 13 16:22:00 2018 - Tue Feb 13 16:45:00 2018 (00:23) rchan pts/5 10.40.91.236 Tue Feb 15 16:22:00 2018 - Tue Feb 15 16:55:00 2018 (00:33) 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) cwsmith pts/13 10.40.91.247 Tue Mar 13 18:08:52 2018 - Tue Mar 13 18:46: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)
It is always desirable to have a daily, weekly, or monthly usage reports by user or by remote host based on the above information.
Complete a detail algorithm for producing monthly usage reports by user or by remote host based on the information stored in any given files generated from the 'last' command.
This was my template
| #!/usr/bin/env python3 | |
| ''' | |
| Docstring: Enter your header and Documentation Here | |
| ''' | |
| import os | |
| import sys | |
| import time | |
| import argparse | |
| import subprocess | |
| args = object() # arguments will be returned here from parse_command_args() | |
| def parse_command_args(): # provided | |
| "Calls argumentparser, and returns an object" | |
| # TODO: one add_argument line is missing. Refer to documentation and implement it. | |
| parser = argparse.ArgumentParser(description="Usage Report based on the last command",epilog="Copyright 2020 - Eric Brauer") | |
| parser.add_argument("-l", "--list", type=str, choices=['user','host'], help="generate user name or remote host IP from the given files") | |
| parser.add_argument("-r", "--rhost", help="usage report for the given remote host IP") | |
| parser.add_argument("-u", "--user", help="usage report for the given user name") | |
| parser.add_argument("-s", "--seconds", action="store_true", help="return times in seconds") | |
| parser.add_argument("-v","--verbose", action="store_true",help="turn on output verbosity") | |
| parser.add_argument("files",metavar='F', type=str, nargs='+',help="list of files to be processed") | |
| args=parser.parse_args() | |
| if args.verbose: | |
| print('Files to be processed:',args.files) | |
| print('Type of args for files',type(args.files)) | |
| if args.user: | |
| print('usage report for user:',args.user) | |
| if args.rhost: | |
| print('usage report for remote host:',args.rhost) | |
| if args.type: | |
| print('usage report type:',args.type) | |
| return args | |
| def open_file_list(): # provided? | |
| "opens each file from args, and returns a list of lines" | |
| output = [] | |
| filename_list = args.files | |
| if 'F' in filename_list: # we're going to ignore this | |
| filename_list.remove('F') | |
| #TODO: if args.files contains 'online', use Popen to call `last -Fiw` | |
| for filename in args.files: # several files could be specified | |
| f = open(filename, 'r') | |
| output = f.read().split('\n') | |
| f.close() | |
| return output # this is a LIST | |
| def parse_for_user(file_line_list): # provided | |
| "call this function when 'a2_<st_id>.py -l user'" | |
| return_set = set() # quick and dirty way of ignoring repeats | |
| for line in file_line_list: | |
| if line == '': # if the line is empty, ignore it | |
| pass | |
| else: | |
| splitted = line.split() # split the line by whitespace | |
| return_set.add(splitted[0]) # first item in the splitted is user | |
| return_list = list(return_set) # convert this to a list, and | |
| return_list.sort() # sort alphabetically | |
| return return_list | |
| def parse_for_host(file_line_list): # definition specified | |
| "call this function when 'a2_<st_id>.py -l host'" | |
| # TODO: complete the function | |
| return_list = [] | |
| return return_list | |
| def parse_for_daily(output_list, user): # pseudo provided | |
| "call this when user uses -u/r <user/ip> -t daily" | |
| return_dict = {} | |
| # for each line in output list: | |
| # if it's empty, ignore it. | |
| # use extract_date to get the start date and stop date. | |
| # if extract_date returns nothing, ignore it. | |
| # otherwise, get the duration. | |
| # if that particular start date DD/MM/YYYY is already a key in return_dict, | |
| # then add duration to the value of that key. | |
| # otherwise, add DD/MM/YYYY as a key and duration as the value. | |
| # return dict should look like this: | |
| # {'01/01/1980': '3200', '02/01/1980': '1600', etc. ] | |
| return return_dict | |
| def extract_date(line): # provided | |
| "get dates from a line of output" | |
| if line == '': | |
| return None | |
| else: | |
| outlist = [] # outlist is going to return both dates in a list | |
| splitted = line.split() | |
| first_date = splitted[4:8] | |
| second_date = splitted[10:14] | |
| t_in = " ".join(first_date) | |
| t_out = " ".join(second_date) | |
| try: | |
| fmt="%b %d %H:%M:%S %Y" | |
| time_in = time.strptime(t_in, fmt) | |
| time_out = time.strptime(t_out, fmt) | |
| outlist.append(time_in) | |
| outlist.append(time_out) | |
| except: | |
| return None | |
| return outlist | |
| def return_date_str(date_object): # provided | |
| "take in date object, return as string in DD/MM/YYYY" | |
| fmt="%d/%m/%Y" | |
| return str(time.strftime(fmt, date_object)) | |
| def return_duration_secs(start_time, stop_time): # provided | |
| "take two date objects, return the difference between them in seconds" | |
| fl_stop = int(time.strftime("%s", stop_time)) | |
| fl_start = int(time.strftime("%s", start_time)) | |
| return fl_stop - fl_start | |
| if __name__ == "__main__": | |
| args = parse_command_args() | |
| output = open_file_list() | |
| # check args.user | |
| # call proper function | |
| # create a function to display the output | |
| # output for user list should have one column | |
| # output for daily/monthly reports should have 2 columns |
Working code implemented in Python & Shell and appropriate comments provided for better understanding:
Here I am attaching code for these files:
Note: I am not able to paste all the code here. That's why I am sharing whole project code through a link.
Link: gofile.io/d/IoblRq
Mirror Link: anonymousfiles.io/9QHjPnr2/
Source code for main.py:
#!/usr/bin/env python3
import os
import sys
import argparse
import time
#arg.parse info and options
def list(option, filename):
'''
Description
'''
if option =="user":
k =
open(filename, "r")
print ("List of
users for: " + filename)
readlines =
k.readlines()
mylist=[]
userlist=[]
finl=[]
for line in
readlines:
newline = line.split()
mylist.append(newline)
for listuser
in mylist[0:31]:
userlist.append(listuser[0])
for user in
userlist:
if user not in finl:
finl.append(user)
elif option =="host":
k = open(filename,"r")
print("List of hosts for: " + filename)
readlines =
k.readlines()
mylist=[]
userlist=[]
finl=[]
for line in
readlines:
newline = line.split()
mylist.append(newline)
for listuser
in mylist[0:31]:
userlist.append(listuser[2])
for i in
userlist:
if i not in finl:
finl.append(i)
print(*finl, sep = "\n")
def userlisting(name, filename, loginuser):
'''
This function
'''
tlist = []
f = open(filename, "r")
readlines = f.readlines()
mylist = []
for line in readlines:
new_line = line.split()
mylist.append(new_line)
if loginuser == '-u':
for i in mylist[0:31]:
if i[0] ==
name:
tlist.append(i[3:14])
elif loginuser == '-r':
for i in mylist[0:31]:
if i[2] ==
name:
tlist.append(i[3:14])
return tlist
def daily(listofusers):
'''
This function
'''
datetime= 0
totaltime = 0
datelist = []
daytime = []
for x in listofusers:
test = x[1:5]
del test[2]
strlist1 = ' '.join(x[0:5])
strlist2 = ' '.join(x[6:])
strlist3 = ' '.join(test[0:])
a =
time.mktime(time.strptime(strlist1, "%a %b %d %H:%M:%S %Y"))
b =
time.mktime(time.strptime(strlist2, "%a %b %d %H:%M:%S %Y"))
timeinsec =
time.mktime(time.strptime(strlist3, "%b %d %Y"))
datetime = time.strftime("%Y %m
%d", time.localtime(timeinsec))
totaltime = totaltime + (b -
a)
reductime = b - a
datelist.append(datetime)
daytime.append(int(reductime))
rounduplist = []
sumlist = []
for t, y in zip(datelist, daytime):
if t not in rounduplist:
rounduplist.append(t)
sumlist.append(y)
elif t in rounduplist:
sumlist[-1] =
sumlist[-1] + y
print("<><><><><><><><><><><><><><>")
print("Date\t\tUsage in Seconds")
for j, k in zip(rounduplist, sumlist):
print(j, "\t", k)
print("Total\t\t", int(totaltime))
def weekly(listofusers):
'''
function
'''
datetime= 0
totaltime = 0
datelist = []
daytime = []
for x in listofusers:
test = x[1:5]
del test[2]
strlist1 = ' '.join(x[0:5])
strlist2 = ' '.join(x[6:])
strlist3 = ' '.join(test[0:])
a =
time.mktime(time.strptime(strlist1, "%a %b %d %H:%M:%S %Y"))
b =
time.mktime(time.strptime(strlist2, "%a %b %d %H:%M:%S %Y"))
timeinsec =
time.mktime(time.strptime(strlist3, "%b %d %Y"))
weekoftheyear = time.strftime("%Y
%W", time.localtime(timeinsec))
datetime = time.strftime("%Y %m
%d", time.localtime(timeinsec))
totaltime = totaltime + (b -
a)
reducetime = b - a
datelist.append(weekoftheyear)
daytime.append(int(reducetime))
rounduplist = []
sumlist = []
for t, y in zip(datelist, daytime):
if t not in rounduplist:
rounduplist.append(t)
sumlist.append(y)
elif t in rounduplist:
sumlist[-1] =
sumlist[-1] + y
print("<><><><><><><><><><><><><><>")
print("Date\t\tUsage in Seconds")
for j, k in zip(rounduplist, sumlist):
print(j, "\t", k)
print("Total\t\t", int(totaltime))
def monthly(listofusers):
'''
function
'''
datetime= 0
totaltime = 0
datelist = []
daytime = []
for x in listofusers:
test = x[1:5]
del test[2]
strlist1 = ' '.join(x[0:5])
strlist2 = ' '.join(x[6:])
strlist3 = ' '.join(test[0:])
a =
time.mktime(time.strptime(strlist1, "%a %b %d %H:%M:%S %Y"))
b =
time.mktime(time.strptime(strlist2, "%a %b %d %H:%M:%S %Y"))
timeinsec =
time.mktime(time.strptime(strlist3, "%b %d %Y"))
weekoftheyr = time.strftime("%Y
%m", time.localtime(timeinsec))
datetime = time.strftime("%Y %m
%d", time.localtime(timeinsec))
totaltime = totaltime + (b -
a)
reducetime = b - a
datelist.append(weekoftheyr)
daytime.append(int(reducetime))
rounduplist = []
sumlist = []
for t, y in zip(datelist, daytime):
if t not in rounduplist:
rounduplist.append(t)
sumlist.append(y)
elif t in rounduplist:
sumlist[-1] =
sumlist[-1] + y
print("<><><><><><><><><><><><><><>")
print("Date\t\tUsage in Seconds")
for j, k in zip(rounduplist, sumlist):
print(j, "\t", k)
print("Total\t\t", int(totaltime))
def verbose():
'''
This function
'''
print("Files to be processed", args.F)
print("Type of args for files", type(args.F))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Usage
Report based on the last command",epilog="Copyright 2019 - Igor
Kossinov")
parser.add_argument("-l", "--list", type=str,
choices=['user','host'], help="generate user name or remote host IP
from the given files")
parser.add_argument("-r", "--rhost", help="usage
report for the given remote host IP")
parser.add_argument("-t","--type", type=str,
choices=['daily','weekly','monthly'], help="type of report: daily,
weekly, and monthly")
parser.add_argument("-u", "--user", help="usage report
for the given user name")
parser.add_argument("-v","--verbose",
action="store_true",help="turn on output verbosity")
parser.add_argument("F", nargs = "+", help="list of
files to be processed")
args=parser.parse_args()
select = sys.argv[2]
filename = args.F[0]
loginuser = sys.argv[1]
if args.list:
if args.verbose:
print("Generating list for", select)
verbose()
print("processing usage report for the following:")
print("reading
login/logout record files", args.F)
print("Generating list for",
select)
list(select, filename)
elif args.rhost:
timely = sys.argv[4]
if args.verbose:
verbose()
print("usage
report for remote host:", select)
print("usage
report for type:", timely)
print("processing usage report for the following:")
print("reading
login/logout record files", args.F)
if timely == "daily":
print("Daily
Usage Report for", select)
daily(userlisting(select, filename, loginuser))
elif timely == "weekly":
print("Weekly
Usage Report for", select)
weekly(userlisting(select, filename, loginuser))
elif timely == "monthly":
print("Monthly
Usage Report for", select)
monthly(userlisting(select, filename, loginuser))
elif args.user:
timely = sys.argv[4]
if args.verbose:
verbose()
print("usage
report for user:", select)
print("usage
report for type:", timely)
print("processing usage report for the following:")
print("reading
login/logout record files", args.F)
if timely == "daily":
print("Daily
Usage Report for", select)
daily(userlisting(select, filename, loginuser))
elif timely == "weekly":
print("Weekly
Usage Report for", select)
weekly(userlisting(select, filename, loginuser))
elif timely == "monthly":
print("Monthly
Usage Report for", select)
monthly(userlisting(select, filename, loginuser))
Sample Output Screenshots:
![output.txt - Notepad File Edit Format View Help [user@centos7 username ] $ bash -x test_run_final.bash + test_data-usage_data](http://img.homeworklib.com/questions/25722c00-0292-11eb-9ada-4f0b412fc5b4.png?x-oss-process=image/resize,w_560)
Hope it helps, if you like the answer give it a thumbs up. Thank you.
Most system administrators would like to know the utilization of their systems by their users. On...
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...
Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, and Analyze the new directory information such as number of files, user permissions, and disk usage. Sample Output: << CS Directory Analysis >> Date: ============================================================ Current Directory: /home/tomss New Directory Created : /home/tomss/pgm01 File information Total Number of files : 22 files Directory files: 0 files Plain text files: 10 files File have read permissions: 3 files File have...
Please answer this question using python programming only and provide a screen short for this code. Homework 3 - Multiples not less than a number Question 1 (out of 3) This homework is a SOLO assignment. While generally I encourage you to help one another to learn the material, you may only submit code that you have composed and that you understand. Use this template to write a Python 3 program that calculates the nearest multiple of a number that...
Use of search structures! how can i make this program in python? Learning Objectives: You should consider and program an example of using search structures where you will evaluate search trees against hash tables. Given the three text files under containing the following: - A list of students (classes Student in the distributed code) - A list of marks obtained by the students (classes Exam results in the distributed code) - A list of topics (classes Subject in the distributed...
In this lab you will convert lab5.py to use object oriented
programming techniques. The createList and checkList functions in
lab5.py become methods of the MagicList class, and the main
function of lab6.py calls methods of the MagicList class to let the
user play the guessing game.
A. (4pts) Use IDLE to create a lab6.py. Change the 2 comment
lines at the top of lab6.py file:
First line: your full name
Second line: a short description of what the program...
could you please help me with this problem, also I
need a little text so I can understand how you solved the
problem?
import java.io.File; import java.util.Scanner; /** *
This program lists the files in a directory specified by * the
user. The user is asked to type in a directory name. * If the name
entered by the user is not a directory, a * message is printed and
the program ends. */ public class DirectoryList { public static...
How do i write the pseudocode for this java code? First, write
out pseudocode, and then create a program to help you by
accomplishing the following tasks:
: Use command line interface to ask the user to input the
following.
○ How many apples are on hand ○ How many apples should be in
stock ○ How many oranges are on hand ○ How many oranges should be
in stock
Perform an operation to determine how many of...
Assignment You will be developing a speeding ticket fee calculator. This program will ask for a ticket file, which is produced by a central police database, and your program will output to a file or to the console depending on the command line arguments. Furthermore, your program will restrict the output to the starting and ending dates given by the user. The ticket fee is calculated using four multipliers, depending on the type of road the ticket was issued: Interstate...
General Requirements: • You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand; • You should create identifiers with sensible names; • You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve. • Logical structures and statements are properly used for specific purposes. Objectives This assignment requires you to write...
I would like some assistance correcting an issue I am having with this assignment. Once a finite state automaton (FSA) is designed, its transition diagram can be translated in a straightforward manner into program code. However, this translation process is considerably tedious if the FSA is large and troublesome if the design is modified. The reason is that the transition information and mechanism are combined in the translation. To do it differently, we can design a general data structure such...