Python-License-LoadSensor

From GridWiki
Jump to: navigation, search

Introduction

This method uses a resource and a load sensor to monitor the number of free licenses. The script calculates the total number of free licenses across a number of license servers and can reserve licenses for use outside of gridware.

If the load sensor reports that there are four licenses free then gridware will start four jobs. If on the next scheduling interval there are still four licenses free, i.e. the newly started programs haven't claimed a license yet, gridware will start another four jobs. To avoid this you can increase the scheduling interval using

qconf -msconf

or use the trick in the script below which effectively one report the real number of free licenses at 4*scheduling interval, thus slowing down the rate of just the jobs that require this license.

Python Script to check licenses (Original Code)

#!/usr/bin/python -u
# Important stdout must be un-buffered hence the -u command line option to python
import os, re, sys,datetime
LM_LICENSE_FILE="1717@server1:1717@server2"
lmstat="/usr/local/bin/lmstat"
features=["licenseA"]
reserve=3
count=4
while True:
   inputString=raw_input()
   if inputString=="quit":
       sys.exit(1)
   if count>3:
       usage={}
       licenses=os.popen("%s -a -c %s" % (lmstat, LM_LICENSE_FILE))
       for line in licenses:
           for feature in features:
               if re.search(feature, line):
                   m=re.findall(r"(\d+) license", line)
                   if m:
                       free=int(m[0])-int(m[1])
                       if feature in usage:
                           usage[feature]+=free
                       else:
                           usage[feature]=free
   print "begin"
   for key,val in usage.items():
       if count>3:
           free=val-reserve
       else:
           free=0
       print "global:%s:%d" % (key, free)
   print "end"
   if count>3:
       count=1
   else:
       count+=1

Python Script to check licenses (Modified by brs)

#!/usr/bin/python -u
# Important stdout must be un-buffered hence the -u command line option to python
import os, re, sys,datetime
LM_LICENSE_FILE="1717@server1:1717@server2"
lmstat="/usr/local/bin/lmstat"
features=["licenseA"]
reserve=3
count=4
interval=3
while True:
   inputString=raw_input()
   if inputString=="quit":
       sys.exit(1)
   if count>3:
       usage={}
       licenses=os.popen("%s -a -c %s" % (lmstat, LM_LICENSE_FILE))
       for line in licenses:
           # instead of iterating over each feature to see if it exists in the line
           # just check to see if the line matches a regex for the issued/used info
           # This is much faster when your features list gets long
           if re.search("^Users of [a-zA-Z\-\_]+:.*$", line):
              m=re.findall("([0-9]+) license", line)
              fe=re.findall("^Users of ([a-zA-Z\-\_]+):.*$",line)
              feature=fe[0]
              # Also, make sure we ignore erroneous issued/used lines like
              # Users of blah:  (Error: 1 licenses, unsupported by licensed server)
              # We don't want the load sensor dying because of this
              if len(m)>1 and feature in features:
                  free=int(m[0])-int(m[1])
                  if feature in usage:
                      usage[feature]+=free
                  else:
                      usage[feature]=free
   print "begin"
   for key,val in usage.items():
       if count>interval:
           free=val-reserve
       else:
           free=0
       print "global:%s:%d" % (key, free)
   print "end"
   if count>interval:
       count=1
   else:
       count+=1