ICQ bug-informer in PHP + ActiveMQ

I am a developer and supervisor of a fairly large online booking system for one of the Moscow tour operators. Since this system has very high requirements in terms of reliability and security, I have to monitor all errors that occur in it, however, constantly going in and looking for new reports is not very convenient and therefore there is a need for some kind of instant notification tool, and it should Support the ability to send messages from both the web part and desktop applications.

In this article I want to talk about my experience in writing a script for instant notification of errors that occur in the system via ICQ messages. An ActiveMQ message broker is used as an intermediate link and a drive for reporting; I will tell you how to install it and configure it for working with MySQL. The main part is an ICQ bot written in PHP, it is his responsibility to listen to a specific channel in the broker and forward messages to the specified ICQ numbers. I will also tell you how to run this PHP script as a Windows service.


The scheme of this system is as follows: the application (web or desktop), when an error occurs, generates a certain XML message, which contains a brief description of the error and the recipient's ICQ number. This message is sent via the STOMP protocol to the ActiveMQ broker and enters the queue. At the other end of the world or on the same machine, there is an ICQ bot that listens to this queue and, when a message gets into it, immediately forwards it to the specified address. And now in the details.

Install and configure ActiveMQ


All programs will be installed under Windows. A few words about ActiceMQ itself, it is a message broker in which there are 2 basic concepts: queues and threads.
Topic is designed to notify a large number of subscribers about an event, such as news or the release of the next patch.
Queue is a job queue, several listeners can subscribe to it, but only one of the subscribers will receive any message, this is just right for us.

To get started, download the distribution. Installation is reduced to unpacking the archive into some distant folder. For convenience, we will install it as a Windows service, for this you need to run the ActiveMQ script \ bin \ win32 \ InstallService.bat, the presence of the installed JAVA machine is assumed by itself.
You can check the performance through "Administration" -> "Services", but just wait 10 seconds after starting and update the list, on Win 2003 server I had a problem: the service seemed to start, but immediately crashed, a long digging into the logs and google led to A simple solution is to create a work folder in the ActiveMQ \ bin \ win32 directory.

To configure ActiveMQ, you need to open the file \ conf \ activemq.xml We
describe our broker:

Pay attention to the persistent parameter, it is responsible for the fact that all your messages will be stored in the database and will not be lost after the service is restarted. Whether you need it or not, decide for yourself, I connected this function, and reconfigured it from the built-in kahaDB to a more understandable and transparent MySQL. This is done as follows:
Download the connector for Java , from it we take mysql-connector-java-5.1.14-bin.jar and drop it into the \ lib folder. In MySQL itself, we create a database called activemq, for it we will create an activemq user with the same password. Let's describe it all in the config: The block should be located outside the tag. After restarting the service, 3 tables should be created in the database, if this happened, then everything was done correctly. We go further through the config, configure the possible connections:
        

               



 



   

   

   

   

   







 

 

Here I set 2 different connections using different protocols, this is due to the fact that in PHP I use the STOMP library to connect, and under Delphi I managed to find a component that works stably over TCP.

Next, we will do the authorization, add the following piece to the block: Now create the groups.properties file in the / conf folder with the following contents: In the same place, create the users.properties file and write it into it: You also need the login.config file, it’s easier to download the whole file and drop it into folder / conf. After all the settings, we restart the service and go to the address http: // localhost: 8161 / admin / , if everything was done correctly, you will be greeted by the control panel, if so, then go to Queues and create a queue with the name icq there.


 

 

  

   

    

     " read="admins" write="admins" admin="admins" />

     " read="users" write="users" admin="admins" />

     " read="guests,users" write="guests,users" admin="guests,users" />

    


   


  


 



admins=system,sslclient,client,broker1,broker2
users=icq
guests=guest

system=password
icq=secret


ICQ bot script


I took the WebIcqPro library as the basis of the bot ; it is quite simple and stable. You will also need a library to send messages to the broker, I found a quite stable solution on the STOMP protocol , but when the connection was disconnected it was knocked out, so I had to modify it a bit to make it possible to reconnect. I will not give all the code to the bot; anyone who is really interested can download the archive.

