From f8ced56ef172169b9c1b7df674eb9be8bf0ccaa4 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Tue, 5 Sep 2023 17:40:38 +0200
Subject: [PATCH] WIP: check_http first lines

---
 check_http | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 144 insertions(+)
 create mode 100755 check_http

diff --git a/check_http b/check_http
new file mode 100755
index 0000000..83b6d28
--- /dev/null
+++ b/check_http
@@ -0,0 +1,144 @@
+#!/bin/bash
+# ================================================================================
+#
+# CHECK HTTP
+#
+#
+# -------------------------------------------------------------------------------
+# 2023-09-05  v0.1  <axel.hahn@unibe.ch>
+# ================================================================================
+
+. $( dirname $0 )/inc_pluginfunctions
+
+export self_APPVERSION=1.0
+
+# ----------------------------------------------------------------------
+# FUNCTIONS
+# ----------------------------------------------------------------------
+
+# show help text
+function showHelp(){
+    local _self; _self=$(basename $0)
+cat <<EOF
+$( ph.showImlHelpHeader )
+
+TODO
+
+SYNTAX:
+  $_self [-h] ...
+
+OPTIONS:
+
+  -h               this help
+
+  -u URL           Set url to fetch
+  -s STATUSCODE    Statuscode to ceck
+  -r REGEX         String to check in http response header
+  -b REGEX         String to check in response body 
+
+EXAMPLES:
+
+  $_self -u 
+
+EOF
+}
+
+function getHeader(){
+    set -vx
+    echo "$1" | sed -n "1,${2}p"
+}
+
+function getHeader(){
+    set -vx
+    echo "$1" | sed -n "1,${2}p"
+}
+
+# ----------------------------------------------------------------------
+# MAIN
+# ----------------------------------------------------------------------
+
+ph.hasParamoption "h" "$@"; bOptHelp=$?
+
+if [ $bOptHelp -eq 0 -o $# -eq 0 ]; then
+    showHelp
+    exit 0
+fi
+
+ph.require "curl"
+
+sUrl=$(     ph.getValueWithParam ''    u "$@")
+iStatus=$(  ph.getValueWithParam '200' s "$@")
+sHeader=$(  ph.getValueWithParam ''    r "$@")
+sBody=$(    ph.getValueWithParam ''    b "$@")
+
+curlParams="-si"
+sProblems=
+sOK=
+
+if [ -z "$sUrl" ]; then
+    ph.setStatus unknown
+    ph.status "Wrong parameters - no url was given."
+    ph.exit
+fi
+ 
+test -z "$sBody"  && curlParams+=" -I"
+
+
+
+out=$( curl $curlParams "$sUrl" )
+
+
+iHeaderEnd=$( echo "$out" | grep -n ^$'\r' | cut -f 1 -d ':')
+
+_header=$(echo "$out" | sed -n "1,${iHeaderEnd}p")
+_body=$(  echo "$out" | sed -n "${iHeaderEnd},\$p")
+
+# echo "HEADER"; echo "$_header"
+# echo "BODY"; echo "$_body"
+
+
+# --- test status
+if [ -n "$iStatus" ]; then
+    if ! grep -i "^HTTP/[0-9\.]* ${iStatus}" <<< "${_header}" >/dev/null; then
+        ph.setStatus critical
+        sProblems+="- Statuscode is not [${iStatus}];\n"
+    else
+        sOK+="- Statuscode is [${iStatus}];\n"
+    fi
+fi
+
+
+if [ -n "$sHeader" ]; then
+    if ! grep -i "$sHeader" <<< "${_header}" >/dev/null; then
+        ph.setStatus critical
+        sProblems+="- Header does not contain [${sHeader}];\n"
+    else
+        sOK+="- [${sHeader}] was found in header;\n"
+    fi
+    
+fi
+
+if [ -n "$sBody" ]; then
+    if ! grep -i "$sBody" <<< "${_body}" >/dev/null; then
+        ph.setStatus critical
+        sProblems+="- Body does not contain [${sBody}];\n"
+    else
+        sOK+="- [${sBody}] was found in body;\n"
+    fi
+    
+fi
+
+
+test -n "$sProblems" && sProblems="Problems:\n$sProblems\n"
+test -n "$sOK"       && sOK="Found:\n$sOK"
+
+ph.status "Url ${sUrl}"
+echo
+echo -e "${sProblems}${sOK}"
+echo
+
+# echo "HEADER:"; echo "$_header"
+
+ph.exit
+
+# ----------------------------------------------------------------------
-- 
GitLab