Cmd_api_show_user_usage

jefke007

Verified User
Joined
Jan 21, 2008
Messages
15
i'm having some problems with api of direct admin.
I changed server and the api is not working good.
when i use the CMD_API_SHOW_USER_USAGE
i get

bandwidth=%33%30%2E%34&db%5Fquota=%31%36%32%38%30&domainptr=%30&email%5Fquota=%38%31%39%32&ftp=%34%33&mysql=%31&nemailf=%31&nemailml=%30&nemailr=%30&nemails=%31&nsubdomains=%34%32&quota=%30%2E%30%31%35%35&vdomains=%31

DirectAdmin 1.36.2 Running
Exim 4.72 Running
MySQL 5.1.41 Running
Named 9.6 Running
ProFTPd 1.3.3c Running
sshd Running
dovecot 1.2.16 Running
Php 5.2.15 Installed
 
How do u uncode the array?
i used the code on the page of DA (http://www.directadmin.com/api.html )
Still not working,
I'm using this code in php

include("httpsocket.php");

//nou de socket is geinclude kunnen we hem starten
$sock = new HTTPSocket;

//nu gaan we inloggen in Direct Admin (verander wel de gegevens)
$sock->connect($_SESSION['wwwdomain'],2222);
$sock->set_login($_SESSION['da_logon'],$_SESSION['da_passw']);
$domain=$_SESSION['domain'] ;
//$sock->set_method('GET');

// get user usage info
$sock->query('/CMD_API_SHOW_USER_USAGE?domain='.$domain);
$user_usage = $sock->fetch_parsed_body();


I used
echo parse_str($user_usage['bandwidth']);
also tryed
unhtmlentities($user_usage);
echo parse_str($user_usage['bandwidth']);

Still nothing, is there any good example of the api for php??
 
Here is an example to split the result. But to convert from HEX to ASCII you need to search on google, there are a heaps of solutions. You need to find one that suits your needs.

You simply have to learn PHP

PHP:
include("httpsocket.php"); 

//nou de socket is geinclude kunnen we hem starten 
$sock = new HTTPSocket; 

$sock->connect($_SESSION['wwwdomain'],2222); 
$sock->set_login($_SESSION['da_logon'],$_SESSION['da_passw']); 
$domain=$_SESSION['domain'] ;
//$sock->set_method('GET');
$sock->query("/CMD_API_SHOW_USER_USAGE?domain=$domain");
$result = $sock->fetch_body();

list($bandwidth, $quota, $domainptr, $ftp, $mysql, $nemailf, $nemailml, $nemailr, $nemails, $nsubdomains, $vdomains) = explode("&", $result);

echo $bandwidth."<br>";
echo $quota."<br>";
echo $domainptr."<br>";
echo $ftp;
// etc

Other solutions are to get account usage for a user, the result is not in HEX, then it is much more easier to split up the result

PHP:
include("httpsocket.php"); 

//nou de socket is geinclude kunnen we hem starten 
$sock = new HTTPSocket; 

$sock->connect($_SESSION['wwwdomain'],2222); 
$sock->set_login($_SESSION['da_logon'],$_SESSION['da_passw']); 
$sock->set_method('GET');
$sock->query("/CMD_API_SHOW_USER_USAGE",
           array(
		'user' => $_SESSION['da_logon']
    ));
$result = $sock->fetch_body();

list($bandwidth, $quota, $domainptr, $ftp, $mysql, $nemailf, $nemailml, $nemailr, $nemails, $nsubdomains, $vdomains) = explode("&", $result);

echo $bandwidth."<br>";
echo $quota."<br>";
echo $domainptr."<br>";
echo $ftp;
// etc
 
Thanks but is not working, this is the output.
string(0) ""

The code i use is
include("httpsocket.php");

//nou de socket is geinclude kunnen we hem starten
$sock = new HTTPSocket;

$sock->connect('xxx.xxx.xxx.xxx', 2222);
$sock->set_login('xxx', 'xxx');
$sock->set_method('GET');
$sock->query("/CMD_API_SHOW_USER_USAGE?domain=xxx.be");
var_dump($result = $sock->fetch_body());
 
Just changed
$sock->connect('xxx.xxx.xxx.xxx', 2222);
to
$sock->connect('127.0.0.1', 2222);
And that worked - WTF ??
 
Last edited:
This is the code i use
if(empty($_SESSION['traffic_color'])){
//als eerst includen wij de socket zodat we die kunnen gebruiken
include("httpsocket.php");

//nou de socket is geinclude kunnen we hem starten
$sock = new HTTPSocket;

//nu gaan we inloggen in Direct Admin (verander wel de gegevens)
$sock->connect($_SESSION['wwwdomain'],2222);
$sock->set_login($_SESSION['da_logon'],$_SESSION['da_passw']);
$domain=$_SESSION['domain'] ;
//$sock->set_method('GET');

// get user usage info
$sock->query('/CMD_API_SHOW_USER_USAGE?domain='.$domain);
$user_usage = $sock->fetch_parsed_body();

// get user config info
$sock->query('/CMD_API_SHOW_USER_CONFIG?domain='.$domain);
$user_config = $sock->fetch_parsed_body();
//get system info
$sock->query('/CMD_API_SYSTEM_INFO?domain='.$domain);
$system_info = $sock->fetch_parsed_body();
//calc bandwidth usage
$traffic_percent = ($user_usage['bandwidth'] * 100) / $user_config['bandwidth'] ;
$traffic_percent = number_format($traffic_percent,2);

if($traffic_percent >= "100"){
$_SESSION['traffic_color'] = "red";
$_SESSION['traffic_w'] = 100;
}elseif($traffic_percent >= "75"){
$_SESSION['traffic_color'] = "red";
$_SESSION['traffic_w'] = $traffic_percent;
}elseif($traffic_percent >= "50" ){
$_SESSION['traffic_color'] = "orange";
$_SESSION['traffic_w'] = $traffic_percent;
}else{
$_SESSION['traffic_color'] ="green";
if($traffic_percent < "10"){
$_SESSION['traffic_w'] = 2;
}else{
$_SESSION['traffic_w'] =$traffic_percent;
}
}
// calc disk usage
$disk_percent = ($user_usage['quota'] * 100) / $user_config['quota'] ;
$disk_percent = number_format($disk_percent,2);

if($disk_percent >= "100"){
$_SESSION['disk_color'] = "red";
$_SESSION['disk_w'] = 100;
}elseif($disk_percent >= "75"){
$_SESSION['disk_color'] = "red";
$_SESSION['disk_w'] = $disk_percent;
}elseif($disk_percent >= "50" ){
$_SESSION['disk_color'] = "orange";
$_SESSION['disk_w'] = $disk_percent;
}else{
$_SESSION['disk_color'] ="green";
if($disk_percent < "10"){
$_SESSION['disk_w'] = 2;
}else{
$_SESSION['disk_w'] =$disk_percent;
}
}
$_SESSION['usage_nemails'] = $user_usage['nemails'] ;
$_SESSION['config_nemails'] = $user_config['nemails'];
$_SESSION['usage_ftp'] = $user_usage['ftp'];
$_SESSION['config_ftp'] = $user_config['ftp'];
$_SESSION['usage_mysql'] = $user_usage['mysql'];
$_SESSION['config_mysql'] = $user_config['mysql'];
$_SESSION['system_uptime'] = $system_info['uptime'];
}
when $_SESSION['wwwdomain'] = server ip => NOT WORKING
$_SESSION['wwwdomain'] = 127.0.0.1 => WORKING
 
I doubt that the result you get when it's IP is correct.

PHP:
$traffic_percent = ($user_usage['bandwidth'] * 100) / $user_config['bandwidth'] ;

Because this line is wrong when your code does not give any result. You can not divide a number with nothing. And it is right. The entire code is incorrect. For what reason you spend so much session here and there? Will fix everything that's here.
 
The line I talked about above. When you add in the IP address of the result will be a number, therefore disappears error. But the result is not correct
 
The code is working :)
this is the result.
http://www.wibs.be/directadmin.jpg
the reason why i put it in sessions is, it does't have to get the data every time u visit the page. It is for my cms.

