summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/hooks/cron.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/rebootcontrol/hooks/cron.inc.php')
-rw-r--r--modules-available/rebootcontrol/hooks/cron.inc.php37
1 files changed, 27 insertions, 10 deletions
diff --git a/modules-available/rebootcontrol/hooks/cron.inc.php b/modules-available/rebootcontrol/hooks/cron.inc.php
index e387e055..289426c7 100644
--- a/modules-available/rebootcontrol/hooks/cron.inc.php
+++ b/modules-available/rebootcontrol/hooks/cron.inc.php
@@ -5,15 +5,18 @@
*/
if (in_array((int)date('G'), [6, 7, 9, 12, 15]) && in_array(date('i'), ['00', '01', '02', '03'])) {
$res = Database::simpleQuery('SELECT hostid, host, port, username, sshkey, script FROM reboot_jumphost');
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ foreach ($res as $row) {
RebootControl::wakeViaJumpHost($row, '255.255.255.255', [['macaddr' => '00:11:22:33:44:55']]);
}
}
+// CRON for Scheduler
+Scheduler::cron();
+
/*
* Client reachability test -- can be disabled
*/
-if (mt_rand(1, 2) !== 1 || Property::get(RebootControl::KEY_AUTOSCAN_DISABLED))
+if (Property::get(RebootControl::KEY_AUTOSCAN_DISABLED))
return;
class Stuff
@@ -21,7 +24,7 @@ class Stuff
public static $subnets;
}
-function destSawPw($destTask, $destMachine, $passwd)
+function destSawPw(array $destTask, array $destMachine, string $passwd): bool
{
return strpos($destTask['data']['result'][$destMachine['machineuuid']]['stdout'], "passwd=$passwd") !== false;
}
@@ -36,7 +39,7 @@ function spawnDestinationListener($dstid, &$destMachine, &$destTask, &$destDeadl
$destDeadline = 0;
foreach ($destMachines as $machine) {
cron_log("Trying to use {$machine['clientip']} as listener for " . long2ip($machine['bcast']));
- $destTask = RebootControl::runScript([$machine], "echo 'Running-MARK'\nbusybox timeout -t 8 jawol -v -l", 10);
+ $destTask = RebootControl::runScript([$machine], "echo 'Running-MARK'\nbusybox timeout 8 jawol -v -l -l", 10);
Taskmanager::release($destTask);
$destDeadline = time() + 10;
if (!Taskmanager::isRunning($destTask))
@@ -82,6 +85,11 @@ function testClientToClient($srcid, $dstid)
usleep(250000);
$destTask = Taskmanager::status($destTask);
}
+ // Wait for destination listener task to finish; we might want to reuse that client,
+ // and trying to spawn a new listener while the old one is running will fail
+ for ($to = 0; $to < 30 && Taskmanager::isRunning($destTask); ++$to) {
+ usleep(250000);
+ }
cron_log($destTask['data']['result'][$destMachine['machineuuid']]['stdout']);
// Final moment: did dest see the packets from src? Determine this by looking for the generated password
if (destSawPw($destTask, $destMachine, $passwd))
@@ -108,6 +116,11 @@ function testServerToClient($dstid)
$task = Taskmanager::waitComplete($task, 2000);
$destTask = Taskmanager::status($destTask);
}
+ // Wait for destination listener task to finish; we might want to reuse that client,
+ // and trying to spawn a new listener while the old one is running will fail
+ for ($to = 0; $to < 30 && Taskmanager::isRunning($destTask); ++$to) {
+ usleep(250000);
+ }
cron_log($destTask['data']['result'][$destMachine['machineuuid']]['stdout']);
if (destSawPw($destTask, $destMachine, $passwd))
return 1;
@@ -127,7 +140,7 @@ function resultToTime($result)
$next = 86400 * 7; // a week
} else {
// Test finished, reachable
- $next = 86400 * 30; // a month
+ $next = 86400 * 14; // two weeks
}
return time() + round($next * mt_rand(90, 133) / 100);
}
@@ -137,12 +150,12 @@ function resultToTime($result)
*/
// First, cleanup: delete orphaned subnets that don't exist anymore, or don't have any clients using our server
-$cutoff = strtotime('-180 days');
+$cutoff = strtotime('-720 days');
Database::exec('DELETE FROM reboot_subnet WHERE fixed = 0 AND lastseen < :cutoff', ['cutoff' => $cutoff]);
// Get machines running, group by subnet
$cutoff = time() - 301; // Really only the ones that didn't miss the most recent update
-$res = Database::simpleQuery("SELECT s.subnetid, s.end AS bcast, m.machineuuid, m.clientip, m.macaddr
+$res = Database::simpleQuery("SELECT s.subnetid, s.end AS bcast, m.machineuuid, m.clientip, m.macaddr, m.locationid
FROM reboot_subnet s
INNER JOIN machine m ON (
(m.state = 'IDLE' OR m.state = 'OCCUPIED')
@@ -158,12 +171,13 @@ if ($res->rowCount() === 0)
return;
Stuff::$subnets = [];
-while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+foreach ($res as $row) {
if (!isset(Stuff::$subnets[$row['subnetid']])) {
Stuff::$subnets[$row['subnetid']] = [];
}
Stuff::$subnets[$row['subnetid']][] = $row;
}
+unset($res);
$task = Taskmanager::submit('DummyTask', []);
$task = Taskmanager::waitComplete($task, 4000);
@@ -181,7 +195,7 @@ $res = Database::simpleQuery("SELECT subnetid FROM reboot_subnet
WHERE subnetid IN (:active) AND nextdirectcheck < UNIX_TIMESTAMP() AND fixed = 0
ORDER BY nextdirectcheck ASC LIMIT 10", ['active' => array_keys(Stuff::$subnets)]);
cron_log('Direct checks: ' . $res->rowCount() . ' (' . implode(', ', array_keys(Stuff::$subnets)) . ')');
-while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+foreach ($res as $row) {
$dst = (int)$row['subnetid'];
cron_log('Direct check for subnetid ' . $dst);
$result = testServerToClient($dst);
@@ -201,6 +215,9 @@ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
* Try client to client
*/
+if (!Property::get(RebootControl::KEY_SCAN_CLIENT_TO_CLIENT))
+ return;
+
// Query all possible combos
$combos = [];
foreach (Stuff::$subnets as $src => $_) {
@@ -223,7 +240,7 @@ if (count($combos) > 0) {
ORDER BY sxs.nextcheck ASC
LIMIT 10", ['combos' => $combos]);
cron_log('C2C checks: ' . $res->rowCount());
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ foreach ($res as $row) {
$src = (int)$row['srcid'];
$dst = (int)$row['dstid'];
$result = testClientToClient($src, $dst);