From f9a95f3fb100bc75522f05d18c0fe82d110dc177 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Fri, 22 Dec 2023 10:44:24 +0100
Subject: [PATCH 1/3] add check_rearbackup

---
 check_rearbackup | 170 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 170 insertions(+)
 create mode 100755 check_rearbackup

diff --git a/check_rearbackup b/check_rearbackup
new file mode 100755
index 0000000..cb1644e
--- /dev/null
+++ b/check_rearbackup
@@ -0,0 +1,170 @@
+#!/bin/bash
+# ======================================================================
+#
+# Check REAR BACKUP
+#
+# ----------------------------------------------------------------------
+# 2023-12-22  v1.0  <axel.hahn@unibe.ch>
+# ======================================================================
+
+. $( dirname $0 )/inc_pluginfunctions
+
+export self_APPVERSION=1.0
+
+dir_reardata=/rearshare/rear-backup
+
+typeset -i iAgeSec
+typeset -i iAgeD
+
+typeset -i iTotal
+typeset -i iOK
+typeset -i iWarning
+typeset -i iCritical
+
+typeset -i iLimitWarning=7
+typeset -i iLimitCritical=14
+
+
+# ----------------------------------------------------------------------
+# FUNCTIONS
+# ----------------------------------------------------------------------
+
+# show help text
+function showHelp(){
+    local _self; _self=$(basename $0)
+cat <<EOF
+$( ph.showImlHelpHeader )
+
+Check if iso files of rear backup are not out of date.
+You can customize the values for
+* backup directory
+* limits for warning and critical
+
+This plugin sends performancedata.
+
+SYNTAX:
+
+  $_self [-h] [--dir PATH] [--critical VALUE] [--warning VALUE]
+
+OPTIONS:
+
+  -h, --help 
+      this help
+
+PARAMETERS:
+
+  -d, --dir PATH
+      set installation dir of iml deployment to find its check skript
+      default dir: ${dir_reardata}
+
+  -c, --critical VALUE
+      critical level in days (default: $iLimitCritical)
+
+  -w. --warning VALUE
+      warning level in days (default: $iLimitWarning)
+
+EXAMPLE:
+
+  $_self
+      Check backup data with initial values
+
+  $_self -d /data/mybackups
+      Check iso files a given directory
+    
+  $_self -d /data/mybackups -w 14 -c 28
+      Check iso files a given directory and customized limits
+
+EOF
+}
+
+
+# get age of a file in sec
+# param  string  filename to test
+function _getFileAge(){
+    echo $(($(date +%s) - $(date +%s -r "$1")))
+}
+
+# get a list of iso files in rear backup dir in alphabetic order
+function _getIsofiles(){
+        find "${dir_reardata}" -type f -name "*.iso" | sort
+}
+
+
+# ----------------------------------------------------------------------
+# MAIN
+# ----------------------------------------------------------------------
+
+
+# --- check param -h
+
+while [[ "$#" -gt 0 ]]; do case $1 in
+    -h|--help)      showHelp; exit 0;;
+    -d|--dir)       dir_reardata=$2; shift ;shift;;
+    -c|--critcal)   iLimitCritical=$2; shift ;shift;;
+    -w|--warning)   iLimitWarning=$2; shift ;shift;;
+    *) echo "ERROR: Unknown parameter: $1"; showHelp; exit 1;
+esac; done
+
+
+# --- test
+
+if [ $iLimitWarning -ge $iLimitCritical ]; then
+        ph.abort "ERROR: limit for warning ($iLimitWarning) must be lower than critical ($iLimitCritical). Get help with -h."
+fi
+
+if [ ! -d "$dir_reardata" ]; then
+        ph.abort "ERROR: directory [$dir_reardata] does not exist. Set it with -d. Get help with -h."
+fi
+
+if [ ! -r "$dir_reardata" ]; then
+        ph.abort "ERROR: unable to read the existing dir [$dir_reardata]. Check the permissions for $USER."
+fi
+
+
+# --- output data
+
+data="$( for myiso in $( _getIsofiles )
+        do
+                iAgeSec=$( _getFileAge "$myiso" )
+                iAgeD=$iAgeSec/60/60/24
+                sBar=$( yes '#' | head -n $iAgeD | tr -d "\n" )
+                sStatus="OK"
+
+                if [ $iAgeD -ge $iLimitWarning ]; then
+                        if [ $iAgeD -ge $iLimitCritical ]; then
+                                sStatus="CRITICAL"
+                        else
+                                sStatus="WARNING"
+                        fi
+                fi
+
+                printf "%-9s %3s d %-10s  %s\n" $sStatus $iAgeD "$sBar" "$( ls -l $myiso )"
+        done
+)"
+
+# --- Counters
+
+iTotal=$(    grep -c "."         <<< "$data" )
+iOK=$(       grep -c "^OK"       <<< "$data" )
+iWarning=$(  grep -c "^WARNING"  <<< "$data" )
+iCritical=$( grep -c "^CRITICAL" <<< "$data" )
+
+test $iWarning -gt 0  && ph.setStatus "warning"
+test $iCritical -gt 0 && ph.setStatus "critical"
+
+# --- ouput
+
+ph.status "Rear backup $dir_reardata - $iTotal total - $iCritical critical ($iLimitCritical d) .. $iWarning warnings ($iLimitWarning d) .. $iOK OK"
+
+echo "All iso files in alphabetic order:"
+echo "$data"
+
+# --- performance data
+
+ph.perfadd "ok"       "$iOK"       "" "" 0 $iTotal
+ph.perfadd "warning"  "$iWarning"  "" "" 0 $iTotal
+ph.perfadd "critical" "$iCritical" "" "" 0 $iTotal
+
+ph.exit
+
+# ----------------------------------------------------------------------
-- 
GitLab


