Obtaining Usage Statistics in WHMCS
-----------------------------------

Thi section describes how to obtain usage statistics via in WHMCS for billing or other purposes.

.. note:: Delete statistics objects after collecting the required data.

Listing Statistics Objects
~~~~~~~~~~~~~~~~~~~~~~~~~~

You can list all available statistics objects with the ``ostor-usage`` service and no parameters. IThe output only contains objects that have not been deleted. WHMCS lists the available statistics objects from S3 cluster when you click **List statistics objects (on/off)**. Create a file ``S3_listStatsObjects.php`` with the following contents:

::

   <?php
   
   // Load configuration and libraries.
   require('../../includes/VirtuozzoStorage/S3_getConfig.php');
   require('../../includes/VirtuozzoStorage/S3_requestCurl.php');
   require('../../init.php');
   
   // List s3 statistics objects.
   function S3_listStatsObjects() {
   
       // Hide now.
       if ($_SESSION['s3_stat_objects'] == 1) {
   
           // Hide.
           $_SESSION['s3_stat_objects'] = 0;
   
           // Redirect back.
           header('Location: ' . $_SERVER['HTTP_REFERER']);
   
   	// Return immediately.
           return;
       }
   
       // Load configuration.
       $s3_config = s3_getConfig();
   
       // Get s3 statistics objects.
       $s3_client = S3_requestCurl(
           $s3_config['s3_key'],
           $s3_config['s3_secret'],
           $s3_config['s3_gateway'],
           "/?ostor-usage",
           "GET"
       );
   
       // Store s3 result.
       $_SESSION['s3_stat_objects'] = 1;
       $_SESSION['s3_stat'] = $s3_client;
   
       // Redirect back.
       header('Location: ' . $_SERVER['HTTP_REFERER']);
   }
   
   // Call function.
   S3_listStatsObjects();
   
   ?>

.. image:: ../../../images/stor_saas_whmcs_integration10.png
   :align: center
   :class: align-center

Querying Statistics Objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can display usage statistics with the ``ostor-usage`` service and parameter ``obj`` specifying the statistics object. WHMCS displays the accessed buckets, user ID, and counters when you click the **Get** button. Create a file ``S3_getStatsForObject.php`` with the following contents:

::

   <?php
   
   // Load configuration and libraries.
   require('../../includes/VirtuozzoStorage/S3_getConfig.php');
   require('../../includes/VirtuozzoStorage/S3_requestCurl.php');
   require('../../init.php');
   
   // Get s3 statistics object.
   function S3_getStatsObjects($object) {
   
       // Load configuration.
       $s3_config = s3_getConfig();
   
       // Get s3 statistics object.
       $s3_client = S3_requestCurl(
           $s3_config['s3_key'],
           $s3_config['s3_secret'],
           $s3_config['s3_gateway'],
           "/?ostor-usage&obj=" . $object,
           "GET"
       );
   
       // Store s3 result.
       $_SESSION['s3_object_statistic'] = $s3_client;
       $_SESSION['s3_object'] = $object;
   
       // Redirect back.
       header('Location: ' . $_SERVER['HTTP_REFERER']);
   }
   
   // Call function.
   S3_getStatsObjects($_GET['object']);
   
   ?>

.. image:: ../../../images/stor_saas_whmcs_integration9.png
   :align: center
   :class: align-center

Deleting Statistics Objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can delete existing statistics objects with the ``ostor-usage`` service and parameter ``obj`` specifying the statistics object. WHMCS removes the statistics object from S3 cluster when you click the **Delete** button. Create a file ``S3_deleteStatsForObject.php`` with the following contents:

::

   <?php
   
   // Load configuration and libraries.
   require('../../includes/VirtuozzoStorage/S3_getConfig.php');
   require('../../includes/VirtuozzoStorage/S3_requestCurl.php');
   require('../../init.php');
   
   // Delete s3 statistics object.
   function S3_deleteStatsForObject($object) {
   
       // Load configuration.
       $s3_config = s3_getConfig();
   
       // Delete s3 statistics object.
       S3_requestCurl(
           $s3_config['s3_key'],
           $s3_config['s3_secret'],
           $s3_config['s3_gateway'],
           "/?ostor-usage&obj=" . $object,
           "DELETE"
       );
   
       // Clear array.
       $_SESSION['s3_limits_bucket'] = null;
   
       // Redirect back.
       header('Location: ' . $_SERVER['HTTP_REFERER']);
   }
   
   // Call function.
   S3_deleteStatsForObject($_GET['object']);
   
   ?>
