2013-06-10 11:08:42 +00:00
|
|
|
#!/usr/bin/env python2.6
|
|
|
|
#
|
2015-11-05 11:03:02 +00:00
|
|
|
# Copyright(c) 2013-2015 Christoph Niethammer <niethammer@hlrs.de>
|
2013-06-10 11:08:42 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
from optparse import OptionParser
|
2014-02-05 14:58:12 +00:00
|
|
|
import re
|
2014-02-05 11:49:08 +00:00
|
|
|
import subprocess
|
2013-06-10 11:08:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
modules = list()
|
|
|
|
modulestats = dict()
|
2013-06-12 15:13:38 +00:00
|
|
|
moduleusers = dict()
|
2013-06-12 18:01:16 +00:00
|
|
|
total_modules = 0
|
|
|
|
total_users = set()
|
2013-06-10 11:08:42 +00:00
|
|
|
|
|
|
|
logdir = '/sw/laki/hlrs/system/modules/logs/'
|
|
|
|
enddate = datetime.date.today()
|
|
|
|
startdate = datetime.date(enddate.year, enddate.month, 1)
|
|
|
|
modulepattern=""
|
|
|
|
|
|
|
|
parser = OptionParser(usage="%prog [options] [pattern]")
|
|
|
|
parser.add_option("--logdir", metavar="DIR", dest="logdir")
|
|
|
|
parser.add_option("--startdate", metavar="YYYY-MM-DD", dest="startdate")
|
|
|
|
parser.add_option("--enddate", metavar="YYYY-MM-DD", dest="enddate")
|
2013-06-12 15:13:38 +00:00
|
|
|
parser.add_option("--nohpc", action="store_true", default=False, dest="nohpc", help="Exclude hpc* accounts from stats")
|
2013-12-12 11:06:49 +00:00
|
|
|
parser.add_option("--printusers", action="store_true", default=False, dest="printusers", help="Print detailed user lists for module usage.")
|
2014-02-13 09:21:05 +00:00
|
|
|
parser.add_option("--excludeusers", metavar="REGEX", dest="excludeusers", help="Exclude users mathich given regular expression")
|
2014-02-05 11:49:08 +00:00
|
|
|
parser.add_option("--list-unused", action="store_true", default=False, dest="list_unused", help="List only unused modules.")
|
2013-06-10 11:08:42 +00:00
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
if options.logdir :
|
|
|
|
logdir = options.logdir
|
|
|
|
if options.startdate :
|
|
|
|
d = options.startdate
|
|
|
|
startdate = datetime.date(int(d[0:4]),int(d[5:7]),int(d[8:10]))
|
|
|
|
if options.enddate :
|
|
|
|
d = options.enddate
|
|
|
|
enddate = datetime.date(int(d[0:4]),int(d[5:7]),int(d[8:10]))
|
|
|
|
if len(args) > 0 :
|
|
|
|
modulepattern = args[0]
|
2014-02-13 09:21:05 +00:00
|
|
|
if options.excludeusers :
|
|
|
|
excludeusers_re = re.compile(str(options.excludeusers))
|
2013-06-10 11:08:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
for logfilename in os.listdir(logdir) :
|
|
|
|
d = datetime.date(int(logfilename[0:4]),int(logfilename[4:6]),int(logfilename[6:8]))
|
|
|
|
if d < startdate :
|
|
|
|
continue
|
|
|
|
if d > enddate :
|
|
|
|
continue
|
|
|
|
date = d.strftime('%Y-%m-%d')
|
|
|
|
logfile = os.path.join(logdir, logfilename)
|
|
|
|
f = open(logfile)
|
|
|
|
for line in f :
|
|
|
|
if ' load' in line and modulepattern in line :
|
|
|
|
module = line.split()[-1]
|
2013-06-12 15:13:38 +00:00
|
|
|
user = line.split()[-3][5:] # remove user: in front
|
|
|
|
if options.nohpc and 'hpc' in user :
|
|
|
|
continue
|
2014-02-13 09:21:05 +00:00
|
|
|
if options.excludeusers and excludeusers_re.match(user):
|
|
|
|
continue
|
2013-06-12 18:01:16 +00:00
|
|
|
total_users.add(user)
|
|
|
|
total_modules = total_modules + 1
|
2013-06-10 11:08:42 +00:00
|
|
|
if module not in modules:
|
|
|
|
modules.append(module)
|
|
|
|
modulestats[module] = 1
|
2013-06-12 15:13:38 +00:00
|
|
|
moduleusers[module] = dict()
|
|
|
|
moduleusers[module][user] = 1
|
2013-06-10 11:08:42 +00:00
|
|
|
else :
|
|
|
|
modulestats[module] = modulestats[module] + 1
|
2013-06-12 15:13:38 +00:00
|
|
|
if user not in moduleusers[module] :
|
|
|
|
moduleusers[module][user] = 1
|
|
|
|
else :
|
|
|
|
moduleusers[module][user] = moduleusers[module][user] + 1
|
2013-06-10 11:08:42 +00:00
|
|
|
# if ' swap' in line :
|
|
|
|
# print line.split()
|
2015-11-05 11:03:02 +00:00
|
|
|
|
2013-06-10 11:08:42 +00:00
|
|
|
print "-"*78
|
|
|
|
print "Module statistics for " + startdate.strftime('%Y-%m-%d') + " - " + enddate.strftime('%Y-%m-%d')
|
|
|
|
print "-"*78
|
2014-02-05 11:49:08 +00:00
|
|
|
if options.list_unused :
|
|
|
|
print "Unused modules:"
|
|
|
|
print "-"*78
|
|
|
|
avail_modules = []
|
2014-02-05 14:58:12 +00:00
|
|
|
r = re.compile("\(.*\)")
|
|
|
|
p = subprocess.Popen("module av -t " + modulepattern, shell=True, stderr=subprocess.PIPE).stderr
|
2014-02-05 11:49:08 +00:00
|
|
|
for line in p :
|
2014-02-05 14:58:12 +00:00
|
|
|
if line[0] == '/' :
|
|
|
|
continue
|
2014-02-05 11:49:08 +00:00
|
|
|
if modulepattern in line :
|
2014-02-05 14:58:12 +00:00
|
|
|
avail_modules.append(r.sub('', line.strip()))
|
2014-02-05 11:49:08 +00:00
|
|
|
for module in avail_modules :
|
|
|
|
if not module in modulestats :
|
|
|
|
print module
|
|
|
|
else :
|
|
|
|
print "{0:60s}{1:>8s} {2:>8s}".format('module name', '# uses', '# users')
|
|
|
|
print "-"*78
|
|
|
|
for (v,m) in sorted( ((v,k) for k,v in modulestats.iteritems()), reverse=True) :
|
|
|
|
print "{0:60s}{1:>8d} {2:>8d}".format(m, v, len(moduleusers[m].keys()))
|
|
|
|
if options.printusers :
|
|
|
|
print moduleusers[m]
|
|
|
|
print "-"*78
|
|
|
|
print "{0:60s}{1:>8d} {2:>8d}".format('total', total_modules, len(total_users))
|
2013-12-12 11:06:49 +00:00
|
|
|
if options.printusers :
|
2014-02-05 11:49:08 +00:00
|
|
|
print sorted([ (u) for u in total_users ])
|
2013-06-12 18:01:16 +00:00
|
|
|
print "-"*78
|