Commit af43dff4 authored by ericw's avatar ericw
Browse files

Test of FroNtier squid at local site

parent 937fbcc2
#!/usr/bin/env python
"""
Tests the local FroNtier squid
"""
#
# Assumes:
#
# 1) environmental variables SAME_OK and SAME_ERROR are defined
# 2) environmental variable $CMS_PATH is defined
# 3) file $CMS_PATH/SITECONF/local/JobConfig/site-local-config.xml
# contains the location of the local FroNtier squid server
#
__revision__ = "$Id: test_squid.py,v 1.1 2007/02/09 21:27:01 ericw Exp $"
import os
import sys
import urllib2
from xml.dom.minidom import parseString
from xml.dom.minidom import parse
import base64
import zlib
import curses.ascii
import time
#
# Check that environmental variable SAME_OK is set
#
if not os.environ.has_key("SAME_OK"):
print "test_squid.py: Error. SAME_OK not defined"
sys.exit(1)
same_ok = int(os.environ["SAME_OK"])
#
# Check that environmental variable SAME_ERROR is set
#
if not os.environ.has_key("SAME_ERROR"):
print "test_squid.py: Error. SAME_ERROR not defined"
sys.exit(1)
same_error = int(os.environ["SAME_ERROR"])
#
# Check that environmental variable CMS_PATH is set
#
if not os.environ.has_key("CMS_PATH"):
print "test_squid.py: Error. CMS_PATH not defined"
sys.exit(same_error)
#
# Check that file $CMS_PATH/SITECONF/local/JobConfig/site-local-config.xml
# exists
#
slc = os.environ["CMS_PATH"] + \
"/SITECONF/local/JobConfig/site-local-config.xml"
if not os.path.exists(slc):
print "test_squid.py: Error. file " + slc + " does not exist"
#
# Parse site-local-config.xml for local FroNtier squid server
# See http://docs.python.org/lib/module-xml.dom.minidom.html
#
slcdom = parse(slc)
plist = slcdom.getElementsByTagName("proxy")
#
# Check for at least one proxy tag
#
if len(plist) == 0:
print "test_squid.py: Error. no proxy tag in file " + slc
sys.exit(same_error)
#
# Set proxy
#
ptag = plist[0]
squid = ptag.getAttribute("url")
print "squid " + squid
os.environ["http_proxy"] = squid
#
# Following code from Sinisa Veseli
#
# Set parameters
#
frontierUrl = "http://cmsfrontier.cern.ch:8080/FrontierInt/Frontier"
frontierQuery = "SELECT 1 FROM DUAL"
decodeFlag = True
refreshFlag = True
statsFlag = False
retrieveZiplevel = ""
#
# Print parameters
#
if statsFlag:
pass
else:
print "Using Frontier URL: ", frontierUrl
print "Query: ", frontierQuery
print "Decode results: ", decodeFlag
print "Refresh cache: ", refreshFlag
#
# Encode query
#
compQuery = zlib.compress(frontierQuery, 9)
encQuery = base64.binascii.b2a_base64(compQuery).replace("+", ".")
#
# Set up FroNtier request
#
format = "%s?type=frontier_request:1:DEFAULT&encoding=BLOB%s&p1=%s"
frontierRequest = format % (frontierUrl, retrieveZiplevel, encQuery)
if statsFlag:
pass
else:
print "\nFrontier Request:\n", frontierRequest
#
# Add refresh header if needed
#
request = urllib2.Request(frontierRequest)
if refreshFlag:
request.add_header("pragma", "no-cache")
#
# Start and time query
#
queryStart = time.localtime()
if statsFlag:
pass
else:
print "\nQuery started: ", time.strftime("%m/%d/%y %H:%M:%S", queryStart)
t1 = time.time()
#
try:
result = urllib2.urlopen(request).read()
except urllib2.URLError:
print "test_squid.py: Error. squid " + squid + " is down or unreachable"
sys.exit(same_error)
except:
print "test_squid.py: Error."
sys.exit(same_error)
#
t2 = time.time()
queryEnd = time.localtime()
if statsFlag:
duration = (t2-t1)
size = len(result)
print duration, size, size/duration
else:
print "Query ended: ", time.strftime("%m/%d/%y %H:%M:%S", queryEnd)
print "Query time: %s [seconds]\n" % (t2-t1)
#
# Print out result
#
if decodeFlag:
print "Query result:\n", result
dom = parseString(result)
dataList = dom.getElementsByTagName("data")
# Control characters represent records, but I won't bother with that now,
# and will simply replace those by space.
for data in dataList:
if data.firstChild is not None:
row = base64.decodestring(data.firstChild.data)
if retrieveZiplevel != "":
row = zlib.decompress(row)
control = [ '\x00', '\x01', '\x02', '\x03', '\x04',
'\x05', '\x06', '\x08', '\x09', '\x0a',
'\x0b', '\x0c', '\x0d', '\x1b', '\x17' ]
for c in control:
row = row.replace(c, ' ')
print "\nFields: "
endFirstRow = row.find('\x07')
firstRow = row[:endFirstRow]
for c in firstRow:
if curses.ascii.isctrl(c):
firstRow = firstRow.replace(c, '\n')
print firstRow
print "\nRecords:"
pos = endFirstRow + 1
while True:
newrow = row[pos:]
endRow = newrow.find('\x07')
if endRow < 0:
break
fixedRow = newrow[:endRow]
pos = pos + endRow + 1
fixedRow = fixedRow.replace('\n', '')
print fixedRow
print "OK"
sys.exit(same_ok)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment