From 7144be7ce63c17a3e9df892356c43011f0523bb5 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@iml.unibe.ch>
Date: Fri, 4 Mar 2022 17:09:54 +0100
Subject: [PATCH] abort on http 5xx error

---
 director-cli.sh  | 5 +++++
 icinga-cli.sh    | 7 ++++++-
 inc_functions.sh | 4 ++++
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/director-cli.sh b/director-cli.sh
index a6311bb..5c7af16 100755
--- a/director-cli.sh
+++ b/director-cli.sh
@@ -16,6 +16,7 @@
 # ----------------------------------------------------------------------
 # ah = axel.hahn@iml.unibe.ch
 # 2022-02-16  v0.2  ah  add --cfg param
+# 2022-03-04  v0.3  ah  abort on http 5xx error
 # ======================================================================
 
 tmpfile=/tmp/outcurl.tmp
@@ -365,6 +366,10 @@ typeset cfg_dryrun=false
     if [ ${_paramDryrun} = false ]; then
       _wd ">>>>> $_paramAction $_paramObj [${_object_name}] >> $_sMethod $_sUrl $_jsondata"
       http.makeRequest "$_sMethod" "$_sUrl" "$_jsondata"
+      if http.isServerError >/dev/null; then
+        echo "CRITICAL ERROR: Director API request failed with a server error $_sMethod $_sUrl"
+        exit 1
+      fi
       if [ $_bShowResponse = true ]; then
         http.getResponseHeader
         http.getResponse
diff --git a/icinga-cli.sh b/icinga-cli.sh
index 1db04e0..74c36a2 100755
--- a/icinga-cli.sh
+++ b/icinga-cli.sh
@@ -13,11 +13,12 @@
 # 2021-03-..  init
 # 2022-01-11  v0.7  ah  shellcheck
 # 2022-02-16  v0.8  ah  add --cfg param
+# 2022-03-04  v0.9  ah  abort on http 5xx error
 # ======================================================================
 
 
 _product="ICINGA PASSIVE CLIENT"
-_version="0.8"
+_version="0.9"
 _license="GNU GPL 3.0"
 _copyright='(c) 2020 Institute for Medical Education * University of Bern'
 
@@ -248,6 +249,10 @@ function icingaHost(){
       http.setCacheTtl     $_iRefreshCache
       http.setCacheFile    $_localCache
       http.makeRequest GET $_apiRequest
+      if http.isServerError >/dev/null; then
+        echo "CRITICAL ERROR: Icinga2 API request failed with a server error GET $_apiRequest"
+        exit 1
+      fi
 
       # set return code of GET action
       http.isOk >/dev/null
diff --git a/inc_functions.sh b/inc_functions.sh
index c1affcc..2cb5268 100644
--- a/inc_functions.sh
+++ b/inc_functions.sh
@@ -232,6 +232,10 @@ function _getIpPrivate(){
   function _APIcall(){
 
     http.makeRequest "$1" "$2" "$3"
+    if http.isServerError >/dev/null; then
+      echo "CRITICAL ERROR: API request failed with a server error $1 $2"
+      exit 1
+    fi
 
     http.getResponseHeader
     http.getResponse
-- 
GitLab