Skip to content

Instantly share code, notes, and snippets.

@lazymutt
Last active December 8, 2015 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazymutt/82bcac8d9a150dd4b04f to your computer and use it in GitHub Desktop.
Save lazymutt/82bcac8d9a150dd4b04f to your computer and use it in GitHub Desktop.
consumes the output of /usr/bin/last and parses out the the user with largest number of minutes logged into the console.
#!/usr/bin/python
# Copyright (c) 2015 University of Utah Student Computing Labs. ################
# All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appears in all copies and
# that both that copyright notice and this permission notice appear
# in supporting documentation, and that the name of The University
# of Utah not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission. This software is supplied as is without expressed or
# implied warranties of any kind.
################################################################################
# census.py ####################################################################
# 12/08/15, v1.0, todd.mcdaniel@utah.edu
#
# consumes the output of last and parses out the the user with largest number
# of minutes logged into the console.
#
#
################################################################################
# Notes: ######################################################################
# Not sure how year is handled when session crosses date boundary.
# u0942941 console Thu Dec 3 14:21 still logged in
# testing seems to show that years aren't displayed in last...
#
# what if two users have the same minutes?
#
#
#
################################################################################
from subprocess import check_output
from re import search
import datetime
import os
def main():
# last requires root.
if os.geteuid():
print "Must be root to script."
exit(2)
raw_data = check_output('/usr/bin/last')
raw_list = raw_data.split("\n")
raw_list = [x for x in raw_list if x]
grid = {}
for x in raw_list:
tmp_day = None
tmp_hour = None
tmp_minute = None
tmp_clock = None
running_minutes = 0
if search('console', x):
try:
# pull out username
match = search("\A(\S*)", x)
tmp_name = match.group(1)
try:
# pull out time block
match = search("[(](.*)[)]", x)
tmp_clock = match.group(1)
try:
# pull out three value time
full_match = search("(\d*)[+](\d*)[:](\d+)", tmp_clock)
tmp_day = full_match.group(1)
tmp_hour = full_match.group(2)
tmp_minute = full_match.group(3)
except:
try:
# pull out two value time
mid_match = search("(\d*)[:](\d+)", tmp_clock)
tmp_hour = mid_match.group(1)
tmp_minute = mid_match.group(2)
except:
pass
except:
# this is where "still logged in" time is calculated
match = search("\A\w*\s*\w*\s(.*)\s*still", x)
raw_time = match.group(1)
match = search("\A\s*(\w+)\s*(\w+)\s*(\d+)\s(\d+:\d+)", raw_time)
raw_weekday = match.group(1)
raw_month = match.group(2)
raw_day = match.group(3)
raw_time = match.group(4)
raw_now = datetime.datetime.today().replace(microsecond=0)
raw_year = raw_now.year
raw_string = str(raw_year) + "-" + str(raw_month) + "-" + str(raw_day) + " " + str(raw_time)
time_login = datetime.datetime.strptime(raw_string, "%Y-%b-%d %H:%M")
time_now = datetime.datetime.strptime(str(raw_now), "%Y-%m-%d %H:%M:%S")
time_diff = time_now - time_login
tmp_minute = (time_diff.days * 1440) + int(time_diff.seconds/60 )
except:
pass
if tmp_day:
running_minutes = running_minutes + (int(tmp_day) * 1440)
if tmp_hour:
running_minutes = running_minutes + (int(tmp_hour) * 60)
if tmp_minute:
running_minutes = running_minutes + int(tmp_minute)
try:
tmp_total = grid[tmp_name]
grid[tmp_name] = tmp_total + running_minutes
except:
grid[tmp_name] = running_minutes
final_user, final_time = sorted(grid.items(), key=lambda x: x[1], reverse=True)[0]
print "%s, %i minutes." % (final_user, final_time)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment