Jump to content
lims

[ask] Module VM Status Client Area

Recommended Posts

i want t create VM status in Client area using  API Key and API Hash Client in SolusVM

i have found single PHP at github  ==> https://github.com/liamjack/SolusVM-Status single php Here ==> https://raw.githubusercontent.com/liamjack/SolusVM-Status/master/index.php

its Work with single PHP, but i don't know how to added in whmcs module

 already have created vmstatus folder and php in modules/servers

<?php
function vmstatus_MetaData()
{
    return array( "DisplayName" => "VM Status", "APIVersion" => "1.0" );
}
// I don't know after this

687474703a2f2f692e696d6775722e636f6d2f37

Edited by lims

Share this post


Link to post
Share on other sites

In the server module, you would use the _clientarea($vars) function to output your content to the browser.  So in this case you would get the status of the VM by incorporating that status file you found in to a function and calling that function in _clientarea() for the status to then output via the server module's template file. 

Share this post


Link to post
Share on other sites
15 hours ago, steven99 said:

In the server module, you would use the _clientarea($vars) function to output your content to the browser.  So in this case you would get the status of the VM by incorporating that status file you found in to a function and calling that function in _clientarea() for the status to then output via the server module's template file. 

can u help how to use this file on .tpl ?

<?php if($action == "info") { ?>
	<h3>Hostname</h3>
	<?php echo $return['hostname']; ?>
	<h3>Status</h3>
	<?php echo $return['vmstat']; ?>
	<h3>IP Address</h3>
	<?php echo $return['ipaddress']; ?>
<?php } ?>

a above work in single php but i don't know, how tu use ini tpl

Share this post


Link to post
Share on other sites

Okay, lets presume you return an array like this in _clientarea():

<?php
$aR = array( 'vars' => array("Status"=>$return['vmstat'], 'hostname'=>$return['hostname'], 'IP'=>$return['ipaddress']),
            'templatefile' => 'clientarea');
return $aR;

templatefile is the actual template file you want to use without the .tpl .  If you put templates in to a sub folder of the server module's folder, then just add that folder name before it like any other path.  Just remember to leave off the .tpl as smarty (the template engine) adds it on.

vars is what will be provided to the template.

Now in the clientarea.tpl, to output your variables, you just use {$variableName} such as:

<h3>Host</h3>
{$Hostname}
<h3>Status</h3>
{$Status} 
<h3>IP Address</h3>
{$IP}

 

  • Thanks 1

Share this post


Link to post
Share on other sites

@steven99 thanks so much for help, but i don't know did not show in client area

here this full original source code :

<?php

function humanFileSize($size, $unit="") {
  if( (!$unit && $size >= 1<<30) || $unit == "GB")
    return number_format($size/(1<<30),2)." GB";
  if( (!$unit && $size >= 1<<20) || $unit == "MB")
    return number_format($size/(1<<20),2)." MB";
  if( (!$unit && $size >= 1<<10) || $unit == "KB")
    return number_format($size/(1<<10),2)." KB";
  return number_format($size)." bytes";
}

function post($action) {
	// Prepare the POST data

	// <configure> :

	$postfields["key"] = "YOUR API KEY";
	$postfields["hash"] = "YOUR API HASH";
	
	// This is the hostname of the master SolusVM server where you go to boot / reboot / configure / reinstall / get API keys for your VPS
	
	$masterurl = "https://master.vpshost.tld";
	
	// </configure>
	
	$postfields["action"] = $action;
	$postfields["status"] = "true";
	
	if($action == "info") {
		$postfields["hdd"] = "true";
		$postfields["mem"] = "true";
		$postfields["bw"] = "true";
	}

	// Prepare the POST request

	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, "{$masterurl}/api/client/command.php");
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect:  "));
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	
	// Execute the request

	$data = curl_exec($ch);
	
	$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	
	if($code != 200) {
		$return['error'] = 1;
		
		if($code == 405) {
			$return['message'] = "Incorrect API credentials.";
			return $return;
		}
		
		$return['message'] = "Invalid status code.";
		
		return $return;
	}

	// Close the Curl handle

	curl_close($ch);
	
	if(!$data) {
		$return['error'] = 1;
		$return['message'] = "Error connecting to API.";
		
		return $return;
	}

	// Extract the data

	preg_match_all('/<(.*?)>([^<]+)<\/\\1>/i', $data, $match);

	$result = array();

	foreach ($match[1] as $x => $y) {
		$result[$y] = $match[2][$x];
	}
	
	if($result['status'] == "error") {
		$return['error'] = 1;
		$return['message'] = $result['statusmsg'];
		
		return $return;
	}

	$return = $result;
	$return['error'] = 0;
	
	return $return;
}

