📜 ⬆️ ⬇️

The implementation of sending sms-notifications

In view of the rather large fleet of servers / switches / modems and other active equipment in the office, the zabbix monitoring system was installed and successfully used for a long time. Zabbix has a great opportunity to send notifications about problems.
For this, a script was written to send sms messages through the email operator's email-to-sms gateway, the limitation on the number of SMS messages from one address per day was circumvented by rotating outgoing addresses, it worked more or less well, but recently SMS messages started through this gateway reach a delay of about 10-15 minutes, which is not very pleasant.
So, it was decided to organize the sending of notifications through our own GSM-terminal, rummaging in the price lists of suppliers and not finding there suitable at the price and characteristics of GSM modems was very upset.
And then I remembered that the old Siemens CX65 was lying at home, and even the data cable to it, after connecting the phone and smoking the docks by sending sms messages came to a not very happy conclusion, it turns out that siemens does not support sending sms in text mode, AT + CMGF = 1 returns error.
Sending messages in these devices is possible only in the PDU mode, for the sake of sporting interest and for brainwashing it was decided to implement this system, a script was written for recoding the message format to the PDU and sent via the phone.

Manual on the message sending 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 the correctness of the code, I do not often do programming python, so I’m ready to accept any optimization of the script.

The phone connects 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-comp bundle.
For the script to work correctly, you need 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, each time the cable is connected, the com-port parameters are set automatically; there is no need to start applications manually.
Further, in zabbix, the setting is not complicated and the system for sending notifications is ready, the budget version with a GSM terminal works much better than email-to-sms services, plus it can send notifications that the channel has dropped to the Internet, previously, after the Internet dropped, sending could not be carried out.

Perhaps this material will be useful to someone, the old mobile phone is probably lying around each house, and I don’t really want to buy a gsm modem for more than 8,000 rubles.

')

Source: https://habr.com/ru/post/58348/


All Articles