Cleanup config.nodes_of
[check_mk.git] / cmk / notification_plugins / asciimail.py
blob69fb70d3c5561832edd681c61ad9ac5ff265227b
1 # +------------------------------------------------------------------+
2 # | ____ _ _ __ __ _ __ |
3 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
4 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
5 # | | |___| | | | __/ (__| < | | | | . \ |
6 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
7 # | |
8 # | Copyright Mathias Kettner 2013 mk@mathias-kettner.de |
9 # +------------------------------------------------------------------+
11 # This file is part of Check_MK.
12 # The official homepage is at http://mathias-kettner.de/check_mk.
14 # check_mk is free software; you can redistribute it and/or modify it
15 # under the terms of the GNU General Public License as published by
16 # the Free Software Foundation in version 2. check_mk is distributed
17 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
18 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
20 # tails. You should have received a copy of the GNU General Public
21 # License along with GNU Make; see the file COPYING. If not, write
22 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 # Boston, MA 02110-1301 USA.
25 # This script creates an ASCII email. It replaces the builtin ASCII email feature and
26 # is configurable via WATO with named parameters (only).
28 import sys
29 from email.mime.text import MIMEText
30 from cmk.notification_plugins import utils
32 opt_debug = '-d' in sys.argv
33 bulk_mode = '--bulk' in sys.argv
35 # Note: When you change something here, please also change this
36 # in web/plugins/wato/notifications.py in the default values of the configuration
37 # ValueSpec.
38 tmpl_host_subject = 'Check_MK: $HOSTNAME$ - $EVENT_TXT$'
39 tmpl_service_subject = 'Check_MK: $HOSTNAME$/$SERVICEDESC$ $EVENT_TXT$'
40 tmpl_common_body = """Host: $HOSTNAME$
41 Alias: $HOSTALIAS$
42 Address: $HOSTADDRESS$
43 """
44 tmpl_host_body = """Event: $EVENT_TXT$
45 Output: $HOSTOUTPUT$
46 Perfdata: $HOSTPERFDATA$
47 $LONGHOSTOUTPUT$
48 """
49 tmpl_service_body = """Service: $SERVICEDESC$
50 Event: $EVENT_TXT$
51 Output: $SERVICEOUTPUT$
52 Perfdata: $SERVICEPERFDATA$
53 $LONGSERVICEOUTPUT$
54 """
55 tmpl_alerthandler_host_body = """Alert handler: $ALERTHANDLERNAME$
56 Handler output: $ALERTHANDLEROUTPUT$
57 """
58 tmpl_alerthandler_service_body = "Service: $SERVICEDESC$\n" + tmpl_alerthandler_host_body
61 def construct_content(context):
63 # Create a notification summary in a new context variable
64 # Note: This code could maybe move to cmk --notify in order to
65 # make it available every in all notification scripts
66 # We have the following types of notifications:
68 # - Alerts OK -> CRIT
69 # NOTIFICATIONTYPE is "PROBLEM" or "RECOVERY"
71 # - Flapping Started, Ended
72 # NOTIFICATIONTYPE is "FLAPPINGSTART" or "FLAPPINGSTOP"
74 # - Downtimes Started, Ended, Cancelled
75 # NOTIFICATIONTYPE is "DOWNTIMESTART", "DOWNTIMECANCELLED", or "DOWNTIMEEND"
77 # - Acknowledgements
78 # NOTIFICATIONTYPE is "ACKNOWLEDGEMENT"
80 # - Custom notifications
81 # NOTIFICATIONTYPE is "CUSTOM"
83 notification_type = context["NOTIFICATIONTYPE"]
84 if notification_type in ["PROBLEM", "RECOVERY"]:
85 txt_info = "$PREVIOUS@HARDSHORTSTATE$ -> $@SHORTSTATE$"
87 elif notification_type.startswith("FLAP"):
88 if "START" in notification_type:
89 txt_info = "Started Flapping"
90 else:
91 txt_info = "Stopped Flapping ($@SHORTSTATE$)"
93 elif notification_type.startswith("DOWNTIME"):
94 what = notification_type[8:].title()
95 txt_info = "Downtime " + what + " ($@SHORTSTATE$)"
97 elif notification_type == "ACKNOWLEDGEMENT":
98 txt_info = "Acknowledged ($@SHORTSTATE$)"
100 elif notification_type == "CUSTOM":
101 txt_info = "Custom Notification ($@SHORTSTATE$)"
103 else:
104 txt_info = notification_type # Should neven happen
106 txt_info = utils.substitute_context(txt_info.replace("@", context["WHAT"]), context)
108 context["EVENT_TXT"] = txt_info
110 # Prepare the mail contents
111 if "PARAMETER_COMMON_BODY" in context:
112 tmpl_body = context['PARAMETER_COMMON_BODY']
113 else:
114 tmpl_body = tmpl_common_body
116 if "ALERTHANDLERNAME" in context:
117 my_tmpl_host_body = tmpl_alerthandler_host_body
118 my_tmpl_service_body = tmpl_alerthandler_service_body
119 else:
120 my_tmpl_host_body = tmpl_host_body
121 my_tmpl_service_body = tmpl_service_body
123 # Compute the subject and body of the mail
124 if context['WHAT'] == 'HOST':
125 tmpl = context.get('PARAMETER_HOST_SUBJECT') or tmpl_host_subject
126 if "PARAMETER_HOST_BODY" in context:
127 tmpl_body += context["PARAMETER_HOST_BODY"]
128 else:
129 tmpl_body += my_tmpl_host_body
130 else:
131 tmpl = context.get('PARAMETER_SERVICE_SUBJECT') or tmpl_service_subject
132 if "PARAMETER_SERVICE_BODY" in context:
133 tmpl_body += context["PARAMETER_SERVICE_BODY"]
134 else:
135 tmpl_body += my_tmpl_service_body
137 context['SUBJECT'] = utils.substitute_context(tmpl, context)
138 body = utils.substitute_context(tmpl_body, context)
140 return body
143 def main():
144 if bulk_mode:
145 content_txt = ""
146 parameters, contexts = utils.read_bulk_contexts()
147 hosts = set([])
148 for context in contexts:
149 context.update(parameters)
150 content_txt += construct_content(context)
151 mailto = context['CONTACTEMAIL'] # Assume the same in each context
152 subject = context['SUBJECT']
153 hosts.add(context["HOSTNAME"])
155 # Use the single context subject in case there is only one context in the bulk
156 if len(contexts) > 1:
157 subject = utils.get_bulk_notification_subject(contexts, hosts)
159 else:
160 # gather all options from env
161 context = utils.collect_context()
162 content_txt = construct_content(context)
163 mailto = context['CONTACTEMAIL']
164 subject = context['SUBJECT']
166 if not mailto: # e.g. empty field in user database
167 sys.stdout.write("Cannot send ASCII email: empty destination email address\n")
168 sys.exit(2)
170 # Create the mail and send it
171 from_address = context.get("PARAMETER_FROM")
172 reply_to = context.get("PARAMETER_REPLY_TO")
173 m = utils.set_mail_headers(mailto, subject, from_address, reply_to,
174 MIMEText(content_txt, 'plain', _charset='utf-8'))
175 try:
176 sys.exit(utils.send_mail_sendmail(m, mailto, from_address))
177 except Exception as e:
178 sys.stderr.write("Unhandled exception: %s\n" % e)
179 # unhandled exception, don't retry this...
180 sys.exit(2)