setThreads.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 «Threads» column of «Instances» table according
  22. to them suspending or limiting threads, or not doing it, or the case it’s not
  23. known.
  24. OPTIONS
  25. -h, --help
  26. Show this help text and exit.'.N;
  27. for ($i=1; $i<$argc; $i++) {
  28. if ($argv[$i]=='-h' || $argv[$i]=='--help') {
  29. mexit($help,0);
  30. } else {
  31. mexit('Don’t know how to interpret «'.$argv[$i].'», please read the help text using «-h» or «--help» option.'.N,1);
  32. }
  33. }
  34. use function mysqli_real_escape_string as myesc;
  35. $iniarr=@parse_ini_file(CONFIGFP)
  36. or mexit('Could not open config file «'.CONFIGFP.'».'.N,1);
  37. try { $link=@mysqli_connect($iniarr['db_host'],$iniarr['db_admin_name'],$iniarr['db_admin_password'],$iniarr['db_name'],$iniarr['db_port'],$iniarr['db_socket']); }
  38. catch (Exception $error) { mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true); }
  39. // for php versions < 8
  40. if ($link===false) mexit('could not connect to MySQL server: '.mysqli_connect_error().'.'.N,1,true);
  41. try { $res=mysqli_set_charset($link,'utf8mb4'); }
  42. catch (Exception $error) { mexit('could not set «utf8mb4» charset for MySQL: '.mysqli_error($link).'.'.N,1,true); }
  43. // for php versions < 8
  44. if ($res===false) mexit('could not set MySQL charset: '.mysqli_errno($link).': '.mysqli_error($link).'.'.N,1,true);
  45. $insts=[];
  46. $res=myq($link,'SELECT * FROM Instances');
  47. while ($row=mysqli_fetch_assoc($res)) $insts[]=$row;
  48. $cinsts=count($insts);
  49. $i=0;
  50. $now=time();
  51. foreach ($insts as $inst) {
  52. $i++;
  53. echo('Working on instance '.$i.'/'.$cinsts.' ('.round(100/$cinsts*$i,2).'%) with ID = '.$inst['ID'].' and URI = «'.$inst['URI'].'».'.N);
  54. $res=myq($link,'SELECT * FROM InstBlocks WHERE InstID='.$inst['ID']);
  55. if (mysqli_num_rows($res)>0) {
  56. $threads='"accessible"';
  57. while ($row=mysqli_fetch_assoc($res)) {
  58. if (preg_match('#^(threads.net|.*\.threads.net)$#i',$row['Domain'])===1) {
  59. if ($row['Severity']=='silence')
  60. $threads='"limited"';
  61. elseif ($row['Severity']=='suspend')
  62. $threads='"suspended"';
  63. else
  64. $threads='"'.$row['Severity'].'"';
  65. break;
  66. }
  67. }
  68. } else {
  69. $threads='NULL';
  70. }
  71. $que='UPDATE Instances SET Threads='.$threads.' WHERE ID='.$inst['ID'];
  72. echo 'Executing query «'.$que.'» ...'.N;
  73. myq($link,$que);
  74. }
  75. mysqli_close($link);
  76. echo('Done updating '.$cinsts.' «Instances» records :-).'.N);
  77. exit(0);
  78. // functions
  79. function myq(&$l,$q) {
  80. try {
  81. $res=mysqli_query($l,$q);
  82. }
  83. catch (Exception $e) {
  84. echo('query «'.$q.'» failed: '.$e->getMessage().' (error code: '.$e->getCode().').'.N);
  85. exit(3);
  86. }
  87. if ($res===false) {
  88. echo('query «'.$q.'» failed: '.mysqli_errno($l).': '.mysqli_error($l).'.'.N);
  89. exit(3);
  90. }
  91. return($res);
  92. }
  93. function mexit($msg,$code) {
  94. global $link;
  95. if (isset($link) && $link!==false) mysqli_close($link);
  96. if ($code>0)
  97. fwrite(STDERR,$msg);
  98. else
  99. echo($msg);
  100. exit($code);
  101. }
  102. function utstd($val) {
  103. if (is_null($val)) return(null);
  104. $val=round($val);
  105. return(date('Y-m-d H:i:s',$val));
  106. }
  107. function pr($val) {
  108. if (is_null($val)) return('NULL');
  109. return($val);
  110. }
  111. function eecho($msg) {
  112. echo($msg);
  113. }
  114. ?>