BOSWatch/plugins/httpRequest/httpRequest.py

76 lines
2.3 KiB
Python
Raw Normal View History

2015-05-20 19:31:50 +02:00
#!/usr/bin/python
# -*- coding: cp1252 -*-
#########
# USAGE
#
# Config
# ======
# to read a option from config File
# VALUE = globals.config.get("SECTION", "OPTION")
#
# Data from boswatch.py
# =====================
# use data["KEY"] for Alarm Data from boswatch.py
# for usable KEYs in different Functions (FMS|ZVEI|POC) see interface.txt
#
# LOG Messages
# ============
# send Log Messages with logging.LOGLEVEL("MESSAGE")
# usable Loglevels debug|info|warning|error|exception|critical
# if you use .exception in Try:Exception: Construct, it logs the Python EX.message too
import logging # Global logger
2015-05-22 10:44:39 +02:00
import httplib #for the HTTP request
from includes import globals # Global variables
2015-05-20 19:31:50 +02:00
def run(typ,freq,data):
try:
#ConfigParser
logging.debug("reading config file")
try:
2015-05-20 19:41:18 +02:00
for key,val in globals.config.items("httpRequest"):
2015-05-20 19:31:50 +02:00
logging.debug(" - %s = %s", key, val)
except:
logging.exception("cannot read config file")
2015-05-22 10:44:39 +02:00
########## User Plugin CODE ##########
try:
logging.debug("send %s HTTP request", typ)
if typ == "FMS":
httprequest = httplib.HTTPConnection(globals.config.get("httpRequest", "fms_url"))
httprequest.request("HEAD", "/")
elif typ == "ZVEI":
httprequest = httplib.HTTPConnection(globals.config.get("httpRequest", "zvei_url"))
httprequest.request("HEAD", "/")
elif typ == "POC":
httprequest = httplib.HTTPConnection(globals.config.get("httpRequest", "poc_url"))
httprequest.request("HEAD", "/")
else:
logging.warning("Invalid Typ: %s", typ)
except:
2015-05-23 08:40:35 +02:00
logging.exception("cannot send HTTP request")
2015-05-20 19:31:50 +02:00
else:
2015-05-22 10:44:39 +02:00
try:
httpresponse = httprequest.getresponse()
if str(httpresponse.status) == "200": #Check HTTP Response an print a Log or Error
logging.debug("HTTP response: %s - %s" , str(httpresponse.status), str(httpresponse.reason))
else:
logging.warning("HTTP response: %s - %s" , str(httpresponse.status), str(httpresponse.reason))
except NameError: #if var httprequest does not exist
logging.exception("no HTTP request been sended")
except: #otherwise
logging.exception("cannot get HTTP response")
2015-05-22 20:17:57 +02:00
finally:
2015-05-22 22:58:27 +02:00
logging.debug("close HTTP-Connection")
2015-05-22 20:17:57 +02:00
httprequest.close()
2015-05-22 10:00:09 +02:00
########## User Plugin CODE ##########
2015-05-20 19:31:50 +02:00
except:
logging.exception("unknown error")