Determining active user hours on a system
This recipe makes use of the system logs to find out how many hours each user has spent on the server and ranks them according to the total usage hours. A report is generated with the details, including rank, user, first logged in date, last logged in date, number of times logged in, and total usage hours.
Getting ready
The raw data about user sessions is stored in a binary format in the /var/log/wtmp
file. The last
command returns details about login sessions. The sum of the session hours for each user is that user's total usage hours.
How to do it...
This script will determine the active users and generate the report:
#!/bin/bash #Filename: active_users.sh #Description: Reporting tool to find out active users log=/var/log/wtmp if [[ -n $1 ]]; then log=$1 fi printf "%-4s %-10s %-10s %-6s %-8s\n" "Rank" "User" "Start" \ "Logins" "Usage hours" last -f $log | head -n -2 > /tmp/ulog.$$ cat /tmp/ulog.$$ | cut -d' ' -f1 | sort | uniq...