From 7e9a6fd598d0f62f84e9461aa847f5f19abe32d4 Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Thu, 6 Jan 2022 11:38:17 +0100 Subject: [PATCH 1/6] add native python client example --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 9694afa..d950c9b 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,11 @@ For example: -H "application/json" \ -d "{ "service_name" : "name", "token" : "secret_token" } \ https://server:port/ + +Or directly in native python: + + import requests + requests.post("https://server:port/", json={"service_name" : "name", "token" : "secret_token" }) ## Icinga (Serverside) Requests Use the [python-script]() as a command to execute, you can pass *protocol*, *host*, *port* and *service\_name* as arguments. From be6f36e6090028222db76e4929124e7b55b4e4df Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Thu, 6 Jan 2022 11:53:59 +0100 Subject: [PATCH 2/6] clarify info & status field in submitted json --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d950c9b..92e98eb 100644 --- a/README.md +++ b/README.md @@ -17,19 +17,20 @@ Services are configured in *services.json* as objects like this: } ## Client Requests -Client must send a *POST-request* with *Content-Type: application/json* containing the service name and secret token as fields. +Client must send a *POST-request* with *Content-Type: application/json* containing the service name, secret token and status as fields, the *'info'* field is optional. For example: curl -X POST \ -H "application/json" \ - -d "{ "service_name" : "name", "token" : "secret_token" } \ + -d "{ "service_name" : "name", "token" : "secret_token", "status" : "OK|WARNING|CRITICAL", "info" : "additional information" } \ https://server:port/ Or directly in native python: import requests - requests.post("https://server:port/", json={"service_name" : "name", "token" : "secret_token" }) + requests.post("https://server:port/", json={"service_name" : "name", "token" : "secret_token", + "status" : "OK|WARNING|CRITICAL", "info" : "additional information" }) ## Icinga (Serverside) Requests Use the [python-script]() as a command to execute, you can pass *protocol*, *host*, *port* and *service\_name* as arguments. From fbd46c3c5f44b1faeffcd11a636c1421d1991793 Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Thu, 6 Jan 2022 11:54:50 +0100 Subject: [PATCH 3/6] break overlong line --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 92e98eb..17ed129 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,8 @@ For example: curl -X POST \ -H "application/json" \ - -d "{ "service_name" : "name", "token" : "secret_token", "status" : "OK|WARNING|CRITICAL", "info" : "additional information" } \ + -d "{ "service_name" : "name", "token" : "secret_token", \ + "status" : "OK|WARNING|CRITICAL", "info" : "additional information" } \ https://server:port/ Or directly in native python: From f009e3928063dac9b5accce74871878f6dd995f1 Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Thu, 6 Jan 2022 12:56:21 +0100 Subject: [PATCH 4/6] reply with correct service name Reply with the correct service name if the service is configured but has never reported in as the icinga command expects it. --- server.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server.py b/server.py index e67cedd..678a282 100755 --- a/server.py +++ b/server.py @@ -72,7 +72,8 @@ def default(): sqlalchemy.desc(Status.timestamp)).first() if not lastSuccess and not lastFail: - return flask.jsonify(buildReponseDict(None)) + # service has never reported in # + return flask.jsonify(buildReponseDict(None, service=service)) elif not lastSuccess and lastFail: return flask.jsonify(buildReponseDict(lastFail)) else: From 1e008aef741f0f8ec95ddf47c618ab3208189c9b Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Thu, 6 Jan 2022 12:58:21 +0100 Subject: [PATCH 5/6] add check for 404 service not configured --- icinga-gateway-command.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/icinga-gateway-command.py b/icinga-gateway-command.py index 9c14e95..41fd40f 100755 --- a/icinga-gateway-command.py +++ b/icinga-gateway-command.py @@ -34,6 +34,10 @@ if __name__ == "__main__": response = requests.get(url) # check response status # + if response.status_code == 404: + print("The gateway does not have this service configured (404)") + sys.exit(STATUS_UNKOWN) + response.raise_for_status() # validate response content # From d1af8eb0f1132b9666c4611439fa99a1066ee568 Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Thu, 6 Jan 2022 12:59:13 +0100 Subject: [PATCH 6/6] fix typo and break line --- icinga-gateway-command.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/icinga-gateway-command.py b/icinga-gateway-command.py index 41fd40f..85e81ed 100755 --- a/icinga-gateway-command.py +++ b/icinga-gateway-command.py @@ -48,7 +48,8 @@ if __name__ == "__main__": if not args.service == jsonDict["service"]: retService = jsonDict["service"] - print("Gateway returned wrong bad name ({} for {})".format(retService, args.service)) + fmtText = "Gateway returned wrong or bad service name ({} for {})" + print(fmtText.format(retService, args.service)) # handle content # parsedTime = datetime.datetime.fromtimestamp(int(jsonDict["timestamp"]))