Ejemplo n.º 1
0
 function Update(INTERVAL &$ivl = NULL, $dim = DIM_AUTO, $flags = 0)
 {
     if (!$ivl) {
         if ($this->req instanceof DATARequest) {
             $iinfo = $this->req->GetIntervalInfo();
         } else {
             $iinfo = NULL;
         }
         $ivl = $this->reader->CreateInterval($this->group, $iinfo);
     }
     $res = $this->resolution->Get($ivl, 1);
     $resolution = $this->resolution->GetWindowSize($res);
     /*
         $res = $this->resolution->Minimal($ivl);
         $resolution = $this->resolution->GetWindowSize($res);
     */
     if ($this->lock) {
         $lock = NULL;
     } else {
         $lock = new LOCK("cache" . $this->default_postfix);
         $lock->Lock(LOCK::BLOCK | LOCK::EXCLUSIVE);
     }
     if (!$this->omit_raw_cache) {
         if (!$this->current_raw_end) {
             $info = $this->GetCacheInfo();
             /* 
                Tricky. We can do '+1' that, since 
                 a) The interval end is determined by data available in the source database,
                 and therefore we have already in the source all the data before the interval end
                 b) we always perform complete seconds and incomplete second are not processed
                 
                 This means what if we have in RAW cache any value for given second, then we already
                 have all data for that second in the RAW cache.
             */
             if ($info) {
                 $this->current_raw_end = floor($info['last']) + 1;
             }
         }
     }
     if ($resolution && (!$this->current_cache_end || $this->current_cache_resolution != $resolution)) {
         $info = $this->GetTableInfo($resolution);
         if ($info) {
             $this->current_cache_end = $info['last'] + $resolution;
             $this->current_cache_resolution = $resolution;
         } else {
             $this->current_cache_end = false;
             $this->current_cache_resolution = false;
         }
     }
     /*
         echo date('c', $this->current_raw_end) . "\n";
         echo date('c', $this->current_cache_end) . "\n";
         echo "here $resolution\n";
         exit;
     */
     switch ($dim) {
         case CACHE::DIM_AUTO:
         case CACHE::DIM_WINDOW:
             $from = $ivl->GetWindowStart();
             $to = $ivl->GetWindowEnd();
             break;
         case CACHE::DIM_EXTENDEDWINDOW:
             $from = $ivl->window_start - $ivl->window_size;
             if ($from < $ivl->interval_start) {
                 $from = $ivl->interval_start;
             }
             $to = $ivl->window_start + 2 * $ivl->window_size;
             if ($to > $ivl->interval_end) {
                 $to = $ivl->interval_end;
             }
             break;
         case CACHE::DIM_INTERVAL:
             $from = $ivl->interval_start;
             $to = $ivl->interval_end;
             break;
         default:
             if ($lock) {
                 $lock->UnLock();
                 unset($lock);
             }
             throw new ADEIException(translate("Invalid dimmension parameter is specified"));
     }
     //    echo "$from - $to\n";
     if (!$from && !$to || $to < $from) {
         if ($lock) {
             $lock->UnLock();
             unset($lock);
         }
         if (!$from && !$to) {
             return;
             //throw new ADEIException(translate("Data source contains no data"));
         } else {
             throw new ADEIException(translate("Invalid data range (%d - %d) is detected by CACHE updating code", $from, $to));
         }
     }
     if ($this->current_cache_end && $from < $this->current_cache_end) {
         $from = $this->current_cache_end;
     }
     if ($resolution > 0) {
         $ifrom = ceil($from);
         $rem = $ifrom % $resolution;
         if ($rem) {
             $curfrom = $ifrom + ($resolution - $rem);
         } else {
             $curfrom = $ifrom;
         }
         $ito = floor($to);
         $rem = $ito % $resolution;
         if ($rem) {
             $curto = $ito - $rem;
         } else {
             $curto = $ito;
         }
         if ($this->fill_raw_first) {
             if ($this->current_raw_end > $from) {
                 $rawfrom = $this->current_raw_end;
             } else {
                 $rawfrom = $from;
             }
             if ($rawfrom < $ito) {
                 $this->FillCache($this->resolution->RAW(), $rawfrom, $ito, $this->optimize_empty_cache);
                 $this->current_raw_end = $ito;
             }
             if (!$this->all_mask) {
                 $this->all_mask = $this->CreateMask($empty = array());
             }
         }
         /*
         	echo $from . " - " . $to . "\n";
         	echo $curfrom . " - " . $curto . "\n";
         	echo date("c", $curfrom) . " - " . date("c", $curto) . "\n";
         	echo "\n";
         */
         if ($from != $curfrom) {
             $subres = $this->resolution->Minimal();
             $subresolution = $this->resolution->GetWindowSize($subres);
             if ($subresolution == $resolution) {
                 $subfrom = $curfrom;
             } else {
                 $rem = $ifrom % $subresolution;
                 if ($rem) {
                     $subfrom = $ifrom + ($subresolution - $rem);
                 } else {
                     $subfrom = $ifrom;
                 }
             }
             if ($from != $subfrom) {
                 if ($this->current_raw_end > $from) {
                     $rawfrom = $this->current_raw_end;
                 } else {
                     $rawfrom = $from;
                 }
                 if ($rawfrom < $subfrom) {
                     //		    echo "$from - $subfrom (0)\n";
                     $this->FillCache($this->resolution->RAW(), $rawfrom, $subfrom, $this->optimize_empty_cache, false, $flags & CACHE::VERBOSE ? 1 : 0);
                 }
             }
             while ($subresolution < $resolution) {
                 $nextres = $this->resolution->Larger($subres);
                 $nextresolution = $this->resolution->GetWindowSize($nextres);
                 if ($nextresolution == $resolution) {
                     $subnext = $curfrom;
                 } else {
                     $rem = $ifrom % $nextresolution;
                     if ($rem) {
                         $subnext = $ifrom + ($nextresolution - $rem);
                     } else {
                         $subnext = $ifrom;
                     }
                 }
                 //echo "$subfrom - $subnext ($subresolution)\n";
                 $this->FillCache($subres, $subfrom, $subnext, $this->optimize_empty_cache, false, $flags & CACHE::VERBOSE ? 1 : 0);
                 $subres = $nextres;
                 $subresolution = $nextresolution;
                 $subfrom = $subnext;
             }
         }
         $this->FillCache($res, $curfrom, $curto, $this->optimize_empty_cache, false, $flags & CACHE::VERBOSE ? 1 : 0);
         // We are not filling last incomplete second
         if ($ito != $curto) {
             $subres = $this->resolution->Smaller($res);
             $subresolution = $this->resolution->GetWindowSize($subres);
             $subfrom = $curto;
             while ($subresolution > 0) {
                 $rem = $ito % $subresolution;
                 if ($rem) {
                     $subnext = $ito - $rem;
                 } else {
                     $subnext = $ito;
                 }
                 //		echo "$subfrom - $subnext\n";
                 $this->FillCache($subres, $subfrom, $subnext, $this->optimize_empty_cache, false, $flags & CACHE::VERBOSE ? 1 : 0);
                 $subres = $this->resolution->Smaller($subres);
                 $subresolution = $this->resolution->GetWindowSize($subres);
                 $subfrom = $subnext;
             }
             if ($this->current_raw_end > $subfrom) {
                 $subfrom = $this->current_raw_end;
             }
             if ($ito != $subfrom) {
                 //		echo "$subfrom - $ito\n";
                 $this->FillCache($subres, $subfrom, $ito, $this->optimize_empty_cache, false, $flags & CACHE::VERBOSE ? 1 : 0);
             }
         }
         $this->current_raw_end = $ito;
     } else {
         $ito = floor($to);
         if ($this->current_raw_end > $from) {
             $from = $this->current_raw_end;
         }
         if ($from < $ito) {
             $this->FillCache($res, $from, $ito, $this->optimize_empty_cache);
             $this->current_raw_end = $ito;
         }
     }
     if ($lock) {
         $lock->UnLock();
         unset($lock);
     }
 }
