Howto:Monitor device uptime and gather traces with trap information via automated script

From innovaphone wiki
Jump to navigation Jump to search

This article provides an example script used to download a trap trace in a short time after device reboot.

Applies To

This information applies to

  • all innovaphone devices


More Information

In some cases a gateway or a phone may become instable due to configuration or firmware bugs resulting in not predictable restarts of the device. In this case may be useful for diagnosis to download a trace from the device right away. This article provides a simple PHP script for this purpose.

Configuration

At first get and install the PHP as described in the related article. Then create a new file called uptime.php with following code in it:

<?php // PBX ip addresses $pbx_ips = array( "192.168.0.1", "192.168.0.2" );

// admin username $user = "admin"; // admin password $pass = "ip6000";

//loop script $loop = true;

//$DEBUG = true; $DEBUG = false;

// GET request for V9 and later firmware $get_uptime = "/CMD0/box_info.xml?xsl=box_info.xsl&m3=&help=General/Info";

// GET request for V8 and older firmware, uncomment the line below //$get_uptime = "/CMD0/mod_cmd_no_pwd.xml?cmd=box-info&xsl=box_info.xsl";

$get_trace = "/log.txt";

while($loop){

   foreach($pbx_ips as $pbx_ip){
       //URL
       $url_uptime = "http://".$pbx_ip.$get_uptime;
       $url_trace = "http://".$user.":".$pass."@".$pbx_ip.$get_trace;
      
       if ($DEBUG) echo "GET ".$url_uptime."\r\n";
  
       // get XML Object List form PBX
       $xml_string = file_get_contents($url_uptime);
 
       if(!$xml_string) {
           if ($DEBUG) echo "GET ".$url_uptime." failed\r\n";
           continue;
       }
 
       if ($DEBUG) var_dump($xml_string);
 
       // convert to UTF-8, create SimpleXMLElement
       $xml = new SimpleXMLElement(utf8_encode($xml_string));
  
       if ($DEBUG) echo "Uptime ".$pbx_ip." ".$xml->sys['uptime']." s.\r\n";
      
       if ($xml->sys['uptime'] < 20){
           $current_datetime_log = date("d.m.Y H:i:s",time());
           $current_datetime_file = date("d.m.Y-H-i-s",time());
          
           $message = $current_datetime_log." ".$pbx_ip." ALARM uptime ".$xml->sys['uptime']." seconds!!!\r\n";
           echo $message;
           file_put_contents("traps.txt",$message, FILE_APPEND);
           $trace_text = file_get_contents($url_trace);
           file_put_contents("trace-".$pbx_ip."-".$current_datetime_file.".txt",$trace_text);
       }
   }
   if ($DEBUG) echo "Sleep 20 s.\r\n";
   if($loop) sleep(20);

} ?>

You have to change $pbx_ips, $user and $pass.

Usage

Open a command prompt and change to the location you stored uptime.php in. In our example it is the same directory we have extracted PHP to.

Now you can run the script:

C:\Temp\php-5.2.5-Win32> php.exe uptime.php


The script will run until you stop it with Ctrl+C. In case that any of the devices listed in $pbx_ips performs a reboot, a trace will be dowloaded in the current directory.

Known Problems

To run this script on a web server (thus polling all listed devices once on each refresh of this page), set $loop to false.

Related Articles

Howto:Export_PBX_userlist_from_PBX_with_e164_numbering_schema