4 # rafael@postgresql.org.es / http://www.postgresql.org.es/
6 # Copyright (c) 2015 USIT-University of Oslo
8 # sms_send: Custom alertscript to send SMS notifications from Zabbix
11 # sms_send is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
16 # sms_send is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with sms_send. If not, see <http://www.gnu.org/licenses/>.
29 from datetime import datetime
31 # ###################################################################
32 # Method: send_sms_message()
33 # Parameters: phone_number, message
35 # Used to send the notification to a SMS service via HTTPS
36 # ###################################################################
38 def send_sms_message(phone_number, message):
40 log_file = '/var/log/zabbixsrv/zabbix_sms_send.log'
43 sms_server_url = 'https://sms.uio.no/sms/send'
46 auth_info = get_auth_info()
47 username = auth_info[0]
48 password = auth_info[1]
51 # Definition of the parameters that will be sent to sms.uio.no
54 parameters = {'b':username,'p':password,'s':locale_code,'t':phone_number,'m':message}
55 encoded_parameters = urllib.urlencode(parameters)
57 req = urllib2.Request(sms_server_url,encoded_parameters)
58 response = urllib2.urlopen(req,timeout=5)
59 output = response.read()
62 # Very simple log file with the output from the sms gateway.
65 with open(log_file,'a') as file:
66 file.write('[' + str(datetime.now()) + ']\n')
67 file.write(str(output))
70 except Exception as e:
72 with open(log_file,'a') as file:
73 file.write('[' + str(datetime.now()) + ']\n')
80 # ####################################################################
81 # Method: get_auth_info()
83 # Used to get the username and password information needed to log into
84 # the SMS service. This information can be found in the
85 # .zabbix_sms_send_auth file in the home directory of the user running
88 # We use this auth file to avoid having to define the username and
89 # password information in this script.
90 # #####################################################################
95 auth_file = '/var/lib/zabbix/.zabbix_send_sms_auth'
97 if os.path.isfile(auth_file):
100 os.chmod(auth_file,0400)
102 with open(auth_file,'r') as file:
106 # Format of the file: username::password
108 auth_info= line.split('::')
110 auth_info[1] = auth_info[1].replace('\n','')
113 except Exception as e:
114 raise Exception("[ERROR]: %s\n" % e)
117 raise Exception("[ERROR]: The file with auth information: %s does not exist" % auth_file)
120 # ############################################
122 # ############################################
124 if __name__ == '__main__':
127 if len(sys.argv) == 4:
128 phone_number = sys.argv[1]
129 subject = sys.argv[2]
130 message = sys.argv[3]
133 # We send only the subject of the notification. We could
134 # also merge the subject and the message if we want to get
135 # all the information by SMS.
138 send_sms_message(phone_number,subject)
141 print "Error: Wrong number of parameters"
142 print 'Format: ' + sys.argv[0] + ' <phone number> <subject> <message>'
145 except Exception as e: