📜 ⬆️ ⬇️

SMS from Bash or teach Zabbix new tricks

This article is devoted to the organization of SMS alerts in a very budget.
This method is suitable for home use or use in SOHO. For something more, this scheme is not capable, keep this in mind.
Earlier on Habré there were already articles on the topic of SMS informing, but it all came down to local USB modems or email2sms services.
This article will consider a different interaction scheme. Namely: the Mikrotik equipment will act as a GSM gateway, and Zabbix will send SMS via the terminal.

What you need:
1) Mikrotik 951 series (active USB hub is highly recommended)
2) USB modem with a SIM card
3) and a deployed Zabbix server.

And this all works according to RFC2217 .

The whole configuration is divided into 3 stages:
A) Mikrotik setup
B) Work with the send script
C) Configuring Zabbix
')

Mikrotik setup


A USB modem is connected to Mikrotik, basic configuration and modem testing are performed.
Immediately find out the channels to send SMS. In my case, this was done experimentally, channels 1 and 2 are responsible for this function.

In the documentation for ROS, the function of forwarding a COM port over TCP (RFC2217) was found. It allows you to access the equipment behind the router through a normal terminal.
Setup in Winbox
System -> Ports -> Remote Access


All parameters are intuitive. Data channels SMS Settings and Remote Port should not match!
From the network equipment side, the setup is over.

SMS sending script


In my case, Ubuntu 14.04.2 serves as the guest VM system. So historically, you have to live with it.
You can use both “iron” Zabbix and virtual.

By reading a ton of instructions, an SMS sending script was written to Bash, first in text format, and then in PDU format. PDU format allows you to send SMS in Unicode, i.e. Latin and Cyrillic characters (in this case, only they interest us).
The final script attached here allows you to send “multipage” SMS of any content, i.e. more than 70 characters.
For those who want to penetrate, I will leave a few links: tyk and tyk
Script on Bash
The script requires the Recode utility.
In case you want to test the script from the terminal, remove "-e" from "echo".
#!/bin/bash # tel=$1 header=$2 mes=$3 ip=XXX.XXX.XXX.XXX #IP  port=Y #  #  !!!   !!! TP_MR0=0 #  TP-MR IED31=1 #  IED3   UDH #... ########################################################################################################################################### #    #       UDH UDH=`echo $mes | recode ..U2/x2` #    UCS2 UDH=`echo $UDH | sed 's/0x\|,\| //g' | sed 's/000A$//g'` TP_UD=$UDH #TP-UD -   UDH=`echo -n $UDH | wc -c | gawk '{print $1}'` UDH=$(($UDH/4)) #    -      #     tel="$tel"F"" i=`echo -n $tel | wc -c | gawk '{print $1}'` i=$(($i/2)) while [ "$i" != "0" ] do R=`echo $tel | cut --complement -b '3-12' | rev` TPTEL="$TPTEL$R" tel=`echo $tel | cut --complement -b '1-2'` i=$(($i-1)) done ########################################################################################################################################### #  70  ! if [ "$UDH" -le "70" ]; then #      UCS2 TP_UDL=`echo -n $TP_UD | wc -c | gawk '{print $1}'` #       XX TP_UDL=$((($TP_UDL+1)/2)) TP_UDL=`printf '%02x' $TP_UDL | sed 's/[[:lower:]]/\u&/g'` #        #   > TPDU=""0011000B91"$TPTEL"0008AA"$TP_UDL$TP_UD" #    > #   AT+CMGS= Byte=`echo -n $TPDU | cut --complement -b '1-2'` #  2 ,      Byte=`echo -n $Byte | wc -c | gawk '{print $1}'` Byte=$((($Byte)/2)) #    .     RFC2217,  RAW ( sleep 2 echo "AT+CMGF=0" #1 -  , 0 - PDU    !!! sleep 1 echo "AT+CSCS=\"UCS2\"" # sleep 1 echo "AT+CMGS=$Byte" #     sleep 1 echo -e "$TPDU\\032" #   + Ctrl+Z sleep 3 # ,    echo -e "\\033" #ESC   ,        sleep 3 ) | telnet $ip $port #Telnet  ,     exit 0 ################################################################################################################################## #   70! else #  ,    UDH=$((($UDH/67)+1)) # UDH    ( 1 ,     67 ) IED2=$UDH #    -   UDH IED2=`printf '%02x' $IED2 | sed 's/[[:lower:]]/\u&/g'` #    .     RFC2217,  RAW ( sleep 2 echo "AT+CMGF=0" #1 -  , 0 - PDU    !!! sleep 1 echo "AT+CSCS=\"UCS2\"" # sleep 1 #   AT+CMGS= while [ $UDH -ne 0 ]; do #      UCS2 TPUD=`echo -n $TP_UD | cut --complement -b '269-100000000'` #  67      TP_UD=`echo -n $TP_UD | cut --complement -b '1-268'` #   67 ,      TP_MR=`printf '%02x' $TP_MR0 | sed 's/[[:lower:]]/\u&/g'` # TP-MR (00, 01  ..) IED3=`printf '%02x' $IED31 | sed 's/[[:lower:]]/\u&/g'` #   -   UDH UDH_TP_UD=""050003FF"$IED2$IED3$TPUD" #     TP-UDL TP_UDL=`echo -n $UDH_TP_UD | wc -c | gawk '{print $1}'` #      XX TP_UDL=$(($TP_UDL/2)) TP_UDL=`printf '%02x' $TP_UDL | sed 's/[[:lower:]]/\u&/g'` #        TPDU=""0041"$TP_MR"0B91"$TPTEL"0008"$TP_UDL$UDH_TP_UD" #    > TP_MR0=$(($TP_MR0+1)) # $TP-MR0  1    IED31=$(($IED31+1)) #     UDH UDH=$(($UDH-1)) #       #   AT+CMGS= Byte=`echo -n $TPDU | cut --complement -b '1-2'` #  2 ,      Byte=`echo -n $Byte | wc -c | gawk '{print $1}'` Byte=$((($Byte)/2)) #      echo "AT+CMGS=$Byte" #     sleep 1 echo -e "$TPDU\\032" #   + Ctrl+Z sleep 3 # ,    echo -e "\\033" #ESC   ,        sleep 3 done ) | telnet $ip $port #Telnet  ,     fi exit 0 # ,        ,  "-e"  "echo" 


In the script you need to change two variables to yours - IP and Port.
Three variables are transferred to the script in order: phone number, title (not used, just Zabbix sends data to the script in such a sequence) and the message itself. Additionally, variables need not be escaped, Zabbix does it itself.
By default, the script should be in:
for version 2.2 - / usr / local / share / zabbix / alertscripts
for version 2.4 - / usr / lib / zabbix / alertscripts.
Do not forget to give the appropriate rights to the script file!

Zabbix configuration


On the Zabbix side, the setup procedure is trivial, but I will describe it again for posting.
1) Specify the notification method
2) Specify the desired phone in the user profile
The phone is entered in 11-digit format, i.e. 7 **********
3) Configure actions on a triggered trigger.
Pictures for posting






Do not forget that the “Default Subject” action field is not taken into account in the script, therefore, you should put everything you need into the “Default Message”. I use the following construct for this: {TRIGGER.NAME} {TRIGGER.DESCRIPTION} {ITEM.NAME} - {ITEM.LASTVALUE} . It is more than informative.

Afterword


As I warned at the beginning of the article, everything is very primitive and is not suitable for "production". But the method allows for quite modest money to receive a little more operational information from your monitoring system. For this, allow me to bow out.

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


All Articles