Example #1
0
function vacuumdb()
{
    $unix = new unix();
    $TimeFile = "/usr/local/ArticaStats/bin/vacuumdb.forced.time";
    $pidfile = "/usr/local/ArticaStats/bin/vacuumdb.forced.pid";
    $pid = $unix->get_pid_from_file($pidfile);
    if ($unix->process_exists($pid, basename(__FILE__))) {
        return;
    }
    @file_put_contents($pidfile, getmypid());
    $TimeExec = $unix->file_time_min($TimeFile);
    if ($TimeExec < 10080) {
        return;
    }
    @unlink($TimeFile);
    @file_put_contents($TimeFile, time());
    $postgres = new postgres_sql(true);
    if ($postgres->isRemote) {
        return;
    }
    if (!class_exists("usersMenus")) {
        include_once dirname(__FILE__) . "/ressources/class.users.menus.inc";
    }
    $users = new usersMenus();
    $InfluxAdminRetentionTime = intval(@file_get_contents("/etc/artica-postfix/settings/Daemons/InfluxAdminRetentionTime"));
    if ($InfluxAdminRetentionTime == 0) {
        $InfluxAdminRetentionTime = 365;
    }
    if (!$users->CORP_LICENSE) {
        $InfluxAdminRetentionTime = 5;
    }
    $postgres->QUERY_SQL("DELETE FROM access_log WHERE time < NOW() - INTERVAL '{$InfluxAdminRetentionTime} days'");
    $postgres->QUERY_SQL("DELETE FROM main_size WHERE zdate < NOW() - INTERVAL '{$InfluxAdminRetentionTime} days'");
    $postgres->QUERY_SQL("DELETE FROM system WHERE zdate < NOW() - INTERVAL '30 days'");
    $t1 = time();
    $NICE = $unix->EXEC_NICE();
    exec("{$NICE} /usr/local/ArticaStats/bin/vacuumdb -f -v -h /var/run/ArticaStats --dbname=proxydb --username=ArticaStats 2>&1", $results);
    $Took = $unix->distanceOfTimeInWords($t1, time());
    squid_admin_mysql(2, "Indexing Statistics Database took: {$Took}", @implode("\n", $results), __FILE__, __LINE__);
    InfluxDbSize();
}
Example #2
0
function remove_db()
{
    $unix = new unix();
    $rm = $unix->find_program("rm");
    build_progress_rdb(15, "Remove databases files...");
    shell_exec("{$rm} -rf /home/artica/squid/InfluxDB");
    @mkdir("/home/artica/squid/InfluxDB", 0755, true);
    build_progress_rdb(20, "Stopping service");
    stop(true);
    build_progress_rdb(50, "Starting service");
    start(true);
    shell_exec("{$rm} -rf /etc/artica-postfix/DIRSIZE_MB_CACHE/*");
    InfluxDbSize();
    system("/etc/init.d/squid-tail restart");
    build_progress_rdb(100, "{done}");
}
function ACCESS_BACKUP_SCAN($filename)
{
    $unix = new unix();
    $BaseName = basename($filename);
    $tempfile = $unix->FILE_TEMP();
    if (preg_match("#\\.gz\$#", $BaseName)) {
        $zcat = $unix->find_program("zcat");
        shell_exec("{$zcat} {$filename} >{$tempfile}");
    } else {
        @copy($filename, $tempfile);
    }
    $handle = @fopen($tempfile, "r");
    if (!$handle) {
        echo "Failed to open file\n";
        return;
    }
    $prefix = "INSERT INTO access_log (zdate,category,ipaddr,mac,sitename,familysite,proxyname,userid,size,rqs) VALUES ";
    $MASTER_C = 0;
    $MAIN = array();
    $MASTER_G = 0;
    while (!feof($handle)) {
        $pattern = trim(fgets($handle));
        if ($pattern == null) {
            continue;
        }
        $ARRAY = LineToArray($pattern);
        if (count($ARRAY) < 5) {
            echo "{$pattern} no match\n";
            continue;
        }
        if (!isset($ARRAY["MAC"])) {
            $ARRAY["MAC"] = "00:00:00:00:00:00";
        }
        if ($ARRAY["MAC"] === null) {
            $ARRAY["MAC"] = "00:00:00:00:00:00";
        }
        $IP = new IP();
        if (!$IP->isValid($ARRAY["IPADDR"])) {
            continue;
        }
        if (!$IP->IsvalidMAC($ARRAY["MAC"])) {
            $ARRAY["MAC"] = "00:00:00:00:00:00";
        }
        if (!isset($ARRAY["USERID"])) {
            $ARRAY["USERID"] = null;
        }
        if (!isset($ARRAY["CATEGORY"])) {
            $ARRAY["CATEGORY"] = null;
        }
        if (strlen($ARRAY["SITE"]) > 128) {
            $ARRAY["SITE"] = substr(0, 127, $ARRAY["SITE"]);
        }
        if (strlen($ARRAY["FAMILYSITE"]) > 128) {
            $ARRAY["FAMILYSITE"] = substr(0, 127, $ARRAY["FAMILYSITE"]);
        }
        if (strlen($ARRAY["PROXYNAME"]) > 128) {
            $ARRAY["PROXYNAME"] = substr(0, 127, $ARRAY["PROXYNAME"]);
        }
        $date = $ARRAY["ZDATE"];
        $MAIN[] = "('{$date}','{$ARRAY["CATEGORY"]}','{$ARRAY["IPADDR"]}','{$ARRAY["MAC"]}','{$ARRAY["SITE"]}','{$ARRAY["FAMILYSITE"]}','{$ARRAY["PROXYNAME"]}','{$ARRAY["USERID"]}','{$ARRAY["SIZE"]}','{$ARRAY["RQS"]}')";
        $MASTER_C++;
        $CountOfMain = count($MAIN);
        if ($CountOfMain > 1000) {
            $MASTER_G++;
            squid_admin_mysql(1, "{$MASTER_G}]: Injecting {$CountOfMain} events from {$BaseName}", null, __FILE__, __LINE__);
            $q = new postgres_sql();
            $q->QUERY_SQL($prefix . @implode(",", $MAIN));
            if (!$q->ok) {
                $MAIN = array();
                squid_admin_mysql(0, "Failed Injecting {$CountOfMain} events from {$BaseName}", $q->mysql_error, __FILE__, __LINE__);
                echo $q->mysql_error;
                return false;
            }
            $MAIN = array();
        }
    }
    $CountOfMain = count($MAIN);
    if (count($MAIN) > 0) {
        $q = new postgres_sql();
        squid_admin_mysql(1, "(FINAL) Injecting {$CountOfMain} events from {$BaseName}", null, __FILE__, __LINE__);
        $q->QUERY_SQL($prefix . @implode(",", $MAIN));
        if (!$q->ok) {
            squid_admin_mysql(0, "(FINAL) SQL error: {$q->mysql_error}", null, __FILE__, __LINE__);
            echo $q->mysql_error;
            return false;
        }
    }
    echo "{$filename}, Injected {$MASTER_C} items\n";
    echo "Vaccum....\n";
    squid_admin_mysql(1, basename($filename) . ": running  vacuumdb...", null, __FILE__, __LINE__);
    shell_exec("/usr/local/ArticaStats/bin/vacuumdb -h /var/run/ArticaStats --dbname=proxydb --username=ArticaStats");
    squid_admin_mysql(1, basename($filename) . ": Injected {$MASTER_C} items ", null, __FILE__, __LINE__);
    InfluxDbSize();
    return true;
}
function restore($targetFilename)
{
    build_progress_idb("{restore_database}", 20);
    $unix = new unix();
    if (is_file("/tmp/postgressql-restore.running")) {
        $pid = $unix->PIDOF("/usr/local/ArticaStats/bin/psql");
        if ($unix->process_exists($pid)) {
            $timepid = $unix->PROCCESS_TIME_MIN($pid);
            echo "Already running {$pid} since {$timepid}mn\n";
            return;
        }
        @unlink("/tmp/postgressql-restore.running");
    }
    if (is_file("/tmp/postgressql-restore.running")) {
        return;
    }
    if (!is_file($targetFilename)) {
        echo "targetFilename: {$targetFilename} no such file\n";
        build_progress_idb("{restore_database} {failed}", 110);
        return;
    }
    $basename = basename($targetFilename);
    if (!preg_match("#\\.gz\$#", $basename)) {
        echo "targetFilename: {$basename} do in gzip format\n";
        build_progress_idb("{restore_database} {failed}", 110);
        return;
    }
    $UnCompressFileName = $targetFilename . ".sql";
    $su = $unix->find_program("su");
    $gunzip = $unix->find_program("gunzip");
    build_progress_idb("{restore_database} {uncompress}", 50);
    if (!$unix->uncompress($targetFilename, $UnCompressFileName)) {
        echo "Unable to uncompress {$targetFilename}\n";
        build_progress_idb("{restore_database} {failed}", 110);
        return;
    }
    $psql = "/usr/local/ArticaStats/bin/psql -f {$UnCompressFileName}  -h /var/run/ArticaStats -U ArticaStats -w proxydb";
    $f[] = "#!/bin/sh";
    $f[] = ". /lib/init/vars.sh";
    $f[] = ". /lib/lsb/init-functions";
    $f[] = "LANG=en_US.UTF-8";
    $f[] = "HOME=/home/ArticaStats";
    $f[] = "PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games";
    $f[] = "rm /tmp/postgressql-restore.sh";
    $f[] = "if [ -f \"/tmp/postgressql-restore.running\" ]; then";
    $f[] = "exit";
    $f[] = "fi";
    $f[] = "touch /tmp/postgressql-restore.running\n";
    $cmdline = "{$psql}";
    $f[] = "{$cmdline}";
    $f[] = "echo \"OK FINISH\"";
    $f[] = "rm /tmp/postgressql-restore.running";
    $f[] = "touch /tmp/postgressql-restore.OK\n";
    if (is_file("/tmp/postgressql-restore.OK")) {
        @unlink("/tmp/postgressql-restore.OK");
    }
    @file_put_contents("/tmp/postgressql-restore.sh", @implode("\n", $f));
    @chmod("/tmp/postgressql-restore.sh", 0755);
    echo $cmdline;
    build_progress_idb("{restore_database} {run}", 50);
    $text = "{please_wait}";
    $i = 0;
    $prc = 60;
    while (true) {
        $i++;
        if (is_file("/tmp/postgressql-restore.OK")) {
            break;
        }
        build_progress_idb("{restore_database} {$text} ({$i})", $prc);
        sleep(3);
        if (is_file("/tmp/postgressql-restore.running")) {
            $pid = $unix->PIDOF("/usr/local/ArticaStats/bin/psql");
            $timepid = $unix->PROCCESS_TIME_MIN($pid);
            $size = $unix->DIRSIZE_BYTES_NOCACHE("/home/ArticaStatsDB");
            $array_load = sys_getloadavg();
            $internal_load = $array_load[0];
            $text = "{running} load:{$internal_load} {$pid} {since} {$timepid}mn " . FormatBytes($size / 1024);
            $prc = 70;
        }
    }
    build_progress_idb("{restore_database} {done}", 90);
    sleep(5);
    InfluxDbSize();
    build_progress_idb("{restore_database} {success}", 100);
}
function install($filekey = 0)
{
    include_once dirname(__FILE__) . "/ressources/class.ccurl.inc";
    $unix = new unix();
    $filename = null;
    $MD5 = null;
    $DebianVersion = DebianVersion();
    if ($DebianVersion < 7) {
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]}, influxdb Debian version incompatible!\n";
        }
        build_progress_idb("Incompatible system!", 110);
        die;
    }
    $filename = "influxdb-0.9.0.0.tar.gz";
    $MD5 = "4a5cef145fd676cb6752fec4170a32e4";
    $curl = new ccurl("http://mirror.articatech.com/download/InfluxDatabase/{$filename}");
    $tmpdir = $unix->TEMP_DIR();
    $php = $unix->LOCATE_PHP5_BIN();
    $rm = $unix->find_program("rm");
    $sock = new sockets();
    build_progress_idb("{downloading}", 1);
    $curl->WriteProgress = true;
    $curl->ProgressFunction = "download_progress";
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: Downloading {$filename}\n";
    }
    if (!$curl->GetFile("{$tmpdir}/{$filename}")) {
        build_progress_idb("{$curl->error}", 110);
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$curl->error}\n";
        }
        while (list($key, $value) = each($curl->errors)) {
            if ($GLOBALS["OUTPUT"]) {
                echo "Starting......: " . date("H:i:s") . " [INIT]: {$value}\n";
            }
        }
        if ($GLOBALS["OUTPUT"]) {
            echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]}, influxdb unable to install....\n";
        }
        @unlink("{$tmpdir}/{$filename}");
        return;
    }
    if ($MD5 != null) {
        $DESTMD5 = md5_file("{$tmpdir}/{$filename}");
        if ($DESTMD5 != $MD5) {
            echo "{$DESTMD5}<>{$MD5}\n";
            @unlink("{$tmpdir}/{$filename}");
            build_progress_idb("{install_failed} {corrupted_package}", 110);
            return;
        }
    }
    build_progress_idb("{cleaning_backup}", 50);
    $InFluxBackupDatabaseDir = $sock->GET_INFO("InFluxBackupDatabaseDir");
    if ($InFluxBackupDatabaseDir == null) {
        $InFluxBackupDatabaseDir = "/home/artica/influx/backup";
    }
    if (is_dir($InFluxBackupDatabaseDir)) {
        shell_exec("{$rm} -rf {$InFluxBackupDatabaseDir}");
    }
    build_progress_idb("{stopping_service}", 60);
    system("/etc/init.d/influx-db stop");
    build_progress_idb("{removing_databases}", 70);
    shell_exec("{$rm} -rf /home/artica/squid/InfluxDB");
    @mkdir("/home/artica/squid/InfluxDB", 0755, true);
    shell_exec("{$rm} -rf /etc/artica-postfix/DIRSIZE_MB_CACHE/*");
    build_progress_idb("{extracting}", 80);
    if ($GLOBALS["OUTPUT"]) {
        echo "Starting......: " . date("H:i:s") . " [INIT]: {$GLOBALS["TITLENAME"]}, extracting....\n";
    }
    $tar = $unix->find_program("tar");
    shell_exec("{$tar} xvf {$tmpdir}/{$filename} -C /");
    build_progress_idb("{restarting_service} (1/2)", 90);
    system("/etc/init.d/influx-db restart");
    build_progress_idb("{restarting_service} (2/2)", 92);
    system("/etc/init.d/influx-db restart");
    @unlink("/etc/artica-postfix/settings/Daemons/ArticaTechNetInfluxRepo");
    build_progress_idb("{refresh_status}", 95);
    shell_exec("{$php} /usr/share/artica-postfix/exec.squid.interface-size.php --force");
    build_progress_idb("{refresh_status}", 96);
    shell_exec("{$php} php5 /usr/share/artica-postfix/exec.web-community-filter.php --squid-repo --verbose >/dev/null 2>&1");
    build_progress_idb("{refresh_status}", 97);
    system("/etc/init.d/squid-tail restart");
    build_progress_idb("{refresh_status}", 98);
    system("/etc/init.d/hypercache-tail restart");
    build_progress_idb("{refresh_status}", 99);
    system("/etc/init.d/ufdb-tail restart");
    InfluxDbSize();
    build_progress_idb("{done}", 100);
}