1 answer

Most system administrators would like to know the utilization of their systems by their users. On...

Question:

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

Answers

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

Hope it helps, if you like the answer give it a thumbs up. Thank you.

.

Similar Solved Questions

1 answer
In C language, how to remove duplicate '*' ? to make “**hello2019***happy1314**newyear****is*fun****” become “hello2019*happy1314*newyear*is*fun”.
In C language, how to remove duplicate '*' ? to make “**hello2019***happy1314**newyear****is*fun****” become “hello2019*happy1314*newyear*is*fun”....
1 answer
1. A discrete-time LTI system has the system function H() given below: (a) Sketch the pole-zero...
1. A discrete-time LTI system has the system function H() given below: (a) Sketch the pole-zero plot for this system How many possible regions of convergence (ROCs) are there for H(). List the possible ROCs and indicate what type of sequence (left-sided, right-sided, two-sided, finite-length) they c...
1 answer
What is the change in C oxidation number when carbon monoxide reacts with oxygen to make...
What is the change in C oxidation number when carbon monoxide reacts with oxygen to make carbon dioxide? It may help to write out the chemical equation. с 0 0 0 0...
1 answer
If a bacterium obtain its energy and carbon from organic molecules how would you classify it?...
If a bacterium obtain its energy and carbon from organic molecules how would you classify it? Bacteria that cause the disease anthrax, bacillus anthracis, can survive for extremely long periods of time in harsh conditions because they possess a protective structure called a ? The produce known as th...
1 answer
Help please +-/3 points My Notes Ask Your Tea Find the correlation coefficient between the variables...
Help please +-/3 points My Notes Ask Your Tea Find the correlation coefficient between the variables X and Y presented in the table: X Y 1 1 3 2 4 4 6 4 8 5 9 7 11 8 18 15 Round inal answer to 2 decimal places: 12. -13 points My Notes Ask Your Teache Medical records indicate that people with more...
1 answer
05)The article "Measuring and Understanding the Aging of Kraft Insulating Paper in Power Transformers" (IEEE Electr...
05)The article "Measuring and Understanding the Aging of Kraft Insulating Paper in Power Transformers" (IEEE Electrical Insul. Mag., 1996: 28-34) contained the following observations on degree of polymerization for paper specimens for which viscosity times concentration fell in a certain mid...
1 answer
TATP (triacetone triperoxide) 1) Assuming the small stable product molecules are gaseous water, c...
TATP (triacetone triperoxide) 1) Assuming the small stable product molecules are gaseous water, carbon dioxide, and nitrogen, write the balanced chemical equation that illustrates your explosives decomposition. On a clean sheet of paper, show the balanced chemical equation using the lewis structures...
1 answer
What info and concept do i need to know? Before pmv. During ??? explosion
what info and concept do i need to know? Before pmv. During ??? explosion...
1 answer
(c) Is a gauche interaction an example of steric strain or torsional strain? For each of...
(c) Is a gauche interaction an example of steric strain or torsional strain? For each of the molecules below, draw a Newman projection of the conformation shown. Label any gauche interactions. (b) Draw the Newman projections of the most and least stable conformations of (S) -2, 3, 5-trimethylhexane,...
1 answer
Why is it a fallacy to confuse causation and correlation? Provide an example of a statement...
Why is it a fallacy to confuse causation and correlation? Provide an example of a statement that confuses causation with correlation. 2 paragraph...
1 answer
O test and Chi Square Complete the following exercises individually followed by a discussion and review...
O test and Chi Square Complete the following exercises individually followed by a discussion and review of your answers your group 1 Neonates gain (on average) 100 grams/wk in the first 4 weeks. A sample of 25 infants given a new nutrition formula gained 112 grams/wk (on average) with a standard dev...
1 answer
Cswebdav/pd-203 1099-dt-content-rid-127647332/courses/201 920-PHYS 1402010/Homework%281962 PHYS 1402 (General Physics II) Homework 2 1. When we defined the...
cswebdav/pd-203 1099-dt-content-rid-127647332/courses/201 920-PHYS 1402010/Homework%281962 PHYS 1402 (General Physics II) Homework 2 1. When we defined the electric potential, we said that we take a point in- finitely far away to have a potential of zero when dealing with a finite distribution of ch...
1 answer
A company makes windows for use in homes and commercial buildings. The standards for glass thickness...
A company makes windows for use in homes and commercial buildings. The standards for glass thickness call for the glass to average 0.4000 inch with a standard deviation equal to 0.0500 inch. Suppose a random sample of n=48 windows yields a sample mean of 0.4190inch. Complete parts a and b below. a. ...
1 answer
A PN 200 Fundamentals of Nursing IT Case Study Bronchial Asthma Michael y dinosed with bronchial...
A PN 200 Fundamentals of Nursing IT Case Study Bronchial Asthma Michael y dinosed with bronchial asthma. His mother her have to the past year, Michael has had three asthma attacks that ne trend th e and autol Preventi inhaler. At the office visit today. pod prethod for a week and the order is writte...
1 answer
How do you solve the following system: #-6x + y = -8, 6y + 4x = 12 #?
How do you solve the following system: #-6x + y = -8, 6y + 4x = 12 #?...
1 answer
When salts are involved in hydrolysis reactions, they will do it in only three ways. When...
When salts are involved in hydrolysis reactions, they will do it in only three ways. When the salt is a metal ion it will pull the an HO from water (use M+ for the salt), look at the top of pg 87 for the actual details: The hydrolysis reaction should be + H20 --> Where the hydrolysis equilibrium ...
1 answer
7. Sketch the graph of f(z, y) 2.5 60 40 20 1.5 20 -40 0.5 -1 0.5 0 0.5 1 -3-2-101 2 3 (a) s 3 2 ...
Could you please show all work? Thanks! 7. Sketch the graph of f(z, y) 2.5 60 40 20 1.5 20 -40 0.5 -1 0.5 0 0.5 1 -3-2-101 2 3 (a) s 3 2 1 0 1 -2 10 1 -2 01 7. Sketch the graph of f(z, y) 2.5 60 40 20 1.5 20 -40 0.5 -1 0.5 0 0.5 1 -3-2-101 2 3 (a) s 3 2 1 0 1 -2 10 1 -2 01...
1 answer
A U.S. importer wants to purchase merchandise from a German exporter invoiced in euros at a...
A U.S. importer wants to purchase merchandise from a German exporter invoiced in euros at a cost of €569,450. The U.S. importer’s bank quotes an exchange rate of €1.1389/$1.00. After accepting the quote, the bank _______ the importer's account in the amount of _______. Multiple C...
1 answer
How do you write 13 quarts to 4 gallons in fractional notation in lowest terms?
How do you write 13 quarts to 4 gallons in fractional notation in lowest terms?...