if(isset($_GET['action'])) {
	$action = $_GET['action'];
} else {
	$action = "info";
}

switch($action)
{
	case 'info':
		$result = post("info");
		
		if($result['error'] == 0) {
			$return = $result;
			
			$return['hdd'] = explode(",", $return['hdd']);
			$return['mem'] = explode(",", $return['mem']);
			$return['bw'] = explode(",", $return['bw']);
		} else {
			$return = $result;
		}
		
		break;
	
	case 'reboot':
		$result = post("reboot");
		
		if($result['error'] == 0) {
			$return = $result;
			
			$return['message'] = "Server rebooting now.";
		} else {
			$return = $result;
		}
		
		break;
		
	case 'boot':
		$result = post("boot");
		
		if($result['error'] == 0) {
			$return = $result;
			
			$return['message'] = "Server booting now.";
		} else {
			$return = $result;
		}
		
		break;
			
	case 'shutdown':
		$result = post("shutdown");
		
		if($result['error'] == 0) {
			$return = $result;
			
			$return['message'] = "Server shutting down now.";
		} else {
			$return = $result;
		}
		
		break;	
	
	default:
		$return['error'] = 1;
		$return['message'] = "Invalid action specified.";
}

?>

<!doctype html>
<html>
<head>
	<title>SolusVM-Status</title>
	<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
	<div style="width: 90%; max-width: 800px; margin: 0 auto;">
		<h1>SolusVM-Status</h1>
		<hr/>
		
		<?php if($return['error'] == 1) { ?>
			<div class="alert alert-danger" role="alert"><?php echo $return['message']; ?></div>
			
			<a class="btn btn-default btn-block" href="?action=info" role="button">Return to status page</a>
		<?php } else { ?>
		
		<?php if($action == "info") { ?>
		
			<h3>Hostname</h3>
			<?php echo $return['hostname']; ?>
			
			<h3>Status</h3>
			<?php echo $return['vmstat']; ?>
			
			<h3>IP Address</h3>
			<?php echo $return['ipaddress']; ?>
		
			<h3>Hard Disk usage <small><?php echo humanFileSize($return['hdd'][1]); ?> used of <?php echo humanFileSize($return['hdd'][0]); ?></small></h3>
			<div class="progress">
				<div class="progress-bar" role="progressbar" aria-valuenow="<?php echo $return['hdd'][3]; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $return['hdd'][3]; ?>%;">
					<?php echo $return['hdd'][3]; ?>%
				</div>
			</div>
		
			<h3>RAM usage <small><?php echo humanFileSize($return['mem'][1]); ?> used of <?php echo humanFileSize($return['mem'][0]); ?></small></h3>
			<div class="progress">
				<div class="progress-bar" role="progressbar" aria-valuenow="<?php echo $return['mem'][3]; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $return['mem'][3]; ?>%;">
					<?php echo $return['mem'][3]; ?>%
				</div>
			</div>
		
			<h3>Bandwith usage <small><?php echo humanFileSize($return['bw'][1]); ?> used of <?php echo humanFileSize($return['bw'][0]); ?></small></h3>
			<div class="progress">
				<div class="progress-bar" role="progressbar" aria-valuenow="<?php echo $return['bw'][3]; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $return['bw'][3]; ?>%;">
					<?php echo $return['bw'][3]; ?>%
				</div>
			</div>
			
			
			<div class="panel panel-default">
				<div class="panel-heading">
					<h3 class="panel-title">Actions</h3>
				</div>
				
				<div class="panel-body">
				<?php if($return['vmstat'] == "offline") { ?>
					<a class="btn btn-primary btn-block" href="?action=boot" role="button">Boot</a>
				<?php } else { ?>
					<a class="btn btn-primary btn-block" href="?action=shutdown" role="button">Shutdown</a>
					<a class="btn btn-primary btn-block" href="?action=reboot" role="button">Reboot</a>
					<a class="btn btn-default btn-block" href="?action=info" role="button">Reload the page</a>
				<?php } ?>
				</div>
			</div>
		
		<?php } ?>
		
		<?php if($action == "reboot" || $action == "boot" || $action == "shutdown") { ?>
		
			<?php if($return['error'] == 0) { ?>
			<div class="alert alert-success" role="alert"><?php echo $return['message']; ?></div>
			<?php } ?>
			
			<a class="btn btn-default btn-block" href="?action=info" role="button">Return to status page</a>
			
		<?php } ?>
		
		
		<?php } ?>
		<footer style="margin: 20px 0; text-align: center;">
			<a href="https://github.com/cuonic/SolusVM-Status" target="_blank">SolusVM-Status</a> by Cuonic
		</footer>
	</div>
