#!/usr/bin/env python
#
# Authors:
# rafael@postgresql.org.es / http://www.postgresql.org.es/
#
# Copyright (c) 2015 USIT-University of Oslo
#
# sms_send: Custom alertscript to send SMS notifications from Zabbix
# to a SMS service.
#
# sms_send is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# sms_send is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with sms_send. If not, see .
#
import sys
import os
import urllib2
import urllib
from datetime import datetime
# ###################################################################
# Method: send_sms_message()
# Parameters: phone_number, message
#
# Used to send the notification to a SMS service via HTTPS
# ###################################################################
def send_sms_message(phone_number, message):
log_file = '/var/log/zabbixsrv/zabbix_sms_send.log'
try:
sms_server_url = 'https://sms.uio.no/sms/send'
locale_code = 'USIT'
auth_info = get_auth_info()
username = auth_info[0]
password = auth_info[1]
#
# Definition of the parameters that will be sent to sms.uio.no
#
parameters = {'b':username,'p':password,'s':locale_code,'t':phone_number,'m':message}
encoded_parameters = urllib.urlencode(parameters)
req = urllib2.Request(sms_server_url,encoded_parameters)
response = urllib2.urlopen(req,timeout=5)
output = response.read()
#
# Very simple log file with the output from the sms gateway.
#
with open(log_file,'a') as file:
file.write('[' + str(datetime.now()) + ']\n')
file.write(output)
file.write('\n')
except Exception as e:
with open(log_file,'a') as file:
file.write('[' + str(datetime.now()) + ']\n')
file.write(e)
file.write('\n')
sys.exit(1)
# ####################################################################
# Method: get_auth_info()
#
# Used to get the username and password information needed to log into
# the SMS service. This information can be found in the
# .zabbix_sms_send_auth file in the home directory of the user running
# this script.
#
# We use this auth file to avoid having to define the username and
# password information in this script.
# #####################################################################
def get_auth_info():
auth_info = []
auth_file = os.getenv('HOME') + '/.zabbix_send_sms_auth'
if os.path.isfile(auth_file):
try:
os.chmod(auth_file,0400)
with open(auth_file,'r') as file:
for line in file:
#
# Format of the file: username::password
#
auth_info= line.split('::')
auth_info[1] = auth_info[1].replace('\n','')
return auth_info
except Exception as e:
raise Exception("[ERROR]: %s\n" % e)
else:
raise Exception("[ERROR]: The file with auth information: %s does not exist" % auth_file)
# ############################################
# MAIN
# ############################################
if __name__ == '__main__':
try:
if len(sys.argv) == 4:
phone_number = sys.argv[1]
subject = sys.argv[2]
message = sys.argv[3]
#
# We send only the subject of the notification. We could
# also merge the subject and the message if we want to get
# all the information by SMS.
#
send_sms_message(phone_number,subject)
else:
print "Error: Wrong number of parameters"
print 'Format: ' + sys.argv[0] + ' '
sys.exit(1)
except Exception as e:
print e
sys.exit(1)