-
Notifications
You must be signed in to change notification settings - Fork 0
/
ilert-check-mk.py
executable file
·118 lines (92 loc) · 3.67 KB
/
ilert-check-mk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# iLert Check_MK Native Plugin
#
# Copyright (c) 2013-2020, iLert GmbH. <[email protected]>
# All rights reserved.
import os
import syslog
import fcntl
import urllib
import uuid
from xml.sax.saxutils import escape
from xml.sax.saxutils import quoteattr
from cmk.notification_plugins import utils
import argparse
import io
import sys
import datetime
PLUGIN_VERSION = "1.0"
def log(level, message):
sys.stdout.write("%s %s %s\n" %
(datetime.datetime.now().isoformat(), level, message))
def create_and_send(endpoint, port, apiKey, context):
xml_doc = create_xml(context)
send(endpoint, port, apiKey, xml_doc)
def send(endpoint, port, apiKey, xml):
log("INFO", "Sending event to iLert...")
headers = {"Content-type": "application/xml", "Accept": "application/xml"}
url = "%s:%s/api/v1/events/checkmk/%s" % (endpoint, port, apiKey)
try:
req = urllib.request.Request(url, str.encode(xml), headers)
urllib.request.urlopen(req, timeout=60)
except urllib.error.HTTPError as e:
if e.code == 429:
log("WARNING", "too many requests, will try later. Server response: %s" % e.read())
exit(1)
elif 400 <= e.code <= 499:
log("WARNING", "event not accepted by iLert. Reason: %s" % e.read())
else:
log("ERROR", "could not send event to iLert. HTTP error code %s, reason: %s, %s" % (
e.code, e.reason, e.read()))
exit(1)
except urllib.error.URLError as e:
log("ERROR", "could not send event to iLert. Reason: %s\n" % e.reason)
exit(1)
except Exception as e:
log("ERROR", "an unexpected error occurred. Please report a bug. Cause: %s %s" % (
type(e), e.args))
exit(1)
else:
log("INFO", "Event has been sent to iLert")
def create_xml(context):
xml_doc = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><event><payload>'
for entry in context:
xml_doc += "<entry key=%s>%s</entry>" % (
quoteattr(entry), str(escape(context[entry])))
xml_doc += "</payload></event>"
log("INFO", xml_doc)
return xml_doc
def main():
parser = argparse.ArgumentParser(
description='send events from CheckMK to iLert')
parser.add_argument(
'-a', '--apikey', help='API key for the alert source in iLert')
parser.add_argument('-e', '--endpoint', default='https://api.ilert.com',
help='iLert API endpoint (default: %(default)s)')
parser.add_argument('-p', '--port', type=int, default=443,
help='endpoint port (default: %(default)s)')
parser.add_argument('--version', action='version', version=PLUGIN_VERSION)
parser.add_argument('payload', nargs=argparse.REMAINDER,
help='event payload as key value pairs in the format key1=value1 key2=value2 ...')
args = parser.parse_args()
# get all env vars to dict
context = utils.collect_context()
# ... and payload specified via command line
for arg in args.payload:
if arg:
a = arg.split('=', 1)
if a and a[0] and a[1]:
context.update({a[0]: a[1]})
if args.apikey is not None:
apikey = args.apikey
elif 'PARAMETER_WEBHOOK_URL' in context:
apikey = context.get['PARAMETER_WBHOOK_URL']
else:
apikey = None
if apikey is None:
log("ERROR", "parameter apikey is required in save mode and must be provided either via command line or in the pager field of the contact definition in CheckMK")
exit(1)
create_and_send(args.endpoint, args.port, apikey, context)
exit(0)
if __name__ == '__main__':
main()