Documentation for PythonAPI
22.40.00py2

example

#!/usr/bin/env python
""" 
Script to extract connlog data for a specific date range.
"""

__version__ = "1.1 2008-11-14 Procera Networks"

###############################################################################
#
#                          NO WARRANTY
#
#  BECAUSE THE PROGRAM IS PROVIDED FREE OF CHARGE, THERE IS NO WARRANTY
#  FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
#  OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
#  PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
#  OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
#  TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
#  PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
#  REPAIR OR CORRECTION.
#
#  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
#  WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
#  REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
#  INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
#  OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
#  TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
#  YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
#  PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGES.
#
###############################################################################

import os
import sys
import getopt
import packetlogic2

CMD_NAME = os.path.basename(sys.argv[0])

def error(message):
    print "%s: %s" % (CMD_NAME, message)
    print "Try `%s --help' for more information." % (CMD_NAME)
    sys.exit(1)

def usage():
    print """Usage: %(name)s [OPTION] -H hostname -U username -P password -s startdate -e enddate
  Creates connlog-n for each IP protocol.

    -H, --hostname   Hostname of PacketLogic
    -U, --username   Username to connect with
    -P, --password   Password for user

    -s, --startdate  start date to dump from (YYYY-MM-DD)
    -e, --enddate    end date to dump to (YYYY-MM-DD)
    -p, --prefix     filename prefix that will be used instead of
                     `connlog-'

        --help     display this help and exit
    """ % {'name': CMD_NAME}

if len(sys.argv) < 2:
    usage()
    sys.exit(2)

try:
    opts, args = getopt.gnu_getopt(sys.argv[1:], "s:e:p:H:U:P:", ["help", "hostname=", "username=", "password=", "startdate=", "enddate=", "prefix="])
except getopt.GetoptError, (msg, opt):
    error(msg)

startdate = None
enddate = None
hostname = None
username = None
password = None
prefix = "connlog-"

for option, argument in opts:
    if option in "--help":
        usage()
        sys.exit(0)
    if option in ("-H", "--hostname"):
        hostname = argument
    if option in ("-U", "--username"):
        username = argument
    if option in ("-P", "--password"):
        password = argument
    if option in ("-p", "--prefix"):
        prefix = argument
    if option in ("-s", "--startdate"):
        startdate = argument
    if option in ("-e", "--enddate"):
        enddate = argument

# check for mandatory arguments
if hostname is None:
    error("missing hostname argument")
if username is None:
    error("missing username argument")
if password is None:
    error("missing password argument")
if startdate is None:
    error("missing startdate argument")
if enddate is None:
    error("missing enddate argument")


# Connect to PLDB
pl = packetlogic2.connect(hostname, username, password)
r = pl.ConnLog()


class DumpFile(file):
    def dump(self, x):
        # Continuously dump '.' to stdout (once every ~750 entries) so the
        # script does not appear hung.
        sys.stdout.write(".")
        sys.stdout.flush()
        for line in x:
            self.write("%s\n" % line)

print "Dumping data from %s 00:00 to %s 00:00. Please wait." % (startdate, enddate)

# Iterate over available ip protocols and extract connlog data for each into
# a seperate file.
for i in range(0, 256):
    my_file = DumpFile("%s%d" % (prefix, i), 'w')
    r.search("%s 00:00" % (startdate), "%s 00:00" % (enddate), protocol=i, max_hits=0, callback=my_file.dump)

print "\ndone."