Hi, now I tell you how I resolved:
I did a simple php script that run daily in crontab and deletes the oldest results/subdirectories of a number of days taken from db,
I take the script below for those who would think it useful
[list]
[]I left all the “echo” for displaying the operation on a page but they can be deleted
[]Be careful with the “IP_autorizzato” function
[/list]
crontab configuration may be the following:
5 0 * * * wget -qO- http://myurl/tools/myscript.php &> /dev/null > /dev/null 2>&1
Comments and suggestions are welcome
[php]
date_default_timezone_set (‘UTC’);
class data_calc {
public $dbh;
public $realtime_days;
public $ultima_data = array();
public $oggi = array();
function __construct() {
if (is_file('../common/dbconn.ini')) {
$dbini_array=parse_ini_file('../common/dbconn.ini', true);
$dbdefault=$dbini_array['default']['dbactive'];
$dbhost=$dbini_array[$dbdefault]['host'];
$dbname=$dbini_array[$dbdefault]['dbname'];
$dbuser=$dbini_array[$dbdefault]['user'];
$dbpassword=$dbini_array[$dbdefault]['password'];
$this->dbh = new PDO("pgsql: dbname=".$dbname."; host=".$dbhost."; port=5432", $dbuser, $dbpassword);
$query = $this->dbh->prepare("
select value as realtime_days from webpagetest.settings where setting='realtime_days'
");
$query->execute();
$result = $query->fetchAll();
$this->realtime_days = $result[0]['realtime_days'];
$this->delete_failed_buffer();
} else {
throw new Exception('errore apertura dbconn.ini');
}
$this->oggi = new DateTime();
$this->ultima_data = new DateTime();
$this->ultima_data->sub(new DateInterval('P'.$this->realtime_days.'D'));
echo $this->oggi->format('y-m-d').'<br>';
echo $this->ultima_data->format('y-m-d').'<br>';
}
function delete_failed_buffer() {
$query = $this->dbh->prepare("
delete from webpagetest.result_failed_buffer WHERE created_at::date < current_date -
(select value from webpagetest.settings where setting='realtime_days')::INTEGER
");
$query->execute();
}
function if_dir_giorno_old($anno_dir, $mese_dir, $giorno_dir) {
$data_x = new DateTime();
$data_x->setDate(intval($anno_dir)+2000,intval($mese_dir),intval($giorno_dir));
if ($data_x < $this->ultima_data) {
return true;
} else {
return false;
}
}
function if_dir_mese_old($anno_dir, $mese_dir) {
$data_x = new DateTime();
if (intval($mese_dir)==12) {
$m=1;
$a=intval($anno_dir)+1+2000;
} else {
$m=intval($mese_dir)+1;
$a=intval($anno_dir)+2000;
}
$data_x->setDate($a,$m,01);
date_sub($data_x,date_interval_create_from_date_string("1 day"));
echo $data_x->format('Y-m-d').' ';
if ($data_x < $this->ultima_data) {
return true;
} else {
return false;
}
}
function if_dir_anno_old($anno_dir) {
$data_x = new DateTime();
$data_x->setDate(intval($anno_dir)+2000+1,01,01);
date_sub($data_x,date_interval_create_from_date_string("1 day"));
echo $data_x->format('Y-m-d').' ';
if ($data_x < $this->ultima_data) {
return true;
} else {
return false;
}
}
}
class cleaner {
public $auth_ip;
public $root_dir = '/store/webpagetest/doc_root/results';
public $calc_data;
function __construct() {
if (is_file('clockrun.ini')) {
$clockrun_ini_array=parse_ini_file('clockrun.ini', true);
$this->auth_ip=$clockrun_ini_array['auth_ip'];
$this->calc_data = new data_calc();
} else {
throw new Exception('errore apertura clockrun.ini');
}
}
function IP_autorizzato() {
$ip=$_SERVER['REMOTE_ADDR'];
if ($ip==$this->auth_ip) {
return true;
} else {
return false;
}
}
function delete_dir_giorno ($anno, $mese, $giorno) {
echo 'delete dir:'.$anno.'/'.$mese.'/'.$giorno.'<br>';
echo shell_exec('nohup rm -rf '.$this->root_dir.'/'.$anno.'/'.$mese.'/'.$giorno.' >/dev/null 2>&1 &').'<br>';
}
function scan_dir_giorni($anno, $mese) {
echo 'scan dir: '.$anno.'/'.$mese.'<br>';
$giorni = scandir($this->root_dir.'/'.$anno.'/'.$mese);
foreach ($giorni as $giorno) {
if (strlen($giorno)==2) {
if ($giorno<>'..') {
echo ' '.$giorno.' - ';
if ($this->calc_data->if_dir_giorno_old($anno, $mese, $giorno)) {
$this->delete_dir_giorno($anno, $mese, $giorno);
} else {
echo 'ok<br>';
}
} else {
echo '..<br>';
}
} else {
echo $giorno.'<br>';
}
}
}
function delete_dir_mese($anno,$mese) {
echo 'delete dir:'.$anno.'/'.$mese.'<br>';
echo shell_exec('nohup rm -rf '.$this->root_dir.'/'.$anno.'/'.$mese.' >/dev/null 2>&1 &').'<br>';
}
function scan_dir_mesi($anno) {
echo 'scan dir: '.$anno.'<br>';
$mesi = scandir($this->root_dir.'/'.$anno);
foreach ($mesi as $mese) {
if (strlen($mese)==2) {
if ($mese<>'..') {
echo ' '.$mese.' - ';
if ($this->calc_data->if_dir_mese_old($anno, $mese)) {
$this->delete_dir_mese($anno,$mese);
} else {
$this->scan_dir_giorni($anno,$mese);
}
} else {
echo '..<br>';
}
} else {
echo $mese.'<br>';
}
}
}
function delete_dir_anno($anno) {
echo 'delete dir:'.$anno.'<br>';
echo shell_exec('nohup rm -rf '.$this->root_dir.'/'.$anno.' >/dev/null 2>&1 &').'<br>';
}
function scan_dir_anni() {
echo 'scan dir: /RESULTS<br>';
$anni = scandir($this->root_dir);
foreach($anni as $anno) {
if (strlen($anno)==2) {
if ($anno<>'..') {
echo $anno.' - ';
if ($this->calc_data->if_dir_anno_old($anno)) {
$this->delete_dir_anno($anno);
} else {
$this->scan_dir_mesi($anno);
}
} else {
echo '..<br>';
}
} else {
echo $anno.'<br>';
}
}
}
function scan_dir_video() {
echo 'scan dir: /RESULTS/VIDEO<br>';
$this->root_dir=$this->root_dir.'/video';
$anni = scandir($this->root_dir);
foreach($anni as $anno) {
if (strlen($anno)==2) {
if ($anno<>'..') {
echo $anno.' - ';
if ($this->calc_data->if_dir_anno_old($anno)) {
$this->delete_dir_anno($anno);
} else {
$this->scan_dir_mesi($anno);
}
} else {
echo '..<br>';
}
} else {
echo $anno.'<br>';
}
}
}
function free_disk_space() {
$this->scan_dir_anni();
$this->scan_dir_video();
}
}
?>
body {
font-family: "Courier New", Courier, "Lucida Sans Typewriter", "Lucida Typewriter", monospace;
padding-left:50px;
font-size:12px;
}
<?php
try {
$clear = new cleaner; // legge clockrun.ini + connect db
if ($clear->IP_autorizzato()) {
$clear->free_disk_space();
} else {
echo 'cancellazione disco non autorizzata da questo ip: '.$_SERVER['REMOTE_ADDR'];
}
} catch (Exception $e) {
$r=fopen('spazio_disco.log','a+');
$ErrMsg = 'Errore: '.$e->getMessage()."\n";
fwrite($r,$ErrMsg);
fclose($r);
}
unset($clear);
?>
[/php]