From bd603a444bf51615a27cd4cb226aa16b3f1cbbdc Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Fri, 22 Dec 2023 10:44:32 +0100
Subject: [PATCH 2/3] add docs

---
 docs/20_Checks/_index.md           |  1 +
 docs/20_Checks/check_rearbackup.md | 89 ++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+)
 create mode 100644 docs/20_Checks/check_rearbackup.md

diff --git a/docs/20_Checks/_index.md b/docs/20_Checks/_index.md
index 839ca53..04adc02 100644
--- a/docs/20_Checks/_index.md
+++ b/docs/20_Checks/_index.md
@@ -40,6 +40,7 @@ There is one include script used by all checks:
 * check_proc_ressources
 * check_proc_zombie
 * [check_psqlserver](check_psqlserver.md)
+* [check_rearbackup](check_rearbackup.md)
 * [check_reboot_required](check_reboot_required.md)
 * check_requirements
 * check_sensuplugins
diff --git a/docs/20_Checks/check_rearbackup.md b/docs/20_Checks/check_rearbackup.md
new file mode 100644
index 0000000..c718824
--- /dev/null
+++ b/docs/20_Checks/check_rearbackup.md
@@ -0,0 +1,89 @@
+# Check Rear backup
+
+## Introduction
+
+This check can verify the age of all iso files in the backup target directory.
+
+## Requirements
+
+There can be a permission issue to reach the backup target directory.
+On our servers the backup target is using a user *imlbackup*. To give access to its resources we added the group "imlrear" for icinga.
+
+```txt
+$ id icingaclient
+uid=1041(icingaclient) gid=1041(icingaclient) groups=1041(icingaclient),1056(imlrear)
+                                                                             ^
+                                                                             |
+                                additional group for icinga client user -----+
+```
+
+## Syntax
+
+```txt
+______________________________________________________________________
+
+CHECK_REARBACKUP
+v1.0
+
+(c) Institute for Medical Education - University of Bern
+Licence: GNU GPL 3
+
+https://os-docs.iml.unibe.ch/icinga-checks/Checks/check_rearbackup.html
+______________________________________________________________________
+
+Check if iso files of rear backup are not out of date.
+You can customize the values for
+* backup directory
+* limits for warning and critical
+
+This plugin sends performancedata.
+
+SYNTAX:
+
+  check_rearbackup [-h] [--dir PATH] [--critical VALUE] [--warning VALUE]
+
+OPTIONS:
+
+  -h, --help 
+      this help
+
+PARAMETERS:
+
+  -d, --dir PATH
+      set installation dir of iml deployment to find its check skript
+      default dir: /rearshare/rear-backup
+
+  -c, --critical VALUE
+      critical level in days (default: 14)
+
+  -w. --warning VALUE
+      warning level in days (default: 7)
+
+EXAMPLE:
+
+  check_rearbackup
+      Check backup data with initial values
+
+  check_rearbackup -d /data/mybackups
+      Check iso files a given directory
+    
+  check_rearbackup -d /data/mybackups -w 14 -c 28
+      Check iso files a given directory and customized limits
+
+```
+
+## Examples
+
+### Rear backup status
+
+```txt
+# ./check_rearbackup
+CRITICAL: Rear backup /rearshare/rear-backup - 20 total - 0 critical (7 d) .. 2 warnings (14 d) .. 18 OK
+
+All iso files in alphabetic order:
+OK          4 d ####        -rw-rw-r--. 1 imlrear imlrear 3907082240 Dec 17 10:28 /rearshare/rear-backup/admin/admin.example.com_backup.iso
+OK          5 d #####       -rw-rw-r--. 1 imlrear imlrear 573384704 Dec 16 18:07 /rearshare/rear-backup/awx/awx.example.com_backup.iso
+(...)
+
+|ok=18;;;0;20 warning=2;;;0;20 critical=0;;;0;20
+```
-- 
GitLab


From 528c306a62837d03d3a16345d934f498bc34e4f1 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Fri, 22 Dec 2023 10:44:44 +0100
Subject: [PATCH 3/3] update iml_backup_server

---
 check_iml_backup_server                   | 39 ++++++++++++++++-------
 docs/20_Checks/check_iml_backup_server.md | 33 +++++++++++--------
 2 files changed, 47 insertions(+), 25 deletions(-)

