[packages/nagios-plugin-check_json_health] Ret 3; check for json header (loosely) and report if we get something else.
arekm
arekm at pld-linux.org
Thu Apr 9 14:56:16 CEST 2026
commit 99385337a757e6ce60337efda5289b4f4c8374ac
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date: Thu Apr 9 14:56:02 2026 +0200
Ret 3; check for json header (loosely) and report if we get something else.
check_json_health.py | 14 +++++++++++---
nagios-plugin-check_json_health.spec | 2 +-
2 files changed, 12 insertions(+), 4 deletions(-)
---
diff --git a/nagios-plugin-check_json_health.spec b/nagios-plugin-check_json_health.spec
index ec45bd9..af0f7f4 100644
--- a/nagios-plugin-check_json_health.spec
+++ b/nagios-plugin-check_json_health.spec
@@ -2,7 +2,7 @@
Summary: Nagios plugin to check JSON health endpoints
Name: nagios-plugin-%{plugin}
Version: 1.0
-Release: 2
+Release: 3
License: Public Domain (CC0 1.0)
Group: Networking
Source0: %{plugin}.py
diff --git a/check_json_health.py b/check_json_health.py
index 33c170c..9ea89ba 100644
--- a/check_json_health.py
+++ b/check_json_health.py
@@ -106,6 +106,7 @@ def main():
'Cache-Control': 'no-cache',
})
resp = conn.getresponse()
+ content_type = resp.getheader('Content-Type', '')
body = resp.read().decode('utf-8')
conn.close()
else:
@@ -113,7 +114,13 @@ def main():
headers={'Cache-Control': 'no-cache'})
with urllib.request.urlopen(req, timeout=args.timeout,
context=ctx) as resp:
+ content_type = resp.getheader('Content-Type', '')
body = resp.read().decode('utf-8')
+
+ if 'json' not in content_type:
+ snippet = sanitize(body[:200]) if body else '(empty response)'
+ print(f'UNKNOWN - {url}: unexpected Content-Type: {sanitize(content_type)} [{snippet}]')
+ sys.exit(UNKNOWN)
except Exception as e:
# Connectivity/infrastructure failure = UNKNOWN (service state is indeterminate)
print(f'UNKNOWN - {url}: {e}')
@@ -148,9 +155,10 @@ def main():
print(output)
sys.exit(exit_code)
except Exception as e:
- # JSON parse or field extraction failure = CRITICAL (service returned garbage)
- print(f'CRITICAL - {url}: {e}')
- sys.exit(CRITICAL)
+ # JSON parse or field extraction failure = UNKNOWN (service state is indeterminate)
+ snippet = sanitize(body[:200]) if body else '(empty response)'
+ print(f'UNKNOWN - {url}: {e} [{snippet}]')
+ sys.exit(UNKNOWN)
if __name__ == '__main__':
================================================================
---- gitweb:
http://git.pld-linux.org/gitweb.cgi/packages/nagios-plugin-check_json_health.git/commitdiff/99385337a757e6ce60337efda5289b4f4c8374ac
More information about the pld-cvs-commit
mailing list