Difference between revisions of "Advanced Cloud Hosting/Cloud API"

From XMission Wiki
Jump to: navigation, search
Line 139: Line 139:
 
}
 
}
 
</pre></code>
 
</pre></code>
 +
 +
Here is some explanation of what this will do:
 +
:Lines 8 - 12 define memory limits
 +
<pre>
 +
define("LOWER_RAM_LIMIT", 256); # The lowest amount of memory for the server
 +
define("UPPER_RAM_LIMIT", 1024); # The highest amount of memory for the server
 +
define("LOWER_TOLERANCE", 0.25); # Decrease the memory if ratio is below this
 +
define("UPPER_TOLERANCE", 0.8); # Increase the memory if ratio is above this
 +
define("MEMORY_INCREMENT", 256); # Increment the memory by this amount
 +
</pre>
 +
 +
:Line 15 defines the API URL, and should be fine for all XMission Cloud Services customers
 +
<pre>
 +
define("API_URL", "https://api.cloud.xmission.com:4464/paci/v1.0/");
 +
</pre>
 +
 +
:Lines 18 and 19 are the user name and password for the control panel <https://cp.xmission.com>
 +
<pre>
 +
define("USER", '<YOUR_USER_NAME>');
 +
define("PASS", '<YOUR_PASSWORD>');
 +
</pre>
 +
 +
:Line 22 is the hostname of the VPS
 +
<pre>
 +
define("VPS_HOST_NAME", 'webserver01');
 +
</pre>
 +
 +
:Lines 28 through 35 gather data about the current memory load on the VPS.
 +
<pre>
 +
$memory_lines = explode("\n", $raw_output);
 +
$current_memory = preg_split("/[\s:]+/",$memory_lines[1]);
 +
 +
// $current_memory[1] is total, $current_memory[2] is used
 +
$current_memory_load = $current_memory[2] / $current_memory[1];
 +
error_log("resize_check: Current memory use is: " . $current_memory_load) ;
 +
 +
// Is the current memory load in tolerance?
 +
</pre>
 +
 +
:Lines 38 through 69 are the logic for keeping the server memory within boundaries set in the configuration.
 +
<pre>
 +
 +
    $current_memory_setting = get_mem();
 +
    error_log("resize_check: Current memory setting is: " . $current_memory_setting) ;
 +
 +
    if ($current_memory_setting > LOWER_RAM_LIMIT) {
 +
        decrease_memory($current_memory_setting, MEMORY_INCREMENT);
 +
        error_log("resize_check: Decreased memory by " . MEMORY_INCREMENT ) ;
 +
    } else {
 +
        error_log("resize_check: Could not decrease memory more, already at lowest limit.") ;
 +
    }
 +
 +
    exit(0);
 +
} else if ($current_memory_load > UPPER_TOLERANCE) {
 +
    error_log("resize_check: Current memory usage of $current_memory_load above tolerance. Resizing.");
 +
 +
    $current_memory_setting = get_mem();
 +
    error_log("resize_check: Current memory setting is: " . $current_memory_setting) ;
 +
 +
    if ($current_memory_setting < UPPER_RAM_LIMIT) {
 +
        increase_memory($current_memory_setting, MEMORY_INCREMENT);
 +
        error_log("resize_check: Increased memory by " . MEMORY_INCREMENT ) ;
 +
    } else {
 +
        error_log("resize_check: Could not increase memory more, already at highest limit.") ;
 +
    }
 +
 +
    exit(0);
 +
} else {
 +
    error_log("resize_check: Current memory usage of $current_memory_load in tolerance. Exiting.");
 +
    exit(0);
 +
}
 +
</pre>
 +
 +
:Lines 73 though 76 provide

Revision as of 14:32, 24 September 2013

If you find yourself with traffic to your website and consistently trying to log in to your Control Panel to adjust your servers RAM to compensate for the heavy load. We have a solution that may help you get some sleep.

Cloud Infrastructure API can Automatically Resize your Server. The RESTful API is on of the most useful and more interesting features of our Cloud Infrastructure service. With the ability to expose nearly all the features of your control panel the API will definitely help you get some 'REST'.

The Cloud Infrastructure RESTful API is well documented and available to all Cloud Infrastructure and Cloud Server subscribers. You can read the full documentation here:

