fbtracex.py

import fdb
import argparse
import os

def main():
    # CLI interface
    parser = argparse.ArgumentParser(description="Tool to manage trace sessions")

    parser.add_argument('-o','--host',
                        help="Server host.")
    parser.add_argument('-u','--user',
                        default=os.environ.get('ISC_USER', 'sysdba'),
                        help="User name")
    parser.add_argument('-p','--password',
                        default=os.environ.get('ISC_PASSWORD', None),
                        help="User password")
    parser.add_argument('-l','--list', action='store_true',
                        help="List trace sessions.")
    parser.add_argument('-s','--suspend', type=int, metavar='SESSION_ID',
                        help="Pause trace # session.")
    parser.add_argument('-r','--resume', type=int, metavar='SESSION_ID',
                        help="resume trace # session.")
    parser.add_argument('-c','--cancel', type=int, metavar='SESSION_ID',
                        help="cancel trace # session.")

    args = parser.parse_args()
    # We need password
    if not args.password:
        print "A password is required to use the Services Manager."
        print
        parser.print_help()
        return
    # Connect to Service Manager
    if args.host:
        svc_host = args.host + ':service_mgr'
    else:
        svc_host = 'service_mgr'
    svc = fdb.services.connect(host=svc_host,user=args.user,password=args.password)
    # Adn action!
    try:
        if args.list:
            sessions = svc.trace_list()
            if len(sessions) == 0:
                print "No trace sessions."
            else:
                for sid, data in sessions.items():
                    print "Trace session %i" % sid
                    if data.has_key('name'):
                        print "  name:", data['name']
                    print "  date:", data['date'].strftime('%Y-%m-%d %H:%M:%S')
                    print "  user:", data['user']
                    print " flags:", ', '.join(data['flags'])
        elif args.suspend:
            print svc.trace_suspend(args.suspend)
        elif args.resume:
            print svc.trace_resume(args.resume)
        elif args.cancel:
            print svc.trace_stop(args.cancel)
    except Exception as e:
        print "ERROR:"
        print e.args

if __name__ == '__main__':
    main()

Help printout:

usage: fbtracex.py [-h] [-o HOST] [-u USER] [-p PASSWORD] [-l] [-s SESSION_ID]
                   [-r SESSION_ID] [-c SESSION_ID]

Tool to manage trace sessions

optional arguments:
  -h, --help            show this help message and exit
  -o HOST, --host HOST  Server host.
  -u USER, --user USER  User name
  -p PASSWORD, --password PASSWORD
                        User password
  -l, --list            List trace sessions.
  -s SESSION_ID, --suspend SESSION_ID
                        Pause trace # session.
  -r SESSION_ID, --resume SESSION_ID
                        resume trace # session.
  -c SESSION_ID, --cancel SESSION_ID
                        cancel trace # session.

Example:

> fbtracex.py -p masterkey -l
Trace session 2
  date: 2012-10-15 12:35:07
  user: SYSDBA
 flags: active,  admin,  trace
> fbtracex.py -p masterkey -c 2
Trace session ID 2 stopped

Previous topic

follow_log.py

Next topic

fbtracein.py