Gathering TIBCO Queue Statistics

From PeformIQ Upgrade
Jump to navigation Jump to search

Overview

The following are examples of Python scripts which are used to collect TIBCO queuing statistics as part of the SVT data collection process.

Site sensitive data is collected together in a CONFIG.py file which is imported into the script.

$ cat CONFIG.py

SSH_Host      = 'xxx'
SSH_UserId    = 'xxx'
TIBCO_Host    = 'xxx'
TIBCO_Port    = 9000
TIBCO_UserId  = 'xxx'
TIBCO_Queue   = 'xxx.xxx.xxx.xx.XXX'

The layout of the script is as follows:

#! /usr/bin/env python
#
#    $Id:$
#
#  Purpose:  Scrape queue length from TIBCO queue
#
#-------------------------------------------------------------------------------

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

import CONFIG

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

__version__        = '1.2.0'

debug_flg          = False
verbose_flg        = False

sample_period      = 10    # seconds
no_samples         = 1440  # 4 hours

fd                 = None

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

tibco_host   = CONFIG.TIBCO_Host
port         = CONFIG.TIBCO_Port
tibco_userid = CONFIG.TIBCO_UserId
queue_name   = CONFIG.TIBCO_Queue
ssh_host     = CONFIG.SSH_Host
ssh_userid   = CONFIG.SSH_UserId

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

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.XXX
 Queue:                 XXXX.XXX
 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:Port>
"""

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])

   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' % (tibco_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", ssh_userid, ssh_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:])

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