Skip to content

Commit

Permalink
#71, added support of external images, availably by adding 'external_…
Browse files Browse the repository at this point in the history
…image' into action and specify link to that image
  • Loading branch information
Ilya Ableev committed Jun 28, 2017
1 parent 375a640 commit fb36a8b
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions zbxtg.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import json
import re
import stat
import hashlib
#import sqlite3
from os.path import dirname
import zbxtg_settings
Expand Down Expand Up @@ -407,6 +408,24 @@ def file_append(filename, text):
return True


def external_image_get(url, tmp_dir, timeout=6):
image_hash = hashlib.md5()
image_hash.update(url)
file_img = tmp_dir + "/external_{0}.png".format(image_hash.hexdigest())
try:
res = requests.get(url, timeout=timeout)
except requests.exceptions.ReadTimeout as ex:
print_message("Can't get external image from '{0}': timeout".format(url))
return False
res_code = res.status_code
if res_code == 404:
print_message("Can't get external image from '{0}': HTTP 404 error".format(url))
return False
res_img = res.content
file_write(file_img, res_img)
return file_img


def main():

tmp_dir = zbxtg_settings.zbx_tg_tmp_dir
Expand Down Expand Up @@ -494,6 +513,7 @@ def main():
"signature": False,
"signature_disable": False,
"graph_buttons": False,
"extimg": None,
}
settings_description = {
"itemid": {"name": "zbxtg_itemid", "type": "list"},
Expand All @@ -516,6 +536,7 @@ def main():
"signature": {"name": "signature", "type": "bool"},
"signature_disable": {"name": "signature_disable", "type": "bool"},
"graph_buttons": {"name": "graph_buttons", "type": "bool"},
"external_image": {"name": "extimg", "type": "str"},
}

for line in zbxtg_body:
Expand Down Expand Up @@ -667,9 +688,12 @@ def main():
if not zbx.cookie:
print_message("Login to Zabbix web UI has failed, check manually...")
else:
zbxtg_file_img = zbx.graph_get(settings["zbxtg_itemid"], settings["zbxtg_image_period"],
settings["zbxtg_title"], settings["zbxtg_image_width"],
settings["zbxtg_image_height"], tmp_dir)
if not settings["extimg"]:
zbxtg_file_img = zbx.graph_get(settings["zbxtg_itemid"], settings["zbxtg_image_period"],
settings["zbxtg_title"], settings["zbxtg_image_width"],
settings["zbxtg_image_height"], tmp_dir)
else:
zbxtg_file_img = external_image_get(settings["extimg"], tmp_dir=tmp_dir)
zbxtg_body_text, is_modified = list_cut(zbxtg_body_text, 200)
if result:
message_id = result["result"]["message_id"]
Expand Down

0 comments on commit fb36a8b

Please sign in to comment.