Difference between revisions of "Python-License-LoadSensor"

From GridWiki
Jump to: navigation, search
m (Python Script to check licenses (Original Code))
m (Reverted edits by LindaLopez (talk) to last revision by Kallies)
 
(6 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
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.
 
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.
+
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
 
  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) =
 
= Python Script to check licenses (Original Code) =
Line 17: Line 19:
 
  reserve=3
 
  reserve=3
 
  count=4
 
  count=4
  while (1):
+
  while True:
     inputString=sys.stdin.readline()
+
     inputString=raw_input()
     if inputString.strip()=="quit":
+
     if inputString=="quit":
 
         sys.exit(1)
 
         sys.exit(1)
 
     if count>3:
 
     if count>3:
Line 27: Line 29:
 
             for feature in features:
 
             for feature in features:
 
                 if re.search(feature, line):
 
                 if re.search(feature, line):
                     m=re.findall("([0-9]+) license", line)
+
                     m=re.findall(r"(\d+) license", line)
 
                     if m:
 
                     if m:
 
                         free=int(m[0])-int(m[1])
 
                         free=int(m[0])-int(m[1])
                         if usage.has_key(feature):
+
                         if feature in usage:
                             usage[feature]=usage[feature]+free
+
                             usage[feature]+=free
 
                         else:
 
                         else:
 
                             usage[feature]=free
 
                             usage[feature]=free
 
     print "begin"
 
     print "begin"
     for key in usage.keys():
+
     for key,val in usage.items():
 
         if count>3:
 
         if count>3:
             free=usage[key]-reserve
+
             free=val-reserve
 
         else:
 
         else:
 
             free=0
 
             free=0
Line 45: Line 47:
 
         count=1
 
         count=1
 
     else:
 
     else:
         count=count+1
+
         count+=1
  
 
= Python Script to check licenses (Modified by brs) =
 
= Python Script to check licenses (Modified by brs) =
Line 51: Line 53:
 
  #!/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 57: Line 59:
 
  reserve=3
 
  reserve=3
 
  count=4
 
  count=4
  while (1):
+
interval=3
     inputString=sys.stdin.readline()
+
  while True:
     if string.strip(inputString)=="quit":
+
     inputString=raw_input()
 +
     if inputString=="quit":
 
         sys.exit(1)
 
         sys.exit(1)
 
     if count>3:
 
     if count>3:
Line 77: Line 80:
 
               if len(m)>1 and feature in features:
 
               if len(m)>1 and feature in features:
 
                   free=int(m[0])-int(m[1])
 
                   free=int(m[0])-int(m[1])
                   if usage.has_key(feature):
+
                   if feature in usage:
                       usage[feature]=usage[feature]+free
+
                       usage[feature]+=free
 
                   else:
 
                   else:
 
                       usage[feature]=free
 
                       usage[feature]=free
 
     print "begin"
 
     print "begin"
     for key in usage.keys():
+
     for key,val in usage.items():
         #if count>interval:
+
         if count>interval:
        free=usage[key]-reserve
+
            free=val-reserve
         #else:
+
         else:
        #    free=0
+
            free=0
 
         print "global:%s:%d" % (key, free)
 
         print "global:%s:%d" % (key, free)
 
     print "end"
 
     print "end"
Line 92: Line 95:
 
         count=1
 
         count=1
 
     else:
 
     else:
         count=count+1
+
         count+=1

Latest revision as of 03:48, 5 October 2012

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