summaryrefslogtreecommitdiffstats
path: root/modules-available/statistics/hooks
diff options
context:
space:
mode:
authorSimon Rettberg2017-11-28 16:41:33 +0100
committerSimon Rettberg2017-11-28 16:41:33 +0100
commit23d8c6fc0c3b031aa081ee1a245f9b0792514fd3 (patch)
tree59ce05e8add76d7b63213a0fa447a42565ea2e3d /modules-available/statistics/hooks
parent[dnbd3] Disable SLX_SYSTEM_STANDBY_TIMEOUT (diff)
downloadslx-admin-23d8c6fc0c3b031aa081ee1a245f9b0792514fd3.tar.gz
slx-admin-23d8c6fc0c3b031aa081ee1a245f9b0792514fd3.tar.xz
slx-admin-23d8c6fc0c3b031aa081ee1a245f9b0792514fd3.zip
[statistics] Introduce state column to get rid of complicated state determination
This also adds support for the STANDBY state
Diffstat (limited to 'modules-available/statistics/hooks')
-rw-r--r--modules-available/statistics/hooks/cron.inc.php18
1 files changed, 14 insertions, 4 deletions
diff --git a/modules-available/statistics/hooks/cron.inc.php b/modules-available/statistics/hooks/cron.inc.php
index 575ab6ba..eb88173d 100644
--- a/modules-available/statistics/hooks/cron.inc.php
+++ b/modules-available/statistics/hooks/cron.inc.php
@@ -1,18 +1,28 @@
<?php
-function logstats() {
+function logstats()
+{
$NOW = time();
$cutoff = $NOW - 86400 * 30;
- $online = $NOW - 610;
$known = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE lastseen > $cutoff");
- $on = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE lastseen > $online");
- $used = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE lastseen > $online AND logintime <> 0");
+ $on = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE state IN ('IDLE', 'OCCUPIED')");
+ $used = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE state = 'OCCUPIED'");
Database::exec("INSERT INTO statistic (dateline, typeid, clientip, username, data) VALUES (:now, '~stats', '', '', :vals)", array(
'now' => $NOW,
'vals' => $known['val'] . '#' . $on['val'] . '#' . $used['val'],
));
}
+function state_cleanup()
+{
+ // Fix online state of machines that crashed
+ $standby = time() - 86400 * 2; // Reset standby machines after two days
+ $on = time() - 610; // Reset others after ~10 minutes
+ Database::exec("UPDATE machine SET state = 'OFFLINE' WHERE lastseen < If(state = 'STANDBY', $standby, $on) AND state <> 'OFFLINE'");
+}
+
+state_cleanup();
+
logstats();
if (mt_rand(1, 10) === 1) {