Refactoring: Moved check parameters from unsorted.py to dedicated modules (CMK-1393)
[check_mk.git] / notifications / sms
blobdeb6ab26f1dc785c3484a0d1e5385d2c3af134e0
1 #!/usr/bin/env python
2 # SMS (using smstools)
4 # +------------------------------------------------------------------+
5 # | ____ _ _ __ __ _ __ |
6 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
7 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
8 # | | |___| | | | __/ (__| < | | | | . \ |
9 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
10 # | |
11 # | Copyright Mathias Kettner 2013 mk@mathias-kettner.de |
12 # +------------------------------------------------------------------+
14 # This file is part of Check_MK.
15 # The official homepage is at http://mathias-kettner.de/check_mk.
17 # check_mk is free software; you can redistribute it and/or modify it
18 # under the terms of the GNU General Public License as published by
19 # the Free Software Foundation in version 2. check_mk is distributed
20 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
21 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
22 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
23 # tails. You should have received a copy of the GNU General Public
24 # License along with GNU Make; see the file COPYING. If not, write
25 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
26 # Boston, MA 02110-1301 USA.
28 # Notification via sms using the sms tools
29 # Note: You have to add the side user to the sendsms group
30 # and restart the site
32 import os
33 import sys
34 import shutil
35 import tempfile
37 send_path = None
38 for binary in ['sendsms', 'smssend']:
39 if os.system('which %s >/dev/null 2>/dev/null' % binary) == 0:
40 send_path = binary
42 smsd_user = 'smsd'
43 spool_dir = '/var/spool/sms/outgoing'
44 if not os.path.exists(spool_dir):
45 spool_dir = None
47 if not send_path and not spool_dir:
48 sys.stderr.write(
49 'Error: SMS Tools binaries (sendsms or smssend) not found and spool dir does not exists.\n')
50 sys.exit(2) # Fatal error, no retry
52 max_len = 160
53 message = os.environ['NOTIFY_HOSTNAME'] + " "
55 notification_type = os.environ["NOTIFY_NOTIFICATIONTYPE"]
57 # Prepare Default information and Type PROBLEM, RECOVERY
58 if os.environ['NOTIFY_WHAT'] == 'SERVICE':
59 if notification_type in ["PROBLEM", "RECOVERY"]:
60 message += os.environ['NOTIFY_SERVICESTATE'][:2] + " "
61 avail_len = max_len - len(message)
62 message += os.environ['NOTIFY_SERVICEDESC'][:avail_len] + " "
63 avail_len = max_len - len(message)
64 message += os.environ['NOTIFY_SERVICEOUTPUT'][:avail_len]
65 else:
66 message += os.environ['NOTIFY_SERVICEDESC']
68 else:
69 if notification_type in ["PROBLEM", "RECOVERY"]:
70 message += "is " + os.environ['NOTIFY_HOSTSTATE']
72 # Ouput the other State
73 if notification_type.startswith("FLAP"):
74 if "START" in notification_type:
75 message += " Started Flapping"
76 else:
77 message += " Stopped Flapping"
79 elif notification_type.startswith("DOWNTIME"):
80 what = notification_type[8:].title()
81 message += " Downtime " + what
82 message += " " + os.environ['NOTIFY_NOTIFICATIONCOMMENT']
84 elif notification_type == "ACKNOWLEDGEMENT":
85 message += " Acknowledged"
86 message += " " + os.environ['NOTIFY_NOTIFICATIONCOMMENT']
88 elif notification_type == "CUSTOM":
89 message += " Custom Notification"
90 message += " " + os.environ['NOTIFY_NOTIFICATIONCOMMENT']
92 recipient = os.environ['NOTIFY_CONTACTPAGER'].replace(" ", "")
95 def quote_message(msg, max_length=None):
96 if max_length:
97 return "'" + msg.replace("'", "'\"'\"'")[:max_length - 2] + "'"
98 return "'" + msg.replace("'", "'\"'\"'") + "'"
101 if send_path:
102 if os.system("%s %s %s" % (send_path, recipient, quote_message(message, 160))) >> 8 != 0:
103 sys.exit(1)
104 elif spool_dir:
105 # On some distros, like debian, smstools does not ship with the sendsms/smssend helper
106 # script. On this distro, simply drop the SMS in the outgoing spool directory.
107 fd, path = tempfile.mkstemp(prefix='cmk_sms_')
108 os.write(fd, 'To: %s\n\n%s' % (recipient, quote_message(message)))
109 os.close(fd)
110 os.chmod(path, 0660)
111 filename = path.split('/')[-1]
112 shutil.move(path, spool_dir + '/' + filename)