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

From XMission Wiki
Jump to: navigation, search
Line 150: Line 150:
 
</pre>
 
</pre>
  
If you want to set up your cron schedule for other times then this document you can visit:
+
If you want to set up your cron schedule for other times then this document you can visit the following link on how to set up a cron job to meat your needs:
 
:[http://en.wikipedia.org/wiki/Cron http://en.wikipedia.org/wiki/Cron]
 
:[http://en.wikipedia.org/wiki/Cron http://en.wikipedia.org/wiki/Cron]
  
  
 
This documentation is to be used as a guide. Your server will require different settings to optimize your traffic and this code is not perfect. We encourage you to customize it to fit your needs, or write it in a language of your choice. Remember to refer to the documentation.
 
This documentation is to be used as a guide. Your server will require different settings to optimize your traffic and this code is not perfect. We encourage you to customize it to fit your needs, or write it in a language of your choice. Remember to refer to the documentation.

Revision as of 14:41, 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;
    }
}


For an explanation of what is going on here visit our blog at:

http://transmission.xmission.com/2013/08/19/use-the-cloud-infrastructure-api-to-automatically-resize-your-server


You can save this script to usr/local/sbin/resize_check.php, then you will add it to the crontab using crontab -e with the following entry:

* * * * * /usr/local/sbin/resize_check.php

If you want to set up your cron schedule for other times then this document you can visit the following link on how to set up a cron job to meat your needs:

http://en.wikipedia.org/wiki/Cron


This documentation is to be used as a guide. Your server will require different settings to optimize your traffic and this code is not perfect. We encourage you to customize it to fit your needs, or write it in a language of your choice. Remember to refer to the documentation.