And what u say is working
$traffic_percent = ($user_usage['bandwidth'] * 100) / $user_config['bandwidth'] ;
$traffic_percent = (143 *100 / 50 000 ) ;
143 =traffic in MB
50 0000 = max bandwidth in MB
so 143 * 100 / 50 000 = 0.286 % usage of 50 000 :)
other example
5000 mb traffic used
5000 * 100 / 50 000 = 10 % used of 50 000 mb
 
here is the whole code ;)
<?php

if(empty($_SESSION['traffic_color'])){
//als eerst includen wij de socket zodat we die kunnen gebruiken
include("httpsocket.php");

//nou de socket is geinclude kunnen we hem starten
$sock = new HTTPSocket;

//nu gaan we inloggen in Direct Admin (verander wel de gegevens)
$sock->connect($_SESSION['wwwdomain'],2222);
$sock->set_login($_SESSION['da_logon'],$_SESSION['da_passw']);
$domain=$_SESSION['domain'] ;
//$sock->set_method('GET');

// get user usage info
$sock->query('/CMD_API_SHOW_USER_USAGE?domain='.$domain);
$user_usage = $sock->fetch_parsed_body();

// get user config info
$sock->query('/CMD_API_SHOW_USER_CONFIG?domain='.$domain);
$user_config = $sock->fetch_parsed_body();
//get system info
$sock->query('/CMD_API_SYSTEM_INFO?domain='.$domain);
$system_info = $sock->fetch_parsed_body();
//calc bandwidth usage
$traffic_percent = ($user_usage['bandwidth'] * 100) / $user_config['bandwidth'] ;
$traffic_percent = number_format($traffic_percent,2);

if($traffic_percent >= "100"){
$_SESSION['traffic_color'] = "red";
$_SESSION['traffic_w'] = 100;
}elseif($traffic_percent >= "75"){
$_SESSION['traffic_color'] = "red";
$_SESSION['traffic_w'] = $traffic_percent;
}elseif($traffic_percent >= "50" ){
$_SESSION['traffic_color'] = "orange";
$_SESSION['traffic_w'] = $traffic_percent;
}else{
$_SESSION['traffic_color'] ="green";
if($traffic_percent < "10"){
$_SESSION['traffic_w'] = 2;
}else{
$_SESSION['traffic_w'] =$traffic_percent;
}
}
// calc disk usage
$disk_percent = ($user_usage['quota'] * 100) / $user_config['quota'] ;
$disk_percent = number_format($disk_percent,2);

if($disk_percent >= "100"){
$_SESSION['disk_color'] = "red";
$_SESSION['disk_w'] = 100;
}elseif($disk_percent >= "75"){
$_SESSION['disk_color'] = "red";
$_SESSION['disk_w'] = $disk_percent;
}elseif($disk_percent >= "50" ){
$_SESSION['disk_color'] = "orange";
$_SESSION['disk_w'] = $disk_percent;
}else{
$_SESSION['disk_color'] ="green";
if($disk_percent < "10"){
$_SESSION['disk_w'] = 2;
}else{
$_SESSION['disk_w'] =$disk_percent;
}
}
$_SESSION['usage_nemails'] = $user_usage['nemails'] ;
$_SESSION['config_nemails'] = $user_config['nemails'];
$_SESSION['usage_ftp'] = $user_usage['ftp'];
$_SESSION['config_ftp'] = $user_config['ftp'];
$_SESSION['usage_mysql'] = $user_usage['mysql'];
$_SESSION['config_mysql'] = $user_config['mysql'];
$_SESSION['system_uptime'] = $system_info['uptime'];
}

