summaryrefslogtreecommitdiffstats
path: root/modules-available/statistics
diff options
context:
space:
mode:
authorUdo Walter2017-11-21 17:52:04 +0100
committerUdo Walter2017-11-21 17:52:04 +0100
commit653851f35d0eea172c2302e9f1b6f0d03c70096c (patch)
tree42f267c360104b98d2e3199273adba62a1e2023b /modules-available/statistics
parent[internetaccess] reworked permission system from "click and you get error" to... (diff)
parent[statistics] Also adapt MAC-UUID fixing code to mltk changes (diff)
downloadslx-admin-653851f35d0eea172c2302e9f1b6f0d03c70096c.tar.gz
slx-admin-653851f35d0eea172c2302e9f1b6f0d03c70096c.tar.xz
slx-admin-653851f35d0eea172c2302e9f1b6f0d03c70096c.zip
Merge remote-tracking branch 'origin/master' into permission-manager
# Conflicts: # modules-available/backup/templates/_page.html # style/default.css
Diffstat (limited to 'modules-available/statistics')
-rw-r--r--modules-available/statistics/api.inc.php71
-rw-r--r--modules-available/statistics/hooks/cron.inc.php17
-rw-r--r--modules-available/statistics/lang/de/template-tags.json3
-rw-r--r--modules-available/statistics/lang/en/template-tags.json3
-rw-r--r--modules-available/statistics/page.inc.php21
-rw-r--r--modules-available/statistics/templates/filterbox.html3
6 files changed, 84 insertions, 34 deletions
diff --git a/modules-available/statistics/api.inc.php b/modules-available/statistics/api.inc.php
index 126c6e91..c395220a 100644
--- a/modules-available/statistics/api.inc.php
+++ b/modules-available/statistics/api.inc.php
@@ -16,7 +16,7 @@ if ($type{0} === '~') {
$uuid = Request::post('uuid', '', 'string');
if (strlen($uuid) !== 36) die("Invalid UUID.\n");
$macaddr = Request::post('macaddr', '', 'string');
- if (!empty($macaddr) && substr($uuid, 0, 16) === '000000000000000-') {
+ if (!empty($macaddr) && substr($uuid, 0, 16) === '000000000000001-') {
// Override uuid if the mac is known and unique
$res = Database::simpleQuery('SELECT machineuuid FROM machine WHERE macaddr = :macaddr AND machineuuid <> :uuid', compact('macaddr', 'uuid'));
$override = false;
@@ -31,14 +31,17 @@ if ($type{0} === '~') {
$uuid = $override;
}
}
+ // External mode of operation?
+ $mode = Request::post('mode', false, 'string');
$NOW = time();
- $old = Database::queryFirst('SELECT clientip, logintime, lastseen FROM machine WHERE machineuuid = :uuid', array('uuid' => $uuid));
+ $old = Database::queryFirst('SELECT clientip, logintime, lastseen, lastboot FROM machine WHERE machineuuid = :uuid', array('uuid' => $uuid));
if ($old !== false) {
settype($old['logintime'], 'integer');
settype($old['lastseen'], 'integer');
+ settype($old['lastboot'], 'integer');
}
// Handle event type
- if ($type === '~poweron') {
+ if ($mode === false && $type === '~poweron') {
// Poweron & hw stats
$uptime = Request::post('uptime', '', 'integer');
if (strlen($macaddr) > 17) die("Invalid MAC.\n");
@@ -138,42 +141,54 @@ if ($type{0} === '~') {
} else if ($type === '~runstate') {
// Usage (occupied/free)
$sessionLength = 0;
- $used = Request::post('used', 0, 'integer');
if ($old === false) die("Unknown machine.\n");
if ($old['clientip'] !== $ip) {
EventLog::warning("[runstate] IP address of client $uuid seems to have changed ({$old['clientip']} -> $ip)");
die("Address changed.\n");
}
- // Figure out what's happening
- if ($used === 0) {
- // Is not in use
+ $used = Request::post('used', 0, 'integer');
+ if ($old['lastboot'] === 0 && $NOW - $old['lastseen'] > 300) {
+ $strUpdateBoottime = ' lastboot = UNIX_TIMESTAMP(), ';
+ } else {
+ $strUpdateBoottime = '';
+ }
+ // 1) Log last session length if we didn't see the machine for a while
+ if ($NOW - $old['lastseen'] > 610 && $old['lastseen'] !== 0) {
+ // Old session timed out - might be caused by hard reboot
if ($old['logintime'] !== 0) {
- // Was in use, is free now
- // 1) Log last session length
- if ($NOW - $old['lastseen'] > 610) {
- // Old session timed out - might be caused by hard reboot
+ if ($old['lastseen'] > $old['logintime']) {
$sessionLength = $old['lastseen'] - $old['logintime'];
- } else {
- $sessionLength = $NOW - $old['logintime'];
}
+ $old['logintime'] = 0;
}
- Database::exec('UPDATE machine SET lastseen = UNIX_TIMESTAMP(), logintime = 0 WHERE machineuuid = :uuid', array('uuid' => $uuid));
- } else {
- // Machine is in use
- if ($old['logintime'] !== 0 && $NOW - $old['lastseen'] > 610) {
- // Old session timed out - might be caused by hard reboot
- $sessionLength = $old['lastseen'] - $old['logintime'];
- }
+ $old['lastboot'] = 0;
+ }
+ // Figure out what's happening - state changes
+ if ($used === 0 && $old['logintime'] !== 0) {
+ // Is not in use, was in use before
+ $sessionLength = $NOW - $old['logintime'];
+ Database::exec('UPDATE machine SET lastseen = UNIX_TIMESTAMP(),'
+ . $strUpdateBoottime
+ . ' logintime = 0, currentuser = NULL WHERE machineuuid = :uuid', array('uuid' => $uuid));
+ } elseif ($used === 1 && $old['logintime'] === 0) {
+ // Machine is in use, was free before
if ($sessionLength !== 0 || $old['logintime'] === 0) {
// This event is a start of a new session, rather than an update
- Database::exec('UPDATE machine SET lastseen = UNIX_TIMESTAMP(), logintime = UNIX_TIMESTAMP() WHERE machineuuid = :uuid', array('uuid' => $uuid));
- } else {
- // Nothing changed, simple lastseen update
- Database::exec('UPDATE machine SET lastseen = UNIX_TIMESTAMP() WHERE machineuuid = :uuid', array('uuid' => $uuid));
+ Database::exec('UPDATE machine SET lastseen = UNIX_TIMESTAMP(),'
+ . $strUpdateBoottime
+ . ' logintime = UNIX_TIMESTAMP(), currentuser = :user WHERE machineuuid = :uuid', array(
+ 'uuid' => $uuid,
+ 'user' => Request::post('user', null, 'string'),
+ ));
}
+ } else {
+ // Nothing changed, simple lastseen update
+ Database::exec('UPDATE machine SET '
+ . $strUpdateBoottime
+ . ' lastseen = UNIX_TIMESTAMP() WHERE machineuuid = :uuid', array('uuid' => $uuid));
}
// 9) Log last session length if applicable
- if ($sessionLength > 0 && $sessionLength < 86400*2 && $old['logintime'] !== 0) {
+ if ($mode === false && $sessionLength > 0 && $sessionLength < 86400*2 && $old['logintime'] !== 0) {
Database::exec('INSERT INTO statistic (dateline, typeid, machineuuid, clientip, username, data)'
. " VALUES (:start, '~session-length', :uuid, :clientip, '', :length)", array(
'start' => $old['logintime'],
@@ -188,7 +203,7 @@ if ($type{0} === '~') {
EventLog::warning("[poweroff] IP address of client $uuid seems to have changed ({$old['clientip']} -> $ip)");
die("Address changed.\n");
}
- if ($old['logintime'] !== 0) {
+ if ($mode === false && $old['logintime'] !== 0) {
$sessionLength = $old['lastseen'] - $old['logintime'];
if ($sessionLength > 0 && $sessionLength < 86400*2) {
Database::exec('INSERT INTO statistic (dateline, typeid, machineuuid, clientip, username, data)'
@@ -201,7 +216,7 @@ if ($type{0} === '~') {
}
}
Database::exec('UPDATE machine SET logintime = 0, lastseen = UNIX_TIMESTAMP(), lastboot = 0 WHERE machineuuid = :uuid', array('uuid' => $uuid));
- } elseif ($type === '~screens') {
+ } elseif ($mode === false && $type === '~screens') {
$screens = Request::post('screen', false, 'array');
if (is_array($screens)) {
// `devicetype`, `devicename`, `subid`, `machineuuid`
@@ -273,7 +288,7 @@ if ($type{0} === '~') {
}
}
} else {
- die("INVALID ACTION '$type'");
+ die("INVALID ACTION '$type'\n");
}
die("OK. (RESULT=0)\n");
}
diff --git a/modules-available/statistics/hooks/cron.inc.php b/modules-available/statistics/hooks/cron.inc.php
index 94c65248..575ab6ba 100644
--- a/modules-available/statistics/hooks/cron.inc.php
+++ b/modules-available/statistics/hooks/cron.inc.php
@@ -1,8 +1,5 @@
<?php
-Database::exec("DELETE FROM statistic WHERE (UNIX_TIMESTAMP() - dateline) > 86400 * 190");
-Database::exec("DELETE FROM machine WHERE (UNIX_TIMESTAMP() - lastseen) > 86400 * 365");
-
function logstats() {
$NOW = time();
$cutoff = $NOW - 86400 * 30;
@@ -15,4 +12,18 @@ function logstats() {
'vals' => $known['val'] . '#' . $on['val'] . '#' . $used['val'],
));
}
+
logstats();
+
+if (mt_rand(1, 10) === 1) {
+ Database::exec("DELETE FROM statistic WHERE (UNIX_TIMESTAMP() - 86400 * 190) > dateline");
+ if (mt_rand(1, 100) === 1) {
+ Database::exec("OPTIMIZE TABLE statistic");
+ }
+}
+if (mt_rand(1, 10) === 1) {
+ Database::exec("DELETE FROM machine WHERE (UNIX_TIMESTAMP() - 86400 * 365) > lastseen");
+ if (mt_rand(1, 100) === 1) {
+ Database::exec("OPTIMIZE TABLE machine");
+ }
+}
diff --git a/modules-available/statistics/lang/de/template-tags.json b/modules-available/statistics/lang/de/template-tags.json
index ab22ee86..474d952a 100644
--- a/modules-available/statistics/lang/de/template-tags.json
+++ b/modules-available/statistics/lang/de/template-tags.json
@@ -70,6 +70,7 @@
"lang_showList": "Liste",
"lang_showVisualization": "Visualisierung",
"lang_sockets": "Sockel",
+ "lang_subnet": "Subnetz",
"lang_tempPart": "Temp. Partition",
"lang_tempPartStats": "Tempor\u00e4re Partition",
"lang_thoseAreProjectors": "Diese Modellnamen werden als Beamer behandelt, auch wenn die EDID-Informationen des Ger\u00e4tes anderes berichten.",
@@ -82,4 +83,4 @@
"lang_virtualCores": "Virtuelle Kerne",
"lang_when": "Wann",
"lang_withBadSectors": "Clients mit potentiell defekten Festplatten (mehr als 10 defekte Sektoren)"
-} \ No newline at end of file
+}
diff --git a/modules-available/statistics/lang/en/template-tags.json b/modules-available/statistics/lang/en/template-tags.json
index 6597d953..f514b894 100644
--- a/modules-available/statistics/lang/en/template-tags.json
+++ b/modules-available/statistics/lang/en/template-tags.json
@@ -70,6 +70,7 @@
"lang_showList": "List",
"lang_showVisualization": "Visualization",
"lang_sockets": "Sockets",
+ "lang_subnet": "Subnet",
"lang_tempPart": "Temp. partition",
"lang_tempPartStats": "Temporary partition",
"lang_thoseAreProjectors": "These model names will always be treated as beamers, even if the device's EDID data says otherwise.",
@@ -82,4 +83,4 @@
"lang_virtualCores": "Virtual cores",
"lang_when": "When",
"lang_withBadSectors": "Clients with potentially bad HDDs (more than 10 reallocated sectors)"
-} \ No newline at end of file
+}
diff --git a/modules-available/statistics/page.inc.php b/modules-available/statistics/page.inc.php
index dd3e5ee6..a003a303 100644
--- a/modules-available/statistics/page.inc.php
+++ b/modules-available/statistics/page.inc.php
@@ -308,6 +308,14 @@ class Page_Statistics extends Page
}
}
+ private function redirectFirst($where, $join, $args)
+ {
+ $res = Database::queryFirst("SELECT machineuuid FROM machine $join WHERE ($where) LIMIT 1", $args);
+ if ($res !== false) {
+ Util::redirect('?do=statistics&uuid=' . $res['machineuuid']);
+ }
+ }
+
/**
* @param \FilterSet $filterSet
*/
@@ -316,6 +324,10 @@ class Page_Statistics extends Page
$filterSet->makeFragments($where, $join, $sort, $args);
$known = Database::queryFirst("SELECT Count(*) AS val FROM machine $join WHERE ($where)", $args);
+ // If we only have one machine, redirect to machine details
+ if ($known['val'] == 1) {
+ $this->redirectFirst($where, $join, $args);
+ }
$on = Database::queryFirst("SELECT Count(*) AS val FROM machine $join WHERE lastboot <> 0 AND ($where)", $args);
$used = Database::queryFirst("SELECT Count(*) AS val FROM machine $join WHERE lastboot <> 0 AND logintime <> 0 AND ($where)", $args);
$hdd = Database::queryFirst("SELECT Count(*) AS val FROM machine $join WHERE badsectors >= 10 AND ($where)", $args);
@@ -566,7 +578,13 @@ class Page_Statistics extends Page
. " $join WHERE $where $sort", $args);
$rows = array();
$NOW = time();
+ $singleMachine = 'none';
while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ if ($singleMachine === 'none') {
+ $singleMachine = $row['machineuuid'];
+ } else {
+ $singleMachine = false;
+ }
if ($row['lastboot'] == 0) {
$row['state_off'] = true;
} elseif ($row['logintime'] == 0) {
@@ -598,6 +616,9 @@ class Page_Statistics extends Page
}
$rows[] = $row;
}
+ if ($singleMachine !== false && $singleMachine !== 'none') {
+ Util::redirect('?do=statistics&uuid=' . $singleMachine);
+ }
Render::addTemplate('clientlist', array(
'rowCount' => count($rows),
'rows' => $rows,
diff --git a/modules-available/statistics/templates/filterbox.html b/modules-available/statistics/templates/filterbox.html
index 4bbc1d82..c2630ed9 100644
--- a/modules-available/statistics/templates/filterbox.html
+++ b/modules-available/statistics/templates/filterbox.html
@@ -98,7 +98,8 @@ var slxFilterNames = {
clientip: '{{lang_ip}}',
state: '{{lang_usageState}}',
location: '{{lang_location}}',
- currentuser: '{{lang_currentUser}}'
+ currentuser: '{{lang_currentUser}}',
+ subnet: '{{lang_subnet}}'
};
slxLocations = {{{locations}}};