</body>
</html>

i'm using templare like this

<?php
function vmstatus_MetaData()
{
    return array( "DisplayName" => "VM Status", "APIVersion" => "1.0" );
}
function vmstatus_ClientArea(array $params)
{
        $templateFile = 'templates/overview.tpl';
        return array(
            'tabOverviewReplacementTemplate' => $templateFile,
        );
}

need more integrated to client area..

Share this post


Link to post
Share on other sites
3 hours ago, steven99 said:

What is in the overview.tpl file?  

want to input this..

Spoiler

<!doctype html>
<html>
<head>
	<title>SolusVM-Status</title>
	<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
</head>

<body>
	<div style="width: 90%; max-width: 800px; margin: 0 auto;">
		<h1>SolusVM-Status</h1>
		<hr/>
		
		<?php if($return['error'] == 1) { ?>
			<div class="alert alert-danger" role="alert"><?php echo $return['message']; ?></div>
			
			<a class="btn btn-default btn-block" href="?action=info" role="button">Return to status page</a>
		<?php } else { ?>
		
		<?php if($action == "info") { ?>
		
			<h3>Hostname</h3>
			<?php echo $return['hostname']; ?>
			
			<h3>Status</h3>
			<?php echo $return['vmstat']; ?>
			
			<h3>IP Address</h3>
			<?php echo $return['ipaddress']; ?>
		
			<h3>Hard Disk usage <small><?php echo humanFileSize($return['hdd'][1]); ?> used of <?php echo humanFileSize($return['hdd'][0]); ?></small></h3>
			<div class="progress">
				<div class="progress-bar" role="progressbar" aria-valuenow="<?php echo $return['hdd'][3]; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $return['hdd'][3]; ?>%;">
					<?php echo $return['hdd'][3]; ?>%
				</div>
			</div>
		
			<h3>RAM usage <small><?php echo humanFileSize($return['mem'][1]); ?> used of <?php echo humanFileSize($return['mem'][0]); ?></small></h3>
			<div class="progress">
				<div class="progress-bar" role="progressbar" aria-valuenow="<?php echo $return['mem'][3]; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $return['mem'][3]; ?>%;">
					<?php echo $return['mem'][3]; ?>%
				</div>
			</div>
		
			<h3>Bandwith usage <small><?php echo humanFileSize($return['bw'][1]); ?> used of <?php echo humanFileSize($return['bw'][0]); ?></small></h3>
			<div class="progress">
				<div class="progress-bar" role="progressbar" aria-valuenow="<?php echo $return['bw'][3]; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $return['bw'][3]; ?>%;">
					<?php echo $return['bw'][3]; ?>%
				</div>
			</div>
			
			
			<div class="panel panel-default">
				<div class="panel-heading">
					<h3 class="panel-title">Actions</h3>
				</div>
				
				<div class="panel-body">
				<?php if($return['vmstat'] == "offline") { ?>
					<a class="btn btn-primary btn-block" href="?action=boot" role="button">Boot</a>
				<?php } else { ?>
					<a class="btn btn-primary btn-block" href="?action=shutdown" role="button">Shutdown</a>
					<a class="btn btn-primary btn-block" href="?action=reboot" role="button">Reboot</a>
					<a class="btn btn-default btn-block" href="?action=info" role="button">Reload the page</a>
				<?php } ?>
				</div>
			</div>
		
		<?php } ?>
		
		<?php if($action == "reboot" || $action == "boot" || $action == "shutdown") { ?>
		
			<?php if($return['error'] == 0) { ?>
			<div class="alert alert-success" role="alert"><?php echo $return['message']; ?></div>
			<?php } ?>
			
			<a class="btn btn-default btn-block" href="?action=info" role="button">Return to status page</a>
			
		<?php } ?>
		
		
		<?php } ?>
		<footer style="margin: 20px 0; text-align: center;">
			<a href="https://github.com/cuonic/SolusVM-Status" target="_blank">SolusVM-Status</a> by Cuonic
		</footer>
	</div>
