From e68f10ce29af3d8ca6d4bedda9e9aa00dfb4208b Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Wed, 30 Aug 2023 13:43:59 +0200
Subject: [PATCH] add check "httpd_status_codes"

---
 check_couchdb                   | 63 ++++++++++++++++++++++++---------
 docs/20_Checks/check_couchdb.md | 37 +++++++++++++++++++
 2 files changed, 84 insertions(+), 16 deletions(-)

diff --git a/check_couchdb b/check_couchdb
index 243798d..6bbdba6 100755
--- a/check_couchdb
+++ b/check_couchdb
@@ -20,11 +20,12 @@
 # 2023-08-28  v0.5  <axel.hahn@unibe.ch>  add checks "open_databases" + "open_os_files"
 # 2023-08-28  v0.6  <axel.hahn@unibe.ch>  add check "httpd_methods"
 # 2023-08-29  v0.7  <axel.hahn@unibe.ch>  add check "httpd"
+# 2023-08-29  v0.8  <axel.hahn@unibe.ch>  add check "httpd_status_codes"
 # ======================================================================
 
 . $(dirname $0)/inc_pluginfunctions
 
-export self_APPVERSION=0.7
+export self_APPVERSION=0.8
 
 cfgfile=/etc/icingaclient/.couchdb
 export RESPONSE
@@ -45,21 +46,22 @@ SYNTAX:
 $_self [-h] [-t FILE] -m MODE
 
 OPTIONS:
-  -h or --help    show this help.
-  -c CFGFILE      set a custom config file
-                  default: /etc/icingaclient/.couchdb
-  -m MODE         test a value; for debugging purposes the full json 
-                  response will be shown
+  -h or --help        show this help.
+  -c CFGFILE          set a custom config file
+                      default: /etc/icingaclient/.couchdb
+  -m MODE             test a value; for debugging purposes the full json 
+                      response will be shown
 
   MODE is one of
-  httpd           Show counters for http request types
-  httpd_methods   Show counters for http request methods
-  open_databases  show number of open databases
-  open_os_files   show number of file descriptors CouchDB has open
-  replication     show last replication status
-  pending         show count of pending updates for nodes, dbs and users
-  replication     show last replication status
-  up              show general couchdb health status
+  httpd               Show counters for http request types
+  httpd_methods       Show counters for http request methods
+  httpd_status_codes  Show counters per http status code
+  open_databases      show number of open databases
+  open_os_files       show number of file descriptors CouchDB has open
+  replication         show last replication status
+  pending             show count of pending updates for nodes, dbs and users
+  replication         show last replication status
+  up                  show general couchdb health status
 
 EXAMPLE:
 $_self -m up
@@ -146,7 +148,7 @@ case "${sMode}" in
         for myVar in $( grep "[a-z\_]" <<< "$_status" | cut -f 1 -d ':' )
         do  
             _iValue=$( grep "^${myVar}:" <<< "$_status" | cut -f 2 -d ':' )
-            _label="couchdb-${myVar//_/-}" # underscrore to minus
+            _label="couchdb-hp-${myVar//_/-}" # underscrore to minus
             _iDelta=$( ph.perfdeltaspeed "${_label}" $_iValue )
             printf "%30s %10s %10s per sec\n" "$myVar" $_iValue $_iDelta
             ph.perfadd "${myVar}" "$_iDelta"    "" ""
@@ -174,7 +176,8 @@ case "${sMode}" in
         for myVar in $( grep "[A-Z]" <<< "$_status" | cut -f 1 -d ':' )
         do            
             _iValue=$( grep "$myVar" <<< "$_status" | cut -f 2 -d ':' )
-            _iDelta=$( ph.perfdeltaspeed "couchdb-method-${myVar}" $_iValue)
+            _label="couchdb-hm-${myVar//_/-}" # underscrore to minus
+            _iDelta=$( ph.perfdeltaspeed "${_label}" $_iValue)
             printf "%10s %10s %10s per sec\n" "$myVar" $_iValue $_iDelta
             ph.perfadd "${myVar}" "$_iDelta"    "" ""
         done
@@ -182,6 +185,34 @@ case "${sMode}" in
         # echo "${_status}" | jq
         ;;
     # ............................................................
+    "httpd_status_codes")
+        REQ=/_node/_local/_stats/couchdb/httpd_status_codes
+        abortOnWrongResponse "${REQ}" '"value":'
+
+        typeset -i _iValue
+        typeset -i _iDelta
+
+        _status=$( jq 'with_entries(.value |= .value)' <<< "${RESPONSE}" | sed 's#[", ]##g' | grep '^[1-9]' )
+        # this returns:
+        # 200:199460
+        # 201:0
+        # 202:0
+        # 204:0
+        # ...
+
+        ph.status "Couchdb :: Http status codes"
+        printf "%10s %10s %10s\n" "Status" "Counter" "Delta" | tr ' ' '_'
+        for myVar in $( grep "[1-9]" <<< "$_status" | cut -f 1 -d ':' )
+        do            
+            _iValue=$( grep "$myVar" <<< "$_status" | cut -f 2 -d ':' )
+            _label="couchdb-hs-${myVar//_/-}" # underscrore to minus
+            _iDelta=$( ph.perfdeltaspeed "${_label}" $_iValue)
+            printf "%10s %10s %10s per sec\n" "$myVar" $_iValue $_iDelta
+            ph.perfadd "http${myVar}" "$_iDelta"    "" ""
+        done
+        ;;
+
+    # ............................................................
     "open_databases"|"open_os_files")
         REQ=/_node/_local/_stats/couchdb/${sMode}
         abortOnWrongResponse "${REQ}" '"value":'
diff --git a/docs/20_Checks/check_couchdb.md b/docs/20_Checks/check_couchdb.md
index 735c6de..eb6cd51 100644
--- a/docs/20_Checks/check_couchdb.md
+++ b/docs/20_Checks/check_couchdb.md
@@ -160,6 +160,43 @@ ____Method____Counter______Delta
  |copy=0;; delete=0;; get=1;; head=0;; options=0;; post=0;; put=0;;
 ```
 
+### httpd_status_codes
+
+Show counters per http status code.
+All values are taken from ``/_node/_local/_stats/couchdb/httpd_status_codes``
+
+``check_couchdb -m httpd_status_codes`` returns
+
+```txt
+OK: Couchdb :: Http status codes
+____Status____Counter______Delta
+       200     199970          1 per sec
+       201          0          0 per sec
+       202          0          0 per sec
+       204          0          0 per sec
+       206          0          0 per sec
+       301          0          0 per sec
+       302          0          0 per sec
+       304          0          0 per sec
+       400          0          0 per sec
+       401          2          0 per sec
+       403          0          0 per sec
+       404          9          0 per sec
+       405          0          0 per sec
+       406          0          0 per sec
+       409          0          0 per sec
+       412          0          0 per sec
+       413          0          0 per sec
+       414          0          0 per sec
+       415          0          0 per sec
+       416          0          0 per sec
+       417          0          0 per sec
+       500          0          0 per sec
+       501          0          0 per sec
+       503          0          0 per sec
+ |http200=1;; http201=0;; http202=0;; http204=0;; http206=0;; http301=0;; http302=0;; http304=0;; http400=0;; http401=0;; http403=0;; http404=0;; http405=0;; http406=0;; http409=0;; http412=0;; http413=0;; http414=0;; http415=0;; http416=0;; http417=0;; http500=0;; http501=0;; http503=0;; 
+```
+
 ### open_databases
 
 Show number of open databases
-- 
GitLab