From ca2e27e11ef53f8ea16891590121900b9804129d Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Tue, 11 Jun 2024 16:31:57 +0200
Subject: [PATCH] OP#7270 Icinga: Check EOL postgres: postgres 15

https://projects.iml.unibe.ch/work_packages/7270
---
 check_eol-versiondetect/detect-postgres | 28 ++++++++++++-------------
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/check_eol-versiondetect/detect-postgres b/check_eol-versiondetect/detect-postgres
index 3a4e107..dcb14ca 100755
--- a/check_eol-versiondetect/detect-postgres
+++ b/check_eol-versiondetect/detect-postgres
@@ -5,31 +5,31 @@
 # detect version of PostgreSQL
 #
 # ----------------------------------------------------------------------------
-# 2020-02-25  v1.0  <axel.hahn@iml.unibe.ch>  initial version
-# 2021-03-26  v1.1  <axel.hahn@iml.unibe.ch>  add locations if postgres is not in PATH
+# 2020-02-25  v1.0  <axel.hahn@iml.unibe.ch>      initial version
+# 2021-03-26  v1.1  <axel.hahn@iml.unibe.ch>      add locations if postgres is not in PATH
 # 2021-08-23  v1.1  <martin.gasser@iml.unibe.ch>  added location for v11
 # 2022-04-07  v1.2  <martin.gasser@iml.unibe.ch>  added location for v13
-
+# 2024-06-11  v1.3  <axel.hahn@iml.unibe.ch>      handle placeholders in otherplaces for latest version
 # ----------------------------------------------------------------------------
 
-#
-# WORKAROUND: add additional locations in $PATH
-#
-#otherlocations="/usr/pgsql-10/bin/ /some/other/postgres/location/bin/"
-
-otherlocations="/usr/pgsql-10/bin/ /usr/lib/postgresql/11/bin /usr/lib/postgresql/13/bin"
+# other locations. When using placeholders (like *) then it looks to the
+# highest number
+otherlocations="
+  /usr/pgsql-10/bin
+  /usr/lib/postgresql/*/bin
+"
 
 POSTGRES=$( which postgres 2>/dev/null )
 test -z "$POSTGRES" &&
   for mypath in ${otherlocations}
-  do
-    test -x "$mypath/postgres" && POSTGRES="$mypath/postgres"
+    do
+      if latest=$(ls -1dtr "${mypath}" 2>/dev/null | sort -n | tail -1 ); then
+        test -x "$latest/postgres" && POSTGRES="$latest/postgres"
+      fi
   done
 
-
-#
 # ----- example output
 #   postgres --version
-#   postgres (PostgreSQL) 9.2.24
+#   postgres (PostgreSQL) 15.6 (Debian 15.6-0+deb12u1)
 
 $POSTGRES --version | cut -f 3 -d " " | cut -f 1,2 -d '.'
-- 
GitLab