http://download.pa.parallels.com/poa/5.4/doc/pdf/POA%20RESTful%20API%20Guide/paci-restful-api-guide-5.4.pdf


Below is a PHP script that you can add to a cron job - this one we will run every minute. The timing is up to completely up to you and you can change it any time you want. A shorter time makes the server more sensitive to traffic and load, however it also puts additional load on the server itself.

#!/usr/bin/php -q
<?php
// Script for resizing server memory using the XMission Cloud Infrastructure API

// Configuration

// Memory tolerances
define("LOWER_RAM_LIMIT", 256); # The lowest amount of memory for the server
define("UPPER_RAM_LIMIT", 1024); # The highest amount of memory for the server
define("LOWER_TOLERANCE", 0.25); # Decrease the memory if ratio is below this
define("UPPER_TOLERANCE", 0.8); # Increase the memory if ratio is above this
define("MEMORY_INCREMENT", 256); # Increment the memory by this amount

// The RESTful API
define("API_URL", "https://api.cloud.xmission.com:4464/paci/v1.0/");

// Your username and password for the API (same as your login to https://cp.xmission.com)
define("USER", '<YOUR_USER_NAME>');
define("PASS", '<YOUR_PASSWORD>');

// The name of the server - should match Control Panel's name of the server
define("VPS_HOST_NAME", 'webserver01');
// Check RAM usage
error_log("resize_check: Checking server RAM usage.");

// Get the raw memory output
$raw_output = shell_exec("free -o -m");
$memory_lines = explode("\n", $raw_output);
$current_memory = preg_split("/[\s:]+/",$memory_lines[1]);

// $current_memory[1] is total, $current_memory[2] is used
$current_memory_load = $current_memory[2] / $current_memory[1];
error_log("resize_check: Current memory use is: " . $current_memory_load) ;

// Is the current memory load in tolerance?
if (LOWER_TOLERANCE > $current_memory_load) {
    error_log("resize_check: Current memory usage of $current_memory_load below tolerance. Resizing.");

    $current_memory_setting = get_mem();
    error_log("resize_check: Current memory setting is: " . $current_memory_setting) ;

    if ($current_memory_setting > LOWER_RAM_LIMIT) {
        decrease_memory($current_memory_setting, MEMORY_INCREMENT);
        error_log("resize_check: Decreased memory by " . MEMORY_INCREMENT ) ;
    } else {
        error_log("resize_check: Could not decrease memory more, already at lowest limit.") ;
    }

    exit(0);
} else if ($current_memory_load > UPPER_TOLERANCE) {
    error_log("resize_check: Current memory usage of $current_memory_load above tolerance. Resizing.");

    $current_memory_setting = get_mem();
    error_log("resize_check: Current memory setting is: " . $current_memory_setting) ;

    if ($current_memory_setting < UPPER_RAM_LIMIT) {
        increase_memory($current_memory_setting, MEMORY_INCREMENT);
        error_log("resize_check: Increased memory by " . MEMORY_INCREMENT ) ;
    } else {
        error_log("resize_check: Could not increase memory more, already at highest limit.") ;
    }

    exit(0);
} else {
    error_log("resize_check: Current memory usage of $current_memory_load in tolerance. Exiting.");
    exit(0);
}
// Helper Functions
function get_mem() {
    $r_xml = req_rest('GET', VPS_HOST_NAME);
    return ((string) $r_xml->{'ram-size'}[0]);
}

function increase_memory($current, $increase_by) {
    $size_to_increase_to = $current + $increase_by;
    error_log("resize_check: setting memory to: " . $size_to_increase_to);
    $xml = '<?xml version="1.0" encoding="UTF-8"?>' .
    "<reconfigure-ve><ram-size>$size_to_increase_to</ram-size></reconfigure-ve>";

    $result = req_rest("PUT", VPS_HOST_NAME, $xml);
    return $result;
}

function decrease_memory($current, $decrease_by) {
    $size_to_decrease_to = $current - $decrease_by;
    error_log("resize_check: setting memory to: " . $size_to_decrease_to);
    $xml = '<?xml version="1.0" encoding="UTF-8"?>' .
    "<reconfigure-ve><ram-size>$size_to_decrease_to</ram-size></reconfigure-ve>";

    $result = req_rest("PUT", VPS_HOST_NAME, $xml);
    return $result;
}

