From 80c73c29ef57824c0bd332720c4c54299499fc42 Mon Sep 17 00:00:00 2001 From: pezcurrel Date: Mon, 19 Aug 2024 18:08:02 +0200 Subject: [PATCH] First commit --- web/clitools/unatantum/fixBlocks.php | 95 +++++++++++++++++++++ web/clitools/unatantum/fixPeers.php | 121 +++++++++++++++++++++++++++ 2 files changed, 216 insertions(+) create mode 100644 web/clitools/unatantum/fixBlocks.php create mode 100644 web/clitools/unatantum/fixPeers.php diff --git a/web/clitools/unatantum/fixBlocks.php b/web/clitools/unatantum/fixBlocks.php new file mode 100644 index 0000000..a2905c8 --- /dev/null +++ b/web/clitools/unatantum/fixBlocks.php @@ -0,0 +1,95 @@ +#!/usr/bin/php +. +*/ + +const N="\n"; +define('SNAME',basename(__FILE__)); +const CONFIGFP=__DIR__.'/../../conf/mustard.ini'; + +$help='SYNOPSYS + '.SNAME.' [options] +DESCRIPTION + This is a script to set the new «PublicBlocksList» columns of «Instances» + table according to the «InstBlocks» table.. +OPTIONS + -h, --help + Show this help text and exit.'.N; + +for ($i=1; $i<$argc; $i++) { + if ($argv[$i]=='-h' || $argv[$i]=='--help') { + mexit($help,0); + } else { + mexit('Don’t know how to interpret «'.$argv[$i].'», please read the help text using «-h» or «--help» option.'.N,1); + } +} + +use function mysqli_real_escape_string as myesc; + +$iniarr=@parse_ini_file(CONFIGFP) + or mexit('Could not open config file «'.CONFIGFP.'».'.N,1); + +try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); } +catch (Exception $error) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); } +// for php versions < 8 +if ($link===false) mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); +try { $res=mysqli_set_charset($link,'utf8mb4'); } +catch (Exception $error) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).'.'.N,1,true); } +// for php versions < 8 +if ($res===false) mexit('could not set MySQL charset: '.mysqli_errno($link).': '.mysqli_error($link).'.'.N,1,true); + +$now=time(); +$res=myq($link,'SELECT DISTINCT InstID FROM InstBlocks'); +$cres=mysqli_num_rows($res); +$i=0; +while ($row=mysqli_fetch_assoc($res)) { + $i++; + echo "{$i}/{$cres} done\r"; + myq($link,'UPDATE Instances SET PublicBlocksList=1 WHERE ID='.$row['InstID']); + +} +echo "{$i}/{$cres} done.\n"; +exit(0); + + +// functions + +function myq(&$l,$q) { + try { + $res=mysqli_query($l,$q); + } + catch (Exception $e) { + echo('query «'.$q.'» failed: '.$e->getMessage().' (error code: '.$e->getCode().').'.N); + exit(3); + } + if ($res===false) { + echo('query «'.$q.'» failed: '.mysqli_errno($l).': '.mysqli_error($l).'.'.N); + exit(3); + } + return($res); +} + +function mexit($msg,$code) { + global $link; + if (isset($link) && $link!==false) mysqli_close($link); + if ($code>0) + fwrite(STDERR,$msg); + else + echo $msg; + exit($code); +} + +?> diff --git a/web/clitools/unatantum/fixPeers.php b/web/clitools/unatantum/fixPeers.php new file mode 100644 index 0000000..d63b412 --- /dev/null +++ b/web/clitools/unatantum/fixPeers.php @@ -0,0 +1,121 @@ +#!/usr/bin/php +. +*/ + +const N="\n"; +define('SNAME',basename(__FILE__)); +const CONFIGFP=__DIR__.'/../../conf/mustard.ini'; + +require '../../lib/grace.php'; + +$help='SYNOPSYS + '.SNAME.' [options] +DESCRIPTION + This is a script to set the new «ChecksAtLastOk», «LastCheckTS» and + «ChecksAtLast» columns of «Peers» table according to its «LastOkCheckTS» + column. +OPTIONS + -h, --help + Show this help text and exit.'.N; + +for ($i=1; $i<$argc; $i++) { + if ($argv[$i]=='-h' || $argv[$i]=='--help') { + mexit($help,0); + } else { + mexit('Don’t know how to interpret «'.$argv[$i].'», please read the help text using «-h» or «--help» option.'.N,1); + } +} + +use function mysqli_real_escape_string as myesc; + +$iniarr=@parse_ini_file(CONFIGFP) + or mexit('Could not open config file «'.CONFIGFP.'».'.N,1); + +try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); } +catch (Exception $error) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); } +// for php versions < 8 +if ($link===false) mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); +try { $res=mysqli_set_charset($link,'utf8mb4'); } +catch (Exception $error) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).'.'.N,1,true); } +// for php versions < 8 +if ($res===false) mexit('could not set MySQL charset: '.mysqli_errno($link).': '.mysqli_error($link).'.'.N,1,true); + +$now=time(); +$peers=[]; +$res=myq($link,'SELECT * FROM Peers'); +while ($row=mysqli_fetch_assoc($res)) $peers[]=$row; +$cpeers=count($peers); +$i=0; +$now=time(); +foreach ($peers as $peer) { + $i++; + echo('Working on peer '.$i.'/'.$cpeers.' ('.round($i*100/$cpeers,2).'%) with ID = '.$peer['ID'].' and Hostname = «'.$peer['Hostname'].'».'.N); + $que='UPDATE Peers SET '; + if (!is_null($peer['LastOkCheckTS'])) + $que.='ChecksAtLastOk='.round(($peer['LastOkCheckTS']-$peer['FirstCheckTS'])/86400).', '; + $que.='LastCheckTS='.$now.', ChecksAtLast='.round(($now-$peer['FirstCheckTS'])/86400).' WHERE ID='.$peer['ID']; + echo 'Executing query «'.$que.'» ...'.N; + myq($link,$que); +} +mysqli_close($link); +echo('Done updating '.$cpeers.' «Peers» records :-).'.N); +exit(0); + + +// functions + +function myq(&$l,$q) { + try { + $res=mysqli_query($l,$q); + } + catch (Exception $e) { + echo('query «'.$q.'» failed: '.$e->getMessage().' (error code: '.$e->getCode().').'.N); + exit(3); + } + if ($res===false) { + echo('query «'.$q.'» failed: '.mysqli_errno($l).': '.mysqli_error($l).'.'.N); + exit(3); + } + return($res); +} + +function mexit($msg,$code) { + global $link; + if (isset($link) && $link!==false) mysqli_close($link); + if ($code>0) + fwrite(STDERR,$msg); + else + echo($msg); + exit($code); +} + +function utstd($val) { + if (is_null($val)) return(null); + $val=round($val); + return(date('Y-m-d H:i:s',$val)); +} + +function pr($val) { + if (is_null($val)) return('NULL'); + return($val); +} + +function eecho($msg) { + echo($msg); +} + +?>