mirror of
https://github.com/FAUSheppy/icinga-webhook-gateway
synced 2025-12-06 07:21:38 +01:00
implement icinga command script
This commit is contained in:
64
icinga-gateway-command.py
Normal file
64
icinga-gateway-command.py
Normal file
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
import sys
|
||||
import argparse
|
||||
import requests
|
||||
|
||||
STATUS_OK = 0
|
||||
STATUS_WARNING = 1
|
||||
STATUS_CRITICAL = 2
|
||||
STATUS_UNKOWN = 3
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
parser = argparse.ArgumentParser(description='Icinga Lazy Report Gateway Connector',
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||
parser.add_argument('--protocol', default="http", help='Protocol to use')
|
||||
parser.add_argument('--host', default="127.0.0.1", help='Host to connect to')
|
||||
parser.add_argument('--port', default=5000, help='Port to connect to')
|
||||
parser.add_argument('--service', required=True, help='Service name to check for')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
urlBase = "{proto}://{host}:{port}/?service={service}".
|
||||
url = urlBase.format(proto=args.protocol, host=args.host, port=args.port)
|
||||
|
||||
try:
|
||||
|
||||
# send request #
|
||||
response = requests.get(url)
|
||||
|
||||
# check response status #
|
||||
response.raise_for_status()
|
||||
|
||||
# validate response content #
|
||||
jsonDict = response.json()
|
||||
if not all([s in jsonDict for s in ["service", "status", "timestamp", "info"]):
|
||||
print("Gateway return a bad response: {}".format(jsonDict))
|
||||
sys.exit(STATUS_UNKOWN)
|
||||
|
||||
if not service == jsonDict["service"]:
|
||||
print("Gateway returned wrong bad name ({} for {})".format(jsonDict["service"], service)
|
||||
|
||||
# handle content #
|
||||
parsedTime = datetime.datetime.from_timestamp(int(jsonDict["timestamp"]))
|
||||
timeString = parsedTime.isoformat()
|
||||
|
||||
baseInfo = "{service}: {status} - {info} ({time})"
|
||||
status = jsonDict["status"]
|
||||
info = jsonDict["info"]
|
||||
|
||||
print(baseInfo.format(service=service, status=status, info=info, timestamp=timeString)
|
||||
if status == "OK":
|
||||
sys.exit(STATUS_OK)
|
||||
elif status == "WARNING":
|
||||
sys.exit(STATUS_WARNING)
|
||||
elif status == "CRITICAL":
|
||||
sys.exit(STATUS_CRITICAL)
|
||||
else:
|
||||
sys.exit(STATUS_UNKOWN)
|
||||
|
||||
except requests.exceptions.HTTPError as e:
|
||||
print("Gateway unavailable")
|
||||
|
||||
sys.exit(STATUS_UNKOWN)
|
||||
Reference in New Issue
Block a user