From a41da22c8ab3b394cc3ba8bd5ae25e7403faf9f0 Mon Sep 17 00:00:00 2001
From: Robin Sonnabend <robin@fsmpi.rwth-aachen.de>
Date: Sat, 22 Jun 2019 23:54:06 +0200
Subject: [PATCH] Use apt to query the installed zabbix version

---
 zabbix-repo/files/check-update.py | 31 ++++++++++++++++++++++++++-----
 zabbix-repo/templates/crontab.j2  |  2 +-
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/zabbix-repo/files/check-update.py b/zabbix-repo/files/check-update.py
index 8fdccd9..fdfdff2 100644
--- a/zabbix-repo/files/check-update.py
+++ b/zabbix-repo/files/check-update.py
@@ -2,10 +2,30 @@
 
 import urllib.request
 import re
+import subprocess as sp
 
 PATTERN = r'<a\shref="(?P<link>\d\.\d/)">(?P<version>\d\.\d)/</a>\s+(?P<date>\d{2}-[A-Z][a-z]{2}-\d{4} \d{2}:\d{2})\s+-'
+PKG_NAME = "zabbix-server-pgsql"
 
-def get_versions():
+
+def parse_apt_version(version_str):
+    return list(map(int, version_str.split(":")[1].split("-")[0].split(".")))[:2]
+
+
+def get_installation():
+    command = ["apt", "show", PKG_NAME]
+    version = None
+    source = None
+    for line in sp.check_output(
+            command, stderr=sp.PIPE, universal_newlines=True).splitlines():
+        if line.startswith("Version:"):
+            version = parse_apt_version(line.split(":", 1)[1])
+        elif line.startswith("APT-Sources:"):
+            source = line.split(":", 1)[1].strip()
+    return version, source
+
+
+def get_remote_versions():
     response = urllib.request.urlopen("https://repo.zabbix.com/zabbix/")
     page_content = response.read().decode("utf-8")
     for match in re.findall(PATTERN, page_content):
@@ -16,10 +36,11 @@ def get_versions():
 
 
 def main():
-    import sys
-    current_version = sys.argv[1]
-    current_major, current_minor = map(int, current_version.split("."))
-    highest_version, date = sorted(get_versions())[-1]
+    current_version, source = get_installation()
+    if not source.startswith("http://repo.zabbix.com/"):
+        return
+    current_major, current_minor = current_version
+    highest_version, date = sorted(get_remote_versions())[-1]
     if (current_major, current_minor) < highest_version:
         print(
             "New Zabbix version is available: {} from {}, please upgrade!".format(
diff --git a/zabbix-repo/templates/crontab.j2 b/zabbix-repo/templates/crontab.j2
index 3e84657..a6e534f 100644
--- a/zabbix-repo/templates/crontab.j2
+++ b/zabbix-repo/templates/crontab.j2
@@ -1 +1 @@
-{{60|random(seed=inventory_hostname)}} {{6|random(seed=inventory_hostname)}} * * * root /usr/local/sbin/check-update.py {{zabbix_version}}
+{{60|random(seed=inventory_hostname)}} {{6|random(seed=inventory_hostname)}} * * * root /usr/local/sbin/check-update.py
-- 
GitLab