Bladeren bron

Updated version to 0.2 after major rework to get more consistency

Michael Honkoop 2 weken geleden
bovenliggende
commit
0ef1d6049a
1 gewijzigde bestanden met toevoegingen van 52 en 7 verwijderingen
  1. 52 7
      localchecks/fusionauth-version.sh

+ 52 - 7
localchecks/fusionauth-version.sh

@@ -1,6 +1,10 @@
 #!/bin/bash
 
 # Version 0.1 - initial start of this localcheck
+# Version 0.2 - refined, and commented code further
+
+# Current active version of the script
+SCRIPT_VERSION=0.2
 
 # Installation
 # This file should be place on a/the fusionauth host in /usr/lib/check_mk_agent/local
@@ -8,23 +12,64 @@
 # (optional) if you want the check to run on a different schedule (like once per hour) put it in a subdirectory of above path
 # named to the number of seconds of the interval wanted, so 3600/ for once an hour, 86400 for once per day.
 #
-# Also do not forget to make this file executable with chmod +x /usr/lib/check_mk_agent/local/fusionauth.sh
+# Also do not forget to make this file executable with chmod +x /usr/lib/check_mk_agent/local/fusionauth-version.sh
 
 # (c) Michael Honkoop <mhonkoop@comsolve.nl>
 
 # License: GNU General Public License v2
 
+######################
+# Configurable Parameters
+######################
+
+# url to check releases against
 RELEASEURL="https://license.fusionauth.io/api/latest-version"
 
-FUSIONAUTH_VERSION_AVAIL=$(curl -s --connect-timeout 5 --fail-with-body $RELEASEURL)
+# Service name
+SERVICE="FusionAuth"
+
+######################
+# The code below should not be changed, unless you know EXACTLY what you are doing.
+# All configureable parameters are above this section.
+######################
+
+# Check if curl package is present on the system (required for this check)
+if ! command -v curl >/dev/null 2>&1; then
+    echo "2 \"$SERVICE\" - $SERVICE localcheck requires curl package to be installed"
+    exit 0
+fi
+
+# Get currently installed package information of FusionAuth
+mapfile -t fusionauth_packages < <(rpm -qa 'fusionauth*')
+
+# Check if package-manager returned packages, if none found report it
+if [ "${#fusionauth_packages[@]}" -eq 0 ]; then
+    echo "2 \"$SERVICE\" - No $SERVICE packages found on this host"
+    exit 0
+fi
+
+# Get latest avaiable version information.
+FUSIONAUTH_VERSION_AVAIL=$(curl -fsS --connect-timeout 5 --max-time 10 "$RELEASEURL")
+curl_rc=$?
 
+# Validate return-code from curl
+if [ "$curl_rc" -ne 0 ]; then
+    echo "3 \"$SERVICE\" - Unable to query release URL (curl exit code $curl_rc)"
+    exit 0
+fi
+
+# Check if the query did return something, if not report it
 if [ -z "$FUSIONAUTH_VERSION_AVAIL" ];
 then
-    echo "1 \"FusionAuth\" - No data received from release-URL, can not determine latest release-version"
+    echo "3 \"$SERVICE\" - No data received from release URL, can not determine latest release-version"
     exit 0
 fi
 
-mapfile -t fusionauth_packages < <(rpm -qa 'fusionauth*')
+# Validate that the query returned a version
+if [[ ! "$FUSIONAUTH_VERSION_AVAIL" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
+    echo "3 \"$SERVICE\" - Unexpected response from release URL: $FUSIONAUTH_VERSION_AVAIL"
+    exit 0
+fi
 
 outdated_packages=()
 
@@ -34,8 +79,8 @@ for package in "${fusionauth_packages[@]}"; do
     fi
 done
 
-if [ ${#outdated_packages[@]} -eq 0 ]; then
-    echo "0 \"FusionAuth\" - All FusionAuth packages are up to date ($FUSIONAUTH_VERSION_AVAIL)"
+if [ "${#outdated_packages[@]}" -eq 0 ]; then
+    echo "0 \"$SERVICE\" - All $SERVICE packages are up to date ($FUSIONAUTH_VERSION_AVAIL)"
 else
-    echo "1 \"FusionAuth\" - Latest version Available ($FUSIONAUTH_VERSION_AVAIL), Update required for: ${outdated_packages[*]}"
+    echo "1 \"$SERVICE\" - Latest version Available ($FUSIONAUTH_VERSION_AVAIL), Update required for: ${outdated_packages[*]}"
 fi