📜 ⬆️ ⬇️

SMS notification of power failure using Asterisk + Dongle and apcupsd

It is necessary that the mobile phone receives SMS in case of disappearance and renewal of power on the server. There is a Freebsd daemon apcupsd UPS control APC and asterisk with GSM modems connected to it.

If you have this daemon, the UPS management is on the same machine as the asterisk, you can write a script:

#!/usr/local/bin/bash /usr/local/sbin/asterisk -rx "dongle sms KS_out +380501111234 Power fackup" 

But I have a different server running the UPS, and AMI Asterisk comes to the rescue.

The first thing you need to do is enable AMI and create a user with which the client program will be authenticated. In the /etc/asterisk/manager.conf file:
')
 [general] enabled = yes port = 5038 bindaddr = 0.0.0.0 

/etc/asterisk/manager.conf

 [admin] secret=FrUyHn6FSaX deny=0.0.0.0/0.0.0.0 permit=192.168.0.0/255.255.0.0 read=system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate write=system,call,log,verbose,command,agent,user,config,command,dtmf,reporting,cdr,dialplan,originate 

To apply the changes, perform the reload:

 asterisk -rx "module reload manager"</>      PHP.   <a href="habrahabr.ru/company/centosadmin/blog/161521/"></a>. poweroff.php: <source lang="php"> <?php # —- define globals —- $strhost = "192.168.1.6"; $strport = "5038"; $timeout = "10"; $d=date(DATE_RFC822); $errno=0 ; $errstr=0 ; $sconn = fsockopen ($strhost, $strport, &$errno, &$errstr, $timeout) or die("Connection to $strhost:$strport failed"); if (!$sconn) { echo "$errstr ($errno)<br>\n"; } else { fputs ($sconn, "Action: login\r\n"); fputs ($sconn, "Username: admin\r\n"); fputs ($sconn, "Secret: FrUyHn6FSaX\r\n"); fputs ($sconn, "Events: on\r\n\r\n"); usleep(1000); fputs ($sconn, "Action: Command\r\n"); fputs ($sconn, "Command: dongle sms MTS_out +380661111234 Power failure. $d \r\n"); fputs ($sconn, "Action: Logoff\r\n\r\n"); usleep (500); fclose ($sconn); } 

And poweron.php:

 <?php # —- define globals —- $strhost = "192.168.1.6"; $strport = "5038"; $timeout = "10"; $d=date(DATE_RFC822); $errno=0 ; $errstr=0 ; $sconn = fsockopen ($strhost, $strport, &$errno, &$errstr, $timeout) or die("Connection to $strhost:$strport failed"); if (!$sconn) { echo "$errstr ($errno)<br>\n"; } else { fputs ($sconn, "Action: login\r\n"); fputs ($sconn, "Username: admin\r\n"); fputs ($sconn, "Secret: FrUyHn6FSaX\r\n"); fputs ($sconn, "Events: on\r\n\r\n"); usleep(1000); fputs ($sconn, "Action: Command\r\n"); fputs ($sconn, "Command: dongle sms MTS_out +380661111234 Power is back. $d \r\n"); fputs ($sconn, "Action: Logoff\r\n\r\n"); usleep (500); fclose ($sconn); } 

Next, go to:

# cd /usr/local/etc/apcupsd/

And we edit two onbattery files - an event when switching to battery and offbattery - switching to main power.

Add lines at the beginning of the file:

/usr/local/bin/php -f /root/scripts/poweroff.php

And also for poweron.php:

/usr/local/bin/php -f /root/scripts/poweron.php

The result - when the UPS transfers to battery operation, an SMS arrives with the text “Power failure. Wed, Jun 05 13 15:53:59 +0300.

And reverse sms when power returns.

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


All Articles