diff options
-rw-r--r-- | apis/clientlog.inc.php | 151 | ||||
-rw-r--r-- | lang/de/templates/statistics/clientlist.json | 5 | ||||
-rw-r--r-- | lang/de/templates/statistics/machine-main.json | 4 | ||||
-rw-r--r-- | lang/en/templates/statistics/clientlist.json | 5 | ||||
-rw-r--r-- | lang/en/templates/statistics/machine-main.json | 4 | ||||
-rw-r--r-- | modules/statistics.inc.php | 23 | ||||
-rw-r--r-- | templates/statistics/clientlist.html | 13 | ||||
-rw-r--r-- | templates/statistics/machine-main.html | 14 |
8 files changed, 188 insertions, 31 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"); } /* diff --git a/lang/de/templates/statistics/clientlist.json b/lang/de/templates/statistics/clientlist.json index 14e21549..8d9c4cbe 100644 --- a/lang/de/templates/statistics/clientlist.json +++ b/lang/de/templates/statistics/clientlist.json @@ -4,8 +4,11 @@ "lang_cpuModel": "CPU", "lang_gbRam": "RAM", "lang_kvmSupport": "64\u2009Bit G\u00e4ste", - "lang_lastBoot": "Letzter boot", + "lang_lastSeen": "Zuletzt gesehn", "lang_machine": "Rechner", + "lang_machineIdle": "Der Rechner ist eingeschaltet und wird zur Zeit nicht benutzt", + "lang_machineOccupied": "Der Rechner ist eingeschaltet und wird benutzt", + "lang_machineOff": "Der Rechner ist ausgeschaltet, oder hat kein bwLehrpool gebootet", "lang_realCores": "Kerne", "lang_tmpGb": "HDD-Temp" }
\ No newline at end of file diff --git a/lang/de/templates/statistics/machine-main.json b/lang/de/templates/statistics/machine-main.json index a0b2182a..acf9c323 100644 --- a/lang/de/templates/statistics/machine-main.json +++ b/lang/de/templates/statistics/machine-main.json @@ -9,6 +9,9 @@ "lang_lastBoot": "Letzter Boot", "lang_lastSeen": "Letzte Aktivit\u00e4t", "lang_macAddr": "MAC-Adresse", + "lang_machineIdle": "Eingeschaltet, ungenutzt", + "lang_machineOccupied": "Eingeschaltet, in Verwendung", + "lang_machineOff": "Kein bwLehrpool gestartet", "lang_machineSummary": "Zusammenfassung", "lang_maximumAbbrev": "Max.", "lang_model": "Modell", @@ -16,6 +19,7 @@ "lang_ramSlots": "Speicher-Slots", "lang_sockets": "Sockel", "lang_tempPart": "Temp. Partition", + "lang_usageState": "Zustand", "lang_uuid": "UUID", "lang_virtualCores": "Virtuelle Kerne" }
\ No newline at end of file diff --git a/lang/en/templates/statistics/clientlist.json b/lang/en/templates/statistics/clientlist.json index 09ea04fd..ae692154 100644 --- a/lang/en/templates/statistics/clientlist.json +++ b/lang/en/templates/statistics/clientlist.json @@ -4,8 +4,11 @@ "lang_cpuModel": "CPU", "lang_gbRam": "RAM", "lang_kvmSupport": "64\u2009Bit guests", - "lang_lastBoot": "Last boot", + "lang_lastSeen": "Last seen", "lang_machine": "Machine", + "lang_machineIdle": "Machine is powered on and is not used", + "lang_machineOccupied": "Machine is powered on and in use", + "lang_machineOff": "Machine is powered down, or is not running bwLehrpool", "lang_realCores": "Cores", "lang_tmpGb": "HDD temp" }
\ No newline at end of file diff --git a/lang/en/templates/statistics/machine-main.json b/lang/en/templates/statistics/machine-main.json index 40ea72f8..ff1e481f 100644 --- a/lang/en/templates/statistics/machine-main.json +++ b/lang/en/templates/statistics/machine-main.json @@ -9,6 +9,9 @@ "lang_lastBoot": "Last boot", "lang_lastSeen": "Last activity", "lang_macAddr": "MAC address", + "lang_machineIdle": "Powered on, unused", + "lang_machineOccupied": "Powered on, in use", + "lang_machineOff": "bwLehrpool not running", "lang_machineSummary": "Summary", "lang_maximumAbbrev": "max.", "lang_model": "Model", @@ -16,6 +19,7 @@ "lang_ramSlots": "Memory slots", "lang_sockets": "Sockets", "lang_tempPart": "Temp. partition", + "lang_usageState": "State", "lang_uuid": "UUID", "lang_virtualCores": "Virtual cores" }
\ No newline at end of file diff --git a/modules/statistics.inc.php b/modules/statistics.inc.php index 3e829847..16161d9f 100644 --- a/modules/statistics.inc.php +++ b/modules/statistics.inc.php @@ -273,10 +273,18 @@ class Page_Statistics extends Page . " logintime, lastboot, realcores, mbram, kvmstate, cpumodel, id44mb, hostname, notes IS NOT NULL AS hasnotes FROM machine" . " WHERE $where ORDER BY lastseen DESC, clientip ASC", $args); $rows = array(); + $NOW = time(); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - $row['firstseen'] = date('d.m.Y H:i', $row['firstseen']); + if ($NOW - $row['lastseen'] > 610) { + $row['state_off'] = true; + } elseif ($NOW - $row['logintime'] > 610) { + $row['state_idle'] = true; + } else { + $row['state_occupied'] = true; + } + //$row['firstseen'] = date('d.m.Y H:i', $row['firstseen']); $row['lastseen'] = date('d.m. H:i', $row['lastseen']); - $row['lastboot'] = date('d.m. H:i', $row['lastboot']); + //$row['lastboot'] = date('d.m. H:i', $row['lastboot']); $row['gbram'] = round(round($row['mbram'] / 500) / 2, 1); // Trial and error until we got "expected" rounding.. $row['gbtmp'] = round($row['id44mb'] / 1024); $octets = explode('.', $row['clientip']); @@ -346,6 +354,14 @@ class Page_Statistics extends Page . " mbram, kvmstate, cpumodel, id44mb, data, hostname, notes FROM machine WHERE machineuuid = :uuid", array('uuid' => $uuid)); // Mangle fields + $NOW = time(); + if ($NOW - $row['lastseen'] > 610) { + $row['state_off'] = true; + } elseif ($NOW - $row['logintime'] > 610) { + $row['state_idle'] = true; + } else { + $row['state_occupied'] = true; + } $row['firstseen'] = date('d.m.Y H:i', $row['firstseen']); $row['lastseen'] = date('d.m.Y H:i', $row['lastseen']); $row['lastboot'] = date('d.m.Y H:i', $row['lastboot']); @@ -394,6 +410,7 @@ class Page_Statistics extends Page $ramOk = false; $ramForm = $ramType = $ramSpeed = $ramClockSpeed = false; foreach ($lines as $line) { + if (empty($line)) continue; if ($line{0} !== "\t" && $line{0} !== ' ') { $section = $line; $ramOk = false; @@ -441,7 +458,7 @@ class Page_Statistics extends Page $out[1] = floor(($out[1] + 100) / 1024); } $row['ramslot'][]['size'] = $out[1] . ' ' . strtoupper($out[2]) . 'iB'; - } else if (count($row['ramslot']) < 8 && (!isset($row['ramslotcount']) || $row['ramslotcount'] <= 8)) { + } else if (!isset($row['ramslot']) || (count($row['ramslot']) < 8 && (!isset($row['ramslotcount']) || $row['ramslotcount'] <= 8))) { $row['ramslot'][]['size'] = '_____'; } } diff --git a/templates/statistics/clientlist.html b/templates/statistics/clientlist.html index 879e27c4..eb3c6bd4 100644 --- a/templates/statistics/clientlist.html +++ b/templates/statistics/clientlist.html @@ -6,7 +6,7 @@ <tr> <th>{{lang_machine}}</th> <th>{{lang_address}}</th> - <th class="text-right">{{lang_lastBoot}}</th> + <th class="text-right">{{lang_lastSeen}}</th> <th>{{lang_kvmSupport}}</th> <th class="text-right">{{lang_gbRam}}</th> <th class="text-right">{{lang_tmpGb}}</th> @@ -16,11 +16,20 @@ <tr> <td class="slx-nowrap"> {{#hasnotes}}<span class="glyphicon glyphicon-exclamation-sign pull-right"></span>{{/hasnotes}} + {{#state_off}} + <span class="glyphicon glyphicon-off" title="{{lang_machineOff}}"></span> + {{/state_off}} + {{#state_idle}} + <span class="glyphicon glyphicon-ok green" title="{{lang_machineIdle}}"></span> + {{/state_idle}} + {{#state_occupied}} + <span class="glyphicon glyphicon-user red" title="{{lang_machineOccupied}}"></span> + {{/state_occupied}} <a href="?do=Statistics&uuid={{machineuuid}}"><b>{{hostname}}</b></a> <div class="small">{{machineuuid}}</div> </td> <td><b><a href="?do=Statistics&filter=subnet&argument={{subnet}}">{{subnet}}</a>{{lastoctet}}</b><br>{{macaddr}}</td> - <td class="text-right">{{lastboot}}</td> + <td class="text-right">{{lastseen}}</td> <td class="{{kvmclass}}">{{kvmstate}}</td> <td class="text-right {{ramclass}}">{{gbram}} GiB</td> <td class="text-right {{hddclass}}">{{gbtmp}} GiB</td> diff --git a/templates/statistics/machine-main.html b/templates/statistics/machine-main.html index 52e0aee8..03e95381 100644 --- a/templates/statistics/machine-main.html +++ b/templates/statistics/machine-main.html @@ -41,6 +41,20 @@ <td>{{lang_lastSeen}}</td> <td>{{lastseen}}</td> </tr> + <tr> + <td>{{lang_usageState}}</td> + <td> + {{#state_off}} + <span class="glyphicon glyphicon-off"></span> {{lang_machineOff}} + {{/state_off}} + {{#state_idle}} + <span class="glyphicon glyphicon-ok green"></span> {{lang_machineIdle}} + {{/state_idle}} + {{#state_occupied}} + <span class="glyphicon glyphicon-user red"></span> {{lang_machineOccupied}} + {{/state_occupied}} + </td> + </tr> </table> </div> </div> |