Friday, June 22, 2012

SMS notification from "The Dude" monitoring system

For now at my server's farm I use perfect (and free) monitoring system named "The dude". I think its wonderful soft because its easy to use, easy to setup and perfectly working at linux host under wine, and have a package (npk) for Mikrotik RouterOS.


So, In my case,  I was setup RouterOS (v 5.17 for x86 at this time) as virtual machine at my PVE cluster. The RouterOS is not a free, but its possible to get demo license from www.mikrotik.com. This license was enought for our purpouse. After that I was put dude-3.6.npk (stable V.3.6) as my RouterOS, reboot, and get ready to use monitoring system!

Ok, but looking at the screen all day is not a good idea. Will be better to get notifications from system  about problems to my phone via SMS. "The dude" have wide range of notifications types. If my mobile company would have a Email2SMS gateway, I could use the email notification. But they have not :(. 
I fond some idea  here, thogh using mobile phone via USB for virtual machine is not good too. But I foud, that my mobile company have site with possibility (after authentication) sending 20 free sms.

Finally my plan is following:
  1. Install additional virual machine with Ubuntu, and run PHP script for: - сatch syslog message- login to my mobile company's site- send received message as sms- logout.
  2. Configure "the dude" to send motification as syslog message
The stage with setup Ubuntu server in minimum configuration (F4 -> virtual machine) is very simple and not need to describe. 

Next time we need setup some additional things to make our PHP script working. We need install a PHP PECL extension/module on Ubuntu. In my case I working via proxy and before making last step, we need configure PEAR as described here: Using PEAR behind a proxy server that requires authentication

Now we need the core of our SysLog-To-SMS-Via-HTTP (omg!) script. So, here is:


#!/usr/bin/php
<?php


$bindaddr = 'udp://0.0.0.0:12345'; //listen all interface at udp port 12345
$url = 'http://xx.xx.xx.xx';       //Mobile company's IP address 
$msisdn = 'xxxxxxx';               //my cell phone number without prefix 
$prefix = 'xxx';                   //cell prefix
$pwd = 'xxxxx';                    //password for logging to site


$contenttype = 'application/x-www-form-urlencoded';
$useragent='Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)';


$debug = false;


echo "SYSLOG2SMS Gateway Server V.1.0\r\n";


$socket = stream_socket_server($bindaddr, 
                               $errno, 
                               $errstr, 
                               STREAM_SERVER_BIND);
if ($socket)
{
  echo "Listen ".$bindaddr."\r\n";
  while ($conn = stream_socket_recvfrom($socket,160)) {
    echo date("D M j H:i:s Y").": received msg:".$conn."\r\n";
    sendsms($conn);
    // fclose($conn);
  }
  fclose($socket);
}




function sendsms($smstxt)
{
global $url, $msisdn, $prefix, $pwd, $contenttype, $useragent, $debug;


$r = new HttpRequest($url.'/server.php', HttpRequest::METH_GET);
$r->addQueryData(array('action'=> 'auth',
                       'msisdn'=> $prefix.$msisdn,
                       'pwd'=> $pwd));
$r->setContentType = $contenttype;
$r->setOptions(array('timeout'=>'60', 'useragent'=> $useragent));
$r->enableCookies();


try {
    echo "Login in progress...";
    $r->send();
    if ($r->getResponseCode() == 200) {
        echo "done. ";


        if ($debug) { 
                    file_put_contents('afterlogin.html', $r->getResponseBody()); 
                    };


        $r = new HttpRequest($url.'/server.php', HttpRequest::METH_POST);
        $r->addPostFields(array('action' => 'sendsms',
                                'msisdn_to' => $msisdn,
                                'sms_text' => $smstxt,
                                'keystring' => '',
                                'lang' => 'tran',
                                'cal' => 'no',
                                'flash' => '0'));
        $r->setContentType = $contenttype;
        try { 


            echo "Sending sms ...";
            $r->send();


            $xml = simplexml_load_string($r->getResponseBody());
            $errorcode = $xml->error_code;
            if ($errorcode == 0) { 
                echo "successful. ";
                                 }
            else { 
                echo "snseccesful. Error code:".$errorcode." ";
                 };


            if ($debug) {
                         file_put_contents('aftersent.html', 
                                            $r->getResponseBody());
                        };


            $r = new HttpRequest($url.'/logout.php', HttpRequest::METH_GET);
            $r->setContentType = $contenttype;
            echo "Logout ...";
            $r->send();


            if ($debug) {
                         file_put_contents('afterlogout.html', 
                                            $r->getResponseBody());
                        };


            echo "done.\r\n";


            } catch (HttpException $ex) { 
                       echo "\r\n".$ex; 
                       }
    }
} catch (HttpException $ex) {
    echo "\r\n".$ex;
}
}


?>


As you see at the first line the code starting with #!/usr/bin/php. If you want to run php script in shell, simply put the following line at the beginning of your php file. 

So, to check our script working we can use this one:


<?php
$fp = stream_socket_client("udp://ip-address-ubuntu-server:12345", $errno, $errstr);


if ($fp)
{
        fwrite($fp, "THIS IS TEST MESSAGE");
        //$buf = fgets($fp);
        //var_dump($buf);
        fclose($fp);
}
?>


Ok, now the only remaining setup our "The Dude" server to send notification messages to our gateway:


Well, fnally lets make auto-start a shell script on Ubuntu ServerAnd let us hope that SMS messages will be sent as rarely as possible :)

Good luck!

No comments:

Post a Comment