diff --git a/check_couchdb b/check_couchdb index 150ceaa93b11fdf43880028ba31bae30fc5f19a8..38388c0877696569feb2d6373ce2e2bbdb1499be 100755 --- a/check_couchdb +++ b/check_couchdb @@ -15,11 +15,12 @@ # ---------------------------------------------------------------------- # 2023-08-28 v0.1 <axel.hahn@unibe.ch> first lines # 2023-08-28 v0.2 <axel.hahn@unibe.ch> first check "up" +# 2023-08-28 v0.3 <axel.hahn@unibe.ch> add check "replication" # ====================================================================== . $(dirname $0)/inc_pluginfunctions -export self_APPVERSION=0.2 +export self_APPVERSION=0.3 cfgfile=/etc/icingaclient/.couchdb @@ -42,7 +43,12 @@ OPTIONS: -h or --help show this help. -c CFGFILE set a custom config file default: ${cfgfile} - -m MODE test a value; for debugging purposes + -m MODE test a value; for debugging purposes the full json + response will be shown + + MODE is one of + up show general couchdb status + replication show last replication status EXAMPLE: $_self -m up @@ -51,6 +57,27 @@ $_self -m up EOF } +# get couchdb status by given url and a filter +# The check aborts here if no data were found. +# param string url to request; the part behind couchdb base url +# param string string to search for in the content +function get(){ + _path="$1" + curl -s "${COUCH_URL}${_path}" +} + +function abortOnWrongResponse(){ + _response="$1" + _path="$2" + _filter="$3" + + if ! grep "$_filter" <<< "$_response" >/dev/null ; then + echo "ERROR: Wrong response from $_path - it does not contain $_filter" + curl -si "${COUCH_URL}${2}" + ph.abort + fi +} + # ---------------------------------------------------------------------- # MAIN # ---------------------------------------------------------------------- @@ -64,6 +91,8 @@ case "$1" in *) esac +ph.require jq + sMode=$(ph.getValueWithParam '' "m" "$@") cfgfile=$(ph.getValueWithParam "${cfgfile}" "c" "$@") @@ -84,12 +113,32 @@ fi case "${sMode}" in "up") - response=$( curl -s "${COUCH_URL}/_up" ) + response=$( get "/_up" ) + abortOnWrongResponse "$response" "/_up" '"status":"' + _status=$( jq '.status' <<< "${response}" | tr -d '"') if ! echo "$_status" | grep "ok" >/dev/null; then ph.setStatus critical fi - ph.status "Couchdb status (value in /_up is '$_status')" + ph.status "Couchdb status (value 'status' in /_up is '$_status')" + echo "Reponse: "; echo "${response}" | jq + ;; + "replication") + response=$( get "/_up" ) + abortOnWrongResponse "$response" "/_up" '"status":"' + + _status=$( jq '.seeds[] | .last_replication_status' <<< "${response}" | grep -v "null" | tr -d '"') + + # there can be multiple sections "seeds" and multiple line responses. + # remove all lines with "ok" and check if there is any "bad content" left + _nonok=$( echo "$_status" | grep -v "ok" ) + if [ -n "$_nonok" ]; then + ph.setStatus critical + fi + if ! echo "$_status" | grep "ok" >/dev/null; then + ph.setStatus critical + fi + ph.status "Couchdb replication" echo "Reponse: "; echo "${response}" | jq ;; *) @@ -101,4 +150,4 @@ esac ph.exit -# ---------------------------------------------------------------------- +# ---------------------------------------------------------------------- \ No newline at end of file