Created
October 13, 2011 08:01
-
-
Save graffic/1283688 to your computer and use it in GitHub Desktop.
Webservices checker script for nagios
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2.7 | |
from random import randrange | |
from datetime import timedelta,datetime | |
def prepare_message(): | |
message = """<?xml version="1.0" encoding="UTF-8"?> | |
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" | |
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" | |
xmlns:xsd="http://www.w3.org/2001/XMLSchema" | |
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> | |
<SOAP-ENV:Body> | |
<df:GetJourneyList xmlns:df="http://tempuri.org/"> | |
<df:requestedJourney xmlns:tns="http://schemas.datacontract.org/2004/07/DreamFlightWCF"> | |
<tns:AdultPrice>0</tns:AdultPrice> | |
<tns:ChildPrice>0</tns:ChildPrice> | |
<tns:ClassCode>Y</tns:ClassCode> | |
<tns:DirectFlight>false</tns:DirectFlight> | |
<tns:FlyBackDate xsi:nil="true" /> | |
<tns:FlyOutDate>%sT00:00:00</tns:FlyOutDate> | |
<tns:FromAirport>SVO</tns:FromAirport> | |
<tns:InfantPrice>0</tns:InfantPrice> | |
<tns:NumOfAdults>1</tns:NumOfAdults> | |
<tns:NumOfChildren>0</tns:NumOfChildren> | |
<tns:NumOfInfants>0</tns:NumOfInfants> | |
<tns:OutFlights/> | |
<tns:ReturnFlights/> | |
<tns:SpecificAirlineCode/> | |
<tns:ToAirport>YVR</tns:ToAirport> | |
<tns:TotalPrice>0</tns:TotalPrice> | |
</df:requestedJourney> | |
<df:numberOfResults>42</df:numberOfResults> | |
<df:domain>airtickets24.com/ru</df:domain> | |
</df:GetJourneyList> | |
</SOAP-ENV:Body> | |
</SOAP-ENV:Envelope>""" | |
departure_days = randrange(15,60) | |
departure_date = datetime.today() + timedelta(days=departure_days) | |
return message %(departure_date.strftime("%Y-%m-%d")) | |
def check_host(ip): | |
import httplib,StringIO,gzip | |
headers = {"Accept-Encoding":"gzip", | |
"SOAPAction":"http://tempuri.org/IJourneySearch/GetJourneyList", | |
"Content-Type":"text/xml; charset=utf-8", | |
"Host":"ws.pamediakopes.gr", | |
"Accpet":"*/*" | |
} | |
ws = httplib.HTTPConnection(ip, timeout=30) | |
message = prepare_message() | |
ws.request("POST","/j.svc",message,headers) | |
response = ws.getresponse() | |
response_information = [response.status, response.reason,response.getheader("X-Powered-By")] | |
is_compressed = False | |
if response.getheader("Content-Encoding") == "gzip": | |
is_compressed = True | |
compressed = StringIO.StringIO(response.read()) | |
gz = gzip.GzipFile(fileobj=compressed) | |
#print gz.read() | |
else: | |
pass | |
#print response.read() | |
ws.close() | |
response_information.append(is_compressed) | |
return response_information | |
def check_arguments(): | |
import argparse | |
parser = argparse.ArgumentParser(description='Query flights webservices') | |
parser.add_argument('ip',help='ip of the server to check') | |
args = parser.parse_args() | |
return args.ip | |
if __name__ == "__main__": | |
# execute and benchmark | |
start = datetime.now() | |
result = check_host(check_arguments()) | |
end = datetime.now() | |
duration = end - start | |
# parse result | |
string_status = "OK" | |
integer_status = 0 | |
if not result[3]: | |
string_status = "WARNING" | |
integer_status = 1 | |
if result[0] != 200 or result[1] != "OK": | |
string_status = "CRITICAL" | |
integer_status = 2 | |
print "%s - %s|response=%fs"%(string_status | |
," ".join(map(lambda x: str(x),result)) | |
,duration.seconds + duration.microseconds/1000000.0) | |
exit(integer_status) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment