
Implementation of sending SMS notifications
In view of the fairly large fleet of servers / switches / modems and other active equipment in the office, the zabbix monitoring system was installed and was used successfully for a long time. Zabbix has the wonderful ability to send notifications of problems.
To do this, a script was written to send sms messages via the email operator's email-to-sms gateway, the limit on the number of SMS messages from one address per day was bypassed by rotating outgoing addresses, it worked more or less tolerably, but recently SMS messages through this gateway started to arrive with a delay of about 10-15 minutes, which was not very pleasant.
So, it was decided to organize the sending of notifications through our own GSM terminal, rummaging through the price lists of suppliers and not finding there suitable GSM modems for the price and characteristics was very upset.
And then I remembered that the old Siemens CX65 was lying around at home, and even the data cable to it, after connecting the phone and smoking the docks to send sms messages, I came to a not very happy conclusion, it turns out siemens does not support sending sms in text mode, the AT + CMGF = command 1 returns error.
Sending messages in these devices is possible only in PDU mode, for the sake of sports interest and to stretch the brain, it was decided to implement this system, a script was written to transcode the message format into PDU and send via phone.
A manual on the messaging system can be found here:dreamfabric.com/sms
The resulting script itself:
I do not pretend to code correctly, I do not often do python programming, so I’m ready to accept any script optimizations.
The phone is connected using a data cable, which is a usb-serial adapter, based on the Prolific pl2303 chip, this chip works stably in linux, there are no complaints about the work of the phone-computer bundle.
For the script to work correctly, it is necessary to set the necessary com port parameters before starting work.
To do this , the following line was added to /etc/udev/rules.d/50-udev.rules :
Now, with each cable connection, the com-port parameters are set automatically, there is no need to start applications manually.
Further, in zabbix, it’s not a difficult setup and the notification sending system is ready, the budget option with a GSM terminal works much better than email-to-sms services, plus it can send notifications that a channel has fallen to the Internet, earlier, after an Internet drop, sending could not be implemented.
Perhaps this material will be useful to someone, the old mobile phone is probably lying around every house, and I don’t really want to buy a gsm-modem with a cost of more than 8,000 rubles.
To do this, a script was written to send sms messages via the email operator's email-to-sms gateway, the limit on the number of SMS messages from one address per day was bypassed by rotating outgoing addresses, it worked more or less tolerably, but recently SMS messages through this gateway started to arrive with a delay of about 10-15 minutes, which was not very pleasant.
So, it was decided to organize the sending of notifications through our own GSM terminal, rummaging through the price lists of suppliers and not finding there suitable GSM modems for the price and characteristics was very upset.
And then I remembered that the old Siemens CX65 was lying around at home, and even the data cable to it, after connecting the phone and smoking the docks to send sms messages, I came to a not very happy conclusion, it turns out siemens does not support sending sms in text mode, the AT + CMGF = command 1 returns error.
Sending messages in these devices is possible only in PDU mode, for the sake of sports interest and to stretch the brain, it was decided to implement this system, a script was written to transcode the message format into PDU and send via phone.
A manual on the messaging system can be found here:dreamfabric.com/sms
The resulting script itself:
#!/usr/bin/python
import os
import sys
import time
def dectobin(i):
b = ''
while i > 0:
j = i & 1
b = str(j) + b
i >>= 1
return b
def SendSMS(dev,number,text):
pdu_data = '00'
pdu_data += '11' #SMS-SUBMIT
pdu_data += '00' #TP-Message-Reference
pdu_data += '0B' #Address-Length
pdu_data += '91' #Address-Length
'''Convert telephone number'''
number += 'F'
pdu_number = ''
for i in range(0,len(number)):
if i%2==0:
continue
pdu_number += number[i] + number[i-1]
pdu_data += pdu_number
pdu_data += '00' #TP-Protocol identifier
pdu_data += '00' #TP-Data coding scheme
pdu_data += 'AA' #TP-Validity-Period
'''Convert text to binary format'''
pdu_text_bin = []
for i in text:
dec_s=ord(i)
if dec_s == 95:
dec_s = 17
if dec_s == 94:
dec_s = 1
if dec_s == 64:
dec_s = 0
if dec_s == 36:
dec_s = 2
if dec_s == 123:
dec_s = 40
if dec_s == 125:
dec_s = 41
if dec_s == 124:
dec_s = 64
if dec_s == 126:
dec_s = 61
if dec_s == 92:
dec_s = 47
if dec_s == 91:
dec_s = 60
if dec_s == 93:
dec_s = 62
bin = dectobin(dec_s)
le = len(bin)
while le<7:
bin='0'+bin
le = len(bin)
pdu_text_bin.append(bin)
'''Encode binary to PDU format'''
pdu_text_bin_cp = []
n=0
for i in range(0,len(text)):
if (i>0) & ((i+1)%8==0):
continue
n+=1
if n==8:
n=1
if i==len(text)-1:
cp = pdu_text_bin[i][0:8-n]
else:
cp = str(pdu_text_bin[i+1][7-n:7] + pdu_text_bin[i])[0:8]
pdu_text_bin_cp.append(cp)
'''Convert PDU to hex'''
pdu_text=''
for i in pdu_text_bin_cp:
hexi = str(hex(int(i,2)))[2:4].upper()
if len(hexi) == 1:
hexi = '0' + str(hexi)
pdu_text += hexi
'''Calculate text length'''
len_hex = hex(len(text))[2:4].upper()
if len(len_hex) == 1:
len_hex = '0' + str(len_hex)
'''Calculate PDU length'''
pdu_data+=len_hex+pdu_text
pdu_len = str(len(pdu_data)/2-1)
if True:
fd = os.open(dev, os.O_RDWR)
os.write(fd, "AT+CMGF=0 \015")
time.sleep(1)
os.write(fd, "AT+CMGS=" + pdu_len + "\015")
time.sleep(1)
os.write(fd, pdu_data + "\032")
os.close(fd)
def main(argv):
SendSMS("/dev/ttyUSB0",argv[1],argv[2] + '\r\n' + argv[3])
return 0
if __name__ == '__main__': main(sys.argv)
I do not pretend to code correctly, I do not often do python programming, so I’m ready to accept any script optimizations.
The phone is connected using a data cable, which is a usb-serial adapter, based on the Prolific pl2303 chip, this chip works stably in linux, there are no complaints about the work of the phone-computer bundle.
For the script to work correctly, it is necessary to set the necessary com port parameters before starting work.
To do this , the following line was added to /etc/udev/rules.d/50-udev.rules :
KERNEL=="ttyUSB[0-9]", RUN+="/bin/stty -F /dev/%k speed 9600 -brkint -icrnl ixoff -imaxbel -opost -onlcr -isig -icanon -echo -echoe"
Now, with each cable connection, the com-port parameters are set automatically, there is no need to start applications manually.
Further, in zabbix, it’s not a difficult setup and the notification sending system is ready, the budget option with a GSM terminal works much better than email-to-sms services, plus it can send notifications that a channel has fallen to the Internet, earlier, after an Internet drop, sending could not be implemented.
Perhaps this material will be useful to someone, the old mobile phone is probably lying around every house, and I don’t really want to buy a gsm-modem with a cost of more than 8,000 rubles.