diff --git a/check_iml_backup_server b/check_iml_backup_server
index b5f6134..3e5a141 100755
--- a/check_iml_backup_server
+++ b/check_iml_backup_server
@@ -10,11 +10,12 @@
 # 2020-07-27  v0.x  <axel.hahn@iml.unibe.ch>
 # 2023-12-20  v0.2  <axel.hahn@iml.unibe.ch>
 # 2023-12-21  v1.0  <axel.hahn@iml.unibe.ch>  first version
+# 2023-12-21  v1.1  <axel.hahn@iml.unibe.ch>  Update help text
 # ======================================================================
 
 
 . $(dirname $0)/inc_pluginfunctions
-export self_APPVERSION=1.0
+export self_APPVERSION=1.1
 
 sInstalldir=/opt/imlbackup/server
 sScript=storage_helper.sh
@@ -24,6 +25,7 @@ sMode=
 # FUNCTIONS
 # ----------------------------------------------------------------------
 
+# show help
 function showHelp(){
     local _self; _self=$(basename $0)
     cat <<EOF
@@ -35,33 +37,48 @@ You can get the count of connections or the status of server backup.
 See https://os-docs.iml.unibe.ch/iml-backup-server/
 
 SYNTAX:
-$_self [-d DIRECTORY] -m MODE
+
+  $_self [-d DIRECTORY] -m MODE
 
 OPTIONS:
-    -h or --help   show this help.
-    -d or --dir    set installation dir of iml deployment to find its check skript
-                   default dir: ${sInstalldir}
-                   script: ${sInstalldir}/${sScript}
-    -m or --mode   mode: set type of result; one of status|backupstatus
-                     backupstatus  status of backup sets of all servers
-                     status        count of connected servers
+
+  -h, --help
+      show this help.
+
+  -d, --dir PATH
+      set installation dir of iml deployment to find its check skript
+      default dir: ${sInstalldir}
+      script: ${sInstalldir}/${sScript}
+
+  -m, --mode MODE
+      mode: set type of result; one of status|backupstatus
+        backupstatus  status of backup sets of all servers
+        status        count of connected servers
 
 EXAMPLE:
-$_self -m status 
-$_self -m backupstatus 
+
+  $_self -m status 
+  $_self -m backupstatus 
 
 EOF
 }
 
+# get all server entries with a given status
+# param  string  status character in 1st column; one of .|?|D|E
+# param  string  text to parse
 function _getServerOfStatus(){
     local _key="$1"
     local _data="$2"
     grep "$_key .*| [0-9].* |" <<< "$_data"
 }
 
+# get count of servers with a given status
+# param  string  status character in 1st column; one of .|?|D|E
+# param  string  text to parse
 function _getStatusCount(){
     _getServerOfStatus "$1" "$2" | wc -l
 }
+
 # ----------------------------------------------------------------------
 # MAIN
 # ----------------------------------------------------------------------
diff --git a/docs/20_Checks/check_iml_backup_server.md b/docs/20_Checks/check_iml_backup_server.md
index d288733..6227e4d 100644
--- a/docs/20_Checks/check_iml_backup_server.md
+++ b/docs/20_Checks/check_iml_backup_server.md
@@ -26,7 +26,7 @@ uid=1041(icingaclient) gid=1041(icingaclient) groups=1041(icingaclient),1031(iml
 ______________________________________________________________________
 
 CHECK_IML_BACKUP_SERVER
-v1.0
+v1.1
 
 (c) Institute for Medical Education - University of Bern
 Licence: GNU GPL 3
@@ -40,20 +40,28 @@ You can get the count of connections or the status of server backup.
 See https://os-docs.iml.unibe.ch/iml-backup-server/
 
 SYNTAX:
-check_iml_backup_server [-d DIRECTORY] -m MODE
+
+  check_iml_backup_server [-d DIRECTORY] -m MODE
 
 OPTIONS:
-    -h or --help   show this help.
-    -d or --dir    set installation dir of iml deployment to find its check skript
-                   default dir: /opt/imlbackup/server
-                   script: /opt/imlbackup/server/storage_helper.sh
-    -m or --mode   mode: set type of result; one of status|backupstatus
-                     backupstatus  status of backup sets of all servers
-                     status        count of connected servers
+
+  -h, --help
+      show this help.
+
+  -d, --dir PATH
+      set installation dir of iml deployment to find its check skript
+      default dir: /opt/imlbackup/server
+      script: /opt/imlbackup/server/storage_helper.sh
+
+  -m, --mode MODE
+      mode: set type of result; one of status|backupstatus
+        backupstatus  status of backup sets of all servers
+        status        count of connected servers
 
 EXAMPLE:
-check_iml_backup_server -m status 
-check_iml_backup_server -m backupstatus 
+
+  check_iml_backup_server -m status 
+  check_iml_backup_server -m backupstatus 
 
 ```
 
@@ -61,9 +69,6 @@ check_iml_backup_server -m backupstatus
 
 ### Backup server status
 
-``check_http -u https://www.example.com/`` is a check that makes an http GET request.
-The queck is OK if the responded status code is no error - if it is 2xx (OK) or a redirect (3xx).
-
 ```txt
 # ./check_iml_backup_server -m backupstatus
 OK: Server Backups - 36 servers in backup - 0 errors - 7 disbled - 0 running
-- 
GitLab