Ejemplo n.º 1
0
function get_swap()
{
    $file = file("/proc/swaps");
    $tmp = explode("\t", substr($file[1], strpos($file[1], "partition")));
    $data = humanSize($tmp[1] * 1024);
    $str = $data[0] . " " . $data[2];
    $data = humanSize(($tmp[1] - $tmp[2]) * 1024, $data[1]);
    $str = $data[0] . "/" . $str;
    return $str;
    /*
     $swap["total"]=round($tmp[1]/(1024*1024),2);
     $swap["free"]=round(($tmp[1]-$tmp[2])/(1024*1024),2);
     return $swap["free"].'/'.$swap["total"];
    */
}
$f_data[] = array('swap_use', get_swap());
function humanSize($bytes, $limit_index = null)
{
    //  $type=array("", "kilo", "mega", "giga", "tera", "peta", "exa", "zetta", "yotta");
    $type = array("", "K", "M", "G", "T", "P");
    $index = 0;
    while ($bytes >= 1024 || $limit_index && $limit_index > $index) {
        $bytes /= 1024;
        $index++;
    }
    $bytes = round($bytes, 2);
    return array($bytes, $index, $type[$index] . "b");
}
function get_space()
{
    //    ini_set("display_errors", 1);
Ejemplo n.º 2
0
function start($skipGrant = false)
{
    if (is_file("/etc/artica-postfix/FROM_ISO")) {
        if (!is_file("/etc/artica-postfix/artica-iso-setup-launched")) {
            return;
        }
    }
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/squiddbstart.pid";
    $sock = new sockets();
    $WORKDIR = $sock->GET_INFO("SquidStatsDatabasePath");
    if ($WORKDIR == null) {
        $WORKDIR = "/opt/squidsql";
    }
    $SERV_NAME = "squid-db";
    $pid = $unix->get_pid_from_file($pidfile);
    $sock = new sockets();
    $nohup = $unix->find_program("nohup");
    $php5 = $unix->LOCATE_PHP5_BIN();
    $lnbin = $unix->find_program("ln");
    if (!$GLOBALS["FORCE"]) {
        if (!$GLOBALS["NOPID"]) {
            if ($unix->process_exists($pid, basename(__FILE__))) {
                $time = $unix->PROCCESS_TIME_MIN($pid);
                if ($GLOBALS["OUTPUT"]) {
                    echo "Starting......: " . date("H:i:s") . " [INIT]: Starting Task Already running PID {$pid} since {$time}mn\n";
                }
                return;
            }
        }
    }
    @file_put_contents($pidfile, getmypid());
    $GetStartedValues = GetStartedValues();
    $sock = new sockets();
    $ProxyUseArticaDB = $sock->GET_INFO("ProxyUseArticaDB");
    $DisableArticaProxyStatistics = $sock->GET_INFO("DisableArticaProxyStatistics");
    if (!is_numeric($ProxyUseArticaDB)) {
        $ProxyUseArticaDB = 0;
    }
    if (!is_numeric($DisableArticaProxyStatistics)) {
        $DisableArticaProxyStatistics = 0;
    }
    if (!is_dir($WORKDIR)) {
        @mkdir($WORKDIR, 0755, true);
    }
    if ($ProxyUseArticaDB == 0) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]:{$SERV_NAME} is disabled...\n";
        }
        stop();
        return;
    }
    $mysqld = $unix->find_program("mysqld");
    if (!is_file($mysqld)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]:{$SERV_NAME} is not installed...\n";
        }
        return;
    }
    $pid = SQUIDDB_PID();
    if ($unix->process_exists($pid)) {
        $time = $unix->PROCCESS_TIME_MIN($pid);
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$SERV_NAME} MySQL Database Engine already running pid {$pid} since {$time}mn\n";
        }
        return;
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$SERV_NAME} writing init.d\n";
    }
    initd();
    $memory = get_memory();
    $swap = get_swap();
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$SERV_NAME} Server available memory `{$memory}MB`\n";
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$SERV_NAME} Server available swap `{$swap}MB`\n";
    }
    $SquidDBTuningParameters = unserialize(base64_decode($sock->GET_INFO("SquidDBTuningParameters")));
    $ListenPort = $SquidDBTuningParameters["ListenPort"];
    if (!is_numeric($ListenPort)) {
        $ListenPort = 0;
    }
    if ($ListenPort == 0) {
        $ListenPort = rand(8900, 45890);
        $SquidDBTuningParameters["ListenPort"] = $ListenPort;
        $sock->SET_INFO("SquidDBTuningParameters", base64_encode(serialize($SquidDBTuningParameters)));
    }
    @mkdir($WORKDIR, 0755, true);
    $mysqlserv = new mysql_services();
    $mysqlserv->WORKDIR = $WORKDIR;
    $mysqlserv->MYSQL_PID_FILE = "/var/run/squid-db.pid";
    $mysqlserv->MYSQL_SOCKET = "/var/run/mysqld/squid-db.sock";
    $mysqlserv->SERV_NAME = $SERV_NAME;
    $mysqlserv->TokenParams = "SquidDBTuningParameters";
    $mysqlserv->INSTALL_DATABASE = true;
    $mysqlserv->MYSQL_BIN_DAEMON_PATH = $unix->find_program("mysqld");
    //$mysqlserv->MYSQL_ERRMSG=$GLOBALS["MYSQL_ERRMSG"];
    $mysqlserv->InnoDB = false;
    $cmdline = $mysqlserv->BuildParams();
    $CREATEDB = false;
    if (!is_file("{$WORKDIR}/my.cnf")) {
        @file_put_contents("{$WORKDIR}/my.cnf", "\n");
    }
    if (!is_file("{$WORKDIR}/bin/my_print_defaults")) {
        $my_print_defaults = $unix->find_program("my_print_defaults");
        shell_exec("{$lnbin} -s {$my_print_defaults} {$WORKDIR}/bin/my_print_defaults");
    }
    if (!is_file("{$WORKDIR}/data/mysql/user.MYD")) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$SERV_NAME} Installing defaults databases, Please Wait...\n";
        }
        install_db($WORKDIR);
        $CREATEDB = true;
    }
    $topCopyMysql["host.frm"] = true;
    $topCopyMysql["host.MYD"] = true;
    $topCopyMysql["host.MYI"] = true;
    $topCopyMysql["servers.frm"] = true;
    $topCopyMysql["servers.MYD"] = true;
    $topCopyMysql["servers.MYI"] = true;
    $MYSQL_DATA_DIR = $unix->MYSQL_DATA_DIR();
    $topCopyMysqlForce["tables_priv.frm"] = true;
    $topCopyMysqlForce["tables_priv.MYD"] = true;
    $topCopyMysqlForce["tables_priv.MYI"] = true;
    $topCopyMysqlForce["columns_priv.frm"] = true;
    $topCopyMysqlForce["columns_priv.MYD"] = true;
    $topCopyMysqlForce["columns_priv.MYI"] = true;
    $topCopyMysqlForce["procs_priv.frm"] = true;
    $topCopyMysqlForce["procs_priv.MYD"] = true;
    $topCopyMysqlForce["procs_priv.MYI"] = true;
    $topCopyMysqlForce["plugin.frm"] = true;
    $topCopyMysqlForce["plugin.MYD"] = true;
    $topCopyMysqlForce["plugin.MYI"] = true;
    $topCopyMysqlForce["user.frm"] = true;
    $topCopyMysqlForce["user.MYD"] = true;
    $topCopyMysqlForce["user.MYI"] = true;
    $topCopyMysqlForce["db.frm"] = true;
    $topCopyMysqlForce["db.MYD"] = true;
    $topCopyMysqlForce["db.MYI"] = true;
    $ToCopyForce = false;
    while (list($filename, $ligne) = each($topCopyMysql)) {
        if (!is_file("{$WORKDIR}/data/mysql/{$filename}")) {
            $ToCopyForce = true;
            if (is_file("{$MYSQL_DATA_DIR}/mysql/{$filename}")) {
                if ($GLOBALS["OUTPUT"]) {
                    echo "Starting......: " . date("H:i:s") . " [INIT]: Installing {$filename}\n";
                }
                @copy("{$MYSQL_DATA_DIR}/mysql/{$filename}", "{$WORKDIR}/data/mysql/{$filename}");
                $CREATEDB = true;
            }
        }
    }
    while (list($filename, $ligne) = each($topCopyMysqlForce)) {
        if (!is_file("{$WORKDIR}/data/mysql/{$filename}")) {
            if (is_file("{$MYSQL_DATA_DIR}/mysql/{$filename}")) {
                if ($GLOBALS["OUTPUT"]) {
                    echo "Starting......: " . date("H:i:s") . " [INIT]: Installing {$filename}\n";
                }
                @copy("{$MYSQL_DATA_DIR}/mysql/{$filename}", "{$WORKDIR}/data/mysql/{$filename}");
            }
        }
    }
    @mkdir("{$WORKDIR}/share/mysql/english", 0755, true);
    $Get_errmsgsys = Get_errmsgsys();
    if (!is_file("{$WORKDIR}/share/mysql/english/errmsg.sys")) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: Creating errmsg.sys\n";
        }
        ini_set('error_reporting', E_ALL);
        ini_set('error_prepend_string', null);
        ini_set('error_append_string', null);
        if (is_file($Get_errmsgsys)) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: copy {$Get_errmsgsys} -> {$WORKDIR}/share/mysql/english/errmsg.sys\n";
            }
            copy(Get_errmsgsys(), "{$WORKDIR}/share/mysql/english/errmsg.sys");
        } else {
            file_put_contents("{$WORKDIR}/share/mysql/english/errmsg.sys", "\n");
        }
    } else {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: errmsg.sys OK\n";
        }
    }
    $TMP = $unix->FILE_TEMP();
    $nohup = $unix->find_program("nohup");
    if ($GLOBALS["VERBOSE"]) {
        echo $cmdline . "\n";
    }
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: Starting MySQL daemon ({$SERV_NAME})\n";
    }
    @unlink("{$WORKDIR}/error.log");
    shell_exec("{$nohup} {$cmdline} >{$TMP} 2>&1 &");
    sleep(1);
    for ($i = 0; $i < 5; $i++) {
        $pid = SQUIDDB_PID();
        if ($unix->process_exists($pid)) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: MySQL daemon ({$SERV_NAME}) started pid .{$pid}..\n";
            }
            break;
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: MySQL daemon wait {$i}/5\n";
        }
        sleep(1);
    }
    sleep(1);
    $pid = SQUIDDB_PID();
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: MySQL daemon ({$SERV_NAME}) failed to start\n";
        }
        if (is_file($TMP)) {
            $f = explode("\n", @file_get_contents($TMP));
            while (list($num, $ligne) = each($f)) {
                if (trim($ligne) == null) {
                    continue;
                }
                if ($GLOBALS["OUTPUT"]) {
                    echo "Starting......: " . date("H:i:s") . " [INIT]: {$ligne}\n";
                }
            }
        }
        $f = explode("\n", @file_get_contents("{$WORKDIR}/error.log"));
        while (list($num, $ligne) = each($f)) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: MySQL Results \"{$ligne}\"\n";
            }
            if (preg_match("#Incorrect information in file: './mysql/proxies_priv.frm'#", $ligne)) {
                if ($GLOBALS["OUTPUT"]) {
                    echo "Starting......: " . date("H:i:s") . " [INIT]: remove MySQL tables and install again...\n";
                }
                shell_exec("/bin/rm -rf {$WORKDIR}/data/mysql/*");
                shell_exec("{$nohup} {$php5} " . __FILE__ . " --start --recall >/dev/ null 2>&1 &");
                return;
            }
        }
    } else {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: MySQL daemon ({$SERV_NAME}) success\n";
        }
        if ($CREATEDB) {
            $q = new mysql_squid_builder();
            $q->CheckTables();
        }
        $q = new mysql_squid_builder();
        $q->MEMORY_TABLES_RESTORE();
    }
    if (!$unix->process_exists($pid)) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$cmdline}\n";
        }
    }
    $unix->THREAD_COMMAND_SET($unix->LOCATE_PHP5_BIN() . " " . __FILE__ . " --databasesize");
}