From 2a3041fb5755ecaa718ad511d5310f489564cb60 Mon Sep 17 00:00:00 2001 From: Alexandr Mikhailov Date: Wed, 16 Dec 2015 16:58:24 +0300 Subject: [PATCH 1/7] Added Zabbix module --- i3pystatus/zabbix.py | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 i3pystatus/zabbix.py diff --git a/i3pystatus/zabbix.py b/i3pystatus/zabbix.py new file mode 100644 index 0000000..96c7d1f --- /dev/null +++ b/i3pystatus/zabbix.py @@ -0,0 +1,63 @@ +from i3pystatus import IntervalModule +from pyzabbix import ZabbixAPI + + +class Zabbix(IntervalModule): + """ + Zabbix alerts watcher + + Requires pyzabbix + """ + settings = ( + ("zabbix_server", "Zabbix Server URL"), + ("zabbix_user", "Zabbix API User"), + ("zabbix_password", "Zabbix users password"), + ("interval", "Update interval"), + "format" + ) + required = ("zabbix_server", "zabbix_user", "zabbix_password") + interval = 60 + format = "{default}" + + def run(self): + + alerts_color = ["#DBDBDB", "#D6F6FF", "#FFF6A5", "#FFB689", "#FF9999", "#FF3838"] + zapi = ZabbixAPI(self.zabbix_server) + zapi.login(self.zabbix_user, self.zabbix_password) + triggers = zapi.trigger.get(only_true=1, + skipDependent=1, + monitored=1, + active=1, + min_severity=2, + output=["priority"], + withLastEventUnacknowledged=1, + ) + alerts_list = [t['priority'] for t in triggers] + alerts = [0, 0, 0, 0, 0, 0] + colors = [] + for i in range(0, 6): + alerts[i] = alerts_list.count(str(i)) + if alerts[i] == 0: + alerts_color[i] = "#FFFFFF" + + + cdict = { + "default": "{0}:{a[5]}/{a[4]}/{a[3]}/{a[2]}/{a[1]}/{a[0]}".format(sum(alerts), a=alerts), + "a5": alerts[5], + "a4": alerts[4], + "a3": alerts[3], + "a2": alerts[2], + "a1": alerts[1], + "a0": alerts[0], + "c5": alerts_color[5], + "c4": alerts_color[4], + "c3": alerts_color[3], + "c2": alerts_color[2], + "c1": alerts_color[1], + "c0": alerts_color[0], + "total": sum(alerts) + } + + self.output = { + "full_text": self.format.format(**cdict) + } From 0c15903d4ef3b32eb070a55489283a8eb77e944a Mon Sep 17 00:00:00 2001 From: Alexandr Mikhailov Date: Thu, 17 Dec 2015 16:44:30 +0300 Subject: [PATCH 2/7] Simplify individual alerts assignment --- i3pystatus/zabbix.py | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/i3pystatus/zabbix.py b/i3pystatus/zabbix.py index 96c7d1f..bdc8cf2 100644 --- a/i3pystatus/zabbix.py +++ b/i3pystatus/zabbix.py @@ -34,29 +34,17 @@ class Zabbix(IntervalModule): ) alerts_list = [t['priority'] for t in triggers] alerts = [0, 0, 0, 0, 0, 0] - colors = [] + cdict = {} for i in range(0, 6): - alerts[i] = alerts_list.count(str(i)) - if alerts[i] == 0: - alerts_color[i] = "#FFFFFF" + alerts[i] = alerts_list.count(str(i)) + cdict["a%s" % i]=alerts[i] + if alerts[i] == 0: + cdict["c%s" % i] = "#FFFFFF" + else: + cdict["c%s" % i] = alerts_color[i] - - cdict = { - "default": "{0}:{a[5]}/{a[4]}/{a[3]}/{a[2]}/{a[1]}/{a[0]}".format(sum(alerts), a=alerts), - "a5": alerts[5], - "a4": alerts[4], - "a3": alerts[3], - "a2": alerts[2], - "a1": alerts[1], - "a0": alerts[0], - "c5": alerts_color[5], - "c4": alerts_color[4], - "c3": alerts_color[3], - "c2": alerts_color[2], - "c1": alerts_color[1], - "c0": alerts_color[0], - "total": sum(alerts) - } + cdict["default"] = "{0}:{a[5]}/{a[4]}/{a[3]}/{a[2]}/{a[1]}/{a[0]}".format(sum(alerts), a=alerts) + cdict["total"] = sum(alerts) self.output = { "full_text": self.format.format(**cdict) From 0c52b5586cff3107ebd967133e75dff8050bb7cf Mon Sep 17 00:00:00 2001 From: Alexandr Mikhailov Date: Thu, 17 Dec 2015 23:18:37 +0300 Subject: [PATCH 3/7] Added Exception for incorrect Zabbix authentication and implemented global color for module result --- i3pystatus/zabbix.py | 58 +++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/i3pystatus/zabbix.py b/i3pystatus/zabbix.py index bdc8cf2..8532fc5 100644 --- a/i3pystatus/zabbix.py +++ b/i3pystatus/zabbix.py @@ -6,6 +6,12 @@ class Zabbix(IntervalModule): """ Zabbix alerts watcher + .. rubric:: Available formatters + * {default} - Full output count alerts like total:a5/a4/a3/a2/a1/a0 + * {total} - Total count of alerts + * {aX_count} - Count alerts of X severity + * {colorX} - Predicted color for X severity. It can be used with Pango markup hint for different colours at each severity with + Requires pyzabbix """ settings = ( @@ -23,29 +29,37 @@ class Zabbix(IntervalModule): alerts_color = ["#DBDBDB", "#D6F6FF", "#FFF6A5", "#FFB689", "#FF9999", "#FF3838"] zapi = ZabbixAPI(self.zabbix_server) - zapi.login(self.zabbix_user, self.zabbix_password) - triggers = zapi.trigger.get(only_true=1, - skipDependent=1, - monitored=1, - active=1, - min_severity=2, - output=["priority"], - withLastEventUnacknowledged=1, - ) - alerts_list = [t['priority'] for t in triggers] - alerts = [0, 0, 0, 0, 0, 0] - cdict = {} - for i in range(0, 6): - alerts[i] = alerts_list.count(str(i)) - cdict["a%s" % i]=alerts[i] - if alerts[i] == 0: - cdict["c%s" % i] = "#FFFFFF" - else: - cdict["c%s" % i] = alerts_color[i] + try: + zapi.login(self.zabbix_user, self.zabbix_password) + triggers = zapi.trigger.get(only_true=1, + skipDependent=1, + monitored=1, + active=1, + min_severity=2, + output=["priority"], + withLastEventUnacknowledged=1, + ) + alerts_list = [t['priority'] for t in triggers] + alerts = [0, 0, 0, 0, 0, 0] + cdict = {} + for i in range(0, 6): + alerts[i] = alerts_list.count(str(i)) + cdict["a%s_count" % i]=alerts[i] + if alerts[i] == 0: + cdict["color%s" % i] = "#FFFFFF" + else: + cdict["color%s" % i] = alerts_color[i] + + cdict["default"] = "{0}:{a[5]}/{a[4]}/{a[3]}/{a[2]}/{a[1]}/{a[0]}".format(sum(alerts), a=alerts) + cdict["total"] = sum(alerts) + color = alerts_color[max(alerts)] + result = self.format.format(**cdict) - cdict["default"] = "{0}:{a[5]}/{a[4]}/{a[3]}/{a[2]}/{a[1]}/{a[0]}".format(sum(alerts), a=alerts) - cdict["total"] = sum(alerts) + except Exception as e: + result = "Zabbix connection error" + color = "#FF0000" self.output = { - "full_text": self.format.format(**cdict) + "full_text": result, + "color": color } From bfe367b83674aea95e6971ff00c7db8b19c492ca Mon Sep 17 00:00:00 2001 From: Alexandr Mikhailov Date: Thu, 17 Dec 2015 23:25:53 +0300 Subject: [PATCH 4/7] Pep8 correction --- i3pystatus/zabbix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/i3pystatus/zabbix.py b/i3pystatus/zabbix.py index 8532fc5..3bd81fb 100644 --- a/i3pystatus/zabbix.py +++ b/i3pystatus/zabbix.py @@ -44,12 +44,12 @@ class Zabbix(IntervalModule): cdict = {} for i in range(0, 6): alerts[i] = alerts_list.count(str(i)) - cdict["a%s_count" % i]=alerts[i] + cdict["a%s_count" % i] = alerts[i] if alerts[i] == 0: cdict["color%s" % i] = "#FFFFFF" else: cdict["color%s" % i] = alerts_color[i] - + cdict["default"] = "{0}:{a[5]}/{a[4]}/{a[3]}/{a[2]}/{a[1]}/{a[0]}".format(sum(alerts), a=alerts) cdict["total"] = sum(alerts) color = alerts_color[max(alerts)] From 38f7c6cd8019188800baeafc2bbb0d6bcee5e9a5 Mon Sep 17 00:00:00 2001 From: Alexandr Mikhailov Date: Fri, 18 Dec 2015 00:46:10 +0300 Subject: [PATCH 5/7] Another PEP8 compatibility fix --- docs/conf.py | 3 ++- i3pystatus/zabbix.py | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/conf.py b/docs/conf.py index 38a49b2..c60eff5 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -32,7 +32,8 @@ MOCK_MODULES = [ "bs4", "dota2py", "novaclient.v2", - "speedtest_cli" + "speedtest_cli", + "pyzabbix" ] for mod_name in MOCK_MODULES: diff --git a/i3pystatus/zabbix.py b/i3pystatus/zabbix.py index 3bd81fb..36db01f 100644 --- a/i3pystatus/zabbix.py +++ b/i3pystatus/zabbix.py @@ -6,14 +6,16 @@ class Zabbix(IntervalModule): """ Zabbix alerts watcher + Requires: pyzabbix + .. rubric:: Available formatters + * {default} - Full output count alerts like total:a5/a4/a3/a2/a1/a0 * {total} - Total count of alerts * {aX_count} - Count alerts of X severity * {colorX} - Predicted color for X severity. It can be used with Pango markup hint for different colours at each severity with - - Requires pyzabbix """ + settings = ( ("zabbix_server", "Zabbix Server URL"), ("zabbix_user", "Zabbix API User"), @@ -21,6 +23,7 @@ class Zabbix(IntervalModule): ("interval", "Update interval"), "format" ) + required = ("zabbix_server", "zabbix_user", "zabbix_password") interval = 60 format = "{default}" From 86d876fd5875fc8d625145ed046890ea5934839d Mon Sep 17 00:00:00 2001 From: Alexandr Mikhailov Date: Fri, 18 Dec 2015 01:20:49 +0300 Subject: [PATCH 6/7] Fixed global color with max severity alert --- i3pystatus/zabbix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i3pystatus/zabbix.py b/i3pystatus/zabbix.py index 36db01f..5e4d2ca 100644 --- a/i3pystatus/zabbix.py +++ b/i3pystatus/zabbix.py @@ -55,7 +55,7 @@ class Zabbix(IntervalModule): cdict["default"] = "{0}:{a[5]}/{a[4]}/{a[3]}/{a[2]}/{a[1]}/{a[0]}".format(sum(alerts), a=alerts) cdict["total"] = sum(alerts) - color = alerts_color[max(alerts)] + color = alerts_color[max(map(int, alerts_list))] result = self.format.format(**cdict) except Exception as e: From 0f2362033603b85336cc25229241b8d7388e9542 Mon Sep 17 00:00:00 2001 From: asmikhailov Date: Tue, 19 Jan 2016 12:33:13 +0300 Subject: [PATCH 7/7] Fixed error with empty triggers list --- i3pystatus/zabbix.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/i3pystatus/zabbix.py b/i3pystatus/zabbix.py index 5e4d2ca..b47c900 100644 --- a/i3pystatus/zabbix.py +++ b/i3pystatus/zabbix.py @@ -55,7 +55,10 @@ class Zabbix(IntervalModule): cdict["default"] = "{0}:{a[5]}/{a[4]}/{a[3]}/{a[2]}/{a[1]}/{a[0]}".format(sum(alerts), a=alerts) cdict["total"] = sum(alerts) - color = alerts_color[max(map(int, alerts_list))] + if alerts_list: + color = alerts_color[max(map(int, alerts_list))] + else: + color = alerts_color[0] result = self.format.format(**cdict) except Exception as e: