Last active
January 1, 2021 18:47
-
-
Save igorhrq/15ee85e32f41e396e412180c411aa0c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Igor Andrade | |
# Date: 29-12-2020 | |
# | |
# Scripts that catch triggers from an group, filters it on a file for not repeat alerts and send it to a gchat using | |
# zabbix API and gchat API. | |
# | |
# remember create a config.ini with your credentials from zabbix at same file of this script | |
# cat config.ini | |
# [zabbix] | |
# server=https://zabbix.YOURZABBIXLINKHERE.com | |
# user=zabbixuser | |
# password=passwordfromuser | |
# | |
# Greetz: Renan S | |
# | |
from pprint import pprint | |
import apizabbix | |
import time | |
import urllib3 | |
from httplib2 import Http | |
from json import dumps | |
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | |
api = apizabbix.connect() | |
triggers = api.trigger.get( | |
groupids='123', | |
min_severity='3', | |
output=[ | |
'triggerid', | |
'description', | |
'lastchange', | |
'priority', | |
], | |
selectHosts=[ | |
'hostid', | |
'host', | |
], | |
selectLastEvent=[ | |
'eventid', | |
'acknowledged', | |
'objectid', | |
'clock', | |
'ns', | |
], | |
sortfield='lastchange', | |
) | |
prioritys = [ | |
'Not classified', | |
'Information', | |
'Warning', | |
'Average', | |
'High', | |
'Disaster' | |
] | |
def send_to_gchat(bot_message): | |
url = 'yourlinkofwebhookgchat.com/TOKEN' | |
message_headers = { 'Content-Type': 'application/json; charset=UTF-8'} | |
payload = {'text': bot_message } | |
http_obj = Http() | |
response = http_obj.request( | |
uri=url, | |
method='POST', | |
headers=message_headers, | |
body=dumps(payload), | |
) | |
print(response) | |
for y in triggers: | |
print(y) | |
try: | |
eventid = y['lastEvent']['eventid'] | |
except: | |
continue | |
idevento = y['triggerid'] | |
nameHost = y["hosts"][0]['host'] | |
age = time.time() - float(y['lastchange']) | |
days = "{0.tm_yday}".format(time.gmtime(age)) | |
daysagain = int(days) - 1 | |
duration = "days {0.tm_hour} horas {0.tm_min} minutos".format(time.gmtime(age)) | |
prioritys_var = prioritys[(int(y['priority']))] | |
last_change = time.strftime("%d/%m/%Y %H:%M:%S", time.localtime(float(y['lastchange']))) | |
with open('/home/zabbix-new/events.txt', 'r') as logfile: | |
loglist = logfile.readlines() | |
if not eventid + "\n" in loglist: | |
with open('/home/zabbix-new/events.txt', 'a') as logfile: | |
logfile.write(eventid + "\n") | |
messagesToG = ( | |
"\nhostname: " + nameHost + | |
"\ndescrição: " + y['description'] + | |
"\nprioridade: " + prioritys_var + | |
"\nlastChange: " + last_change + | |
"\nOcorreu a: " + str(daysagain) + " " + duration.replace("days","dias") + | |
"\nLink: " + ("https://zabbix.yourzabbix.com/tr_events.php?triggerid=%s&eventid=%s" % (idevento, eventid))) | |
send_to_gchat(messagesToG) | |
api.user.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muito bonito!