#!/usr/bin/python
# SMS (using smstools)
# Notification via sms using the sms tools
# Note: You have to add the side user to the sendsms group
# and restart the site

import os

send_path = '/usr/bin/sendsms'
if not os.path.exists(send_path):
    send_path = '/usr/local/bin/sendsms'
    if not os.path.exists(send_path):
        print "SMS Tools not found"
        os._exit(1)

max_len = 160
message = os.environ['NOTIFY_HOSTNAME'] + " "

if os.environ['NOTIFY_WHAT'] == 'SERVICE':
    message += os.environ['NOTIFY_SERVICESTATE'][:2] + " "
    avail_len = max_len - len(message)
    message += os.environ['NOTIFY_SERVICEDESC'][:avail_len] + " "
    avail_len = max_len - len(message)
    message += os.environ['NOTIFY_SERVICEOUTPUT'][:avail_len]

else:
    message += "is " + os.environ['NOTIFY_HOSTSTATE']

empf = os.environ['NOTIFY_CONTACTPAGER']
os.system("%s %s '%s'" % (send_path, empf, message[:160]))
