Skip to content

Commit

Permalink
Merge pull request #6 from iLert/feature/python3-support
Browse files Browse the repository at this point in the history
Feature/python3 support
  • Loading branch information
krystianity authored Nov 28, 2022
2 parents 699a1c7 + 315e5de commit ca4b744
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ilert_check_mk.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
# iLert Check_MK Plugin

COMMAND="/usr/local/bin/ilert_nagios.py"
COMMAND="python3 /usr/local/bin/ilert_nagios.py"

$COMMAND --mode save --apikey $NOTIFY_PARAMETER_1

2 changes: 1 addition & 1 deletion ilert_icinga2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object User "ilert" {
object NotificationCommand "ilert-notification" {
import "plugin-notification-command"

command = "/usr/local/bin/ilert_nagios.py -m save"
command = "python3 /usr/local/bin/ilert_nagios.py -m save"

env = {
ICINGA_CONTACTPAGER = "$user.pager$"
Expand Down
6 changes: 3 additions & 3 deletions ilert_nagios.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ define contact {
host_notification_options d,r
service_notification_commands notify-ilert
host_notification_commands notify-ilert
pager <YOUR API KEY>
pager il1nag0922ee3f86c469f94cca9e0cdb6469ab5a934dcc5091ac
}

define command {
command_name notify-ilert
command_line /usr/local/bin/ilert_nagios.py -m save
command_line python3 /usr/local/bin/ilert_nagios.py -m save
}

#
Expand All @@ -26,6 +26,6 @@ define command {

#define command {
# command_name notify-ilert
# command_line /usr/local/bin/ilert_nagios.py -m save CONTACTPAGER="$CONTACTPAGER$" NOTIFICATIONTYPE="$NOTIFICATIONTYPE$" LONGDATETIME="$LONGDATETIME$" HOSTADDRESS="$HOSTADDRESS$" HOSTNAME="$HOSTNAME$" HOSTSTATE="$HOSTSTATE$" HOSTOUTPUT="$HOSTOUTPUT$" HOSTALIAS="$HOSTALIAS$" SERVICEDESC="$SERVICEDESC$" SERVICESTATE="$SERVICESTATE$" SERVICEOUTPUT="$SERVICEOUTPUT$"
# command_line python3 /usr/local/bin/ilert_nagios.py -m save CONTACTPAGER="$CONTACTPAGER$" NOTIFICATIONTYPE="$NOTIFICATIONTYPE$" LONGDATETIME="$LONGDATETIME$" HOSTADDRESS="$HOSTADDRESS$" HOSTNAME="$HOSTNAME$" HOSTSTATE="$HOSTSTATE$" HOSTOUTPUT="$HOSTOUTPUT$" HOSTALIAS="$HOSTALIAS$" SERVICEDESC="$SERVICEDESC$" SERVICESTATE="$SERVICESTATE$" SERVICEOUTPUT="$SERVICEOUTPUT$"
#}

24 changes: 13 additions & 11 deletions ilert_nagios.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@

# iLert Nagios/Icinga/Check_MK Plugin
#
# Copyright (c) 2013-2021, iLert GmbH. <[email protected]>
# Copyright (c) 2013-2022, iLert GmbH. <[email protected]>
# All rights reserved.


import os
import syslog
import fcntl
import urllib2
from urllib2 import HTTPError
from urllib2 import URLError
import urllib.request
from urllib.request import HTTPError
from urllib.error import URLError
import uuid
import ssl
from xml.sax.saxutils import escape
from xml.sax.saxutils import quoteattr
import argparse
import io

PLUGIN_VERSION = "1.6"
PLUGIN_VERSION = "1.7"


def persist_event(api_key, directory, payload):
Expand Down Expand Up @@ -72,12 +72,12 @@ def flush(endpoint, directory, port, insecure):

# populate list of event files sorted by creation date
events = [os.path.join(directory, f) for f in os.listdir(directory)]
events = filter(lambda x: x.endswith(".ilert"), events)
events.sort(key=lambda x: os.path.getmtime(x))
events = list(filter(lambda x: x.endswith(".ilert"), events))
events = sorted(events, key=lambda x: os.path.getmtime(x))

for event in events:
try:
with open(event, 'r') as f:
with open(event, 'r', encoding='utf-8') as f:
xml_doc = f.read()
except IOError:
continue
Expand All @@ -89,8 +89,9 @@ def flush(endpoint, directory, port, insecure):
if insecure == True:
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
req = urllib2.Request(url, xml_doc, headers)
urllib2.urlopen(req, timeout=60, context=ctx)
data = xml_doc.encode('utf-8')
req = urllib.request.Request(url, data, headers)
urllib.request.urlopen(req, timeout=60, context=ctx)
except HTTPError as e:
if e.code == 429:
syslog.syslog(syslog.LOG_WARNING, "too many requests, will try later. Server response: %s" % e.read())
Expand All @@ -117,7 +118,8 @@ def create_xml(apikey, payload):
xml_doc += "<event><apiKey>%s</apiKey><payload>" % apikey

for entry in payload:
xml_doc += "<entry key=%s>%s</entry>" % (quoteattr(entry), unicode(escape(payload[entry]), encoding='utf-8', errors='ignore'))
byte_str = escape(payload[entry])
xml_doc += "<entry key=%s>%s</entry>" % (quoteattr(entry), escape(payload[entry]))

# XML document end tag
xml_doc += "</payload></event>"
Expand Down
7 changes: 7 additions & 0 deletions python2/ilert_check_mk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
# iLert Check_MK Plugin

COMMAND="/usr/local/bin/ilert_nagios.py"

$COMMAND --mode save --apikey $NOTIFY_PARAMETER_1

53 changes: 53 additions & 0 deletions python2/ilert_icinga2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
object User "ilert" {
display_name = "iLert"
groups = [ "icingaadmins" ]
states = [ OK, Warning, Critical, Unknown]
types = [ Problem, Recovery, Acknowledgement ]
vars.additional_notes = "This user maps to an alert source in iLert."
pager = "<YOUR API KEY>"
}

object NotificationCommand "ilert-notification" {
import "plugin-notification-command"

command = "/usr/local/bin/ilert_nagios.py -m save"

env = {
ICINGA_CONTACTPAGER = "$user.pager$"
ICINGA_NOTIFICATIONTYPE = "$notification.type$"
ICINGA_LONGDATETIME = "$icinga.long_date_time$"
ICINGA_HOSTADDRESS = "$address$"
ICINGA_HOSTNAME = "$host.name$"
ICINGA_HOSTALIAS = "$host.display_name$"
ICINGA_HOSTSTATE = "$host.state$"
ICINGA_HOSTOUTPUT="$host.output$"
ICINGA_SERVICESTATE = "$service.state$"
ICINGA_SERVICEDESC = "$service.name$"
ICINGA_SERVICEDISPLAYNAME = "$service.display_name$"
ICINGA_SERVICEOUTPUT = "$service.output$"
ICINGA_NOTIFICATIONCOMMENT = "$notification.comment$"
}
}

apply Notification "ilert-host-notification" to Host {
command = "ilert-notification"
states = [ Up, Down ]
types = [ Problem, Acknowledgement, Recovery ]
period = "24x7"
users = [ "ilert" ]

assign where host.vars.notification.enable_ilert == true
}

apply Notification "ilert-service-notification" to Service {
command = "ilert-notification"
states = [ OK, Warning, Critical, Unknown ]
types = [ Problem, Acknowledgement, Recovery ]
period = "24x7"
users = [ "ilert" ]

assign where service.vars.notification.enable_ilert == true
}



31 changes: 31 additions & 0 deletions python2/ilert_nagios.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
############################################################################################
# NOTE: use this file if you're using Nagios or Icinga 1.x. If you're using Icinga 2.x, #
# use ilert_icinga2.conf instead. #
############################################################################################

define contact {
contact_name ilert
alias iLert
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-ilert
host_notification_commands notify-ilert
pager <YOUR API KEY>
}

define command {
command_name notify-ilert
command_line /usr/local/bin/ilert_nagios.py -m save
}

#
# Use the following command definition if the configuration option enable_environment_macros is set to 0.
#

#define command {
# command_name notify-ilert
# command_line /usr/local/bin/ilert_nagios.py -m save CONTACTPAGER="$CONTACTPAGER$" NOTIFICATIONTYPE="$NOTIFICATIONTYPE$" LONGDATETIME="$LONGDATETIME$" HOSTADDRESS="$HOSTADDRESS$" HOSTNAME="$HOSTNAME$" HOSTSTATE="$HOSTSTATE$" HOSTOUTPUT="$HOSTOUTPUT$" HOSTALIAS="$HOSTALIAS$" SERVICEDESC="$SERVICEDESC$" SERVICESTATE="$SERVICESTATE$" SERVICEOUTPUT="$SERVICEOUTPUT$"
#}

Loading

0 comments on commit ca4b744

Please sign in to comment.