test_squid.py 5.63 KB
Newer Older
ericw's avatar
ericw committed
1
2
3
4
5
6
7
8
9
10
11
12
#!/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
#
ericw's avatar
ericw committed
13
__revision__ = "$Id: test_squid.py,v 1.5 2007/02/20 17:03:44 ericw Exp $"
ericw's avatar
ericw committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
#
ericw's avatar
ericw committed
47
48
49
50
slcfil = os.environ["CMS_PATH"] + \
         "/SITECONF/local/JobConfig/site-local-config.xml"
if not os.path.exists(slcfil):
    print "test_squid.py: Error. file " + slcfil + " does not exist"
ericw's avatar
ericw committed
51
    sys.exit(same_error)
ericw's avatar
ericw committed
52
#
ericw's avatar
ericw committed
53
# Read and parse site-local-config.xml into a dom
ericw's avatar
ericw committed
54
55
# See http://docs.python.org/lib/module-xml.dom.minidom.html
#
ericw's avatar
ericw committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
slcdom = parse(slcfil)
#
# Work out site name from site-local-config.xml
#
silist = slcdom.getElementsByTagName("site")
if len(silist) == 0:
    site = "UNKNOWN"
else:
    stag = silist[0]
    site = stag.getAttribute("name")
print "site " + site    
#
# Work out local FroNtier squid server from site-local-config.xml
# Note that http_proxy is not set when running on CERN site
#
if site != "CERN":
ericw's avatar
ericw committed
72
73
74
#
# Check for at least one proxy tag
#
ericw's avatar
ericw committed
75
76
77
78
    prlist = slcdom.getElementsByTagName("proxy")
    if len(prlist) == 0:
        print "test_squid.py: Error. no proxy tag in file " + slcfil
        sys.exit(same_error)    
ericw's avatar
ericw committed
79
#
ericw's avatar
ericw committed
80
# Work out address of squid
ericw's avatar
ericw committed
81
#
ericw's avatar
ericw committed
82
83
84
85
86
87
88
    prtag = prlist[0]
    squid = prtag.getAttribute("url")
#
# Set proxy server
#
    print "squid " + squid   
    os.environ["http_proxy"] = squid
ericw's avatar
ericw committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#
# 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:
ericw's avatar
ericw committed
137
    print "\nQuery started: ", time.strftime("%m/%d/%y %H:%M:%S %Z", queryStart)
ericw's avatar
ericw committed
138
139
140
141
142
t1 = time.time()
#
try:
    result = urllib2.urlopen(request).read()
except urllib2.URLError:
ericw's avatar
ericw committed
143
144
145
146
147
148
149
150
    if site != "CERN":
        print "test_squid.py: Error. squid " + squid + \
              " is down or unreachable"
        sys.exit(same_error)
    else:
        print "test_squid.py: Error. FroNtier server " + frontierUrl + \
              " is down or unreachable"
        sys.exit(same_error)
ericw's avatar
ericw committed
151
152
153
154
155
156
157
158
159
160
161
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:
ericw's avatar
ericw committed
162
    print "Query ended: ", time.strftime("%m/%d/%y %H:%M:%S %Z", queryEnd)
ericw's avatar
ericw committed
163
    print "Query time: %0.2f [seconds]\n" % (t2-t1)
ericw's avatar
ericw committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#
# 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
ericw's avatar
ericw committed
201
202
203
#
# Exit with success exit code
#
ericw's avatar
ericw committed
204
205
print "OK"
sys.exit(same_ok)