function req_rest($method, $get_params = "", $parameters = "", $additional_headers = "") {
    $process = curl_init();
    curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', $additional_headers));
    //curl_setopt($process, CURLOPT_VERBOSE, 1);
    curl_setopt($process, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($process, CURLOPT_SSL_VERIFYHOST, 0);
    //curl_setopt($process, CURLOPT_HEADER, 1);
    curl_setopt($process, CURLOPT_USERPWD, USER. ":" . PASS);
    curl_setopt($process, CURLOPT_TIMEOUT, 30);

    switch($method) {
        case 'POST':
            curl_setopt($process, CURLOPT_POST, 1);
            curl_setopt($process, CURLOPT_POSTFIELDS, array($parameters));
            break;
        case 'PUT':
            curl_setopt($process, CURLOPT_CUSTOMREQUEST, "PUT");
            curl_setopt($process, CURLOPT_POSTFIELDS, $parameters);
        case 'GET':
            default:
            break;
    }

    curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($process, CURLOPT_URL, API_URL . "ve/" . $get_params);
    $return = curl_exec($process);
    curl_close($process);

    if (strpos($return, '<') !== false) {
        $xml = new SimpleXmlElement($return);
        return $xml;
    } else {
        return $return;
    }
}

Here is some explanation of what this will do:

Lines 8 - 12 define memory limits
define("LOWER_RAM_LIMIT", 256); # The lowest amount of memory for the server
define("UPPER_RAM_LIMIT", 1024); # The highest amount of memory for the server
define("LOWER_TOLERANCE", 0.25); # Decrease the memory if ratio is below this
define("UPPER_TOLERANCE", 0.8); # Increase the memory if ratio is above this
define("MEMORY_INCREMENT", 256); # Increment the memory by this amount
Line 15 defines the API URL, and should be fine for all XMission Cloud Services customers
define("API_URL", "https://api.cloud.xmission.com:4464/paci/v1.0/");
Lines 18 and 19 are the user name and password for the control panel <https://cp.xmission.com>
define("USER", '<YOUR_USER_NAME>');
define("PASS", '<YOUR_PASSWORD>');
Line 22 is the hostname of the VPS
define("VPS_HOST_NAME", 'webserver01');
Lines 28 through 35 gather data about the current memory load on the VPS.
$memory_lines = explode("\n", $raw_output);
$current_memory = preg_split("/[\s:]+/",$memory_lines[1]);

// $current_memory[1] is total, $current_memory[2] is used
$current_memory_load = $current_memory[2] / $current_memory[1];
error_log("resize_check: Current memory use is: " . $current_memory_load) ;

// Is the current memory load in tolerance?
Lines 38 through 69 are the logic for keeping the server memory within boundaries set in the configuration.

    $current_memory_setting = get_mem();
    error_log("resize_check: Current memory setting is: " . $current_memory_setting) ;

    if ($current_memory_setting > LOWER_RAM_LIMIT) {
        decrease_memory($current_memory_setting, MEMORY_INCREMENT);
        error_log("resize_check: Decreased memory by " . MEMORY_INCREMENT ) ;
    } else {
        error_log("resize_check: Could not decrease memory more, already at lowest limit.") ;
    }

    exit(0);
} else if ($current_memory_load > UPPER_TOLERANCE) {
    error_log("resize_check: Current memory usage of $current_memory_load above tolerance. Resizing.");

    $current_memory_setting = get_mem();
    error_log("resize_check: Current memory setting is: " . $current_memory_setting) ;

    if ($current_memory_setting < UPPER_RAM_LIMIT) {
        increase_memory($current_memory_setting, MEMORY_INCREMENT);
        error_log("resize_check: Increased memory by " . MEMORY_INCREMENT ) ;
    } else {
        error_log("resize_check: Could not increase memory more, already at highest limit.") ;
    }

    exit(0);
} else {
    error_log("resize_check: Current memory usage of $current_memory_load in tolerance. Exiting.");
    exit(0);
}
Lines 73 though 76 provide