]> git.uio.no Git - uio-zabbix.git/blob - sms_send.py
51d52e81846042c12ed4f88dad8f996125f001db
[uio-zabbix.git] / sms_send.py
1 #!/usr/bin/env python
2 #
3 # Authors:
4 # rafael@postgresql.org.es / http://www.postgresql.org.es/
5 #
6 # Copyright (c) 2015 USIT-University of Oslo
7 #
8 # sms_send: Custom alertscript to send SMS notifications from Zabbix
9 # to a SMS service.
10 #
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.
15 #
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.
20 #
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/>.
23 #
24
25 import sys
26 import os
27 import urllib2
28 import urllib
29 from datetime import datetime
30
31 # ###################################################################
32 # Method: send_sms_message()
33 # Parameters: phone_number, message
34 #
35 # Used to send the notification to a SMS service via HTTPS
36 # ###################################################################
37
38 def send_sms_message(phone_number, message):
39
40     log_file = '/var/log/zabbixsrv/zabbix_sms_send.log'
41
42     try:
43         sms_server_url = 'https://sms.uio.no/sms/send'
44         locale_code  = 'USIT'
45
46         auth_info = get_auth_info()
47         username = auth_info[0]
48         password = auth_info[1]
49
50         #
51         # Definition of the parameters that will be sent to sms.uio.no
52         #
53
54         parameters = {'b':username,'p':password,'s':locale_code,'t':phone_number,'m':message}
55         encoded_parameters = urllib.urlencode(parameters)
56
57         req = urllib2.Request(sms_server_url,encoded_parameters)
58         response = urllib2.urlopen(req,timeout=5)
59         output = response.read()
60
61         #
62         # Very simple log file with the output from the sms gateway.
63         #
64
65         with open(log_file,'a') as file:
66             file.write('[' + str(datetime.now()) + ']\n')
67             file.write(str(output))
68             file.write('\n')
69
70     except Exception as e:
71
72         with open(log_file,'a') as file:
73             file.write('[' + str(datetime.now()) + ']\n')
74             file.write(str(e))
75             file.write('\n')
76
77         sys.exit(1)
78
79
80 # ####################################################################
81 # Method: get_auth_info()
82 #
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
86 # this script.
87 #
88 # We use this auth file to avoid having to define the username and
89 # password information in this script.
90 # #####################################################################
91
92 def get_auth_info():
93     
94     auth_info = []
95     auth_file = '/var/lib/zabbix/.zabbix_send_sms_auth'
96
97     if os.path.isfile(auth_file):
98
99         try:
100             os.chmod(auth_file,0400)
101             
102             with open(auth_file,'r') as file:
103                 for line in file:
104
105                     #
106                     # Format of the file: username::password
107                     #
108                     auth_info= line.split('::')
109
110             auth_info[1] = auth_info[1].replace('\n','')
111             return auth_info
112
113         except Exception as e:
114             raise Exception("[ERROR]: %s\n" % e)
115
116     else:
117         raise Exception("[ERROR]: The file with auth information: %s does not exist" % auth_file)
118             
119
120 # ############################################
121 # MAIN
122 # ############################################
123
124 if __name__ == '__main__':
125
126     try:
127         if len(sys.argv) == 4:
128             phone_number = sys.argv[1]
129             subject = sys.argv[2]
130             message = sys.argv[3]
131             
132             #
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.
136             #
137
138             send_sms_message(phone_number,subject)
139
140         else:
141             print "Error: Wrong number of parameters"
142             print 'Format: ' + sys.argv[0] + ' <phone number> <subject> <message>'        
143             sys.exit(1)
144
145     except Exception as e:
146         print e
147         sys.exit(1)
148