Scrape Queue Length from Tibco Queue

From PeformIQ Upgrade
Revision as of 14:46, 4 October 2015 by PeterHarding (talk | contribs) (→‎Python Implementation)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Python Implementation

$ cat SubmitIDOC.py 
#!/usr/bin/env python

#
#  Purpose:  Scrape queue length from TIBCO queue
#
#-------------------------------------------------------------------------------

import sys
import re
import os
import time
import getopt
import string
import datetime

#-------------------------------------------------------------------------------

__version__        = '1.2.0'

debug_flg          = False
verbose_flg        = False

sample_period      = 10 # seconds
no_samples         = 1440    # 4 hours

fd                 = None

#----- Queue specific variables ------------------------------------------

host       = 'jinx.ix'
port       =  9999
user_id    = 'xxxx'
queue_name = 'Wunderland.esb.FlightPlanning.SubmitIDOC'

#-------------------------------------------------------------------------

def send(s):
   os.write(fd, '%s\n' % s)

#-------------------------------------------------------------------------

def match(pat):
   s = ''
   while True:
      m = os.read(fd, 1024)
      s += m
      idx =  m.find(pat)
      print m
      if idx != -1:
         break
   return s

#-------------------------------------------------------------------------

SCRAPE = """
Data to be 'scraped' looks like this...

show queue xxxx
 Queue:                 xxxx
 Type:                  static
 Properties:            failsafe,*prefetch=5
 JNDI Names:            <none>
 Bridges:               <none>
 Receivers:             1
 Pending Msgs:          0
 Delivered Msgs:        0
 Pending Msgs Size:     0.0 Kb
tcp://host:9999>
"""

def scrape(s):
   lines = s.split('\n')

   receivers     = 0
   pending_cnt   = 0
   delivered_cnt = 0
   pending_size  = 0

   for idx in range(len(lines)):
      lines[idx] = lines[idx].replace('\r', '')
      lines[idx] = re.sub('^ ', '', lines[idx])
      lines[idx] = re.sub(' Msgs', 'Msgs', lines[idx])
      lines[idx] = re.sub(' Size', 'Size', lines[idx])
      data = lines[idx].split()

      # print lines[idx], data, len(data)

      if len(data) >= 2:
         if data[0] == 'PendingMsgs:':
            pending_cnt = int(data[1])
         elif data[0] == 'DeliveredMsgs:':
            delivered_cnt = int(data[1])
         elif data[0] == 'Receivers:':
            receivers_cnt = int(data[1])
         elif data[0] == 'PendingMsgsSize:':
            pending_size = float(data[1])
         elif data[0] == 'Receivers:':
            receivers = int(data[1])

   # print "%d %d %f %d" % (pending_cnt, delivered_cnt, pending_size, receivers)
   return (pending_cnt, delivered_cnt, pending_size, receivers)

#-------------------------------------------------------------------------

def do_parent():
   dt_stamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')

   logname = 'SubmitIDOC_%s.log' % dt_stamp

   lfd = open(logname, 'a+')

   now   = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
   t_now = time.time()

   lfd.write("%s ===== Start Log [%d] ======\n" % (now, t_now))
   lfd.write("%s Pending  Delivered  Pend Size  Recv\n" % now)

   match('$')

   send('./tibemsadmin')
   match('> ')

   send('connect %s:%d' % (host, port))
   match(':')

   send(user_id)
   match(':')

   send(user_id)
   match('>')

   cnt = 0

   prompt = '%s>' % port

   while cnt < no_samples:
      now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
      # lfd.write("%s ----\n" % now)
      send('show queue %s'% queue_name)
      v = match(prompt)
      x =  scrape(v)
      lfd.write("%s  %6d  %9d  %9.1f  %4d\n" % (now, x[0], x[1], x[2], x[3]))
      time.sleep(sample_period)
      cnt += 1

   now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
   lfd.write(">>>> %s\n" % now.isoformat())

   send('exit')
   match('$')

   time.sleep(2)

   send('exit')
   match('closed')

   lfd.close()

#-------------------------------------------------------------------------

def do_child():
   os.execv("/bin/ssh", ["ssh", "-l", "xxxx", host])

#-------------------------------------------------------------------------

def do():
   global fd

   (child_pid, fd) = os.forkpty()

   if child_pid == 0:
      do_child()

   do_parent()

#-------------------------------------------------------------------------------

def usage():
   USAGE = """
   
     Usage:
     
       $ dt.py
    
   """
   
   sys.stderr.write(USAGE)
   
#-------------------------------------------------------------------------------

def main(argv):
   global debug_flg, verbose_flg, no_orders, sample_period

   loop_cnt    = 1
   examine_flg = False

   #----- Process command line arguments ----------------------------

   try:
      opts, args = getopt.getopt(argv, "dehl:n:p:v",
              ["debug", "examine", "help", "loop", "no=", "period", "verbose"])
   except getopt.GetoptError:
      usage()
      sys.exit(2)
   else:
      for opt, arg in opts:
         if opt in ("-h", "--help"):
            usage()
            sys.exit(0)
         elif opt in ("-e", "--examine"):
            scrape(SCRAPE)
            return 0
         elif opt in ("-l", "--loop"):
            loop_cnt = int(arg)
         elif opt in ("-n", "--no"):
            no_orders = int(arg)
         elif opt in ("-p", "--period"):
            sample_period = int(arg)
         elif opt in ("-v", "--verbose"):
            verbose_flg = True

   do()

#---------------------------------------------------------------------

if __name__ == "__main__":
   main(sys.argv[1:])

#---------------------------------------------------------------------