To test the health, run the bot through the command line:
"C: \ Program Files \ PHP \ php" C: \ icqbot \ icq.php
If everything is fine with the settings, he should establish a connection with ActiveMQ and the ICQ server, and then start listening to the * Waiting for messages ... channel. To send a test message, you can use the send.php script from the archive: I had to mess with encodings for a long time so that I could send messages with Russian text, so don’t be surprised when you see cascading conversions to different encodings in scripts, otherwise it didn’t want to work . After calling send.php, a message will be sent to the specified UIN and this fact will also be reflected in the logs.

require_once 'Stomp.php';

$c = new StompConnection("localhost");

$result = $c->connect("icq", "bot");

 

$mess = '

            

            send

            111111111

            Test send:

            Проверка русского message

            
';

 

$mess =  iconv('cp1251','UTF-8',$mess);

$c->send("/queue/icq", $mess, array('persistent' => 'true'));

$c->disconnect();

?>


Run the bot as a Windows service


For this we need a set of Windows NT Resource Kit, who do not download . Suppose all the bot files we have are in the C: \ icqbot folder . Open the console and write
“C: \ Program Files \ Windows Resource Kits \ Instsrv.exe” ICQBot “C: \ Program Files \ Windows Resource Kits \ Srvany.exe”
Next, run regedit and go to the section HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ ICQBot inside it, create the Parameters section and in it the Application parameter of type REG_SZ with the value "C: \ Program Files \ PHP \ php.exe" C: \ icqbot \ icq.php
Next, go to the services and run our bot from there, now it is completely autonomous .

The bug catcher for the site


Our ultimate goal is to catch all errors, make a detailed report and send a notification about it on ICQ. Thus, it remains to describe only a script for catching these errors, it looks like this for me and is connected to all scripts where control is required: For obvious reasons, I crypt all the contents of these reports, which I advise everyone to do. Thus, a rather stable, simple and universal notification scheme was obtained, which allows sending messages to ICQ from any application in any language if one of the protocols for interacting with ActiveMQ is described for it .

ini_set('display_errors',0);

error_reporting(2);

 

if($send_report){

  include_once("Stomp.php");

 

  function send_report($message, $to = '1212312', $from = 'SENDER'){

    global $send_report, $stomp_server, $stomp_url, $stomp_user, $stomp_psw;

    if(!$send_report)return false;

    $c = new StompConnection($stomp_server);

    $result = $c->connect($stomp_user, $stomp_psw);

    if(!is_array($to))$to = array($to);

    foreach($to as $i){

      $mess = '

              

              send

              '.$i.'

              '.$from.'

              '.$message.'

              
';

      $c->send($stomp_url, iconv('cp1251','UTF-8',$mess), array('persistent' => 'true'));

    }

    $c->disconnect();

  }

}

 

function user_log ($errno, $errmsg, $file, $line) {

  global $send_report;

  if($errno == 2){

    $filename = strftime('%d.%m.%Y %H-%M-%S_').$_REQUEST['PHPSESSID'].'.err';

    $fl = fopen('errors/'.$filename,'w');

    $_SESSION['ERROR_TEXT'] =  'WARNING: '.$errmsg.' in '.$file.' on line '.$line;

    $_SESSION['ERROR_TIME'] =  strftime('%d.%m.%Y %H-%M-%S');

    $_SESSION['ERROR_PHPSESSID'] =  $_REQUEST['PHPSESSID'];

    $_SESSION['ERROR_TYPE'] =  'PHP SCRIPT ERROR';

    fwrite($fl,serialize($_SESSION));

    fclose($fl);

    if($send_report)send_report($_SESSION['ERROR_TEXT']);

  }

}

 

set_error_handler('user_log');

?>




List of used files
  1. ActiveMQ 5.4.2
  2. Configs for ActiveMQ
  3. MySQL Connector
  4. WebICQPro Library for PHP
  5. STOMP library for PHP
  6. Ready-made set of bot scripts and error interceptor
  7. Set Windows NT Resource Kit

Also popular now: