lists.checkmk.com
Sign In
Sign Up
Sign In
Sign Up
Manage this list
×
Keyboard Shortcuts
Thread View
j
: Next unread message
k
: Previous unread message
j a
: Jump to all threads
j l
: Jump to MailingList overview
2024
September
August
July
June
May
April
March
February
January
2023
December
November
October
September
August
July
June
May
April
March
February
January
2022
December
November
October
September
August
July
June
May
April
March
February
January
2021
December
November
October
September
August
July
June
May
April
March
February
January
2020
December
November
October
September
August
July
June
May
April
March
February
January
2019
December
November
October
September
August
July
June
May
April
March
February
January
2018
December
November
October
September
August
July
June
May
April
March
February
January
2017
December
November
October
September
August
July
June
May
April
March
February
January
2016
December
November
October
September
August
July
June
May
April
March
February
January
2015
December
November
October
September
August
July
June
May
April
March
February
January
2014
December
November
October
September
August
July
June
May
April
March
February
January
2013
December
November
October
September
August
July
June
May
April
March
February
January
2012
December
November
October
September
August
July
June
May
April
March
February
January
2011
December
November
October
September
August
July
June
May
April
March
February
January
2010
December
November
October
List overview
Download
Checkmk git commits
September 2018
----- 2024 -----
September 2024
August 2024
July 2024
June 2024
May 2024
April 2024
March 2024
February 2024
January 2024
----- 2023 -----
December 2023
November 2023
October 2023
September 2023
August 2023
July 2023
June 2023
May 2023
April 2023
March 2023
February 2023
January 2023
----- 2022 -----
December 2022
November 2022
October 2022
September 2022
August 2022
July 2022
June 2022
May 2022
April 2022
March 2022
February 2022
January 2022
----- 2021 -----
December 2021
November 2021
October 2021
September 2021
August 2021
July 2021
June 2021
May 2021
April 2021
March 2021
February 2021
January 2021
----- 2020 -----
December 2020
November 2020
October 2020
September 2020
August 2020
July 2020
June 2020
May 2020
April 2020
March 2020
February 2020
January 2020
----- 2019 -----
December 2019
November 2019
October 2019
September 2019
August 2019
July 2019
June 2019
May 2019
April 2019
March 2019
February 2019
January 2019
----- 2018 -----
December 2018
November 2018
October 2018
September 2018
August 2018
July 2018
June 2018
May 2018
April 2018
March 2018
February 2018
January 2018
----- 2017 -----
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
February 2017
January 2017
----- 2016 -----
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
----- 2015 -----
December 2015
November 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
----- 2014 -----
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
----- 2013 -----
December 2013
November 2013
October 2013
September 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
----- 2012 -----
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
May 2012
April 2012
March 2012
February 2012
January 2012
----- 2011 -----
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
----- 2010 -----
December 2010
November 2010
October 2010
checkmk-commits@lists.checkmk.com
12 participants
371 discussions
Start a n
N
ew thread
Isolate Collecting context
by Óscar Nájera
Module: check_mk Branch: master Commit: 026fbdeaa57fa599d3ffd1f581989697ca5846ae URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=026fbdeaa57fa5…
Author: Óscar Nájera <on(a)mathias-kettner.de> Date: Thu Sep 20 17:18:22 2018 +0200 Isolate Collecting context Change-Id: Idae5f70342c7d61c40f30b6d9155ecae42306745 --- cmk/notification_plugins/asciimail.py | 7 ++---- cmk/notification_plugins/mail.py | 8 +++---- cmk/notification_plugins/pushover.py | 44 +++++++++++------------------------ cmk/notification_plugins/slack.py | 11 +++------ cmk/notification_plugins/spectrum.py | 8 +++---- cmk/notification_plugins/utils.py | 10 ++++++++ 6 files changed, 35 insertions(+), 53 deletions(-) diff --git a/cmk/notification_plugins/asciimail.py b/cmk/notification_plugins/asciimail.py index 9033da5..d199742 100644 --- a/cmk/notification_plugins/asciimail.py +++ b/cmk/notification_plugins/asciimail.py @@ -22,15 +22,14 @@ # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, # Boston, MA 02110-1301 USA. - # This script creates an ASCII email. It replaces the builtin ASCII email feature and # is configurable via WATO with named parameters (only). -import os import re import subprocess import sys from email.mime.text import MIMEText +from cmk.notification_plugins import utils opt_debug = '-d' in sys.argv bulk_mode = '--bulk' in sys.argv @@ -276,9 +275,7 @@ def main(): else: # gather all options from env - context = dict([(var[7:], value.decode("utf-8")) - for (var, value) in os.environ.items() - if var.startswith("NOTIFY_")]) + context = utils.collect_context() content_txt = construct_content(context) mailto = context['CONTACTEMAIL'] subject = context['SUBJECT'] diff --git a/cmk/notification_plugins/mail.py b/cmk/notification_plugins/mail.py index 6c1a705..791ef4e 100644 --- a/cmk/notification_plugins/mail.py +++ b/cmk/notification_plugins/mail.py @@ -44,7 +44,7 @@ from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText -from cmk.notification_plugins.utils import extend_context_with_link_urls +from cmk.notification_plugins import utils def tmpl_head_html(html_section): @@ -868,7 +868,7 @@ def construct_content(context): if context.get('PARAMETER_2'): context["PARAMETER_URL_PREFIX"] = context["PARAMETER_2"] - extend_context_with_link_urls(context, '<a href="%s">%s</a>') + utils.extend_context_with_link_urls(context, '<a href="%s">%s</a>') # Create a notification summary in a new context variable # Note: This code could maybe move to cmk --notify in order to @@ -1068,9 +1068,7 @@ def main(): else: # gather all options from env - context = dict([(var[7:], value.decode("utf-8")) - for (var, value) in os.environ.items() - if var.startswith("NOTIFY_")]) + context = utils.collect_context() content_txt, content_html, attachments = construct_content(context) mailto = context['CONTACTEMAIL'] subject = context['SUBJECT'] diff --git a/cmk/notification_plugins/pushover.py b/cmk/notification_plugins/pushover.py index fc83afc..b17c3f3 100644 --- a/cmk/notification_plugins/pushover.py +++ b/cmk/notification_plugins/pushover.py @@ -23,18 +23,14 @@ # Boston, MA 02110-1301 USA. import requests -import os import re import sys - +from cmk.notification_plugins import utils api_url = "
https://api.pushover.net/1/messages.json
" def main(): - context = dict([(var[7:], value.decode("utf-8")) - for (var, value) in os.environ.items() - if var.startswith("NOTIFY_")]) - + context = utils.collect_context() subject = get_subject(context) text = get_text(context) @@ -44,19 +40,6 @@ def main(): send_push_notification(api_key, recipient_key, subject, text, context) -def get_url(what, context): - url_prefix = context.get("PARAMETER_URL_PREFIX") - if url_prefix: - base_url = url_prefix.rstrip('/') - if base_url.endswith("/check_mk"): - base_url = base_url[:-9] - - if what == "HOST": - return base_url + context['HOSTURL'] - else: - return base_url + context['SERVICEURL'] - - def get_subject(context): s = context["HOSTNAME"] @@ -93,15 +76,14 @@ def get_subject(context): def get_text(context): s = "" - #s += "<font color=\"%s\">" % color - #s += "</font>" s += "$@OUTPUT$" if "PARAMETER_URL_PREFIX" in context: + utils.extend_context_with_link_urls(context, '<a href="%s">%s</a>') s += " <i>Link: </i>" - s += "<a href=\"%s\">Host</a>" % get_url("HOST", context) + s += context["LINKEDHOSTNAME"] if context["WHAT"] != "HOST": - s += ", <a href=\"%s\">Service</a>" % get_url("SERVICE", context) + s += context["LINKEDSERVICEDESC"] return substitute_context(s.replace("@", context["WHAT"]), context) @@ -117,12 +99,12 @@ def substitute_context(template, context): def send_push_notification(api_key, recipient_key, subject, text, context): params = [ - ("token", api_key), - ("user", recipient_key), - ("title", subject.encode("utf-8")), - ("message", text.encode("utf-8")), - ("timestamp", int(context["MICROTIME"])/1000000), - ("html", 1), + ("token", api_key), + ("user", recipient_key), + ("title", subject.encode("utf-8")), + ("message", text.encode("utf-8")), + ("timestamp", int(context["MICROTIME"]) / 1000000), + ("html", 1), ] if context.get("PARAMETER_PRIORITY") in ["-2", "-1", "0", "1"]: @@ -131,8 +113,8 @@ def send_push_notification(api_key, recipient_key, subject, text, context): elif context.get("PARAMETER_PRIORITY_PRIORITY") == "2": params += [ ("priority", context["PARAMETER_PRIORITY_PRIORITY"]), - ("expire", context.get("PARAMETER_PRIORITY_EXPIRE", 0)), - ("retry", context.get("PARAMETER_PRIORITY_RETRY", 0)), + ("expire", context.get("PARAMETER_PRIORITY_EXPIRE", 0)), + ("retry", context.get("PARAMETER_PRIORITY_RETRY", 0)), ] if context.get("PARAMETER_PRIORITY_RECEIPTS"): params.append(("receipts", context["PARAMETER_PRIORITY_RECEIPTS"])) diff --git a/cmk/notification_plugins/slack.py b/cmk/notification_plugins/slack.py index 49f8c4d..5f82560 100644 --- a/cmk/notification_plugins/slack.py +++ b/cmk/notification_plugins/slack.py @@ -30,11 +30,10 @@ Use a slack webhook to send notification messages """ from typing import Dict # pylint: disable=unused-import -import os import sys import requests -from cmk.notification_plugins.utils import extend_context_with_link_urls +from cmk.notification_plugins import utils COLORS = { "CRITICAL": "#EE0000", @@ -51,7 +50,7 @@ def construct_message(context): # type: (Dict) -> Dict """Build the message for slack""" - extend_context_with_link_urls(context, '<%s|%s>') + utils.extend_context_with_link_urls(context, '<%s|%s>') if context.get('SERVICESTATE', None): color = COLORS.get(context["SERVICESTATE"]) @@ -88,11 +87,7 @@ def construct_message(context): def main(): - context = { - var[7:]: value.decode("utf-8") - for (var, value) in os.environ.items() - if var.startswith("NOTIFY_") - } + context = utils.collect_context() url = context.get("PARAMETER_WEBHOOK_URL") diff --git a/cmk/notification_plugins/spectrum.py b/cmk/notification_plugins/spectrum.py index 43bb2e0..54972d0 100644 --- a/cmk/notification_plugins/spectrum.py +++ b/cmk/notification_plugins/spectrum.py @@ -22,9 +22,9 @@ # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, # Boston, MA 02110-1301 USA. -import os import subprocess import sys +from cmk.notification_plugins import utils # Note: This script contains an example configuration # You will probably have to modify the sent information @@ -50,13 +50,13 @@ def send_trap(oids, target, community): def main(): # gather all options from env - context = dict([(var[7:], value.decode("utf-8")) for (var, value) in os.environ.items()]) + context = utils.collect_context() # check if configured via flexible notifications if "PARAMETER_1" in context: - context["PARAMETER_COMMUNITY"] = context["PARAMETER_1"] + context["PARAMETER_COMMUNITY"] = context["PARAMETER_1"] context["PARAMETER_DESTINATION"] = context["PARAMETER_2"] - context["PARAMETER_BASEOID"] = context["PARAMETER_3"] + context["PARAMETER_BASEOID"] = context["PARAMETER_3"] base_oid = context.get("PARAMETER_BASEOID", "1.3.6.1.4.1.1234") diff --git a/cmk/notification_plugins/utils.py b/cmk/notification_plugins/utils.py index 3b7f797..4a4a7df 100644 --- a/cmk/notification_plugins/utils.py +++ b/cmk/notification_plugins/utils.py @@ -24,9 +24,19 @@ # Boston, MA 02110-1301 USA. from typing import Dict # pylint: disable=unused-import +import os import re +def collect_context(): + # type: () -> Dict + return { + var[7:]: value.decode("utf-8") + for (var, value) in os.environ.items() + if var.startswith("NOTIFY_") + } + + def extend_context_with_link_urls(context, link_template): # type: (Dict, str) -> None if context.get("PARAMETER_URL_PREFIX"):
5 years, 12 months
1
0
0
0
Make mail notification plugin a Check_MK module
by Óscar Nájera
Module: check_mk Branch: master Commit: 6b1e13ebbb861f2863d97370c07d673203cbb02f URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=6b1e13ebbb861f…
Author: Óscar Nájera <on(a)mathias-kettner.de> Date: Fri Sep 21 14:42:50 2018 +0200 Make mail notification plugin a Check_MK module - Reuse function to extend contex with links in urls - Apply yapf to file - Wrapper script to call mail module Change-Id: I327a46fab1fe8f6283fc28ee040fe9557afbdafb --- cmk/notification_plugins/mail.py | 1093 ++++++++++++++++++++++++++++++++++++++ notifications/mail | 1022 +---------------------------------- 2 files changed, 1095 insertions(+), 1020 deletions(-) Diff:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commitdiff;h=6b1e13ebbb…
5 years, 12 months
1
0
0
0
Extract sendmail by sendmail
by Óscar Nájera
Module: check_mk Branch: master Commit: 81f797a722c248993ca70726fea633c83526acc7 URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=81f797a722c248…
Author: Óscar Nájera <on(a)mathias-kettner.de> Date: Fri Sep 21 09:55:42 2018 +0200 Extract sendmail by sendmail Change-Id: Id24813276d972597a96a450ba47bd72005a2d53a --- cmk/notification_plugins/asciimail.py | 20 +------------------- cmk/notification_plugins/mail.py | 23 +---------------------- cmk/notification_plugins/utils.py | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 41 deletions(-) diff --git a/cmk/notification_plugins/asciimail.py b/cmk/notification_plugins/asciimail.py index fb7dd41..98b55ad 100644 --- a/cmk/notification_plugins/asciimail.py +++ b/cmk/notification_plugins/asciimail.py @@ -59,24 +59,6 @@ Handler output: $ALERTHANDLEROUTPUT$ tmpl_alerthandler_service_body = "Service: $SERVICEDESC$\n" + tmpl_alerthandler_host_body -def send_mail(m, target, from_address): - cmd = ["/usr/sbin/sendmail"] - if from_address: - cmd += ['-F', from_address, "-f", from_address] - cmd += ["-i", target.encode("utf-8")] - - try: - p = subprocess.Popen(cmd, stdin=subprocess.PIPE) - except OSError: - raise Exception("Failed to send the mail: /usr/sbin/sendmail is missing") - - p.communicate(m.as_string()) - if p.returncode != 0: - raise Exception("sendmail returned with exit code: %d" % p.returncode) - - return 0 - - def construct_content(context): # Create a notification summary in a new context variable @@ -255,7 +237,7 @@ def main(): m = utils.set_mail_headers(mailto, subject, from_address, reply_to, MIMEText(content_txt, 'plain', _charset='utf-8')) try: - sys.exit(send_mail(m, mailto, from_address)) + sys.exit(utils.send_mail_sendmail(m, mailto, from_address)) except Exception, e: sys.stderr.write("Unhandled exception: %s\n" % e) # unhandled exception, don't retry this... diff --git a/cmk/notification_plugins/mail.py b/cmk/notification_plugins/mail.py index a1f9f1f..9a83ce9 100644 --- a/cmk/notification_plugins/mail.py +++ b/cmk/notification_plugins/mail.py @@ -33,7 +33,6 @@ import base64 import os import socket -import subprocess import sys import urllib import urllib2 @@ -45,7 +44,6 @@ from email.mime.text import MIMEText from cmk.notification_plugins import utils - def tmpl_head_html(html_section): return ''' <html> @@ -517,25 +515,6 @@ def multipart_mail(target, subject, from_address, reply_to, content_txt, content return utils.set_mail_headers(target, subject, from_address, reply_to, m) -def send_mail_sendmail(m, target, from_address): - cmd = ["/usr/sbin/sendmail"] - if from_address: - cmd += ['-F', from_address, "-f", from_address] - cmd += ["-i", target.encode("utf-8")] - - try: - p = subprocess.Popen(cmd, stdin=subprocess.PIPE) - except OSError: - raise Exception("Failed to send the mail: /usr/sbin/sendmail is missing") - - p.communicate(m.as_string()) - if p.returncode != 0: - raise Exception("sendmail returned with exit code: %d" % p.returncode) - - sys.stdout.write("Spooled mail to local mail transmission agent\n") - return 0 - - def send_mail_smtp(message, target, from_address, context): import smtplib # for the error messages host_index = 1 @@ -643,7 +622,7 @@ def send_mail(message, target, from_address, context): if "PARAMETER_SMTP_PORT" in context: return send_mail_smtp(message, target, from_address, context) else: - return send_mail_sendmail(message, target, from_address) + return utils.send_mail_sendmail(message, target, from_address) def fetch_pnp_data(context, params): diff --git a/cmk/notification_plugins/utils.py b/cmk/notification_plugins/utils.py index 200be9f..37f61fa 100644 --- a/cmk/notification_plugins/utils.py +++ b/cmk/notification_plugins/utils.py @@ -26,6 +26,8 @@ from typing import Dict # pylint: disable=unused-import import os import re +import sys +import subprocess try: # First try python3 @@ -122,3 +124,21 @@ def set_mail_headers(target, subject, from_address, reply_to, mail): mail['Reply-To'] = target return mail + +def send_mail_sendmail(m, target, from_address): + cmd = ["/usr/sbin/sendmail"] + if from_address: + cmd += ['-F', from_address, "-f", from_address] + cmd += ["-i", target.encode("utf-8")] + + try: + p = subprocess.Popen(cmd, stdin=subprocess.PIPE) + except OSError: + raise Exception("Failed to send the mail: /usr/sbin/sendmail is missing") + + p.communicate(m.as_string()) + if p.returncode != 0: + raise Exception("sendmail returned with exit code: %d" % p.returncode) + + sys.stdout.write("Spooled mail to local mail transmission agent\n") + return 0
5 years, 12 months
1
0
0
0
Upgrade exception syntax
by Óscar Nájera
Module: check_mk Branch: master Commit: 716d19ca6f8be7424a3c9e0a8ba5ea47fe0c4d2f URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=716d19ca6f8be7…
Author: Óscar Nájera <on(a)mathias-kettner.de> Date: Fri Sep 21 11:03:04 2018 +0200 Upgrade exception syntax Change-Id: If5b1ce4d6a7b811da399aefdc6654675254a2061 --- cmk/notification_plugins/asciimail.py | 2 +- cmk/notification_plugins/mail.py | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cmk/notification_plugins/asciimail.py b/cmk/notification_plugins/asciimail.py index 4f407bb..3826661 100644 --- a/cmk/notification_plugins/asciimail.py +++ b/cmk/notification_plugins/asciimail.py @@ -172,7 +172,7 @@ def main(): MIMEText(content_txt, 'plain', _charset='utf-8')) try: sys.exit(utils.send_mail_sendmail(m, mailto, from_address)) - except Exception, e: + except Exception as e: sys.stderr.write("Unhandled exception: %s\n" % e) # unhandled exception, don't retry this... sys.exit(2) diff --git a/cmk/notification_plugins/mail.py b/cmk/notification_plugins/mail.py index c055fa3..f55892a 100644 --- a/cmk/notification_plugins/mail.py +++ b/cmk/notification_plugins/mail.py @@ -533,11 +533,11 @@ def send_mail_smtp(message, target, from_address, context): try: send_mail_smtp_impl(message, target, smarthost, from_address, context) success = True - except socket.timeout, e: + except socket.timeout as e: sys.stderr.write("timeout connecting to \"%s\": %s\n" % (smarthost, str(e))) - except socket.gaierror, e: + except socket.gaierror as e: sys.stderr.write("socket error connecting to \"%s\": %s\n" % (smarthost, str(e))) - except smtplib.SMTPRecipientsRefused, e: + except smtplib.SMTPRecipientsRefused as e: # the exception contains a dict of failed recipients to the respective error. since we # only have one recipient there has to be exactly one element errorcode, message = e.recipients.values()[0] @@ -550,18 +550,18 @@ def send_mail_smtp(message, target, from_address, context): retry_possible = True sys.stderr.write("mail to \"%s\" refused: %d, %s\n" % (target, errorcode, message)) - except smtplib.SMTPHeloError, e: + except smtplib.SMTPHeloError as e: retry_possible = True # server is acting up, this may be fixed quickly sys.stderr.write("protocol error from \"%s\": %s\n" % (smarthost, str(e))) - except smtplib.SMTPSenderRefused, e: + except smtplib.SMTPSenderRefused as e: sys.stderr.write("server didn't accept from-address \"%s\" refused: %s\n" %\ (from_address, str(e))) - except smtplib.SMTPAuthenticationError, e: + except smtplib.SMTPAuthenticationError as e: sys.stderr.write("authentication failed on \"%s\": %s\n" % (smarthost, str(e))) - except smtplib.SMTPDataError, e: + except smtplib.SMTPDataError as e: retry_possible = True # unexpected error - give retry a chance sys.stderr.write("unexpected error code from \"%s\": %s\n" % (smarthost, str(e))) - except smtplib.SMTPException, e: + except smtplib.SMTPException as e: retry_possible = True # who knows what went wrong, a retry might just work sys.stderr.write("undocumented error code from \"%s\": %s\n" % (smarthost, str(e))) @@ -682,7 +682,7 @@ def extract_graph_error(output): def render_pnp_graphs(context): try: num_sources = fetch_num_sources(context) - except GraphException, e: + except GraphException as e: graph_error = extract_graph_error(str(e)) if '.xml" not found.' not in graph_error: sys.stderr.write('Unable to fetch number of graphs: %s\n' % graph_error) @@ -692,7 +692,7 @@ def render_pnp_graphs(context): for source in range(0, num_sources): try: content = fetch_graph(context, source) - except GraphException, e: + except GraphException as e: sys.stderr.write('Unable to fetch graph: %s\n' % e) continue @@ -713,7 +713,7 @@ def render_cmk_graphs(context): try: json_data = urllib2.urlopen(url).read() - except Exception, e: + except Exception as e: if opt_debug: raise sys.stderr.write("ERROR: Failed to fetch graphs: %s\nURL: %s\n" % (e, url)) @@ -721,7 +721,7 @@ def render_cmk_graphs(context): try: base64_strings = json.loads(json_data) - except Exception, e: + except Exception as e: if opt_debug: raise sys.stderr.write( @@ -879,7 +879,7 @@ def construct_content(context): try: attachments, graph_code = render_performance_graphs(context) content_html += graph_code - except Exception, e: + except Exception as e: sys.stderr.write("Failed to add graphs to mail. Continue without them. (%s)\n" % e) extra_html_section = "" @@ -954,7 +954,7 @@ def main(): try: sys.exit(send_mail(m, mailto, from_address, context)) - except Exception, e: + except Exception as e: sys.stderr.write("Unhandled exception: %s\n" % e) # unhandled exception, don't retry this... sys.exit(2)
5 years, 12 months
1
0
0
0
Extract function for setting mail address headers
by Óscar Nájera
Module: check_mk Branch: master Commit: 62c16a385d3074c347a2c9b91656ac46e780f915 URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=62c16a385d3074…
Author: Óscar Nájera <on(a)mathias-kettner.de> Date: Fri Sep 21 09:52:24 2018 +0200 Extract function for setting mail address headers Change-Id: I919770d142d2cd58e9bb5950f112e8969f0226e6 --- cmk/notification_plugins/asciimail.py | 23 ++--------------------- cmk/notification_plugins/mail.py | 14 +------------- cmk/notification_plugins/utils.py | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/cmk/notification_plugins/asciimail.py b/cmk/notification_plugins/asciimail.py index 0a769c4..fb7dd41 100644 --- a/cmk/notification_plugins/asciimail.py +++ b/cmk/notification_plugins/asciimail.py @@ -59,25 +59,6 @@ Handler output: $ALERTHANDLEROUTPUT$ tmpl_alerthandler_service_body = "Service: $SERVICEDESC$\n" + tmpl_alerthandler_host_body -def build_mail(target, subject, from_address, reply_to, content_txt): - # The plain text part - m = MIMEText(content_txt, 'plain', _charset='utf-8') - - m['Subject'] = subject - m['To'] = target - - # Set a few configurable headers - if from_address: - m['From'] = from_address - - if reply_to: - m['Reply-To'] = reply_to - elif len(target.split(",")) > 1: - m['Reply-To'] = target - - return m - - def send_mail(m, target, from_address): cmd = ["/usr/sbin/sendmail"] if from_address: @@ -271,8 +252,8 @@ def main(): # Create the mail and send it from_address = context.get("PARAMETER_FROM") reply_to = context.get("PARAMETER_REPLY_TO") - m = build_mail(mailto, subject, from_address, reply_to, content_txt) - + m = utils.set_mail_headers(mailto, subject, from_address, reply_to, + MIMEText(content_txt, 'plain', _charset='utf-8')) try: sys.exit(send_mail(m, mailto, from_address)) except Exception, e: diff --git a/cmk/notification_plugins/mail.py b/cmk/notification_plugins/mail.py index 72bbfc0..a1f9f1f 100644 --- a/cmk/notification_plugins/mail.py +++ b/cmk/notification_plugins/mail.py @@ -514,19 +514,7 @@ def multipart_mail(target, subject, from_address, reply_to, content_txt, content part.add_header('Content-Disposition', how, filename=name) m.attach(part) - m['Subject'] = subject - m['To'] = target - - # Set a few configurable headers - if from_address: - m['From'] = from_address - - if reply_to: - m['Reply-To'] = reply_to - elif len(target.split(",")) > 1: - m['Reply-To'] = target - - return m + return utils.set_mail_headers(target, subject, from_address, reply_to, m) def send_mail_sendmail(m, target, from_address): diff --git a/cmk/notification_plugins/utils.py b/cmk/notification_plugins/utils.py index 620e596..200be9f 100644 --- a/cmk/notification_plugins/utils.py +++ b/cmk/notification_plugins/utils.py @@ -102,3 +102,23 @@ def substitute_context(template, context): # Remove the rest of the variables and make them empty template = re.sub(r"\$[A-Z_][A-Z_0-9]*\$", "", template) return template + + +############################################################################### +# Mail + + +def set_mail_headers(target, subject, from_address, reply_to, mail): + mail['Subject'] = subject + mail['To'] = target + + # Set a few configurable headers + if from_address: + mail['From'] = from_address + + if reply_to: + mail['Reply-To'] = reply_to + elif len(target.split(",")) > 1: + mail['Reply-To'] = target + + return mail
5 years, 12 months
1
0
0
0
Extract get_bulk_notification_subject
by Óscar Nájera
Module: check_mk Branch: master Commit: 4c727e363b1acb1c09133e189639a3a3fe60032a URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=4c727e363b1acb…
Author: Óscar Nájera <on(a)mathias-kettner.de> Date: Fri Sep 21 10:38:00 2018 +0200 Extract get_bulk_notification_subject Change-Id: I57eacf57c4fee8e79167a6a550687b8d445672b6 --- cmk/notification_plugins/asciimail.py | 40 +---------------------------------- cmk/notification_plugins/mail.py | 40 +---------------------------------- cmk/notification_plugins/utils.py | 31 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 78 deletions(-) diff --git a/cmk/notification_plugins/asciimail.py b/cmk/notification_plugins/asciimail.py index 9fee7ef..4f407bb 100644 --- a/cmk/notification_plugins/asciimail.py +++ b/cmk/notification_plugins/asciimail.py @@ -141,44 +141,6 @@ def construct_content(context): return body -def find_wato_folder(context): - # Same as in notify.py - for tag in context.get("HOSTTAGS", "").split(): - if tag.startswith("/wato/"): - return tag[6:].rstrip("/") - return "" - - -def get_bulk_notification_subject(contexts, hosts): - hosts = list(hosts) - bulk_subject = None - folder = None - bulk_context = {} - for context in contexts: - if context.get("PARAMETER_BULK_SUBJECT"): - bulk_context = context - bulk_subject = context["PARAMETER_BULK_SUBJECT"] - folder = find_wato_folder(context) - break - - if bulk_subject: - subject = bulk_subject - elif len(hosts) == 1: - subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for %s" % hosts[0] - else: - subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for $COUNT_HOSTS$ hosts" - - if "$FOLDER$" in subject and folder is not None: - subject = subject.replace("$FOLDER$", folder) - if "$COUNT_NOTIFICATIONS$" in subject: - subject = subject.replace("$COUNT_NOTIFICATIONS$", str(len(contexts))) - if "$COUNT_HOSTS$" in subject: - subject = subject.replace("$COUNT_HOSTS$", str(len(hosts))) - - subject = utils.substitute_context(subject, bulk_context) - return subject - - def main(): if bulk_mode: content_txt = "" @@ -190,7 +152,7 @@ def main(): mailto = context['CONTACTEMAIL'] # Assume the same in each context subject = context['SUBJECT'] hosts.add(context["HOSTNAME"]) - subject = get_bulk_notification_subject(contexts, hosts) + subject = utils.get_bulk_notification_subject(contexts, hosts) else: # gather all options from env diff --git a/cmk/notification_plugins/mail.py b/cmk/notification_plugins/mail.py index db54919..c055fa3 100644 --- a/cmk/notification_plugins/mail.py +++ b/cmk/notification_plugins/mail.py @@ -917,44 +917,6 @@ def render_elements(context, elements): utils.substitute_context(tmpl_html, context) -def find_wato_folder(context): - # Same as in notify.py - for tag in context.get("HOSTTAGS", "").split(): - if tag.startswith("/wato/"): - return tag[6:].rstrip("/") - return "" - - -def get_bulk_notification_subject(contexts, hosts): - hosts = list(hosts) - bulk_subject = None - folder = None - bulk_context = {} - for context in contexts: - if context.get("PARAMETER_BULK_SUBJECT"): - bulk_context = context - bulk_subject = context["PARAMETER_BULK_SUBJECT"] - folder = find_wato_folder(context) - break - - if bulk_subject: - subject = bulk_subject - elif len(hosts) == 1: - subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for %s" % hosts[0] - else: - subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for $COUNT_HOSTS$ hosts" - - if "$FOLDER$" in subject and folder is not None: - subject = subject.replace("$FOLDER$", folder) - if "$COUNT_NOTIFICATIONS$" in subject: - subject = subject.replace("$COUNT_NOTIFICATIONS$", str(len(contexts))) - if "$COUNT_HOSTS$" in subject: - subject = subject.replace("$COUNT_HOSTS$", str(len(hosts))) - - subject = utils.substitute_context(subject, bulk_context) - return subject - - def main(): if bulk_mode: attachments = [] @@ -971,7 +933,7 @@ def main(): mailto = context['CONTACTEMAIL'] # Assume the same in each context subject = context['SUBJECT'] hosts.add(context["HOSTNAME"]) - subject = get_bulk_notification_subject(contexts, hosts) + subject = utils.get_bulk_notification_subject(contexts, hosts) else: # gather all options from env diff --git a/cmk/notification_plugins/utils.py b/cmk/notification_plugins/utils.py index 3299cd2..573529b 100644 --- a/cmk/notification_plugins/utils.py +++ b/cmk/notification_plugins/utils.py @@ -36,6 +36,7 @@ except ImportError: # Default to python2 from cgi import escape as html_escape +from cmk_base.notify import find_wato_folder def collect_context(): # type: () -> Dict @@ -125,6 +126,7 @@ def set_mail_headers(target, subject, from_address, reply_to, mail): return mail + def send_mail_sendmail(m, target, from_address): cmd = ["/usr/sbin/sendmail"] if from_address: @@ -169,3 +171,32 @@ def read_bulk_contexts(): context[key] = value return parameters, contexts + +def get_bulk_notification_subject(contexts, hosts): + hosts = list(hosts) + bulk_subject = None + folder = None + bulk_context = {} + for context in contexts: + if context.get("PARAMETER_BULK_SUBJECT"): + bulk_context = context + bulk_subject = context["PARAMETER_BULK_SUBJECT"] + folder = find_wato_folder(context) + break + + if bulk_subject: + subject = bulk_subject + elif len(hosts) == 1: + subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for %s" % hosts[0] + else: + subject = "Check_MK: $COUNT_NOTIFICATIONS$ notifications for $COUNT_HOSTS$ hosts" + + if "$FOLDER$" in subject and folder is not None: + subject = subject.replace("$FOLDER$", folder) + if "$COUNT_NOTIFICATIONS$" in subject: + subject = subject.replace("$COUNT_NOTIFICATIONS$", str(len(contexts))) + if "$COUNT_HOSTS$" in subject: + subject = subject.replace("$COUNT_HOSTS$", str(len(hosts))) + + subject = substitute_context(subject, bulk_context) + return subject
5 years, 12 months
1
0
0
0
Extract substitute context from mail, asciimail & pushover
by Óscar Nájera
Module: check_mk Branch: master Commit: 8d352753a0ee8b784b592eec9069600e4a19d38d URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=8d352753a0ee8b…
Author: Óscar Nájera <on(a)mathias-kettner.de> Date: Thu Sep 20 17:52:51 2018 +0200 Extract substitute context from mail, asciimail & pushover Include tests Change-Id: I4077562527b6fe84cf1e13e6946bb004fe43d252 --- cmk/notification_plugins/asciimail.py | 24 +++---------- cmk/notification_plugins/mail.py | 55 +++++------------------------- cmk/notification_plugins/pushover.py | 13 ++----- cmk/notification_plugins/utils.py | 39 +++++++++++++++++++++ tests/unit/cmk/notifications/test_utils.py | 29 ++++++++++++++-- 5 files changed, 81 insertions(+), 79 deletions(-) Diff:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commitdiff;h=8d352753a0…
5 years, 12 months
1
0
0
0
Extract read_bulk_contexts
by Óscar Nájera
Module: check_mk Branch: master Commit: 44fb3de2486ff00abb8a07429e1839cc97e72940 URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=44fb3de2486ff0…
Author: Óscar Nájera <on(a)mathias-kettner.de> Date: Fri Sep 21 10:35:23 2018 +0200 Extract read_bulk_contexts Change-Id: I1c6959e5e84a6dbf2832f54ab2daf6a83ebb14c7 --- cmk/notification_plugins/asciimail.py | 30 +----------------------------- cmk/notification_plugins/mail.py | 25 +------------------------ cmk/notification_plugins/utils.py | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 53 deletions(-) diff --git a/cmk/notification_plugins/asciimail.py b/cmk/notification_plugins/asciimail.py index 98b55ad..9fee7ef 100644 --- a/cmk/notification_plugins/asciimail.py +++ b/cmk/notification_plugins/asciimail.py @@ -141,34 +141,6 @@ def construct_content(context): return body -def read_bulk_contexts(): - parameters = {} - contexts = [] - in_params = True - - # First comes a section with global variables - for line in sys.stdin: - line = line.strip() - if not line: - in_params = False - context = {} - contexts.append(context) - else: - try: - key, value = line.split("=", 1) - value = value.replace("\1", "\n") - except: - sys.stderr.write("Invalid line '%s' in bulked notification context\n" % line) - continue - - if in_params: - parameters[key] = value - else: - context[key] = value - - return parameters, contexts - - def find_wato_folder(context): # Same as in notify.py for tag in context.get("HOSTTAGS", "").split(): @@ -210,7 +182,7 @@ def get_bulk_notification_subject(contexts, hosts): def main(): if bulk_mode: content_txt = "" - parameters, contexts = read_bulk_contexts() + parameters, contexts = utils.read_bulk_contexts() hosts = set([]) for context in contexts: context.update(parameters) diff --git a/cmk/notification_plugins/mail.py b/cmk/notification_plugins/mail.py index 9a83ce9..db54919 100644 --- a/cmk/notification_plugins/mail.py +++ b/cmk/notification_plugins/mail.py @@ -917,29 +917,6 @@ def render_elements(context, elements): utils.substitute_context(tmpl_html, context) -def read_bulk_contexts(): - parameters = {} - contexts = [] - in_params = True - - # First comes a section with global variables - for line in sys.stdin: - line = line.strip() - if not line: - in_params = False - context = {} - contexts.append(context) - else: - key, value = line.split("=", 1) - value = value.replace("\1", "\n") - if in_params: - parameters[key] = value - else: - context[key] = value - - return parameters, contexts - - def find_wato_folder(context): # Same as in notify.py for tag in context.get("HOSTTAGS", "").split(): @@ -983,7 +960,7 @@ def main(): attachments = [] content_txt = "" content_html = "" - parameters, contexts = read_bulk_contexts() + parameters, contexts = utils.read_bulk_contexts() hosts = set([]) for context in contexts: context.update(parameters) diff --git a/cmk/notification_plugins/utils.py b/cmk/notification_plugins/utils.py index 37f61fa..3299cd2 100644 --- a/cmk/notification_plugins/utils.py +++ b/cmk/notification_plugins/utils.py @@ -142,3 +142,30 @@ def send_mail_sendmail(m, target, from_address): sys.stdout.write("Spooled mail to local mail transmission agent\n") return 0 + +def read_bulk_contexts(): + parameters = {} + contexts = [] + in_params = True + + # First comes a section with global variables + for line in sys.stdin: + line = line.strip() + if not line: + in_params = False + context = {} + contexts.append(context) + else: + try: + key, value = line.split("=", 1) + value = value.replace("\1", "\n") + except: + sys.stderr.write("Invalid line '%s' in bulked notification context\n" % line) + continue + + if in_params: + parameters[key] = value + else: + context[key] = value + + return parameters, contexts
5 years, 12 months
1
0
0
0
Test read bulk contexts
by Óscar Nájera
Module: check_mk Branch: master Commit: a5ae42ea0f5cefee6499f87771539b31d43c68f7 URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=a5ae42ea0f5cef…
Author: Óscar Nájera <on(a)mathias-kettner.de> Date: Mon Sep 24 09:53:35 2018 +0200 Test read bulk contexts Change-Id: I6a02904ca2da8240b7d5f5dc5275e0f2f967fa07 --- cmk/notification_plugins/utils.py | 5 ++++- tests/unit/cmk/notifications/test_utils.py | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/cmk/notification_plugins/utils.py b/cmk/notification_plugins/utils.py index 573529b..3b43029 100644 --- a/cmk/notification_plugins/utils.py +++ b/cmk/notification_plugins/utils.py @@ -38,6 +38,7 @@ except ImportError: from cmk_base.notify import find_wato_folder + def collect_context(): # type: () -> Dict return { @@ -145,6 +146,7 @@ def send_mail_sendmail(m, target, from_address): sys.stdout.write("Spooled mail to local mail transmission agent\n") return 0 + def read_bulk_contexts(): parameters = {} contexts = [] @@ -161,7 +163,7 @@ def read_bulk_contexts(): try: key, value = line.split("=", 1) value = value.replace("\1", "\n") - except: + except ValueError: sys.stderr.write("Invalid line '%s' in bulked notification context\n" % line) continue @@ -172,6 +174,7 @@ def read_bulk_contexts(): return parameters, contexts + def get_bulk_notification_subject(contexts, hosts): hosts = list(hosts) bulk_subject = None diff --git a/tests/unit/cmk/notifications/test_utils.py b/tests/unit/cmk/notifications/test_utils.py index b221455..cbc54e2 100644 --- a/tests/unit/cmk/notifications/test_utils.py +++ b/tests/unit/cmk/notifications/test_utils.py @@ -54,3 +54,9 @@ SERVICENOTESURL=localhost ]) def test_substitute_context(context, template, result): assert result == utils.substitute_context(template, context) + + +def test_read_bulk_contents(monkeypatch, capsys): + monkeypatch.setattr('sys.stdin', ('key=val', '\n', 'key2=val2', 'a comment')) + assert utils.read_bulk_contexts() == ({'key': 'val'}, [{'key2': 'val2'}]) + assert capsys.readouterr().err == "Invalid line 'a comment' in bulked notification context\n"
5 years, 12 months
1
0
0
0
6468 FIX docker_node_network: Fix crashing HW/ SW inventory when labels are not set
by Konstantin Büttner
Module: check_mk Branch: master Commit: ef6578b1f556d15a947751930caa9e508fa56754 URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=ef6578b1f556d1…
Author: Konstantin Büttner <kb(a)mathias-kettner.de> Date: Mon Sep 24 09:43:45 2018 +0200 6468 FIX docker_node_network: Fix crashing HW/SW inventory when labels are not set Change-Id: I6024cad3c1d36a0a5b5432ecfb6ccc64758f4747 --- .werks/6468 | 11 +++++++++++ inventory/docker_node_network | 6 ++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.werks/6468 b/.werks/6468 new file mode 100644 index 0000000..2f74612 --- /dev/null +++ b/.werks/6468 @@ -0,0 +1,11 @@ +Title: docker_node_network: Fix crashing HW/SW inventory when labels are not set +Level: 1 +Component: inv +Class: fix +Compatible: compat +Edition: cre +State: unknown +Version: 1.6.0i1 +Date: 1537774944 + + diff --git a/inventory/docker_node_network b/inventory/docker_node_network index 90e45f7..fe610d4 100644 --- a/inventory/docker_node_network +++ b/inventory/docker_node_network @@ -37,7 +37,7 @@ def inv_docker_node_network_networks(info, inventory_tree, status_data_tree): status_networks = status_data_tree.get_dict("software.applications.docker.networks.") for network in parsed: - labels = sorted(["%s: %s" % (k,v) for k,v in network["Labels"].items()]) + labels = sorted(["%s: %s" % (k,v) for k,v in network.get("Labels", {}).items()]) containers = [] for container_id, container in network["Containers"].items(): @@ -57,11 +57,13 @@ def inv_docker_node_network_networks(info, inventory_tree, status_data_tree): inventory_networks[network["Name"]] = { "name" : network["Name"], "host_ifname" : network["Options"]["com.docker.network.bridge.name"], - "labels" : ", ".join(labels), "network_id" : network["Id"][:12], "scope" : network["Scope"], } + if labels: + inventory_networks[network["Name"]]["labels"] = ", ".join(labels) + inv_info["docker_node_network.networks"] = { "inv_function": inv_docker_node_network_networks,
5 years, 12 months
1
0
0
0
← Newer
1
...
11
12
13
14
15
16
17
...
38
Older →
Jump to page:
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
Results per page:
10
25
50
100
200