- [Instructor] Welcome to my next video of section eight,
finding out active user hours on a system.
In the previous video we've learned about
remote disc usage health monitor.
This video is a practice task to make use
of the system logs and to find out how many hours
each of the users have spent on the server
and rank them according to total usage hours.
Consider a web server with shared hosting.
Many users log in and out of the server everyday,
and the user activity gets logged
in the server's system log.
A report should be generated with the details
such as rank, user, first logged-in date,
last logged-in date, number of times logged-in,
and total usage hours.
Let's see how we can approach this problem.
The "last" command is used to list details
about the login sessions of the users in a system.
The log data is stored in the file log wtmp file.
By individually adding the session hours for each user,
we can find out the total usage hours.
Let's go through the script to find out active users
and generate their port.
A sample output is as follows:
In the "active_users.sh" script,
we can either provide the wtmp log file
as a command line argument
or we use the default wtmp log file.
The "last-f" command is used to print the log file contents.
The first column in the log file is the username.
By using "cut," we extract the first column
from the log file.
Then, the unique users are found out
by using the "sort" and "unique" commands.
Now, for each user, the log lines corresponding
to their login sessions are found out using "grep"
and are written to the temporary file.
The last column in the "last" log is the duration
for which the user logged into the session.
Hence in order to find out the total usage hours for a user,
the session duration is to be added.
The usage duration is in our second format
and is converted into minutes using a simple "awk" script.
In order to extract the session hours for the users,
we have you use the "awk" command.
For removing the parentheses, "tr-d" is used.
The list of the usage hours string is passed
to the standard input for the while loop
using the commands operator which acts as a file input.
Each hours string is converted into seconds
by using the date command and added
to the variable "seconds."
The first login time for a user
is in the last line and is extracted.
The number of login attempts is the number of log lines.
In order to calculate the rank of each user
according to the total usage hours,
the data record is to be stored in the descending order,
with the usage hours as the key.
For specifying the number, reverse sort, the "-nr" option,
is used along with "sort" command.
"-k4" is used to specify the key column, usage hour.
Finally, the output of the "sort" is passed to "awk."
The "awk" command prefixes a line number
to each of the lines
which becomes the rank for each user.
Awesome! We've successfully learned how to find
out active user hours on a system.
In the next video, we'll learn how to measure
an optimized power usage.