Current File : //etc/zpanel/panel/diskusage.php |
<?php
include_once('/etc/sentora/panel/cnf/db.php');
include_once('/etc/sentora/panel/dryden/db/driver.class.php');
include_once('/etc/sentora/panel/dryden/ctrl/options.class.php');
include_once('/etc/sentora/panel/dryden/runtime/controller.class.php');
include_once('/etc/sentora/panel/dryden/runtime/hash.class.php');
include_once('/etc/sentora/panel/dryden/ctrl/auth.class.php');
include_once('/etc/sentora/panel/dryden/fs/director.class.php');
include_once('/etc/sentora/panel/dryden/fs/filehandler.class.php');
include_once('/etc/sentora/panel/dryden/runtime/dataobject.class.php');
include_once('/etc/sentora/panel/dryden/runtime/hook.class.php');
include_once('/etc/sentora/panel/dryden/debug/logger.class.php');
include_once('/etc/sentora/panel/dryden/sys/versions.class.php');
include_once('/etc/sentora/panel/dryden/ctrl/users.class.php');
include_once('/etc/sentora/panel/dryden/sys/bandwidth.class.php');
try
{
$zdbh = new db_driver("mysql:host=" . $host . ";dbname=" . $dbname . "", $user, $pass);
} catch (PDOException $e)
{
exit();
}
//global $zdbh;
///////////////////////////////////// Mysql DB check Start ////////////////////////////////////////
$z_db_user = $user;
$z_db_pass = $pass;
$mysqlsql = $zdbh->query("SELECT my_id_pk, my_name_vc FROM x_mysql_databases WHERE my_deleted_ts IS NULL");
while ($database = $mysqlsql->fetch())
{
// SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = 'DBName'
try
{
$currentdb = new db_driver("mysql:host=$host;dbname=" . $database['my_name_vc'] . "", $z_db_user, $z_db_pass);
$currentdb->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbsize = $currentdb->query("SHOW TABLE STATUS");
$dbgetsize = 0;
while ($row = $dbsize->fetch())
{
$dbgetsize = $dbgetsize + ($row['Data_length'] + $row['Index_length']);
}
//$zdbh->query("UPDATE x_mysql_databases SET my_usedspace_bi = '" . $dbgetsize . "' WHERE my_id_pk =" . $database['my_id_pk'] . "");
$numrows = $zdbh->prepare("UPDATE x_mysql_databases SET my_usedspace_bi = :dbgetsize WHERE my_id_pk =:my_id_pk");
$numrows->bindParam(':dbgetsize', $dbgetsize);
$numrows->bindParam(':my_id_pk', $database['my_id_pk']);
$numrows->execute();
}
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
//echo "Database found: " . $database['my_name_vc'] . " - " . $dbgetsize . " \n";
}
/////////////////////////////////////////////// Mysql DB check End ////////////////////////////////////////////////
/* ////////////////////////////////////// Mailbox size calculate Start ////////////////////////////////////////// */
$sql = "SELECT * FROM x_mailboxes WHERE mb_deleted_ts IS NULL ORDER BY mb_address_vc ASC";
$sql = $zdbh->prepare($sql);
//$sql->bindParam(':userid', $currentuser['userid']);
$sql->execute();
while ($rowmailboxes = $sql->fetch()) {
/*kesavan code for memory*/
$mail_input = $rowmailboxes['mb_address_vc'];
$maill_array = explode( '@', $mail_input ) ;
$path = "/var/sentora/vmail/";
$mail_path = $maill_array[0];
$domain_path = $maill_array[1];
$total = $path."".$domain_path."/".$maill_array[0];
$size = shell_exec("du -scL $total | cut -f1 | awk '{print $1; exit}'");
$updatesql = $zdbh->prepare("UPDATE x_mailboxes SET mb_size = :size WHERE mb_address_vc =:mail");
$updatesql->bindParam(':size', $size);
$updatesql->bindParam(':mail', $mail_input);
$updatesql->execute();
/*End*/
}
/* ////////////////////////////// Mailbox size calculate End //////////////////////////// */
/*
* Calculate the home directory size for each 'active' user account on the server.
*/
$userssql = $zdbh->query("SELECT ac_id_pk, ac_user_vc FROM x_accounts WHERE ac_deleted_ts IS NULL");
echo fs_filehandler::NewLine() . "START Calculating disk Usage for all client accounts.." . fs_filehandler::NewLine();
while ($userdir = $userssql->fetch()) {
$homedirectory = ctrl_options::GetSystemOption('hosted_dir') . $userdir['ac_user_vc'];
$size = 0;
if (fs_director::CheckFolderExists($homedirectory)) {
// $size = fs_director::GetDirectorySize($homedirectory);
$size = shell_exec("du -sbL $homedirectory");
}
$currentuser = ctrl_users::GetUserDetail($userdir['ac_id_pk']);
$numrows = $zdbh->prepare("SELECT COUNT(*) AS total FROM x_bandwidth WHERE bd_month_in = :date AND bd_acc_fk = :ac_id_pk");
$date = date("Ym");
$numrows->bindParam(':date', $date);
$numrows->bindParam(':ac_id_pk', $userdir['ac_id_pk']);
$numrows->execute();
$checksql = $numrows->fetch();
if ($checksql['total'] == 0) {
$numrows3 = $zdbh->prepare("INSERT INTO x_bandwidth (bd_acc_fk, bd_month_in, bd_transamount_bi, bd_diskamount_bi, bd_diskover_in, bd_diskcheck_in, bd_transover_in, bd_transcheck_in ) VALUES (:ac_id_pk,:date,0,0,0,0,0,0);");
$date = date("Ym");
$numrows3->bindParam(':date', $date);
$numrows3->bindParam(':ac_id_pk', $userdir['ac_id_pk']);
$numrows3->execute();
}
$updatesql = $zdbh->prepare("UPDATE x_bandwidth SET bd_diskamount_bi = :size WHERE bd_acc_fk =:ac_id_pk");
$updatesql->bindParam(':size', $size);
$updatesql->bindParam(':ac_id_pk', $userdir['ac_id_pk']);
$updatesql->execute();
$numrows = $zdbh->prepare("SELECT * FROM x_bandwidth WHERE bd_month_in = :date AND bd_acc_fk = :ac_id_pk");
$date = date("Ym");
$numrows->bindParam(':date', $date);
$numrows->bindParam(':ac_id_pk', $userdir['ac_id_pk']);
$numrows->execute();
$checksize = $numrows->fetch();
if ($checksize['bd_diskamount_bi'] > $currentuser['diskquota']) {
$updatesql = $zdbh->prepare("UPDATE x_bandwidth SET bd_diskover_in = 1 WHERE bd_acc_fk =:ac_id_pk");
$updatesql->bindParam(':ac_id_pk', $userdir['ac_id_pk']);
$updatesql->execute();
} else {
$updatesql = $zdbh->prepare("UPDATE x_bandwidth SET bd_diskover_in = 0 WHERE bd_acc_fk =:ac_id_pk");
$updatesql->bindParam(':ac_id_pk', $userdir['ac_id_pk']);
$updatesql->execute();
}
echo "Disk usage for user \"" . $userdir['ac_user_vc'] . "\" is: " . $size . " (" . fs_director::ShowHumanFileSize($size) . ")" . fs_filehandler::NewLine();
}
echo "END Calculating disk usage" . fs_filehandler::NewLine();
/*
* Calculate the bandwidth used for each user.
*/
$checksql = $zdbh->query("SELECT COUNT(*) AS total FROM x_vhosts WHERE vh_deleted_ts IS NULL")->fetch();
echo fs_filehandler::NewLine() . "START Calculating bandwidth usage for all client accounts.." . fs_filehandler::NewLine();
if ($checksql['total'] > 0) {
$domainssql = $zdbh->query("SELECT vh_acc_fk, vh_name_vc FROM x_vhosts WHERE vh_deleted_ts IS NULL");
while ($domain = $domainssql->fetch()) {
$domainowner=ctrl_users::GetUserDetail($domain['vh_acc_fk']);
$bandwidthlog=ctrl_options::GetSystemOption('log_dir').'domains/'.$domainowner['username'].'/'.$domain['vh_name_vc'].'-bandwidth.log';
$snapshotfile=ctrl_options::GetSystemOption('log_dir').'domains/'.$domainowner['username'].'/'.$domain['vh_name_vc'].'-snapshot.bw';
$bandwidth = 0;
echo "Processing domain \"" . $domain['vh_name_vc'] . "\"" . fs_filehandler::NewLine();
if (fs_director::CheckFileExists($bandwidthlog)) {
fs_filehandler::CopyFile($bandwidthlog, $snapshotfile);
if (fs_director::CheckFileExists($snapshotfile)) {
fs_filehandler::ResetFile($bandwidthlog);
echo "Generating bandwidth.. " . fs_filehandler::NewLine();
$bandwidth = sys_bandwidth::CalculateFromApacheLog($snapshotfile);
unlink($snapshotfile);
echo "usage: ".$bandwidth." (".fs_director::ShowHumanFileSize($bandwidth).")".fs_filehandler::NewLine();
}
}
if (!fs_director::CheckForEmptyValue($bandwidth)) {
$numrows = $zdbh->prepare("UPDATE x_bandwidth SET bd_transamount_bi=(bd_transamount_bi+:bandwidth) WHERE bd_acc_fk = :vh_acc_fk AND bd_month_in = :date");
$numrows->bindParam(':bandwidth', $bandwidth);
$date = date("Ym");
$numrows->bindParam(':date', $date);
$numrows->bindParam(':vh_acc_fk', $domain['vh_acc_fk']);
$numrows->execute();
} else {
echo "No bandwidth used, skipping!" . fs_filehandler::NewLine();
}
$numrows = $zdbh->prepare("SELECT * FROM x_bandwidth WHERE bd_month_in = :date AND bd_acc_fk = :vh_acc_fk");
$date = date("Ym");
$numrows->bindParam(':date', $date);
$numrows->bindParam(':vh_acc_fk', $domain['vh_acc_fk']);
$numrows->execute();
$checksize = $numrows->fetch();
if ($checksize['bd_transamount_bi'] > $domainowner['bandwidthquota']) {
$updatesql = $zdbh->prepare("UPDATE x_bandwidth SET bd_transover_in = 1 WHERE bd_acc_fk = :vh_acc_fk");
$updatesql->bindParam(':vh_acc_fk', $domain['vh_acc_fk']);
$updatesql->execute();
} else {
$updatesql = $zdbh->prepare("UPDATE x_bandwidth SET bd_transover_in = 0 WHERE bd_acc_fk =:vh_acc_fk");
$updatesql->bindParam(':vh_acc_fk', $domain['vh_acc_fk']);
$updatesql->execute();
}
}
}
echo "END Calculating bandwidth usage" . fs_filehandler::NewLine();
/////////////////////////////////// Disk Usage Start ///////////////////////////////////////
echo "Disk Usage Start ".fs_filehandler::NewLine();
$user_sql= $zdbh->prepare(" select * from x_accounts where ac_deleted_ts IS NULL");
$user_sql->execute();
while( $account_details = $user_sql->fetch() )
{
$acc_key=$account_details['ac_id_pk'];
echo "Account Id : $acc_key Start".fs_filehandler::NewLine();
$retval = 0;
$sql1 = $zdbh->prepare("select sum(my_usedspace_bi) as mysqlusage from x_mysql_databases where my_acc_fk= :acc_key1 AND my_deleted_ts IS NULL");
$sql1->bindParam(':acc_key1',$acc_key);
$sql1->execute();
$row_count1 = $sql1->rowCount();
if($row_count1 >0)
{
$row1 = $sql1->fetch();
$retval1 = $row1['mysqlusage'];
if(is_null($retval1) || $retval1 =="NULL" ) {}
else { $retval = $retval + $retval1; }
}
$sql2 = $zdbh->prepare("select sum(mb_size) as mailsize from x_mailboxes where mb_acc_fk= :acc_key2 AND mb_deleted_ts IS NULL");
$sql2->bindParam(':acc_key2',$acc_key);
$sql2->execute();
$row_count2 = $sql2->rowCount();
if($row_count2 >0)
{
$row2 = $sql2->fetch();
$retval2 = $row2['mailsize'];
if(is_null($retval2) || $retval2 =="NULL" ) {}
else { $retval = $retval + $retval2; }
}
$sql3 =$zdbh->prepare("select vh_name_vc,(select ac_user_vc from x_accounts where x_accounts.ac_id_pk = x_vhosts.vh_acc_fk and x_accounts.ac_deleted_ts IS NULL ) as username from x_vhosts where vh_acc_fk = :acc_key2 and vh_deleted_ts IS NULL ");
$sql3->bindParam(':acc_key2',$acc_key);
$sql3->execute();
$row_count3 = $sql3->rowCount();
if($row_count3 >0)
{
$row3 = $sql3->fetch();
$retval3 = $row3['username'];
$sentora_path =ctrl_options::GetSystemOption('hosted_dir').$retval3;
if(is_dir($sentora_path))
{
$shell_script1 = "du -sL $sentora_path";
$shell_data1 = shell_exec($shell_script1);
$shell_data2 = trim(preg_replace('/\s+/', '', $shell_data1));
$shell_data3 = trim(preg_replace('/\s+/', ' ', $shell_data1));
if(strlen($shell_data2) > 0 )
{
$exp_d = explode(" ",$shell_data3);
$f_size = trim(preg_replace('/\s+/', '', $exp_d[0]));
if(is_numeric($f_size))
{
$f_size_b = $f_size * 1024;
$retval = $retval + $f_size_b;
}
}
}
}
$sql3 =$zdbh->prepare("select vh_name_vc,(select ac_user_vc from x_accounts where x_accounts.ac_id_pk = x_vhosts.vh_acc_fk and x_accounts.ac_deleted_ts IS NULL ) as username from x_vhosts where vh_acc_fk = :acc_key2 and vh_deleted_ts IS NULL ");
$sql3->bindParam(':acc_key2',$acc_key);
$sql3->execute();
$row_count3 = $sql3->rowCount();
if($row_count3 >0)
{
$row3 = $sql3->fetch();
$retval3 = $row3['username'];
$sentora_path ="/var/sentora/logs/domains/".$retval3;
if(is_dir($sentora_path))
{
$shell_script1 = "du -sL $sentora_path";
$shell_data1 = shell_exec($shell_script1);
$shell_data2 = trim(preg_replace('/\s+/', '', $shell_data1));
$shell_data3 = trim(preg_replace('/\s+/', ' ', $shell_data1));
if(strlen($shell_data2) > 0 )
{
$exp_d = explode(" ",$shell_data3);
$f_size = trim(preg_replace('/\s+/', '', $exp_d[0]));
if(is_numeric($f_size))
{
$f_size_b = $f_size * 1024;
$retval = $retval + $f_size_b;
}
}
}
}
/* //////////////////// Update content ////////////////////// */
$sqlupdate =$zdbh->prepare("select * from x_diskusage where ac_id_pk=:acc_key2");
$sqlupdate->bindParam(':acc_key2',$acc_key);
$sqlupdate->execute();
$row_count_update = $sqlupdate->rowCount();
$sqlupdate="";
if($row_count_update >0)
{
//updatesql
$sqlupdate =$zdbh->prepare("Update x_diskusage set ac_used_space=:acc_used where ac_id_pk=:acc_key2");
}
else
{
//insertsql
$sqlupdate =$zdbh->prepare("INSERT INTO x_diskusage(ac_id_pk,ac_used_space) VALUES(:acc_key2,:acc_used)");
}
$sqlupdate->bindParam(':acc_key2',$acc_key);
$sqlupdate->bindParam(':acc_used',$retval);
$sqlupdate->execute();
/* //////////////////// Update content ////////////////////// */
}
echo "Disk Usage End".fs_filehandler::NewLine();
///////////////////////////////////// Disk Usage End ///////////////////////////////
/*
* Code is added by Sangeeth on 04.07.18
* This code is used to check and update the delete time of the databases in sentora which is not having in schema
* Code start here
*/
$query = "UPDATE x_mysql_databases as a LEFT JOIN information_schema.SCHEMATA as b ON a.my_name_vc = b.schema_name SET a.my_deleted_ts=:time, a.ip_deleted=:ip WHERE b.schema_name IS NULL AND a.my_deleted_ts is NULL;";
$sql = $zdbh->prepare($query);
$time = time();
$ip = "111111";
$sql->bindParam(':time', $time);
$sql->bindParam(':ip', $ip);
$sql->execute();
/*
* Code is added by Sangeeth on 04.07.18
* This code is used to check and update the delete time of the databases in sentora which is not having in schema
* Code end here
*/
?>