Difference between revisions of "Python-License-LoadSensor"

From GridWiki
Jump to: navigation, search
m (Python Script to check licenses (Original Code))
Line 11: Line 11:
 
  #!/usr/bin/python -u
 
  #!/usr/bin/python -u
 
  # Important stdout must be un-buffered hence the -u command line option to python
 
  # Important stdout must be un-buffered hence the -u command line option to python
  import os, re, string,sys,datetime
+
  import os, re, sys,datetime
 
  LM_LICENSE_FILE="1717@server1:1717@server2"
 
  LM_LICENSE_FILE="1717@server1:1717@server2"
 
  lmstat="/usr/local/bin/lmstat"
 
  lmstat="/usr/local/bin/lmstat"
Line 19: Line 19:
 
  while (1):
 
  while (1):
 
     inputString=sys.stdin.readline()
 
     inputString=sys.stdin.readline()
     if string.strip(inputString)=="quit":
+
     if inputString.strip()=="quit":
 
         sys.exit(1)
 
         sys.exit(1)
 
     if count>3:
 
     if count>3:

Revision as of 19:43, 22 October 2009

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 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.

qconf -msconf

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 (1):
   inputString=sys.stdin.readline()
   if inputString.strip()=="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("([0-9]+) license", line)
                   if m:
                       free=int(m[0])-int(m[1])
                       if usage.has_key(feature):
                           usage[feature]=usage[feature]+free
                       else:
                           usage[feature]=free
   print "begin"
   for key in usage.keys():
       if count>3:
           free=usage[key]-reserve
       else:
           free=0
       print "global:%s:%d" % (key, free)
   print "end"
   if count>3:
       count=1
   else:
       count=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, string,sys,datetime
LM_LICENSE_FILE="1717@server1:1717@server2"
lmstat="/usr/local/bin/lmstat"
features=["licenseA"]
reserve=3
count=4
while (1):
   inputString=sys.stdin.readline()
   if string.strip(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 usage.has_key(feature):
                      usage[feature]=usage[feature]+free
                  else:
                      usage[feature]=free
   print "begin"
   for key in usage.keys():
       #if count>interval:
       free=usage[key]-reserve
       #else:
       #    free=0
       print "global:%s:%d" % (key, free)
   print "end"
   if count>interval:
       count=1
   else:
       count=count+1