?>
<table width="180px">
<tr><td style="text-align: center;"><font class="da">Dataverkeer</font></td></tr>
<tr bgcolor="#cdcdcd"><td height="15px"><img src="media/toolbar/<?=$_SESSION['traffic_color']?>.jpg" alt="<?=$_SESSION['traffic_color']?>" width="<?=$_SESSION['traffic_w']?>%" height="13px"></td></tr>
<tr><td style="text-align: center;"><font class="da">Webruimte</font></td></tr>
<tr bgcolor="#cdcdcd"><td height="15px"><img src="media/toolbar/<?=$_SESSION['disk_color']?>.jpg" alt="<?=$_SESSION['disk_color']?>" width="<?=$_SESSION['disk_w']?>%" height="13px"></td></tr>
</table>
<table width="180px">
<tr><th></th><th align="center">Gebruikt</th><th align="center">Max</th></tr>
<tr><td>E-Mails</td><td align="center"><?=$_SESSION['usage_nemails']?></td><td align="center"><?=$_SESSION['config_nemails']?></td></tr>
<tr><td>FTP-accounts</td><td align="center"><?=$_SESSION['usage_ftp']?></td><td align="center"><?=$_SESSION['config_ftp']?></td></tr>
<tr><td>Databases</td><td align="center"><?=$_SESSION['usage_mysql']?></td><td align="center"><?=$_SESSION['config_mysql']?></td></tr>
<tr><td colspan="3">Uptime: <?=$_SESSION['system_uptime']?></td></tr>
</table>
result = http://www.wibs.be/directadmin.jpg
 
Last edited:
Back
Top