</body>
</html>

 

 

Edited by lims

Share this post


Link to post
Share on other sites

Okay, you need to rip out the PHP in there as it wont work as expected because the variables are not within the php's scope, php is blocked in tpl files by default for security, and generally want to keep php to processing and use smarty's methods to output and do if blocks.  Smarty, the template engine used in WHMCS, has its own "language" if you will to accomplish most of that.  For if..else, you use {if $somethign eq "something"} where "eq" means equals, "neq" means not equal.  To output variables, you use {$variable} but you need to return that variable within the "vars" array in the array that you return.   For items within arrays, you need to use a period "." instead of ['variable'] and if its an object then you can use -> as normal. 

An example of this starting with _clientarea:

<?php 
function vmstatus_ClientArea(array $params) 
{ 
  $templateFile = 'templates/overview.tpl'; 
  $VMstatus = getVMstatus($params['serviceid']);
  if ($VMstatus) 
  { 
    return array('vars'=>array('VMstatus'=$VMstatus) 'tabOverviewReplacementTemplate' => $templateFile, ); 
  } 
  else 
  { 
    return array('vars'=>array('error'=>true,'errorMessage'=>"Didn't fetch VM status") 
                 'tabOverviewReplacementTemplate' => $templateFile, ); 
  }
}

Overview.tpl

{ if $action eq "info" ) 

   <h3>Hostname</h3>
   {$VMstatus.hostname}

   <h3>Status</h3>
   {$VMstatus.vmstat}            

    <h3>IP Address</h3>
    {$VMstatus.ipaddress}
{else}
  {if $error}
     <div class='alert alert-danger'>{$errorMessage}</div>
  {else}
      <div class="alert alert-info">{$actionResults}</div>
  {/if}
{/if}
        

Note in the templates you don't want the html, head, or body tags and just the content you want to output .
       

  • Thanks 1

Share this post


Link to post
Share on other sites

@steven99 thanks so much for help me..

already try added the last post but get

error.thumb.JPG.3e6f26bab445daa0132f49ae72b420f1.JPG

a;ready try use clientarea.tpl but get the same i think this only for php

Share this post


Link to post
Share on other sites

Under WHMCS admin -> Setup menu -> General settings -> other tab, check "display errors" and that will show the actual error.  Also, my code is more of an example than a verbatim to use.    I suspect the issue as a ")" where a "}" is needed in the overview.tpl file -> { if $action eq "info" )

Share this post


Link to post
Share on other sites

error log..

ParseError: syntax error, unexpected '=', expecting ')' 
in /==/modules/servers/vmstatus/vmstatus.php: 22
Stack trace:
#0 /vendor/whmcs/whmcs-foundation/lib/Module/Server.php(0): WHMCS\Module\AbstractModule->load('vmstatus')
#1 /clientarea.php(0): WHMCS\Module\Server->loadByServiceID(802)
#2 {main}

 

Share this post


Link to post
Share on other sites

You will want to look at that line and see where the code went wrong.   There was likely a "if" block that was removed but the closing was never removed and it closes a different if. 

Though if you are not a developer, you may want to hire someone to finish this as it may take a while to do this piece by piece over a thread. 

  • Thanks 1

Share this post


Link to post
Share on other sites
50 minutes ago, steven99 said:

Though if you are not a developer, you may want to hire someone to finish this as it may take a while to do this piece by piece over a thread. 

+1 to that suggestion - it takes far longer to explain how to do something than it does to actually do it.

  • Thanks 1

Share this post


Link to post
Share on other sites

thanks so much all for help, now i'm removed  $templateFile = 'templates/overview.tpl

    $vmstatus = $result["status"] == "success";
    if ($vmstatus)
    {
        $vmstatus = array('vars' => array(
            "vmstat"=>$result['vmstat'], 
            'hostname'=>$result['hostname'], 
            'ipaddress'=>$result['ipaddress']));
        return $vmstatus;
    }

now, work perfect ....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×

Important Information

By using this site, you agree to our Terms of Use & Guidelines