test_squid.py 5.9 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
#
13
__revision__ = "$Id: test_squid.py,v 1.7 2007/03/01 16:07:53 ericw Exp $"
ericw's avatar
ericw committed
14
15
16
17
18
19
20
21
22
23
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
#
ericw's avatar
ericw committed
24
25
# Print out node name
#
26
print "node " + os.uname()[1] 
ericw's avatar
ericw committed
27
#
ericw's avatar
ericw committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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
51
52
53
54
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
55
    sys.exit(same_error)
ericw's avatar
ericw committed
56
#
ericw's avatar
ericw committed
57
# Read and parse site-local-config.xml into a dom
ericw's avatar
ericw committed
58
59
# See http://docs.python.org/lib/module-xml.dom.minidom.html
#
ericw's avatar
ericw committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
76
77
78
#
# Check for at least one proxy tag
#
ericw's avatar
ericw committed
79
80
81
82
    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
83
#
ericw's avatar
ericw committed
84
# Work out address of squid
ericw's avatar
ericw committed
85
#
ericw's avatar
ericw committed
86
87
88
89
90
91
92
    prtag = prlist[0]
    squid = prtag.getAttribute("url")
#
# Set proxy server
#
    print "squid " + squid   
    os.environ["http_proxy"] = squid
ericw's avatar
ericw committed
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
137
138
139
140
#
# 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
141
    print "\nQuery started: ", time.strftime("%m/%d/%y %H:%M:%S %Z", queryStart)
ericw's avatar
ericw committed
142
143
144
145
t1 = time.time()
#
try:
    result = urllib2.urlopen(request).read()
146
147
except urllib2.URLError, e:
    rc, message = e.reason
ericw's avatar
ericw committed
148
    if site != "CERN":
149
150
151
        print "test_squid.py: Error. squid " + squid + " is down, "
        print "                      unreachable or will not reply."
        print "                      " + message
ericw's avatar
ericw committed
152
153
154
        sys.exit(same_error)
    else:
        print "test_squid.py: Error. FroNtier server " + frontierUrl + \
155
156
157
              " is down, "
        print "                      unreachable or will not reply."
        print "                      " + message
ericw's avatar
ericw committed
158
        sys.exit(same_error)
ericw's avatar
ericw committed
159
160
161
162
163
164
165
166
167
168
169
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
170
    print "Query ended: ", time.strftime("%m/%d/%y %H:%M:%S %Z", queryEnd)
ericw's avatar
ericw committed
171
    print "Query time: %0.2f [seconds]\n" % (t2-t1)
ericw's avatar
ericw committed
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
201
202
203
204
205
206
207
208
#
# 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
209
210
211
#
# Exit with success exit code
#
ericw's avatar
ericw committed
212
213
print "OK"
sys.exit(same_ok)