diff options
author | Simon Rettberg | 2015-12-07 17:41:42 +0100 |
---|---|---|
committer | Simon Rettberg | 2015-12-07 17:41:42 +0100 |
commit | d2202c4b1b365ad6d318352950ee3331846c6a11 (patch) | |
tree | cc801702fbcd91385139d554ccb625ef7e30a34e /apis | |
parent | [statistics] More tweaks, add detailed memory information, show system manufa... (diff) | |
download | slx-admin-d2202c4b1b365ad6d318352950ee3331846c6a11.tar.gz slx-admin-d2202c4b1b365ad6d318352950ee3331846c6a11.tar.xz slx-admin-d2202c4b1b365ad6d318352950ee3331846c6a11.zip |
[statistics] Show PC status (on/off/inuse)
Diffstat (limited to 'apis')
-rw-r--r-- | apis/clientlog.inc.php | 151 |
1 files changed, 127 insertions, 24 deletions
diff --git a/apis/clientlog.inc.php b/apis/clientlog.inc.php index 3f1e02ea..01a470f0 100644 --- a/apis/clientlog.inc.php +++ b/apis/clientlog.inc.php @@ -11,45 +11,84 @@ if (substr($ip, 0, 7) === '::ffff:') $ip = substr($ip, 7); */ if ($type{0} === '~') { + // UUID is mandatory $uuid = Request::post('uuid', '', 'string'); - $NOW = time(); - if ($type === '~poweron') { - $macaddr = Request::post('macaddr', '', 'string'); - $uptime = Request::post('uptime', '', 'integer'); if (strlen($uuid) !== 36) die("Invalid UUID.\n"); - if (strlen($macaddr) > 17) die("Invalid MAC.\n"); - if ($uptime < 0 || $uptime > 4000000) die("Implausible uptime.\n"); - $realcores = Request::post('realcores', 0, 'integer'); - if ($realcores < 0 || $realcores > 512) $realcores = 0; - $mbram = Request::post('mbram', 0, 'integer'); - if ($mbram < 0 || $mbram > 102400) $mbram = 0; - $kvmstate = Request::post('kvmstate', 'UNKNOWN', 'string'); - $valid = array('UNKNOWN', 'UNSUPPORTED', 'DISABLED', 'ENABLED'); - if (!in_array($kvmstate, $valid)) $kvmstate = 'UNKNOWN'; - $cpumodel = Request::post('cpumodel', '', 'string'); - $id44mb = Request::post('id44mb', 0, 'integer'); - if ($id44mb < 0 || $id44mb > 10240000) $id44mb = 0; - $hostname = gethostbyaddr($ip); - if (!is_string($hostname) || $hostname === $ip) { - $hostname = ''; + $macaddr = Request::post('macaddr', '', 'string'); + if (!empty($macaddr) && substr($uuid, 0, 16) === '000000000000000-') { + // Override uuid if the mac is known and unique + $res = Database::simpleQuery('SELECT machineuuid FROM machine WHERE macaddr = :macaddr', array('macaddr' => $macaddr)); + $override = false; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + if ($override !== false) { + $override = false; + break; + } + $override = $row['machineuuid']; + } + if ($override !== false) { + $uuid = $override; + } } - $data = Request::post('data', '', 'string'); + $NOW = time(); + // Handle event type + if ($type === '~poweron') { + // Poweron & hw stats + $uptime = Request::post('uptime', '', 'integer'); + if (strlen($macaddr) > 17) die("Invalid MAC.\n"); + if ($uptime < 0 || $uptime > 4000000) die("Implausible uptime.\n"); + $realcores = Request::post('realcores', 0, 'integer'); + if ($realcores < 0 || $realcores > 512) $realcores = 0; + $mbram = Request::post('mbram', 0, 'integer'); + if ($mbram < 0 || $mbram > 102400) $mbram = 0; + $kvmstate = Request::post('kvmstate', 'UNKNOWN', 'string'); + $valid = array('UNKNOWN', 'UNSUPPORTED', 'DISABLED', 'ENABLED'); + if (!in_array($kvmstate, $valid)) $kvmstate = 'UNKNOWN'; + $cpumodel = Request::post('cpumodel', '', 'string'); + $id44mb = Request::post('id44mb', 0, 'integer'); + if ($id44mb < 0 || $id44mb > 10240000) $id44mb = 0; + $badsectors = Request::post('badsectors', 0, 'integer'); + if ($badsectors < 0 || $badsectors > 100000) $badsectors = 0; + $hostname = gethostbyaddr($ip); + if (!is_string($hostname) || $hostname === $ip) { + $hostname = ''; + } + $data = Request::post('data', '', 'string'); + // See if we have a lingering session, create statistic entry if so + if ($uptime < 120) { + $old = Database::queryFirst('SELECT logintime, lastseen FROM machine WHERE machineuuid = :uuid', array('uuid' => $uuid)); + if ($old !== false && (int)$old['logintime'] !== 0) { + $sessionLength = $old['lastseen'] - $old['logintime']; + if ($sessionLength > 0) { + $start = $old['logintime']; + if ($start === 0) $start = $NOW; + Database::exec('INSERT INTO statistic (dateline, typeid, clientip, username, data)' + . " VALUES (:start, '.session-length', :clientip, '', :length)", array( + 'start' => $start, + 'clientip' => $ip, + 'length' => $sessionLength + )); + } + } + } + // Create/update machine entry Database::exec('INSERT INTO machine ' . '(machineuuid, macaddr, clientip, firstseen, lastseen, logintime, position, lastboot, realcores, mbram,' - . ' kvmstate, cpumodel, id44mb, data, hostname) VALUES ' + . ' kvmstate, cpumodel, id44mb, badsectors, data, hostname) VALUES ' . "(:uuid, :macaddr, :clientip, :firstseen, :lastseen, 0, '', :lastboot, :realcores, :mbram," - . ' :kvmstate, :cpumodel, :id44mb, :data, :hostname)' + . ' :kvmstate, :cpumodel, :id44mb, :badsectors, :data, :hostname)' . ' ON DUPLICATE KEY UPDATE' . ' macaddr = VALUES(macaddr),' . ' clientip = VALUES(clientip),' . ' lastseen = VALUES(lastseen),' - . ' logintime = 0,' + . ($uptime < 120 ? ' logintime = 0,' : '') . ' lastboot = VALUES(lastboot),' . ' realcores = VALUES(realcores),' . ' mbram = VALUES(mbram),' . ' kvmstate = VALUES(kvmstate),' . ' cpumodel = VALUES(cpumodel),' . ' id44mb = VALUES(id44mb),' + . ' badsectors = VALUES(badsectors),' . ' data = VALUES(data),' . " hostname = If(VALUES(hostname) = '', hostname, VALUES(hostname))", array( 'uuid' => $uuid, @@ -63,11 +102,75 @@ if ($type{0} === '~') { 'kvmstate' => $kvmstate, 'cpumodel' => $cpumodel, 'id44mb' => $id44mb, + 'badsectors' => $badsectors, 'data' => $data, 'hostname' => $hostname, )); + } else if ($type === '~runstate') { + // Usage (occupied/free) + $sessionLength = 0; + $used = Request::post('used', 0, 'integer'); + $old = Database::queryFirst('SELECT logintime, lastseen FROM machine WHERE machineuuid = :uuid', array('uuid' => $uuid)); + if ($old === false) die("Unknown machine.\n"); + settype($old['logintime'], 'integer'); + settype($old['lastseen'], 'integer'); + // Figure out what's happening + if ($used === 0) { + // Is not in use + 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 + $sessionLength = $old['lastseen'] - $old['logintime']; + } else { + $sessionLength = $NOW - $old['logintime']; + } + } + 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']; + } + 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)); + } + } + // 9) Log last session length if applicable + if ($sessionLength > 0) { + $start = $old['logintime']; + if ($start === 0) $start = $NOW; + Database::exec('INSERT INTO statistic (dateline, typeid, clientip, username, data)' + . " VALUES (:start, '.session-length', :clientip, '', :length)", array( + 'start' => $start, + 'clientip' => $ip, + 'length' => $sessionLength + )); + } + } elseif ($type === '~poweroff') { + $old = Database::queryFirst('SELECT logintime, lastseen FROM machine WHERE machineuuid = :uuid', array('uuid' => $uuid)); + if ($old !== false && (int)$old['logintime'] !== 0) { + $sessionLength = $old['lastseen'] - $old['logintime']; + if ($sessionLength > 0) { + $start = $old['logintime']; + if ($start === 0) $start = $NOW; + Database::exec('INSERT INTO statistic (dateline, typeid, clientip, username, data)' + . " VALUES (:start, '.session-length', :clientip, '', :length)", array( + 'start' => $start, + 'clientip' => $ip, + 'length' => $sessionLength + )); + } + } + Database::exec('UPDATE machine SET logintime = 0, lastseen = UNIX_TIMESTAMP() - 600 WHERE machineuuid = :uuid', array('uuid' => $uuid)); } - die("OK.\n"); + die("OK. (RESULT=0)\n"); } /* |