set_TotChecks_and_OkChecks.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. define('N',"\n");
  16. define('SNAME',basename(__FILE__));
  17. define('CONFIGFP',__DIR__.'/../../conf/mustard.ini');
  18. $help='SYNOPSYS
  19. '.SNAME.' [options]
  20. DESCRIPTION
  21. This is a script to set the «TotChecks» and «OkChecks» fields of «Instances»
  22. table according to records stored in the «InstChecks» table.
  23. OPTIONS
  24. -h, --help
  25. Show this help text and exit.'.N;
  26. for ($i=1; $i<$argc; $i++) {
  27. if ($argv[$i]=='-h' || $argv[$i]=='--help') {
  28. mexit($help,0);
  29. } else {
  30. mexit('Don’t know how to interpret «'.$argv[$i].'», please read the help text using «-h» or «--help» option.'.N,1);
  31. }
  32. }
  33. use function mysqli_real_escape_string as myesc;
  34. $iniarr=@parse_ini_file(CONFIGFP)
  35. or mexit('Could not open config file «'.CONFIGFP.'».'.N,1);
  36. try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); }
  37. catch (Exception $error) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); }
  38. // for php versions < 8
  39. if ($link===false) mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true);
  40. try { $res=mysqli_set_charset($link,'utf8mb4'); }
  41. catch (Exception $error) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).'.'.N,1,true); }
  42. // for php versions < 8
  43. if ($res===false) mexit('could not set MySQL charset: '.mysqli_errno($link).': '.mysqli_error($link).'.'.N,1,true);
  44. $insts=[];
  45. $res=myq($link,'SELECT * FROM Instances');
  46. while ($row=mysqli_fetch_assoc($res)) $insts[]=$row;
  47. $cinsts=count($insts);
  48. $i=0;
  49. $now=time();
  50. foreach ($insts as $inst) {
  51. $i++;
  52. echo('Working on instance '.$i.'/'.$cinsts.' ('.round(100/$cinsts*$i,2).'%) with ID = '.$inst['ID'].' and URI = «'.$inst['URI'].'».'.N);
  53. $instchecks=[];
  54. $res=myq($link,'SELECT * FROM InstChecks WHERE InstID='.$inst['ID']);
  55. while ($row=mysqli_fetch_assoc($res)) $instchecks[]=$row;
  56. //print_r($instchecks);
  57. $totchecks=0;
  58. $okchecks=0;
  59. foreach ($instchecks as $check) {
  60. $totchecks++;
  61. if ($check['Status']==1)
  62. $okchecks++;
  63. }
  64. echo('Tot checks: '.$totchecks.'; ok checks: '.$okchecks.N);
  65. myq($link,'UPDATE Instances SET TotChecks='.$totchecks.', OkChecks='.$okchecks.' WHERE ID='.$inst['ID']);
  66. echo('Updated :-)'.N.'---'.N);
  67. }
  68. mysqli_close($link);
  69. echo('Done updating '.$cinsts.' «Instances» records :-).'.N);
  70. exit(0);
  71. // functions
  72. function myq(&$l,$q) {
  73. try {
  74. $res=mysqli_query($l,$q);
  75. }
  76. catch (Exception $e) {
  77. echo('query «'.$q.'» failed: '.$e->getMessage().' (error code: '.$e->getCode().').'.N);
  78. exit(3);
  79. }
  80. if ($res===false) {
  81. echo('query «'.$q.'» failed: '.mysqli_errno($l).': '.mysqli_error($l).'.'.N);
  82. exit(3);
  83. }
  84. return($res);
  85. }
  86. function mexit($msg,$code) {
  87. global $link;
  88. if (isset($link) && $link!==false) mysqli_close($link);
  89. if ($code>0)
  90. fwrite(STDERR,$msg);
  91. else
  92. echo($msg);
  93. exit($code);
  94. }
  95. function utstd($val) {
  96. if (is_null($val)) return(null);
  97. $val=round($val);
  98. return(date('Y-m-d H:i:s',$val));
  99. }
  100. function pr($val) {
  101. if (is_null($val)) return('NULL');
  102. return($val);
  103. }
  104. function eecho($msg) {
  105. echo($msg);
  106. }
  107. ?>