Ejemplo n.º 2
0
<?php

//   $res = exec('ps xa | grep "downloads_check.php" | grep -v grep | wc -l');
//   if ($res > 1) exit;
if (preg_match("/(.*)downloads_check.php\$/", $_SERVER['SCRIPT_FILENAME'], $m)) {
    @chdir($m[1]);
}
require "../adei.php";
$lock = new LOCK("downloads_check");
$lock->Lock(LOCK::BLOCK);
global $ADEI;
global $ADEI_ROOTDIR;
$ADEI->RequireClass("download");
$cache = new CACHEDB();
$dm = new DOWNLOADMANAGER();
$res = $cache->GetDownloads("", "ASC");
while ($row = mysql_fetch_array($res)) {
    $download = $row['dl_id'];
    $status = $row['status'];
    $name = $dm->Getfilename($download);
    $file = $ADEI_ROOTDIR . "tmp/downloads/" . $name;
    if (file_exists($file)) {
        $fsize = filesize($file);
        $lastmodified = time() - filemtime($file);
    } else {
        $fsize = 0;
        $lastmodified = 0;
    }
    if ($fsize > 1 && $lastmodified > 1 && $status != 'Ready') {
        // We should check if it's really complete (I think - not), and remove failed downloads
        // We also should check that it is not in progress (old mtime?)
Ejemplo n.º 3
0
 function Rewidth($postfix, $report = false)
 {
     $lock = new LOCK("cache" . $postfix);
     $lock->Lock(LOCK::BLOCK | LOCK::ALL);
     try {
         $current = $this->GetCacheWidth($postfix);
         $reader = $this->CreateReader($postfix);
         $actual = $reader->GetGroupSize();
         if ($current == $actual) {
             $lock->UnLock();
             return;
         }
         if ($report) {
             echo translate("Resizing %s from %d to %d", "cache*_{$postfix}", $current, $actual);
             if ($report === "html") {
                 echo "<br/>";
             }
             echo "\n";
         }
         if ($current < $actual) {
             $spec0 = "";
             $spec = "";
             for ($i = $current; $i < $actual; $i++) {
                 if ($i > $current) {
                     $spec0 .= ", ";
                     $spec .= ", ";
                 }
                 $spec0 .= "ADD `v{$i}` DOUBLE";
                 $spec .= "ADD `min" . $i . "` DOUBLE, ADD `max" . $i . "` DOUBLE, ADD `mean" . $i . "` DOUBLE";
             }
         } else {
             $spec0 = "";
             $spec = "";
             for ($i = $actual; $i < $current; $i++) {
                 if ($i > $actual) {
                     $spec0 .= ", ";
                     $spec .= ", ";
                 }
                 $spec0 .= "DROP `v{$i}`";
                 $spec .= "DROP `min" . $i . "`, DROP `max" . $i . "`, DROP `mean" . $i . "`";
             }
         }
         $resolutions = $this->ListResolutions($postfix);
         rsort($resolutions);
         foreach ($resolutions as $res) {
             if ($res) {
                 $width = $this->GetTableWidth($postfix, $res);
                 if ($width != $current) {
                     if ($width == $actual) {
                         if ($report) {
                             echo translate("Resolution %lu is already converted", $res);
                             if ($report === "html") {
                                 echo "<br/>";
                             }
                             echo "\n";
                         }
                         continue;
                     }
                     throw new ADEIException(translate("CACHE table (%s) have unexpected size: %d (converting from %d to %d)", "cache{$res}{$postfix}", $width, $current, $actual));
                 }
                 $query = "ALTER TABLE `cache{$res}{$postfix}` {$spec}";
             } else {
                 $query = "ALTER TABLE `cache0{$postfix}` {$spec0}";
             }
             if ($report) {
                 echo translate("Converting resolution %lu", $res);
                 if ($report === "html") {
                     echo "<br/>";
                 }
                 echo "\n";
             }
             #	    echo $query . "<br/>\n";
             if (!mysql_query($query, $this->dbh)) {
                 throw new ADEIException(translate("The CACHE (%s) resizing request is not complete. Request '%s' is failed with error: %s", "cache*_{$postfix}", $query, mysql_error($this->dbh)));
             }
         }
     } catch (ADEIException $ae) {
         $lock->UnLock();
         throw $ae;
     }
     $lock->UnLock();
 }
Ejemplo n.º 4
0
 function Backup(&$binfo, $flags = 0)
 {
     global $ADEI_ROOTDIR;
     global $BACKUP_DB;
     if (is_array($dbinfo)) {
         $dbinfo =& $binfo;
     } else {
         $dbinfo = $BACKUP_DB;
         $dbinfo['database'] = $binfo;
     }
     $lock = new LOCK("backup." . $this->srvid);
     $lock->Lock(LOCK::BLOCK);
     try {
         $zeus = new ZEUS($dbinfo);
     } catch (ADEIException $e) {
         if ($flags & READER::BACKUP_FULL) {
             $tmpinfo = $dbinfo;
             unset($tmpinfo['database']);
             $zeus = new ZEUS($tmpinfo);
             $dblist = $zeus->db->ShowDatabases();
             foreach ($dblist as $row) {
                 if (strtolower($row[0]) == strtolower($dbinfo['database'])) {
                     throw new ADEIException(translate("Broken or invalid database is specified for backup"));
                 }
             }
             if (strtolower($dbinfo['driver']) == "mysql" && strtolower($this->server['driver']) == "mysql") {
                 $cli = Database::GetConnectionString($this->server);
                 $srv = Database::GetConnectionString($dbinfo);
                 $zeus->db->CreateDatabase($dbinfo['database']);
                 @system("mysqldump --compact {$cli} | mysql {$srv}", $res);
                 if ($res) {
                     throw new ADEIException(translate("The initialization of the backup database is failed."));
                 }
                 $zeus = new ZEUS($dbinfo);
             } elseif (strtolower($dbinfo['driver']) == "mysql") {
                 $sql = file_get_contents("{$ADEI_ROOTDIR}/sql/zeus71.sql");
                 $zeus->db->CreateDatabase($dbinfo['database']);
                 $zeus = new ZEUS($dbinfo);
                 $zeus->db->Query($sql);
             } else {
                 throw new ADEIException(translate("The initial backup database should setup manualy. The auto mode is only works for the MySQL at the moment"));
             }
         } else {
             throw $e;
         }
     }
     $src_list = $this->GetGroupList(REQUEST::NEED_INFO | REQUEST::NEED_ITEMINFO);
     if ($this->char_mode && !$zeus->char_mode) {
         $pack = 1;
     } else {
         $pack = 0;
     }
     $zeus->db->Query("SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO");
     if ($flags & READER::BACKUP_FULL) {
         $tables = array("config", "blocks", "groups", "items", "g2id", "marker", "masks", "messages", "loginfo", "opc");
         $blobs = array("config" => array(3), "items" => array(3), "masks" => array(3));
         if ($this->opts->Get("lclb_is_varchar", false)) {
             unset($blobs["config"]);
         }
         foreach ($tables as $table) {
             $src = $this->db->Prepare("SELECT * FROM {$table}");
             $src->execute();
             $width = $src->columnCount();
             $full_query = "TRUNCATE TABLE {$table};";
             if ($pack) {
                 $cur_blobs = $blobs[$table];
             } else {
                 $cur_blobs = false;
             }
             if ($width > 1) {
                 $query = "?" . str_repeat(", ?", $width - 1);
             } else {
                 $query = "?";
             }
             $dst = $zeus->db->Prepare("INSERT INTO {$table} VALUES ({$query})");
             $row = array();
             for ($i = 0; $i < $width; $i++) {
                 $row[$i] = false;
                 if ($cur_blobs && in_array($i, $cur_blobs)) {
                     $src->bindColumn($i + 1, $row[$i], PDO::PARAM_LOB);
                     $dst->bindParam($i + 1, $row[$i], PDO::PARAM_LOB);
                 } else {
                     $src->bindColumn($i + 1, $row[$i]);
                     $dst->bindParam($i + 1, $row[$i]);
                 }
             }
             $zeus->db->Query($full_query);
             while ($src->fetch(PDO::FETCH_BOUND)) {
                 if ($cur_blobs) {
                     foreach ($cur_blobs as $i) {
                         $row[$i] = pack("H*", $row[$i]);
                     }
                 }
                 $dst->execute();
             }
             /*
             	    $rows = $src->fetchAll(PDO::FETCH_NUM);
             	    foreach ($rows as $row) {
             		$query = "INSERT INTO $table VALUES(";
             		foreach ($row as $i=>$value) {
             		    if (($cur_blobs)&&(in_array($i, $cur_blobs))) {
             #			echo "Value($table, $i): " . $value . "!\n";
             //			$value = pack("H*", $value);
             		        if ($i) $query .= ", 0x$value";
             			else $query .= "0x$value";
             		    } else {
             		        if ($i) $query .= ", \"$value\"";
             			else $query .= "\"$value\"";
             		    }
             		}
             		$full_query .= "$query); ";
             	    }
             //	    echo $full_query . "\n";
             
             	    $zeus->db->Query($full_query);
             */
         }
         $stmt = $zeus->db->Query("SHOW TABLES LIKE 'log%'");
         $log_tables_result = $stmt->fetchAll(PDO::FETCH_NUM);
         // loginfo additional
         $log_tables = array();
         foreach ($log_tables_result as $i => $table) {
             $log_tables[$i] = $table[0];
         }
         //print_r($log_tables);
         foreach ($src_list as &$grp) {
             $table = "log" . $grp['gid'];
             if (!in_array($table, $log_tables)) {
                 $zeus->db->Query("CREATE TABLE {$table} (lid bigint(20) NOT NULL auto_increment PRIMARY KEY, ts double NOT NULL, data BLOB, INDEX(ts))");
             }
         }
     }
     $dst_list = $zeus->GetGroupList(REQUEST::NEED_INFO | REQUEST::NEED_ITEMINFO);
     if (sizeof($src_list) != sizeof($dst_list) || sizeof($src_list['items']) != sizeof($dst_list['items'])) {
         // or compare names (items by content not lenght, 'name', 'comment')
         throw new ADEIException(translate("The configuration of the current backup not in sync with the data source"));
     }
     /*$tables = array(
         	"messagelog" => "lid", 
     	"syslog" => "lid"
         );*/
     $tables = array("messagelog" => "lid");
     $blobs = array();
     $list = $this->req->GetGroups();
     foreach ($list as $greq) {
         $grp = $greq->CreateGroup($this);
         /*	
         	// Support limited backup if time limiting options are set
         	if ($dst_list[$grp->gid]['__internal__']['last']) $from = $dst_list[$grp->gid]['__internal__']['last'];
         	else $from = false;
         
         	if ($src_list[$grp->gid]['__internal__']['last']) $to = $src_list[$grp->gid]['__internal__']['last'];
         	else $to = false;
         
         	if (($to === false)||(($from !== false)&&(abs(round($from) - round($to))<2))) continue;
         
         	if (($from !== false)&&($from > $to))
         	    throw new ADEIException(translate("The backup had newer data than the source does"));
         */
         $stmt = $zeus->db->Query("SELECT MAX(lid), MAX(ts) FROM " . $grp->table);
         $row = $stmt->fetch(PDO::FETCH_NUM);
         if ($row && isset($row[0])) {
             $first_lid = $row[0];
             $first_ts = $row[1];
         } else {
             $first_lid = false;
         }
         if ($first_lid !== false) {
             $stmt = $this->db->Query("SELECT MAX(lid) AS lid FROM " . $grp->table);
             $row = $stmt->fetch(PDO::FETCH_BOTH);
             // FETCH_NUM looks to be corrupted with MSSQL/ODBC
             if ($row && isset($row['lid'])) {
                 $remote_last_lid = $row['lid'];
             } else {
                 $remote_last_lid = false;
             }
             if ($remote_last_lid && $remote_last_lid < $first_lid) {
                 $stmt = $this->db->Query("SELECT MIN(lid) AS lid FROM " . $grp->table . " WHERE ts > " . $first_ts);
                 $row = $stmt->fetch(PDO::FETCH_NUM);
                 if ($row && isset($row['lid'])) {
                     $diff_lid = $first_lid + 1 - $row['lid'];
                 } else {
                     return;
                 }
             } else {
                 $diff_lid = false;
             }
         }
         try {
             if ($first_lid === false) {
                 $src = $this->db->Prepare("SELECT lid,ts,data FROM " . $grp->table . " ORDER by lid ASC");
             } else {
                 if ($diff_lid === false) {
                     $src = $this->db->Prepare("SELECT lid,ts,data FROM " . $grp->table . " WHERE lid > " . $first_lid . " ORDER by lid ASC");
                 } else {
                     $src = $this->db->Prepare("SELECT (lid + {$diff_lid}) AS lid, ts, data FROM " . $grp->table . " WHERE ts > " . $first_ts . " ORDER by lid ASC");
                 }
             }
             /*
             	    if ($from === false) {
             	        $src = $this->db->Prepare("SELECT lid,ts,data FROM " . $grp->table . " ORDER by lid ASC");
             	    } else {
             	        $src = $this->db->Prepare("SELECT lid,ts,data FROM " . $grp->table . " WHERE ts > " . $from . " ORDER by lid ASC");
             	    }
             */
             if (!$src->execute()) {
                 $einfo = $dst->errorInfo();
                 throw new ADEIException("Failure while backing up group " . $grp->gid . ". SQL Error: " . $einfo[0] . ", Driver Error: " . $einfo[1] . ", Message: " . $einfo[2]);
             }
             $src->bindColumn(1, $lid);
             $src->bindColumn(2, $ts);
             $src->bindColumn(3, $data, PDO::PARAM_LOB);
             $dst = $zeus->db->Prepare("INSERT INTO " . $grp->table . " (lid,ts,data) VALUES (?, ?, ?)");
             $dst->bindParam(1, $lid);
             $dst->bindParam(2, $ts);
             $dst->bindParam(3, $data, PDO::PARAM_LOB);
             while ($src->fetch(PDO::FETCH_BOUND)) {
                 //		echo "$lid, $ts\n";
                 if ($pack) {
                     $data = pack("H*", $data);
                 }
                 if (!$dst->execute()) {
                     $einfo = $dst->errorInfo();
                     throw new ADEIException("Failure while backing up group " . $grp->gid . ", item " . $lid . "(" . date("r", ceil($this->ExportUnixTime($ts))) . ") SQL Error: " . $einfo[0] . ", Driver Error: " . $einfo[1] . ", Message: " . $einfo[2]);
                 }
             }
         } catch (PDOException $e) {
             throw new ADEIException(translate("SQL request is failed with error") . ": " . $e->getMessage(), $e->getCode());
         }
     }
     foreach ($tables as $table => $id) {
         if ($pack) {
             $cur_blobs = $blobs[$table];
         } else {
             $cur_blobs = false;
         }
         $stmt = $zeus->db->Query("SELECT MAX({$id}) FROM " . $table);
         $row = $stmt->fetch(PDO::FETCH_NUM);
         if ($row && isset($row[0])) {
             $first_lid = $row[0];
         } else {
             $first_lid = false;
         }
         if ($first_lid === false) {
             $src = $this->db->Prepare("SELECT * FROM {$table} ORDER BY {$id} ASC");
         } else {
             $src = $this->db->Prepare("SELECT * FROM {$table} WHERE {$id} > {$first_lid} ORDER BY {$id} ASC");
         }
         $src->execute();
         while ($row = $src->fetch(PDO::FETCH_NUM)) {
             $query = "INSERT INTO {$table} VALUES(";
             foreach ($row as $i => $value) {
                 if ($cur_blobs && in_array($i, $cur_blobs)) {
                     if ($i) {
                         $query .= ", 0x{$value}";
                     } else {
                         $query .= "0x{$value}";
                     }
                 } else {
                     if ($i) {
                         $query .= ", \"{$value}\"";
                     } else {
                         $query .= "\"{$value}\"";
                     }
                 }
             }
             $zeus->db->Query("{$query})");
         }
     }
     $lock->UnLock();
     unset($lock);
 }