Integrating OpenSIPS and cacti using xmlrpc and script server

Script for script server:

Put (adjust long lines) following script to /cactipath/scripts/ to file ss_OpensipsXmlrpcStat.php and load mi_xmlrpc in OpenSIPS configuration

—8<—

<?php
/* script will query opensips statistic via xmlrpc interface
 *
 * Parameters: host, port, statistic
 *
 */

// do NOT run this script through a web browser
if(!isset($_SERVER["argv"][0]) || \
    isset($_SERVER['REQUEST_METHOD'])  || \
    isset($_SERVER['REMOTE_ADDR'])) {
    die("<br><strong>This script is only meant \
    to run at the command line.</strong>");
}

// cacti script server stuff
// see http://www.cacti.net/downloads/docs/html/\
   migration_php_scripts_to_script_server.html
$no_http_headers = TRUE;

/* display No errors */
error_reporting(E_ERROR);

include_once(dirname(__FILE__) . "/../include/config.php");
include_once(dirname(__FILE__) . "/../lib/snmp.php");

if (!isset($called_by_script_server)) {
    array_shift($_SERVER["argv"]);
    print call_user_func_array("ss_OpensipsXmlrpcStats", \
                                $_SERVER["argv"]);
}

// main stuff goes here
/* uses depreciated XML_RPC :-/ */
require_once 'XML/RPC.php';

function ss_OpensipsXmlrpcStat($hostname, $port, $stat)
{
    $rpctimeout = 15; // Default is forever, bad for cacti
    $client = new XML_RPC_Client('/RPC2', $hostname, $port);
    $msg = new XML_RPC_Message("get_statistics", \
              array(new XML_RPC_Value($stat,"string")));
    $resp = $client->send($msg, $rpctimeout);

    if(!$resp) {
        echo "Comms error: " . $client->errno ." " . $client->errstr ."\n";
        return;
    }
    if(!$resp->faultCode()) {
        /* read response and put it to format cacti needs
         * assumes OpenSIPS modparam("mi_xmlrpc", "reply_option", 0)
         */
        $data = XML_RPC_decode($resp->value());
        $data = str_replace($stat,"",$data);
        $data = str_replace(":", "_", $data);
        $data = str_replace(" = ", ":", $data);
        $data = str_replace("\n", " ", $data);
        $data = trim($data);
        return $data;
    }
    else {
        /*
         * Display problems that have been gracefully cought
         */
        echo "Fault Code: " . $resp->faultCode() . " ";
        echo "Fault Reason: " . $resp->faultString() . "\n";
    }
    return;
}

?>

—>8—

Cacti stuff

In cacti define new data input method: Data Input Methods -> Add

  • Name: OpenSIPS sl: via xmlrpc
  • Input type: Script -Script Server (PHP)
  • Input string: /cactidir/scripts/ss_OpensipsXmlrpcStat.php ss_OpensipsXmlrpcStat <hostname> <port> sl:

Create -> Add input fields hostname (special type code hostname) and port, sl: is statistics to get

Add needed output fields depending on statistics queried, I’ve been using method where I query statistics group from OpenSIPS (like usrloc:, core: etc.) at a time to reduce connection attempts (every used data input adds one new TCP connection attempt towards OpenSIPS server)

After that create data (port must match OpenSIPS modparam(“mi_xmlrpc”, “port”, 8000)) and graph templates, devices and add graphs to host

Testing script:

# cd cactidir

# php script_server.php

PHP Script Server has Started – Parent is cmd

/cactidir/scripts/ss_OpensipsXmlrpcStat.php ss_OpensipsXmlrpcStat opensips-server 8000 sl:

1xx_replies:28644 2xx_replies:48329 3xx_replies:0 4xx_replies:76906 5xx_replies:273 6xx_replies:0 sent_replies:154152 sent_err_replies:0 received_ACKs:27853

/cactidir/scripts/ss_OpensipsXmlrpcStat.php ss_OpensipsXmlrpcStat opensips-server 8000 tm:

received_replies:124165 relayed_replies:99969 local_replies:27843 UAS_transactions:103607 UAC_transactions:0 2xx_transactions:99926 3xx_transactions:0 4xx_transactions:3691 5xx_transactions:12 6xx_transactions:0 inuse_transactions:15

quit

PHP Script Server Shutdown request received, exiting

Content-type: text/html

X-Powered-By: PHP/4.3.9

Enjoy graphs

Update

After testing comment out echo’s and change returns after them to return NULL;

    if(!$resp) {
        echo "Comms error: " . $client->errno ." " . $client->errstr ."\n";
        return;
    } 

=>
    if(!$resp) {
        // echo "Comms error: " . $client->errno ." " . $client->errstr ."\n";
        return;
    }
and
        echo "Fault Code: " . $resp->faultCode() . " ";
        echo "Fault Reason: " . $resp->faultString() . "\n";
    }
    return;
=>
        // echo "Fault Code: " . $resp->faultCode() . " ";
        // echo "Fault Reason: " . $resp->faultString() . "\n";
    }
    return;

Otherwise when host is down (using data source which in turn uses script) graphs will be distorted by missing values

2 comments for “Integrating OpenSIPS and cacti using xmlrpc and script server

  1. Duane Larson
    07.05.2012 at 20:58

    I’m doing this but my graph isn’t graphing anything. It says nan. I’m wondering what your Data Template and Graph template looked like for one of these scripts. Also was there any .xml file needed for this? Looking at some of the examples they always mention a .xml file.

    • ETP
      10.05.2012 at 20:36

      Have you checked if script is working correctly and returning valid values? In many cases NaN is sign of script not returning any/rubbish values to cacti.

      There’s no need for xml files and in templates there wasn’t anything special

Leave a Reply

Your email address will not be published. Required fields are marked *