summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--modules-available/locations/templates/location-subnets.html4
-rw-r--r--modules-available/locations/templates/locations.html4
-rw-r--r--modules-available/statistics/config.json2
-rw-r--r--modules-available/statistics/inc/filter.inc.php205
-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.php2025
-rw-r--r--modules-available/statistics/templates/clientlist.html264
-rw-r--r--modules-available/statistics/templates/cpumodels.html4
-rw-r--r--modules-available/statistics/templates/id44.html4
-rw-r--r--modules-available/statistics/templates/kvmstate.html4
-rw-r--r--modules-available/statistics/templates/memory.html4
-rw-r--r--modules-available/statistics/templates/summary.html6
13 files changed, 1602 insertions, 930 deletions
diff --git a/modules-available/locations/templates/location-subnets.html b/modules-available/locations/templates/location-subnets.html
index 23035002..a7afaa35 100644
--- a/modules-available/locations/templates/location-subnets.html
+++ b/modules-available/locations/templates/location-subnets.html
@@ -70,8 +70,8 @@
{{/haveDozmod}}
{{#haveStatistics}}
<div>
- <span class="slx-ga2">{{lang_matchingMachines}}:</span> <a href="?do=Statistics&amp;filter=location&amp;argument={{locationid}}">{{machines}} / {{machines_online}} / {{machines_used}} ({{used_percent}}%)</a>
+ <span class="slx-ga2">{{lang_matchingMachines}}:</span> <a href="?do=Statistics&amp;show=list&amp;filters=location={{locationid}}">{{machines}} / {{machines_online}} / {{machines_used}} ({{used_percent}}%)</a>
</div>
{{/haveStatistics}}
</form>
-</div> \ No newline at end of file
+</div>
diff --git a/modules-available/locations/templates/locations.html b/modules-available/locations/templates/locations.html
index 1fb60926..bb13131e 100644
--- a/modules-available/locations/templates/locations.html
+++ b/modules-available/locations/templates/locations.html
@@ -25,7 +25,7 @@
<td class="slx-nowrap" align="right">
{{#havestatistics}}
{{clientCount}}
- <a class="btn btn-default btn-xs" href="?do=Statistics&amp;filter=location&amp;argument={{locationid}}"><span class="glyphicon glyphicon-eye-open"></span></a>
+ <a class="btn btn-default btn-xs" href="?do=Statistics&amp;show=list&amp;filters=location={{locationid}}"><span class="glyphicon glyphicon-eye-open"></span></a>
{{/havestatistics}}
</td>
<td class="slx-nowrap">
@@ -55,7 +55,7 @@
<td>{{lang_unassignedMachines}}</td>
<td class="slx-nowrap" align="right">
{{unassignedCount}}
- <a class="btn btn-default btn-xs" href="?do=Statistics&amp;filter=location&amp;argument=0">
+ <a class="btn btn-default btn-xs" href="?do=Statistics&amp;show=list&amp;filters=location=0">
<span class="glyphicon glyphicon-eye-open"></span>
</a>
</td>
diff --git a/modules-available/statistics/config.json b/modules-available/statistics/config.json
index ab71ddbd..333f881a 100644
--- a/modules-available/statistics/config.json
+++ b/modules-available/statistics/config.json
@@ -1,5 +1,5 @@
{
"category":"main.status",
- "dependencies": [ "js_chart" ],
+ "dependencies": [ "js_chart", "js_selectize", "bootstrap_datepicker"],
"permission":"0"
}
diff --git a/modules-available/statistics/inc/filter.inc.php b/modules-available/statistics/inc/filter.inc.php
new file mode 100644
index 00000000..0f1a0d20
--- /dev/null
+++ b/modules-available/statistics/inc/filter.inc.php
@@ -0,0 +1,205 @@
+<?php
+
+/* base class with rudimentary SQL generation abilities.
+ * WARNING: argument is escaped, but $column and $operator are passed unfiltered into SQL */
+class Filter
+{
+ public $column;
+ public $operator;
+ public $argument;
+ public function __construct($column, $operator, $argument = null)
+ {
+ $this->column = trim($column);
+ $this->operator = trim($operator);
+ $this->argument = trim($argument);
+ }
+ /* returns a where clause and adds needed operators to the passed array */
+ public function whereClause(&$args, &$joins)
+ {
+ global $unique_key;
+ $key = $this->column.'_arg' . ($unique_key++);
+
+ /* check if we have to do some parsing*/
+ if (Page_Statistics::$columns[$this->column]['type'] == 'date') {
+ $args[$key] = strtotime($this->argument);
+ } else {
+ $args[$key] = $this->argument;
+ }
+
+ $op = $this->operator;
+ if ($this->operator == '~') {
+ $op = 'LIKE';
+ } elseif ($this->operator == '!~') {
+ $op = 'NOT LIKE';
+ }
+
+ return $this->column.' '.$op.' :'.$key;
+ }
+ /* parse a query into an array of filters */
+ public static function parseQuery($query)
+ {
+ $operators = ['<=', '>=', '!=', '!~', '=', '~', '<', '>'];
+ $filters = [];
+ foreach (explode(',', $query) as $q) {
+ $q = trim($q);
+ /* find position of first operator */
+ $pos = 10000;
+ $operator;
+ foreach ($operators as $op) {
+ $newpos = strpos($q, $op);
+ if ($newpos > -1 && ($newpos < $pos)) {
+ $pos = $newpos;
+ $operator = $op;
+ }
+ }
+ if ($pos == 10000) {
+ error_log("couldn't find operator in segment ".$q);
+ /* TODO */
+ continue;
+ }
+ $lhs = trim(substr($q, 0, $pos));
+ $rhs = trim(substr($q, $pos + strlen($operator)));
+
+ if ($lhs == 'gbram') {
+ $filters[] = new RamGbFilter($operator, $rhs);
+ } elseif ($lhs == 'state') {
+ error_log('new state filter with ' . $rhs);
+ $filters[] = new StateFilter($operator, $rhs);
+ } elseif ($lhs == 'hddgb') {
+ $filters[] = new Id44Filter($operator, $rhs);
+ } elseif ($lhs == 'location') {
+ $filters[] = new LocationFilter($operator, $rhs);
+ } elseif ($lhs == 'subnet') {
+ $filters[] = new SubnetFilter($operator, $rhs);
+ } else {
+ if (array_key_exists($lhs, Page_Statistics::$columns) && Page_Statistics::$columns[$lhs]['column']) {
+ $filters[] = new Filter($lhs, $operator, $rhs);
+ } else {
+ Message::addError('invalid-filter');
+ }
+ }
+ }
+
+ return $filters;
+ }
+}
+
+class RamGbFilter extends Filter
+{
+ public function __construct($operator, $argument)
+ {
+ parent::__construct('mbram', $operator, $argument);
+ }
+ public function whereClause(&$args, &$joins)
+ {
+ global $SIZE_RAM;
+ $lower = floor(Page_Statistics::findBestValue($SIZE_RAM, (int) $this->argument, false) * 1024 - 100);
+ $upper = ceil(Page_Statistics::findBestValue($SIZE_RAM, (int) $this->argument, true) * 1024 + 100);
+ if ($this->operator == '=') {
+ return " mbram BETWEEN $lower AND $upper";
+ } elseif ($this->operator == '<') {
+ return " mbram < $lower";
+ } elseif ($this->operator == '<=') {
+ return " mbram <= $upper";
+ } elseif ($this->operator == '>') {
+ return " mbram > $upper";
+ } elseif ($this->operator == '>=') {
+ return " mbram >= $lower";
+ } elseif ($this->operator == '!=') {
+ return " (mbram < $lower OR mbram > $upper)";
+ } else {
+ error_log("unimplemented operator in RamGbFilter: $this->operator");
+
+ return ' 1';
+ }
+ }
+}
+class Id44Filter extends Filter
+{
+ public function __construct($operator, $argument)
+ {
+ parent::__construct('id44mb', $operator, $argument);
+ }
+ public function whereClause(&$args, &$joins)
+ {
+ global $SIZE_ID44;
+ $lower = floor(Page_Statistics::findBestValue($SIZE_ID44, $this->argument, false) * 1024 - 100);
+ $upper = ceil(Page_Statistics::findBestValue($SIZE_ID44, $this->argument, true) * 1024 + 100);
+
+ if ($this->operator == '=') {
+ return " id44mb BETWEEN $lower AND $upper";
+ } elseif ($this->operator == '!=') {
+ return " id44mb < $lower OR id44mb > $upper";
+ } elseif ($this->operator == '<=') {
+ return " id44mb < $upper";
+ } elseif ($this->operator == '>=') {
+ return " id44mb > $lower";
+ } elseif ($this->operator == '<') {
+ return " id44mb < $lower";
+ } elseif ($this->operator == '>') {
+ return " id44mb > $upper";
+ } else {
+ error_log("unimplemented operator in Id44Filter: $this->operator");
+
+ return ' 1';
+ }
+ }
+}
+class StateFilter extends Filter
+{
+ public function __construct($operator, $argument)
+ {
+ $this->operator = $operator;
+ $this->argument = $argument;
+ }
+
+ public function whereClause(&$args, &$joins)
+ {
+ $neg = $this->operator == '!=' ? 'NOT ' : '';
+ if ($this->argument === 'on') {
+ return " $neg (lastseen + 600 > UNIX_TIMESTAMP() ) ";
+ } elseif ($this->argument === 'off') {
+ return " $neg (lastseen + 600 < UNIX_TIMESTAMP() ) ";
+ } elseif ($this->argument === 'idle') {
+ return " $neg (lastseen + 600 > UNIX_TIMESTAMP() AND logintime = 0 ) ";
+ } elseif ($this->argument === 'occupied') {
+ return " $neg (lastseen + 600 > UNIX_TIMESTAMP() AND logintime <> 0 ) ";
+ } else {
+ Message::addError('invalid-filter');
+
+ return ' 1';
+ }
+ }
+}
+
+class LocationFilter extends Filter
+{
+ public function __construct($operator, $argument) {
+ parent::__construct('locationid', $operator, $argument);
+ }
+
+ public function whereClause(&$args, &$joins) {
+ settype($this->argument, 'int');
+ if ($this->argument === 0) {
+ $joins[] = 'LEFT JOIN subnet s ON (INET_ATON(machine.clientip) BETWEEN s.startaddr AND s.endaddr)';
+ return 'machine.locationid IS NULL AND s.locationid IS NULL';
+ } else {
+ $joins[] = ' INNER JOIN subnet ON (INET_ATON(clientip) BETWEEN startaddr AND endaddr) ';
+ $args['lid'] = $this->argument;
+ $neg = $this->operator == '=' ? '' : 'NOT';
+ return "$neg (subnet.locationid = :lid OR machine.locationid = :lid)";
+ }
+ }
+}
+
+class SubnetFilter extends Filter
+{
+ public function __construct($operator, $argument) {
+ parent::__construct(null, $operator, $argument);
+ }
+ public function whereClause(&$args, &$joins) {
+ $argument = preg_replace('/[^0-9\.:]/', '', $this->argument);
+ return " clientip LIKE '$argument%'";
+ }
+}
+
diff --git a/modules-available/statistics/lang/de/template-tags.json b/modules-available/statistics/lang/de/template-tags.json
index 40f3dbc4..d4d34fb7 100644
--- a/modules-available/statistics/lang/de/template-tags.json
+++ b/modules-available/statistics/lang/de/template-tags.json
@@ -1,5 +1,7 @@
{
"lang_64bitSupport": "64\u2009Bit Gast-Support",
+ "lang_add": "Hinzuf\u00fcgen",
+ "lang_add_filter": "Filter hinzuf\u00fcgen",
"lang_address": "Adresse",
"lang_clientList": "Liste ausgew\u00e4hlter Rechner",
"lang_cores": "Kerne",
@@ -52,6 +54,7 @@
"lang_ramSlots": "Speicher-Slots",
"lang_realCores": "Kerne",
"lang_reallocatedSectors": "Defekte Sektoren",
+ "lang_refresh": "Ansicht aktualisieren",
"lang_serialNo": "Serien-Nr",
"lang_sockets": "Sockel",
"lang_tempPart": "Temp. Partition",
diff --git a/modules-available/statistics/lang/en/template-tags.json b/modules-available/statistics/lang/en/template-tags.json
index beb9f4f6..286d1a32 100644
--- a/modules-available/statistics/lang/en/template-tags.json
+++ b/modules-available/statistics/lang/en/template-tags.json
@@ -1,5 +1,7 @@
{
"lang_64bitSupport": "64\u2009Bit guest support",
+ "lang_add": "Add",
+ "lang_add_filter": "Add Filter",
"lang_address": "Address",
"lang_clientList": "List of selected machines",
"lang_cores": "Cores",
@@ -52,6 +54,7 @@
"lang_ramSlots": "Memory slots",
"lang_realCores": "Cores",
"lang_reallocatedSectors": "Bad sectors",
+ "lang_refresh": "Refresh",
"lang_serialNo": "Serial no",
"lang_sockets": "Sockets",
"lang_tempPart": "Temp. partition",
diff --git a/modules-available/statistics/page.inc.php b/modules-available/statistics/page.inc.php
index de7f965b..d60e39f1 100644
--- a/modules-available/statistics/page.inc.php
+++ b/modules-available/statistics/page.inc.php
@@ -1,9 +1,10 @@
<?php
global $STATS_COLORS, $SIZE_ID44, $SIZE_RAM;
+global $unique_key;
$STATS_COLORS = array();
for ($i = 0; $i < 10; ++$i) {
- $STATS_COLORS[] = '#55' . sprintf('%02s%02s', dechex((($i+1)*($i+1)) / .3922), dechex(abs((5-$i) * 51)));
+ $STATS_COLORS[] = '#55'.sprintf('%02s%02s', dechex((($i + 1) * ($i + 1)) / .3922), dechex(abs((5 - $i) * 51)));
}
//$STATS_COLORS = array('#57e', '#ee8', '#5ae', '#fb7', '#6d7', '#e77', '#3af', '#666', '#e0e', '#999');
$SIZE_ID44 = array(0, 8, 16, 24, 30, 40, 50, 60, 80, 100, 120, 150, 180, 250, 300, 350, 400, 450, 500);
@@ -11,910 +12,1124 @@ $SIZE_RAM = array(1, 2, 3, 4, 6, 8, 10, 12, 16, 24, 32, 48, 64, 96, 128, 192, 25
class Page_Statistics extends Page
{
+ /* some constants, TODO: Find a better place */
+ public static $op_nominal;
+ public static $op_ordinal;
+ public static $op_element;
+ public static $op_stringcmp;
+ public static $columns;
- protected function doPreprocess()
- {
- User::load();
- if (!User::hasPermission('superadmin')) {
- Message::addError('main.no-permission');
- Util::redirect('?do=Main');
- }
- $action = Request::post('action');
- if ($action === 'setnotes') {
- $uuid = Request::post('uuid', '', 'string');
- $text = Request::post('content', '', 'string');
- if (empty($text)) {
- $text = null;
- }
- Database::exec('UPDATE machine SET notes = :text WHERE machineuuid = :uuid', array(
- 'uuid' => $uuid,
- 'text' => $text
- ));
- Message::addSuccess('notes-saved');
- Util::redirect('?do=Statistics&uuid=' . $uuid);
- }
- }
-
- protected function doRender()
- {
- $uuid = Request::get('uuid', false, 'string');
- if ($uuid !== false) {
- $this->showMachine($uuid);
- return;
- }
- $filter = Request::get('filter', false, 'string');
- if ($filter !== false) {
- $argument = Request::get('argument', false, 'string');
- $this->showMachineList($filter, $argument);
- return;
- }
- Render::openTag('div', array('class' => 'row'));
- $this->showSummary();
- $this->showMemory();
- $this->showId44();
- $this->showKvmState();
- $this->showLatestMachines();
- $this->showSystemModels();
- Render::closeTag('div');
- }
-
- private function capChart(&$json, $cutoff, $minSlice = 0.015)
- {
- $total = 0;
- foreach ($json as $entry) {
- $total += $entry['value'];
- }
- if ($total === 0)
- return;
- $cap = ceil($total * $cutoff);
- $accounted = 0;
- $id = 0;
- foreach ($json as $entry) {
- if (($accounted >= $cap || $entry['value'] / $total < $minSlice) && $id >= 3) break;
- $id++;
- $accounted += $entry['value'];
- }
- $json = array_slice($json, 0, $id);
- if ($accounted / $total < 0.99) {
- $json[] = array(
- 'color' => '#eee',
- 'label' => 'invalid',
- 'value' => ($total - $accounted)
- );
- }
- }
-
- private function showSummary()
- {
- $cutoff = time() - 86400 * 30;
- $online = time() - 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");
- $hdd = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE badsectors > 10 AND lastseen > $cutoff");
- if ($on['val'] != 0) {
- $usedpercent = round($used['val'] / $on['val'] * 100);
- } else {
- $usedpercent = 0;
- }
- $data = array(
- 'known' => $known['val'],
- 'online' => $on['val'],
- 'used' => $used['val'],
- 'usedpercent' => $usedpercent,
- 'badhdd' => $hdd['val']
- );
- // Graph
- $cutoff = time() - 2*86400;
- $res = Database::simpleQuery("SELECT dateline, data FROM statistic WHERE typeid = '~stats' AND dateline > $cutoff ORDER BY dateline ASC");
- $labels = array();
- $points1 = array('data' => array(), 'label' => 'Online', 'fillColor' => '#efe', 'strokeColor' => '#aea', 'pointColor' => '#7e7', 'pointStrokeColor' => '#fff', 'pointHighlightFill' => '#fff', 'pointHighlightStroke' => '#7e7');
- $points2 = array('data' => array(), 'label' => 'In use', 'fillColor' => '#fee', 'strokeColor' => '#eaa', 'pointColor' => '#e77', 'pointStrokeColor' => '#fff', 'pointHighlightFill' => '#fff', 'pointHighlightStroke' => '#e77');
- $sum = 0;
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- $x = explode('#', $row['data']);
- if ($sum === 0) {
- $labels[] = date('H:i', $row['dateline']);
- } else {
- $x[1] = max($x[1], array_pop($points1['data']));
- $x[2] = max($x[2], array_pop($points2['data']));
- }
- $points1['data'][] = $x[1];
- $points2['data'][] = $x[2];
- $sum++;
- if ($sum === 12) {
- $sum = 0;
- }
- }
- $data['json'] = json_encode(array('labels' => $labels, 'datasets' => array($points1, $points2)));
- // Draw
- Render::addTemplate('summary', $data);
- }
-
- private function showSystemModels()
- {
- global $STATS_COLORS;
- $res = Database::simpleQuery("SELECT systemmodel, Round(AVG(realcores)) AS cores, Count(*) AS `count` FROM machine"
- . " GROUP BY systemmodel ORDER BY `count` DESC, systemmodel ASC");
- $lines = array();
- $json = array();
- $id = 0;
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- if (empty($row['systemmodel'])) continue;
- settype($row['count'], 'integer');
- $row['id'] = 'systemid' . $id;
- $row['urlsystemmodel'] = urlencode($row['systemmodel']);
- $lines[] = $row;
- $json[] = array(
- 'color' => $STATS_COLORS[$id % count($STATS_COLORS)],
- 'label' => 'systemid' . $id,
- 'value' => $row['count']
- );
- ++$id;
- }
- $this->capChart($json, 0.92);
- Render::addTemplate('cpumodels', array('rows' => $lines, 'json' => json_encode($json)));
- }
-
- private function showMemory()
- {
- global $STATS_COLORS, $SIZE_RAM;
- $res = Database::simpleQuery("SELECT mbram, Count(*) AS `count` FROM machine GROUP BY mbram");
- $lines = array();
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- $gb = ceil($row['mbram'] / 1024);
- for ($i = 1; $i < count($SIZE_RAM); ++$i) {
- if ($SIZE_RAM[$i] < $gb) continue;
- if ($SIZE_RAM[$i] - $gb >= $gb - $SIZE_RAM[$i-1]) --$i;
- $gb = $SIZE_RAM[$i];
- break;
- }
- if (isset($lines[$gb])) {
- $lines[$gb] += $row['count'];
- } else {
- $lines[$gb] = $row['count'];
- }
- }
- asort($lines);
- $data = array('rows' => array());
- $json = array();
- $id = 0;
- foreach (array_reverse($lines, true) as $k => $v) {
- $data['rows'][] = array('gb' => $k, 'count' => $v, 'class' => $this->ramColorClass($k * 1024));
- $json[] = array(
- 'color' => $STATS_COLORS[$id % count($STATS_COLORS)],
- 'label' => (string)$k,
- 'value' => $v
- );
- ++$id;
- }
- $this->capChart($json, 0.92);
- $data['json'] = json_encode($json);
- Render::addTemplate('memory', $data);
- }
-
- private function showKvmState()
- {
- $colors = array('UNKNOWN' => '#666', 'UNSUPPORTED' => '#ea5', 'DISABLED' => '#e55', 'ENABLED' => '#6d6');
- $res = Database::simpleQuery("SELECT kvmstate, Count(*) AS `count` FROM machine GROUP BY kvmstate ORDER BY `count` DESC");
- $lines = array();
- $json = array();
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- $lines[] = $row;
- $json[] = array(
- 'color' => isset($colors[$row['kvmstate']]) ? $colors[$row['kvmstate']] : '#000',
- 'label' => $row['kvmstate'],
- 'value' => $row['count']
- );
- }
- Render::addTemplate('kvmstate', array('rows' => $lines, 'json' => json_encode($json)));
- }
-
- private function showId44()
- {
- global $STATS_COLORS, $SIZE_ID44;
- $res = Database::simpleQuery("SELECT id44mb, Count(*) AS `count` FROM machine GROUP BY id44mb");
- $lines = array();
- $total = 0;
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- $total += $row['count'];
- $gb = ceil($row['id44mb'] / 1024);
- for ($i = 1; $i < count($SIZE_ID44); ++$i) {
- if ($SIZE_ID44[$i] < $gb) continue;
- if ($SIZE_ID44[$i] - $gb >= $gb - $SIZE_ID44[$i-1]) --$i;
- $gb = $SIZE_ID44[$i];
- break;
- }
- if (isset($lines[$gb])) {
- $lines[$gb] += $row['count'];
- } else {
- $lines[$gb] = $row['count'];
- }
- }
- asort($lines);
- $data = array('rows' => array());
- $json = array();
- $id = 0;
- foreach (array_reverse($lines, true) as $k => $v) {
- $data['rows'][] = array('gb' => $k, 'count' => $v, 'class' => $this->hddColorClass($k));
- if ($k === 0) {
- $color = '#e55';
- } else {
- $color = $STATS_COLORS[$id++ % count($STATS_COLORS)];
- }
- $json[] = array(
- 'color' => $color,
- 'label' => (string)$k,
- 'value' => $v
- );
- }
- $this->capChart($json, 0.95);
- $data['json'] = json_encode($json);
- Render::addTemplate('id44', $data);
- }
-
- private function showLatestMachines()
- {
- $data = array('cutoff' => ceil(time() / 3600) * 3600 - 86400 * 7);
- $res = Database::simpleQuery("SELECT machineuuid, clientip, hostname, firstseen, mbram, kvmstate, id44mb FROM machine"
- . " WHERE firstseen > :cutoff ORDER BY firstseen DESC LIMIT 32", $data);
- $rows = array();
- $count = 0;
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- if (empty($row['hostname'])) {
- $row['hostname'] = $row['clientip'];
- }
- $row['firstseen'] = date('d.m. H:i', $row['firstseen']);
- $row['gbram'] = round(round($row['mbram'] / 500) / 2, 1); // Trial and error until we got "expected" rounding..
- $row['gbtmp'] = round($row['id44mb'] / 1024);
- $row['ramclass'] = $this->ramColorClass($row['mbram']);
- $row['kvmclass'] = $this->kvmColorClass($row['kvmstate']);
- $row['hddclass'] = $this->hddColorClass($row['gbtmp']);
- $row['kvmicon'] = $row['kvmstate'] === 'ENABLED' ? '✓' : '✗';
- if (++$count > 5) {
- $row['style'] = 'display:none';
- }
- $rows[] = $row;
- }
- Render::addTemplate('newclients', array('rows' => $rows, 'openbutton' => $count > 5));
- }
-
- private function showMachineList($filter, $argument)
- {
- global $SIZE_RAM, $SIZE_ID44;
- $join = '';
- $filters = array('cpumodel', 'realcores', 'kvmstate', 'clientip', 'macaddr', 'machineuuid', 'systemmodel');
- if (in_array($filter, $filters)) {
- // Simple filters mapping into db
- $where = " $filter = :argument";
- $args = array('argument' => $argument);
- } elseif ($filter === 'gbram') {
- // Memory by rounded GB
- $lower = floor($this->findBestValue($SIZE_RAM, $argument, false) * 1024 - 100);
- $upper = ceil($this->findBestValue($SIZE_RAM, $argument, true) * 1024 + 100);
- $where = " mbram BETWEEN $lower AND $upper";
- $args = array();
- } elseif ($filter === 'hddgb') {
- // HDD by rounded GB
- $lower = floor($this->findBestValue($SIZE_ID44, $argument, false) * 1024 - 100);
- $upper = ceil($this->findBestValue($SIZE_ID44, $argument, true) * 1024 + 100);
- $where = " id44mb BETWEEN $lower AND $upper";
- $args = array();
- } elseif ($filter === 'subnet') {
- $argument = preg_replace('/[^0-9\.:]/', '', $argument);
- $where = " clientip LIKE '$argument%'";
- $args = array();
- } elseif ($filter === 'badsectors') {
- $where = " badsectors >= :argument ";
- $args = array('argument' => $argument);
- } elseif ($filter === 'state') {
- if ( $argument === 'on') {
- $where = " lastseen + 600 > UNIX_TIMESTAMP() ";
- } elseif ($argument === 'off') {
- $where = " lastseen + 600 < UNIX_TIMESTAMP() ";
- } elseif ($argument === 'idle') {
- $where = " lastseen + 600 > UNIX_TIMESTAMP() AND logintime = 0 ";
- } elseif ($argument === 'occupied') {
- $where = " lastseen + 600 > UNIX_TIMESTAMP() AND logintime <> 0 ";
- } else {
- Message::addError('invalid-filter');
- return;
- }
- } elseif ($filter === 'location') {
- settype($argument, 'int');
- if ($argument === 0) {
- $where = "machine.locationid IS NULL AND s.locationid IS NULL";
- $join = "LEFT JOIN subnet s ON (INET_ATON(machine.clientip) BETWEEN s.startaddr AND s.endaddr)";
- } else {
- $where = "subnet.locationid = :lid OR machine.locationid = :lid";
- $join = " INNER JOIN subnet ON (INET_ATON(clientip) BETWEEN startaddr AND endaddr) ";
- $args = array('lid' => $argument);
- }
- } else {
- Message::addError('invalid-filter');
- return;
- }
- $res = Database::simpleQuery("SELECT machineuuid, macaddr, clientip, firstseen, lastseen,"
- . " logintime, lastboot, realcores, mbram, kvmstate, cpumodel, id44mb, hostname, notes IS NOT NULL AS hasnotes, badsectors FROM machine"
- . " $join WHERE $where ORDER BY lastseen DESC, clientip ASC", $args);
- $rows = array();
- $NOW = time();
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- if ($NOW - $row['lastseen'] > 610) {
- $row['state_off'] = true;
- } elseif ($row['logintime'] == 0) {
- $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['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']);
- if (count($octets) === 4) {
- $row['subnet'] = "$octets[0].$octets[1].$octets[2].";
- $row['lastoctet'] = $octets[3];
- }
- $row['ramclass'] = $this->ramColorClass($row['mbram']);
- $row['kvmclass'] = $this->kvmColorClass($row['kvmstate']);
- $row['hddclass'] = $this->hddColorClass($row['gbtmp']);
- if (empty($row['hostname'])) $row['hostname'] = $row['clientip'];
- $rows[] = $row;
- }
- Render::addTemplate('clientlist', array('rows' => $rows, 'filter' => $filter, 'argument' => $argument));
- }
-
- private function ramColorClass($mb)
- {
- if ($mb < 1500)
- return 'danger';
- if ($mb < 2500)
- return 'warning';
- return '';
- }
-
- private function kvmColorClass($state)
- {
- if ($state === 'DISABLED')
- return 'danger';
- if ($state === 'UNKNOWN' || $state === 'UNSUPPORTED')
- return 'warning';
- return '';
- }
-
- private function hddColorClass($gb)
- {
- if ($gb < 7)
- return 'danger';
- if ($gb < 25)
- return 'warning';
- return '';
- }
-
- private function findBestValue($array, $value, $up)
- {
- $best = 0;
- for ($i = 0; $i < count($array); ++$i) {
- if (abs($array[$i] - $value) < abs($array[$best] - $value)) {
- $best = $i;
- }
- }
- if (!$up && $best === 0) {
- return $array[0];
- }
- if ($up && $best + 1 === count($array)) {
- return $array[$best];
- }
- if ($up) {
- return ($array[$best] + $array[$best + 1]) / 2;
- }
- return ($array[$best] + $array[$best - 1]) / 2;
- }
-
- private function fillSessionInfo(&$row)
- {
- $res = Database::simpleQuery("SELECT dateline, username, data FROM statistic"
- . " WHERE clientip = :ip AND typeid = '.vmchooser-session-name'"
- . " AND dateline BETWEEN :start AND :end", array(
- 'ip' => $row['clientip'],
- 'start' => $row['logintime'] - 60,
- 'end' => $row['logintime'] + 300
- ));
- $session = false;
- while ($r = $res->fetch(PDO::FETCH_ASSOC)) {
- if ($session === false || abs($session['dateline'] - $row['logintime']) > abs($r['dateline'] - $row['logintime'])) {
- $session = $r;
- }
- }
- if ($session !== false) {
- $row['session'] = $session['data'];
- $row['username'] = $session['username'];
- }
- }
-
- private function showMachine($uuid)
- {
- $client = Database::queryFirst("SELECT machineuuid, locationid, macaddr, clientip, firstseen, lastseen, logintime, lastboot,"
- . " mbram, kvmstate, cpumodel, id44mb, data, hostname, notes FROM machine WHERE machineuuid = :uuid",
- array('uuid' => $uuid));
- // Mangle fields
- $NOW = time();
- if ($NOW - $client['lastseen'] > 610) {
- $client['state_off'] = true;
- } elseif ($client['logintime'] == 0) {
- $client['state_idle'] = true;
- } else {
- $client['state_occupied'] = true;
- $this->fillSessionInfo($client);
- }
- $client['firstseen_s'] = date('d.m.Y H:i', $client['firstseen']);
- $client['lastseen_s'] = date('d.m.Y H:i', $client['lastseen']);
- $uptime = $NOW - $client['lastboot'];
- $client['lastboot_s'] = date('d.m.Y H:i', $client['lastboot']);
- if (!isset($client['state_off']) || !$client['state_off']) {
- $client['lastboot_s'] .= ' (Up ' . floor($uptime / 86400) . 'd ' . gmdate('H:i', $uptime) . ')';
- }
- $client['logintime_s'] = date('d.m.Y H:i', $client['logintime']);
- $client['gbram'] = round(round($client['mbram'] / 500) / 2, 1);
- $client['gbtmp'] = round($client['id44mb'] / 1024);
- $client['ramclass'] = $this->ramColorClass($client['mbram']);
- $client['kvmclass'] = $this->kvmColorClass($client['kvmstate']);
- $client['hddclass'] = $this->hddColorClass($client['gbtmp']);
- // Parse the giant blob of data
- $hdds = array();
- if (preg_match_all('/##### ([^#]+) #+$(.*?)^#####/ims', $client['data'] . '########', $out, PREG_SET_ORDER)) {
- foreach ($out as $section) {
- if ($section[1] === 'CPU') {
- $this->parseCpu($client, $section[2]);
- }
- if ($section[1] === 'dmidecode') {
- $this->parseDmiDecode($client, $section[2]);
- }
- if ($section[1] === 'Partition tables') {
- $this->parseHdd($hdds, $section[2]);
- }
- if ($section[1] === 'PCI ID') {
- $client['lspci1'] = $client['lspci2'] = array();
- $this->parsePci($client['lspci1'], $client['lspci2'], $section[2]);
- }
- if (isset($hdds['hdds']) && $section[1] === 'smartctl') {
- // This currently required that the partition table section comes first...
- $this->parseSmartctl($hdds['hdds'], $section[2]);
- }
- }
- }
- unset($client['data']);
- // Get locations
- if (Module::isAvailable('locations')) {
- if (is_null($client['locationid'])) {
- $client['locationid'] = Location::getFromIp($client['clientip']);
- }
- $locs = Location::getLocationsAssoc();
- $next = (int)$client['locationid'];
- $output = array();
- while (isset($locs[$next])) {
- array_unshift($output, $locs[$next]);
- $next = $locs[$next]['parentlocationid'];
- }
- $client['locations'] = $output;
- }
- // Throw output at user
- Render::addTemplate('machine-main', $client);
- // Sessions
- $NOW = time();
- $cutoff = $NOW - 86400 * 7;
- //if ($cutoff < $client['firstseen']) $cutoff = $client['firstseen'];
- $scale = 100 / ($NOW - $cutoff);
- $res = Database::simpleQuery("SELECT dateline, typeid, data FROM statistic"
- . " WHERE dateline > :cutoff AND typeid IN ('~session-length', '~offline-length') AND machineuuid = :uuid ORDER BY dateline ASC", array(
- 'cutoff' => $cutoff - 86400 * 14,
- 'uuid' => $uuid
- ));
- $spans['rows'] = array();
- $spans['graph'] = '';
- $last = false;
- $first = true;
- while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
- if ($first && $row['dateline'] > $cutoff && $client['lastboot'] > $cutoff) {
- // Special case: offline before
- $spans['graph'] .= '<div style="background:#444;left:0%;width:' . round((min($row['dateline'], $client['lastboot']) - $cutoff) * $scale, 2) . '%">&nbsp;</div>';
- }
- $first = false;
- if ($row['dateline'] + $row['data'] < $cutoff || $row['data'] > 864000) continue;
- if ($last !== false && abs($last['dateline'] - $row['dateline']) < 30
- && abs($last['data'] - $row['data']) < 30) continue;
- if ($last !== false && $last['dateline'] + $last['data'] > $row['dateline']) {
- $point = $last['dateline'] + $last['data'];
- $row['data'] -= ($point - $row['dateline']);
- $row['dateline'] = $point;
- }
- if ($row['dateline'] < $cutoff) {
- $row['data'] -= ($cutoff - $row['dateline']);
- $row['dateline'] = $cutoff;
- }
- $row['from'] = date('d.m. H:i', $row['dateline']);
- $row['duration'] = floor($row['data'] / 86400) . 'd ' . gmdate('H:i', $row['data']);
- if ($row['typeid'] === '~offline-length') {
- $row['glyph'] = 'off';
- $color = '#444';
- } else {
- $row['glyph'] = 'user';
- $color = '#e77';
- }
- $spans['graph'] .= '<div style="background:' . $color . ';left:' . round(($row['dateline'] - $cutoff) * $scale, 2) . '%;width:' . round(($row['data']) * $scale, 2) . '%">&nbsp;</div>';
- $spans['rows'][] = $row;
- $last = $row;
- }
- if ($first && $client['lastboot'] > $cutoff) {
- // Special case: offline before
- $spans['graph'] .= '<div style="background:#444;left:0%;width:' . round(($client['lastboot'] - $cutoff) * $scale, 2) . '%">&nbsp;</div>';
- }
- if (isset($client['state_occupied'])) {
- $spans['graph'] .= '<div style="background:#e99;left:' . round(($client['logintime'] - $cutoff) * $scale, 2) . '%;width:' . round(($NOW - $client['logintime'] + 900) * $scale, 2) . '%">&nbsp;</div>';
- } elseif (isset($client['state_off'])) {
- $spans['graph'] .= '<div style="background:#444;left:' . round(($client['lastseen'] - $cutoff) * $scale, 2) . '%;width:' . round(($NOW - $client['lastseen'] + 900) * $scale, 2) . '%">&nbsp;</div>';
- }
- $t = explode('-', date('Y-n-j-G', $cutoff));
- if ($t[3] >= 8 && $t[3] <= 22) {
- $start = mktime(22, 0, 0, $t[1], $t[2], $t[0]);
- } else {
- $start = mktime(22, 0, 0, $t[1], $t[2] - 1, $t[0]);
- }
- for ($i = $start; $i < $NOW; $i += 86400) {
- $spans['graph'] .= '<div style="background:rgba(0,0,90,.2);left:' . round(($i - $cutoff) * $scale, 2) . '%;width:' . round((10 * 3600) * $scale, 2) . '%">&nbsp;</div>';
- }
- if (count($spans['rows']) > 10) {
- $spans['hasrows2'] = true;
- $spans['rows2'] = array_slice($spans['rows'], ceil(count($spans['rows']) / 2));
- $spans['rows'] = array_slice($spans['rows'], 0, ceil(count($spans['rows']) / 2));
- }
- Render::addTemplate('machine-usage', $spans);
- // Any hdds?
- if (!empty($hdds['hdds'])) {
- Render::addTemplate('machine-hdds', $hdds);
- }
- // Client log
- $lres = Database::simpleQuery("SELECT logid, dateline, logtypeid, clientip, description, extra FROM clientlog"
- . " WHERE clientip = :clientip ORDER BY logid DESC LIMIT 25", array('clientip' => $client['clientip']));
- $today = date('d.m.Y');
- $yesterday = date('d.m.Y', time() - 86400);
- $count = 0;
- $log = array();
- while ($row = $lres->fetch(PDO::FETCH_ASSOC)) {
- if (substr($row['description'], -5) === 'on :0' && strpos($row['description'], 'root logged') === false) continue;
- $day = date('d.m.Y', $row['dateline']);
- if ($day === $today) {
- $day = Dictionary::translate('lang_today');
- } elseif ($day === $yesterday) {
- $day = Dictionary::translate('lang_yesterday');
- }
- $row['date'] = $day . date(' H:i', $row['dateline']);
- $row['icon'] = $this->eventToIconName($row['logtypeid']);
- $log[] = $row;
- if (++$count === 10) break;
- }
- Render::addTemplate('syslog', array(
- 'clientip' => $client['clientip'],
- 'list' => $log
- ));
- // Notes
- Render::addTemplate('machine-notes', $client);
- }
-
- private function eventToIconName($event)
- {
- switch ($event) {
- case 'session-open':
- return 'glyphicon-log-in';
- case 'session-close':
- return 'glyphicon-log-out';
- case 'partition-swap':
- return 'glyphicon-info-sign';
- case 'partition-temp':
- case 'smartctl-realloc':
- return 'glyphicon-exclamation-sign';
- default:
- return 'glyphicon-minus';
- }
- }
-
- private function parseCpu(&$row, $data)
- {
- if (0 >= preg_match_all('/^(.+):\s+(\d+)$/im', $data, $out, PREG_SET_ORDER)) return;
- foreach ($out as $entry) {
- $row[str_replace(' ', '', $entry[1])] = $entry[2];
- }
- }
-
- private function parseDmiDecode(&$row, $data)
- {
- $lines = preg_split("/[\r\n]+/", $data);
- $section = false;
- $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;
- if (($ramForm || $ramType) && ($ramSpeed || $ramClockSpeed)) {
- if (isset($row['ramtype']) && !$ramClockSpeed) continue;
- $row['ramtype'] = $ramType . ' ' . $ramForm;
- if ($ramClockSpeed) $row['ramtype'] .= ', ' . $ramClockSpeed;
- elseif ($ramSpeed) $row['ramtype'] .= ', ' . $ramSpeed;
- $ramForm = false;
- $ramType = false;
- $ramClockSpeed = false;
- }
- continue;
- }
- if ($section === 'System Information' || $section === 'Base Board Information') {
- if (empty($row['pcmodel']) && preg_match('/^\s*Product Name: +(\S.+?) *$/i', $line, $out)) {
- $row['pcmodel'] = $out[1];
- }
- if (empty($row['manufacturer']) && preg_match('/^\s*Manufacturer: +(\S.+?) *$/i', $line, $out)) {
- $row['manufacturer'] = $out[1];
- }
- }
- else if ($section === 'Physical Memory Array') {
- if (!$ramOk && preg_match('/Use: System Memory/i', $line)) {
- $ramOk = true;
- }
- if ($ramOk && preg_match('/^\s*Number Of Devices: +(\S.+?) *$/i', $line, $out)) {
- $row['ramslotcount'] = $out[1];
- }
- if ($ramOk && preg_match('/^\s*Maximum Capacity: +(\S.+?)\s*$/i', $line, $out)) {
- $row['maxram'] = preg_replace('/([MGT])B/', '$1iB', $out[1]);
- }
- }
- else if ($section === 'Memory Device') {
- if (preg_match('/^\s*Size:\s*(.*?)\s*$/i', $line, $out)) {
- $row['extram'] = true;
- if (preg_match('/(\d+)\s*(\w)i?B/i', $out[1], $out)) {
- $out[2] = strtoupper($out[2]);
- if ($out[2] === 'K' || ($out[2] === 'M' && $out[1] < 500)) {
- $ramForm = $ramType = $ramSpeed = $ramClockSpeed = false;
- continue;
- }
- if ($out[2] === 'M' && $out[1] >= 1024) {
- $out[2] = 'G';
- $out[1] = floor(($out[1] + 100) / 1024);
- }
- $row['ramslot'][]['size'] = $out[1] . ' ' . strtoupper($out[2]) . 'iB';
- } else if (!isset($row['ramslot']) || (count($row['ramslot']) < 8 && (!isset($row['ramslotcount']) || $row['ramslotcount'] <= 8))) {
- $row['ramslot'][]['size'] = '_____';
- }
- }
- if (preg_match('/^\s*Form Factor:\s*(.*?)\s*$/i', $line, $out) && $out[1] !== 'Unknown') {
- $ramForm = $out[1];
- }
- if (preg_match('/^\s*Type:\s*(.*?)\s*$/i', $line, $out) && $out[1] !== 'Unknown') {
- $ramType = $out[1];
- }
- if (preg_match('/^\s*Speed:\s*(\d.*?)\s*$/i', $line, $out)) {
- $ramSpeed = $out[1];
- }
- if (preg_match('/^\s*Configured Clock Speed:\s*(\d.*?)\s*$/i', $line, $out)) {
- $ramClockSpeed = $out[1];
- }
- }
- }
- if (empty($row['ramslotcount'])) $row['ramslotcount'] = count($row['ramslot']);
- }
-
- private function parseHdd(&$row, $data)
- {
- $hdds = array();
- // Could have more than one disk - linear scan
- $lines = preg_split("/[\r\n]+/", $data);
- $dev = false;
- $i = 0;
- foreach ($lines as $line) {
- if (preg_match('/^Disk (\S+):.* (\d+) bytes/i', $line, $out)) {
- // disk total size and name
- unset($hdd);
- $unit = 0;
- $hdd = array(
- 'devid' => 'devid-' . ++$i,
- 'dev' => $out[1],
- 'size' => round($out[2] / (1024 * 1024 * 1024)),
- 'used' => 0,
- 'partitions' => array(),
- 'json' => array(),
- );
- $hdds[] = &$hdd;
- } elseif (preg_match('/^Units =.*= (\d+) bytes/i', $line, $out)) {
- // Unit for start and end
- $unit = $out[1] / (1024 * 1024); // Convert so that multiplying by unit yields MiB
- } else if (isset($hdd) && $unit !== 0 && preg_match(',^/dev/(\S+)\s+.*\s(\d+)[\+\-]?\s+(\d+)[\+\-]?\s+\d+[\+\-]?\s+([0-9a-f]+)\s+(.*)$,i', $line, $out)) {
- // Some partition
- $type = strtolower($out[4]);
- if ($type === '5' || $type === 'f' || $type === '85') continue;
- $partsize = round(($out[3] - $out[2]) * $unit);
- $hdd['partitions'][] = array(
- 'id' => $out[1],
- 'name' => $out[1],
- 'size' => round($partsize / 1024, $partsize < 1024 ? 1 : 0),
- 'type' => ($type === '44' ? 'OpenSLX' : $out[5]),
- );
- $hdd['json'][] = array(
- 'label' => $out[1],
- 'value' => $partsize,
- 'color' => ($type === '44' ? '#4d4' : ($type === '82' ? '#48f' : '#e55')),
- );
- $hdd['used'] += $partsize;
- }
- }
- unset($hdd);
- $i = 0;
- foreach ($hdds as &$hdd) {
- $hdd['used'] = round($hdd['used'] / 1024);
- $free = $hdd['size'] - $hdd['used'];
- if ($free > 5) {
- $hdd['partitions'][] = array(
- 'id' => 'free-id-' . $i,
- 'name' => Dictionary::translate('unused'),
- 'size' => $free,
- 'type' => '-',
- );
- $hdd['json'][] = array(
- 'label' => 'free-id-' . $i,
- 'value' => $free * 1024,
- 'color' => '#aaa',
- );
- ++$i;
- }
- $hdd['json'] = json_encode($hdd['json']);
- }
- unset($hdd);
- $row['hdds'] = &$hdds;
- }
-
- private function parsePci(&$pci1, &$pci2, $data)
- {
- preg_match_all('/[a-f0-9\:\.]{7}\s+"(Class\s*)?(?<class>[a-f0-9]{4})"\s+"(?<ven>[a-f0-9]{4})"\s+"(?<dev>[a-f0-9]{4})"/is', $data, $out, PREG_SET_ORDER);
- $NOW = time();
- $pci = array();
- foreach ($out as $entry) {
- if (!isset($pci[$entry['class']])) {
- $class = 'c.' . $entry['class'];
- $res = $this->getPciId('CLASS', $class);
- if ($res === false || $res['dateline'] < $NOW) {
- $pci[$entry['class']]['lookupClass'] = 'do-lookup';
- $pci[$entry['class']]['class'] = $class;
- } else {
- $pci[$entry['class']]['class'] = $res['value'];
- }
- }
- $new = array(
- 'ven' => $entry['ven'],
- 'dev' => $entry['ven'] . ':' . $entry['dev'],
- );
- $res = $this->getPciId('VENDOR', $new['ven']);
- if ($res === false || $res['dateline'] < $NOW) {
- $new['lookupVen'] = 'do-lookup';
- } else {
- $new['ven'] = $res['value'];
- }
- $res = $this->getPciId('DEVICE', $new['ven'] . ':' . $new['dev']);
- if ($res === false || $res['dateline'] < $NOW) {
- $new['lookupDev'] = 'do-lookup';
- } else {
- $new['dev'] = $res['value'];
- }
- $pci[$entry['class']]['entries'][] = $new;
- }
- ksort($pci);
- foreach ($pci as $class => $entry) {
- if ($class === '0300' || $class === '0200' || $class === '0403') {
- $pci1[] = $entry;
- } else {
- $pci2[] = $entry;
- }
- }
- }
-
- private function parseSmartctl(&$hdds, $data)
- {
- $lines = preg_split("/[\r\n]+/", $data);
- $i = 0;
- foreach ($lines as $line) {
- if (preg_match('/^NEXTHDD=(.+)$/', $line, $out)) {
- unset($dev);
- foreach ($hdds as &$hdd) {
- if ($hdd['dev'] === $out[1]) $dev =& $hdd;
- }
- continue;
- }
- if (!isset($dev)) continue;
- if (preg_match('/^([A-Z][^:]+):\s*(.*)$/', $line, $out)) {
- $dev['s_' . preg_replace('/\s|-|_/', '', $out[1])] = $out[2];
- } elseif (preg_match('/^\s*\d+\s+(\S+)\s+\S+\s+\d+\s+\d+\s+\d+\s+\S+\s+(\d+)(\s|$)/', $line, $out)) {
- $dev['s_' . preg_replace('/\s|-|_/', '', $out[1])] = $out[2];
- }
- }
- // Format strings
- foreach ($hdds as &$hdd) {
- if (isset($hdd['s_PowerOnHours'])) {
- $hdd['PowerOnTime'] = '';
- $val = (int)$hdd['s_PowerOnHours'];
- if ($val > 8760) {
- $hdd['PowerOnTime'] .= floor($val / 8760) . 'Y, ';
- $val %= 8760;
- }
- if ($val > 720) {
- $hdd['PowerOnTime'] .= floor($val / 720) . 'M, ';
- $val %= 720;
- }
- if ($val > 24) {
- $hdd['PowerOnTime'] .= floor($val / 24) . 'd, ';
- $val %= 24;
- }
- $hdd['PowerOnTime'] .= $val . 'h';
- }
- }
- }
-
- protected function doAjax()
- {
- $param = Request::any('lookup', false, 'string');
- if ($param === false)
- die('No lookup given');
- $add = '';
- if (preg_match('/^([a-f0-9]{4}):([a-f0-9]{4})$/', $param, $out)) {
- $cat = 'DEVICE';
- $host = $out[2] . '.' . $out[1];
- $add = ' (' . $param . ')';
- } elseif (preg_match('/^([a-f0-9]{4})$/', $param, $out)) {
- $cat = 'VENDOR';
- $host = $out[1];
- } elseif (preg_match('/^c\.([a-f0-9]{2})([a-f0-9]{2})$/', $param, $out)) {
- $cat = 'CLASS';
- $host = $out[2] . '.' . $out[1] . '.c';
- } else {
- die('Invalid format requested');
- }
- $cached = $this->getPciId($cat, $param);
- if ($cached !== false && $cached['dateline'] > time()) {
- echo $cached['value'], $add;
- exit;
- }
- $res = dns_get_record($host . '.pci.id.ucw.cz', DNS_TXT);
- if (is_array($res)) {
- foreach ($res as $entry) {
- if (isset($entry['txt']) && substr($entry['txt'], 0, 2) === 'i=') {
- $string = substr($entry['txt'], 2);
- $this->setPciId($cat, $param, $string);
- echo $string, $add;
- exit;
- }
- }
- }
- if ($cached !== false) {
- echo $cached['value'], $add;
- exit;
- }
- die('Not found');
- }
-
- private function getPciId($cat, $id)
- {
- return Database::queryFirst("SELECT value, dateline FROM pciid WHERE category = :cat AND id = :id LIMIT 1",
- array('cat' => $cat, 'id' => $id));
- }
-
- private function setPciId($cat, $id, $value)
- {
- Database::exec("INSERT INTO pciid (category, id, value, dateline) VALUES (:cat, :id, :value, :timeout)"
- . " ON DUPLICATE KEY UPDATE value = VALUES(value), dateline = VALUES(dateline)",
- array(
- 'cat' => $cat,
- 'id' => $id,
- 'value' => $value,
- 'timeout' => time() + mt_rand(10, 30) * 86400
- ), true);
- }
+ /* PHP sucks, no static, const array definitions... Or am I missing something? */
+ public function initConstants()
+ {
+ Page_Statistics::$op_nominal = ['!=', '='];
+ Page_Statistics::$op_ordinal = ['!=', '<=', '>=', '=', '<', '>'];
+ Page_Statistics::$op_element = ['in', 'not in'];
+ Page_Statistics::$op_stringcmp = ['!~', '~', '=', '!='];
+ Page_Statistics::$columns = [
+ 'machineuuid' => [
+ 'op' => Page_Statistics::$op_nominal,
+ 'type' => 'string',
+ 'column' => true,
+ ],
+ 'macaddr' => [
+ 'op' => Page_Statistics::$op_nominal,
+ 'type' => 'string',
+ 'column' => true,
+ ],
+ 'firstseen' => [
+ 'op' => Page_Statistics::$op_ordinal,
+ 'type' => 'date',
+ 'column' => true,
+ ],
+ 'lastseen' => [
+ 'op' => Page_Statistics::$op_ordinal,
+ 'type' => 'date',
+ 'column' => true,
+ ],
+ 'logintime' => [
+ 'op' => Page_Statistics::$op_ordinal,
+ 'type' => 'date',
+ 'column' => true,
+ ],
+ 'position' => [
+ 'op' => Page_Statistics::$op_nominal,
+ 'type' => 'string',
+ 'column' => true,
+ ],
+ 'realcores' => [
+ 'op' => Page_Statistics::$op_ordinal,
+ 'type' => 'int',
+ 'column' => true,
+ ],
+// 'mbram' => [
+// 'op' => Page_Statistics::$op_ordinal,
+// 'type' => 'int',
+// 'column' => true,
+// ],
+ 'systemmodel' => [
+ 'op' => Page_Statistics::$op_stringcmp,
+ 'type' => 'string',
+ 'column' => true,
+ ],
+ 'cpumodel' => [
+ 'op' => Page_Statistics::$op_stringcmp,
+ 'type' => 'string',
+ 'column' => true,
+ ],
+ 'hddgb' => [
+ 'op' => Page_Statistics::$op_ordinal,
+ 'type' => 'int',
+ 'column' => false,
+ 'map_sort' => 'id44mb'
+ ],
+ 'gbram' => [
+ 'op' => Page_Statistics::$op_ordinal,
+ 'type' => 'int',
+ 'map_sort' => 'mbram',
+ 'column' => false,
+ ],
+ 'kvmstate' => [
+ 'op' => Page_Statistics::$op_nominal,
+ 'type' => 'enum',
+ 'column' => true,
+ 'values' => ['ENABLED', 'DISABLED', 'UNSUPPORTED']
+ ],
+ 'badsectors' => [
+ 'op' => Page_Statistics::$op_ordinal,
+ 'type' => 'int',
+ 'column' => true
+ ],
+ 'clientip' => [
+ 'op' => Page_Statistics::$op_nominal,
+ 'type' => 'string',
+ 'column' => true
+ ],
+ 'subnet' => [
+ 'op' => Page_Statistics::$op_nominal,
+ 'type' => 'string',
+ 'column' => false
+ ]
+ ];
+ /* TODO ... */
+ }
+ protected function doPreprocess()
+ {
+ $this->initConstants();
+ User::load();
+ if (!User::hasPermission('superadmin')) {
+ Message::addError('main.no-permission');
+ Util::redirect('?do=Main');
+ }
+ $action = Request::post('action');
+ if ($action === 'setnotes') {
+ $uuid = Request::post('uuid', '', 'string');
+ $text = Request::post('content', '', 'string');
+ if (empty($text)) {
+ $text = null;
+ }
+ Database::exec('UPDATE machine SET notes = :text WHERE machineuuid = :uuid', array(
+ 'uuid' => $uuid,
+ 'text' => $text,
+ ));
+ Message::addSuccess('notes-saved');
+ Util::redirect('?do=Statistics&uuid='.$uuid);
+ }
+ }
+
+ protected function doRender()
+ {
+ $uuid = Request::get('uuid', false, 'string');
+ if ($uuid !== false) {
+ $this->showMachine($uuid);
+
+ return;
+ }
+ $filter = Request::get('filter', false, 'string');
+ $show = Request::get('show', 'stat', 'string');
+ if ($filter !== false || $show == 'list') {
+ $argument = Request::get('argument', false, 'string');
+ $this->showMachineList($filter, $argument);
+
+ return;
+ }
+ Render::openTag('div', array('class' => 'row'));
+ $this->showSummary();
+ $this->showMemory();
+ $this->showId44();
+ $this->showKvmState();
+ $this->showLatestMachines();
+ $this->showSystemModels();
+ Render::closeTag('div');
+ }
+
+ private function capChart(&$json, $cutoff, $minSlice = 0.015)
+ {
+ $total = 0;
+ foreach ($json as $entry) {
+ $total += $entry['value'];
+ }
+ if ($total === 0) {
+ return;
+ }
+ $cap = ceil($total * $cutoff);
+ $accounted = 0;
+ $id = 0;
+ foreach ($json as $entry) {
+ if (($accounted >= $cap || $entry['value'] / $total < $minSlice) && $id >= 3) {
+ break;
+ }
+ ++$id;
+ $accounted += $entry['value'];
+ }
+ $json = array_slice($json, 0, $id);
+ if ($accounted / $total < 0.99) {
+ $json[] = array(
+ 'color' => '#eee',
+ 'label' => 'invalid',
+ 'value' => ($total - $accounted),
+ );
+ }
+ }
+
+ private function showSummary()
+ {
+ $cutoff = time() - 86400 * 30;
+ $online = time() - 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");
+ $hdd = Database::queryFirst("SELECT Count(*) AS val FROM machine WHERE badsectors > 10 AND lastseen > $cutoff");
+ if ($on['val'] != 0) {
+ $usedpercent = round($used['val'] / $on['val'] * 100);
+ } else {
+ $usedpercent = 0;
+ }
+ $data = array(
+ 'known' => $known['val'],
+ 'online' => $on['val'],
+ 'used' => $used['val'],
+ 'usedpercent' => $usedpercent,
+ 'badhdd' => $hdd['val'],
+ );
+ // Graph
+ $cutoff = time() - 2 * 86400;
+ $res = Database::simpleQuery("SELECT dateline, data FROM statistic WHERE typeid = '~stats' AND dateline > $cutoff ORDER BY dateline ASC");
+ $labels = array();
+ $points1 = array('data' => array(), 'label' => 'Online', 'fillColor' => '#efe', 'strokeColor' => '#aea', 'pointColor' => '#7e7', 'pointStrokeColor' => '#fff', 'pointHighlightFill' => '#fff', 'pointHighlightStroke' => '#7e7');
+ $points2 = array('data' => array(), 'label' => 'In use', 'fillColor' => '#fee', 'strokeColor' => '#eaa', 'pointColor' => '#e77', 'pointStrokeColor' => '#fff', 'pointHighlightFill' => '#fff', 'pointHighlightStroke' => '#e77');
+ $sum = 0;
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ $x = explode('#', $row['data']);
+ if ($sum === 0) {
+ $labels[] = date('H:i', $row['dateline']);
+ } else {
+ $x[1] = max($x[1], array_pop($points1['data']));
+ $x[2] = max($x[2], array_pop($points2['data']));
+ }
+ $points1['data'][] = $x[1];
+ $points2['data'][] = $x[2];
+ ++$sum;
+ if ($sum === 12) {
+ $sum = 0;
+ }
+ }
+ $data['json'] = json_encode(array('labels' => $labels, 'datasets' => array($points1, $points2)));
+ // Draw
+ Render::addTemplate('summary', $data);
+ }
+
+ private function showSystemModels()
+ {
+ global $STATS_COLORS;
+ $res = Database::simpleQuery('SELECT systemmodel, Round(AVG(realcores)) AS cores, Count(*) AS `count` FROM machine'
+ .' GROUP BY systemmodel ORDER BY `count` DESC, systemmodel ASC');
+ $lines = array();
+ $json = array();
+ $id = 0;
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ if (empty($row['systemmodel'])) {
+ continue;
+ }
+ settype($row['count'], 'integer');
+ $row['id'] = 'systemid'.$id;
+ $row['urlsystemmodel'] = urlencode($row['systemmodel']);
+ $lines[] = $row;
+ $json[] = array(
+ 'color' => $STATS_COLORS[$id % count($STATS_COLORS)],
+ 'label' => 'systemid'.$id,
+ 'value' => $row['count'],
+ );
+ ++$id;
+ }
+ $this->capChart($json, 0.92);
+ Render::addTemplate('cpumodels', array('rows' => $lines, 'json' => json_encode($json)));
+ }
+
+ private function showMemory()
+ {
+ global $STATS_COLORS, $SIZE_RAM;
+ $res = Database::simpleQuery('SELECT mbram, Count(*) AS `count` FROM machine GROUP BY mbram');
+ $lines = array();
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ $gb = ceil($row['mbram'] / 1024);
+ for ($i = 1; $i < count($SIZE_RAM); ++$i) {
+ if ($SIZE_RAM[$i] < $gb) {
+ continue;
+ }
+ if ($SIZE_RAM[$i] - $gb >= $gb - $SIZE_RAM[$i - 1]) {
+ --$i;
+ }
+ $gb = $SIZE_RAM[$i];
+ break;
+ }
+ if (isset($lines[$gb])) {
+ $lines[$gb] += $row['count'];
+ } else {
+ $lines[$gb] = $row['count'];
+ }
+ }
+ asort($lines);
+ $data = array('rows' => array());
+ $json = array();
+ $id = 0;
+ foreach (array_reverse($lines, true) as $k => $v) {
+ $data['rows'][] = array('gb' => $k, 'count' => $v, 'class' => $this->ramColorClass($k * 1024));
+ $json[] = array(
+ 'color' => $STATS_COLORS[$id % count($STATS_COLORS)],
+ 'label' => (string) $k,
+ 'value' => $v,
+ );
+ ++$id;
+ }
+ $this->capChart($json, 0.92);
+ $data['json'] = json_encode($json);
+ Render::addTemplate('memory', $data);
+ }
+
+ private function showKvmState()
+ {
+ $colors = array('UNKNOWN' => '#666', 'UNSUPPORTED' => '#ea5', 'DISABLED' => '#e55', 'ENABLED' => '#6d6');
+ $res = Database::simpleQuery('SELECT kvmstate, Count(*) AS `count` FROM machine GROUP BY kvmstate ORDER BY `count` DESC');
+ $lines = array();
+ $json = array();
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ $lines[] = $row;
+ $json[] = array(
+ 'color' => isset($colors[$row['kvmstate']]) ? $colors[$row['kvmstate']] : '#000',
+ 'label' => $row['kvmstate'],
+ 'value' => $row['count'],
+ );
+ }
+ Render::addTemplate('kvmstate', array('rows' => $lines, 'json' => json_encode($json)));
+ }
+
+ private function showId44()
+ {
+ global $STATS_COLORS, $SIZE_ID44;
+ $res = Database::simpleQuery('SELECT id44mb, Count(*) AS `count` FROM machine GROUP BY id44mb');
+ $lines = array();
+ $total = 0;
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ $total += $row['count'];
+ $gb = ceil($row['id44mb'] / 1024);
+ for ($i = 1; $i < count($SIZE_ID44); ++$i) {
+ if ($SIZE_ID44[$i] < $gb) {
+ continue;
+ }
+ if ($SIZE_ID44[$i] - $gb >= $gb - $SIZE_ID44[$i - 1]) {
+ --$i;
+ }
+ $gb = $SIZE_ID44[$i];
+ break;
+ }
+ if (isset($lines[$gb])) {
+ $lines[$gb] += $row['count'];
+ } else {
+ $lines[$gb] = $row['count'];
+ }
+ }
+ asort($lines);
+ $data = array('rows' => array());
+ $json = array();
+ $id = 0;
+ foreach (array_reverse($lines, true) as $k => $v) {
+ $data['rows'][] = array('gb' => $k, 'count' => $v, 'class' => $this->hddColorClass($k));
+ if ($k === 0) {
+ $color = '#e55';
+ } else {
+ $color = $STATS_COLORS[$id++ % count($STATS_COLORS)];
+ }
+ $json[] = array(
+ 'color' => $color,
+ 'label' => (string) $k,
+ 'value' => $v,
+ );
+ }
+ $this->capChart($json, 0.95);
+ $data['json'] = json_encode($json);
+ Render::addTemplate('id44', $data);
+ }
+
+ private function showLatestMachines()
+ {
+ $data = array('cutoff' => ceil(time() / 3600) * 3600 - 86400 * 7);
+ $res = Database::simpleQuery('SELECT machineuuid, clientip, hostname, firstseen, mbram, kvmstate, id44mb FROM machine'
+ .' WHERE firstseen > :cutoff ORDER BY firstseen DESC LIMIT 32', $data);
+ $rows = array();
+ $count = 0;
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ if (empty($row['hostname'])) {
+ $row['hostname'] = $row['clientip'];
+ }
+ $row['firstseen'] = date('d.m. H:i', $row['firstseen']);
+ $row['gbram'] = round(round($row['mbram'] / 500) / 2, 1); // Trial and error until we got "expected" rounding..
+ $row['gbtmp'] = round($row['id44mb'] / 1024);
+ $row['ramclass'] = $this->ramColorClass($row['mbram']);
+ $row['kvmclass'] = $this->kvmColorClass($row['kvmstate']);
+ $row['hddclass'] = $this->hddColorClass($row['gbtmp']);
+ $row['kvmicon'] = $row['kvmstate'] === 'ENABLED' ? '✓' : '✗';
+ if (++$count > 5) {
+ $row['style'] = 'display:none';
+ }
+ $rows[] = $row;
+ }
+ Render::addTemplate('newclients', array('rows' => $rows, 'openbutton' => $count > 5));
+ }
+
+
+ private function showMachineList($filter, $argument)
+ {
+ $query = Request::any('filters');
+ $sortColumn = Request::any('sortColumn');
+ $sortDirection = Request::any('sortDirection');
+
+ $filters = Filter::parseQuery($query);
+
+ /* generate where clause & arguments */
+ $where = '';
+ $joins = [];
+ $args = [];
+ error_log('number of filters: '.count($filters));
+ if (empty($filters)) {
+ $where = ' 1 ';
+ } else {
+ foreach ($filters as $filter) {
+ $sep = ($where != '' ? ' AND ' : '');
+ $where .= $sep.$filter->whereClause($args, $joins);
+ }
+ }
+ $join = implode('', array_unique($joins));
+
+
+ if(!in_array($sortDirection, ['ASC', 'DESC'])) {
+ $sortDirection = 'ASC';
+ }
+ if (array_key_exists($sortColumn, Page_Statistics::$columns)) {
+ if (array_key_exists('map_sort', Page_Statistics::$columns[$sortColumn])) {
+ /* use the column mapping */
+ $actualCol = Page_Statistics::$columns[$sortColumn]['map_sort'];
+ $sort = " ORDER BY $actualCol $sortDirection";
+ } else {
+ $sort = " ORDER BY $sortColumn $sortDirection";
+ }
+ } else {
+ die('no sort');
+ $sort = "";
+ }
+
+
+ // /* find the sorting */
+ // $sort = explode(' ', Request::any('sort', 'clientip asc'));
+ // $sort[1] = strtoupper($sort[1]);
+ // if (array_key_exists($sort[0], Page_Statistics::$columns) && ($sort[1] == 'ASC' || $sort[1] == 'DESC')) {
+
+ // /* check map_sort mapping */
+ // if(array_key_exists('map_sort', Page_Statistics::$columns[$sort[0]])) {
+ // $sort[0] = Page_Statistics::$columns[$sort[0]]['map_sort'];
+ // }
+ // $sort = "ORDER BY " . $sort[0] . ' ' . $sort[1];
+ // } else {
+ // $sort = "";
+ // }
+
+ error_log(" JOIN is " . $join);
+ error_log($where);
+ global $SIZE_RAM, $SIZE_ID44;
+ // $filters = array('cpumodel', 'realcores', 'kvmstate', 'clientip', 'macaddr', 'machineuuid', 'systemmodel');
+ // if (in_array($filter, $filters)) {
+ // // Simple filters mapping into db
+ // $where = " $filter = :argument";
+ // $args = array('argument' => $argument);
+ // } elseif ($filter === 'gbram') {
+ // // Memory by rounded GB
+ // $lower = floor($this->findBestValue($SIZE_RAM, $argument, false) * 1024 - 100);
+ // $upper = ceil($this->findBestValue($SIZE_RAM, $argument, true) * 1024 + 100);
+ // $where = " mbram BETWEEN $lower AND $upper";
+ // $args = array();
+ // } elseif ($filter === 'hddgb') {
+ // // HDD by rounded GB
+ // $lower = floor($this->findBestValue($SIZE_ID44, $argument, false) * 1024 - 100);
+ // $upper = ceil($this->findBestValue($SIZE_ID44, $argument, true) * 1024 + 100);
+ // $where = " id44mb BETWEEN $lower AND $upper";
+ // $args = array();
+ // } elseif ($filter === 'subnet') {
+ // $argument = preg_replace('/[^0-9\.:]/', '', $argument);
+ // $where = " clientip LIKE '$argument%'";
+ // $args = array();
+ // } elseif ($filter === 'badsectors') {
+ // $where = ' badsectors >= :argument ';
+ // $args = array('argument' => $argument);
+ // } elseif ($filter === 'state') {
+ // if ($argument === 'on') {
+ // $where = ' lastseen + 600 > UNIX_TIMESTAMP() ';
+ // } elseif ($argument === 'off') {
+ // $where = ' lastseen + 600 < UNIX_TIMESTAMP() ';
+ // } elseif ($argument === 'idle') {
+ // $where = ' lastseen + 600 > UNIX_TIMESTAMP() AND logintime = 0 ';
+ // } elseif ($argument === 'occupied') {
+ // $where = ' lastseen + 600 > UNIX_TIMESTAMP() AND logintime <> 0 ';
+ // } else {
+ // Message::addError('invalid-filter');
+
+ // return;
+ // }
+ // } elseif ($filter === 'location') {
+ // settype($argument, 'int');
+ // if ($argument === 0) {
+ // $where = 'machine.locationid IS NULL AND s.locationid IS NULL';
+ // $join = 'LEFT JOIN subnet s ON (INET_ATON(machine.clientip) BETWEEN s.startaddr AND s.endaddr)';
+ // } else {
+ // $where = 'subnet.locationid = :lid OR machine.locationid = :lid';
+ // $join = ' INNER JOIN subnet ON (INET_ATON(clientip) BETWEEN startaddr AND endaddr) ';
+ // $args = array('lid' => $argument);
+ // }
+ // } else {
+ // //Message::addError('invalid-filter');
+ // // return;
+ // $where = '1';
+ // $args = [];
+ // }
+
+ // $where and $args
+ $res = Database::simpleQuery('SELECT machineuuid, macaddr, clientip, firstseen, lastseen,'
+ .' logintime, lastboot, realcores, mbram, kvmstate, cpumodel, id44mb, hostname, notes IS NOT NULL AS hasnotes, badsectors FROM machine'
+ ." $join WHERE $where $sort", $args);
+ $rows = array();
+ $NOW = time();
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ if ($NOW - $row['lastseen'] > 610) {
+ $row['state_off'] = true;
+ } elseif ($row['logintime'] == 0) {
+ $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['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']);
+ if (count($octets) === 4) {
+ $row['subnet'] = "$octets[0].$octets[1].$octets[2].";
+ $row['lastoctet'] = $octets[3];
+ }
+ $row['ramclass'] = $this->ramColorClass($row['mbram']);
+ $row['kvmclass'] = $this->kvmColorClass($row['kvmstate']);
+ $row['hddclass'] = $this->hddColorClass($row['gbtmp']);
+ if (empty($row['hostname'])) {
+ $row['hostname'] = $row['clientip'];
+ }
+ $rows[] = $row;
+ }
+ Render::addTemplate('clientlist', array('rows' => $rows, 'filter' => $filter,
+ 'query' => $query, 'sortDirection' => $sortDirection, 'sortColumn' => $sortColumn, 'argument' => $argument, 'columns' => json_encode(Page_Statistics::$columns), ));
+ }
+
+ private function ramColorClass($mb)
+ {
+ if ($mb < 1500) {
+ return 'danger';
+ }
+ if ($mb < 2500) {
+ return 'warning';
+ }
+
+ return '';
+ }
+
+ private function kvmColorClass($state)
+ {
+ if ($state === 'DISABLED') {
+ return 'danger';
+ }
+ if ($state === 'UNKNOWN' || $state === 'UNSUPPORTED') {
+ return 'warning';
+ }
+
+ return '';
+ }
+
+ private function hddColorClass($gb)
+ {
+ if ($gb < 7) {
+ return 'danger';
+ }
+ if ($gb < 25) {
+ return 'warning';
+ }
+
+ return '';
+ }
+
+ public static function findBestValue($array, $value, $up)
+ {
+ $best = 0;
+ for ($i = 0; $i < count($array); ++$i) {
+ if (abs($array[$i] - $value) < abs($array[$best] - $value)) {
+ $best = $i;
+ }
+ }
+ if (!$up && $best === 0) {
+ return $array[0];
+ }
+ if ($up && $best + 1 === count($array)) {
+ return $array[$best];
+ }
+ if ($up) {
+ return ($array[$best] + $array[$best + 1]) / 2;
+ }
+
+ return ($array[$best] + $array[$best - 1]) / 2;
+ }
+
+ private function fillSessionInfo(&$row)
+ {
+ $res = Database::simpleQuery('SELECT dateline, username, data FROM statistic'
+ ." WHERE clientip = :ip AND typeid = '.vmchooser-session-name'"
+ .' AND dateline BETWEEN :start AND :end', array(
+ 'ip' => $row['clientip'],
+ 'start' => $row['logintime'] - 60,
+ 'end' => $row['logintime'] + 300,
+ ));
+ $session = false;
+ while ($r = $res->fetch(PDO::FETCH_ASSOC)) {
+ if ($session === false || abs($session['dateline'] - $row['logintime']) > abs($r['dateline'] - $row['logintime'])) {
+ $session = $r;
+ }
+ }
+ if ($session !== false) {
+ $row['session'] = $session['data'];
+ $row['username'] = $session['username'];
+ }
+ }
+
+ private function showMachine($uuid)
+ {
+ $client = Database::queryFirst('SELECT machineuuid, locationid, macaddr, clientip, firstseen, lastseen, logintime, lastboot,'
+ .' mbram, kvmstate, cpumodel, id44mb, data, hostname, notes FROM machine WHERE machineuuid = :uuid',
+ array('uuid' => $uuid));
+ // Mangle fields
+ $NOW = time();
+ if ($NOW - $client['lastseen'] > 610) {
+ $client['state_off'] = true;
+ } elseif ($client['logintime'] == 0) {
+ $client['state_idle'] = true;
+ } else {
+ $client['state_occupied'] = true;
+ $this->fillSessionInfo($client);
+ }
+ $client['firstseen_s'] = date('d.m.Y H:i', $client['firstseen']);
+ $client['lastseen_s'] = date('d.m.Y H:i', $client['lastseen']);
+ $uptime = $NOW - $client['lastboot'];
+ $client['lastboot_s'] = date('d.m.Y H:i', $client['lastboot']);
+ if (!isset($client['state_off']) || !$client['state_off']) {
+ $client['lastboot_s'] .= ' (Up '.floor($uptime / 86400).'d '.gmdate('H:i', $uptime).')';
+ }
+ $client['logintime_s'] = date('d.m.Y H:i', $client['logintime']);
+ $client['gbram'] = round(round($client['mbram'] / 500) / 2, 1);
+ $client['gbtmp'] = round($client['id44mb'] / 1024);
+ $client['ramclass'] = $this->ramColorClass($client['mbram']);
+ $client['kvmclass'] = $this->kvmColorClass($client['kvmstate']);
+ $client['hddclass'] = $this->hddColorClass($client['gbtmp']);
+ // Parse the giant blob of data
+ $hdds = array();
+ if (preg_match_all('/##### ([^#]+) #+$(.*?)^#####/ims', $client['data'].'########', $out, PREG_SET_ORDER)) {
+ foreach ($out as $section) {
+ if ($section[1] === 'CPU') {
+ $this->parseCpu($client, $section[2]);
+ }
+ if ($section[1] === 'dmidecode') {
+ $this->parseDmiDecode($client, $section[2]);
+ }
+ if ($section[1] === 'Partition tables') {
+ $this->parseHdd($hdds, $section[2]);
+ }
+ if ($section[1] === 'PCI ID') {
+ $client['lspci1'] = $client['lspci2'] = array();
+ $this->parsePci($client['lspci1'], $client['lspci2'], $section[2]);
+ }
+ if (isset($hdds['hdds']) && $section[1] === 'smartctl') {
+ // This currently required that the partition table section comes first...
+ $this->parseSmartctl($hdds['hdds'], $section[2]);
+ }
+ }
+ }
+ unset($client['data']);
+ // Get locations
+ if (Module::isAvailable('locations')) {
+ if (is_null($client['locationid'])) {
+ $client['locationid'] = Location::getFromIp($client['clientip']);
+ }
+ $locs = Location::getLocationsAssoc();
+ $next = (int) $client['locationid'];
+ $output = array();
+ while (isset($locs[$next])) {
+ array_unshift($output, $locs[$next]);
+ $next = $locs[$next]['parentlocationid'];
+ }
+ $client['locations'] = $output;
+ }
+ // Throw output at user
+ Render::addTemplate('machine-main', $client);
+ // Sessions
+ $NOW = time();
+ $cutoff = $NOW - 86400 * 7;
+ //if ($cutoff < $client['firstseen']) $cutoff = $client['firstseen'];
+ $scale = 100 / ($NOW - $cutoff);
+ $res = Database::simpleQuery('SELECT dateline, typeid, data FROM statistic'
+ ." WHERE dateline > :cutoff AND typeid IN ('~session-length', '~offline-length') AND machineuuid = :uuid ORDER BY dateline ASC", array(
+ 'cutoff' => $cutoff - 86400 * 14,
+ 'uuid' => $uuid,
+ ));
+ $spans['rows'] = array();
+ $spans['graph'] = '';
+ $last = false;
+ $first = true;
+ while ($row = $res->fetch(PDO::FETCH_ASSOC)) {
+ if ($first && $row['dateline'] > $cutoff && $client['lastboot'] > $cutoff) {
+ // Special case: offline before
+ $spans['graph'] .= '<div style="background:#444;left:0%;width:'.round((min($row['dateline'], $client['lastboot']) - $cutoff) * $scale, 2).'%">&nbsp;</div>';
+ }
+ $first = false;
+ if ($row['dateline'] + $row['data'] < $cutoff || $row['data'] > 864000) {
+ continue;
+ }
+ if ($last !== false && abs($last['dateline'] - $row['dateline']) < 30
+ && abs($last['data'] - $row['data']) < 30) {
+ continue;
+ }
+ if ($last !== false && $last['dateline'] + $last['data'] > $row['dateline']) {
+ $point = $last['dateline'] + $last['data'];
+ $row['data'] -= ($point - $row['dateline']);
+ $row['dateline'] = $point;
+ }
+ if ($row['dateline'] < $cutoff) {
+ $row['data'] -= ($cutoff - $row['dateline']);
+ $row['dateline'] = $cutoff;
+ }
+ $row['from'] = date('d.m. H:i', $row['dateline']);
+ $row['duration'] = floor($row['data'] / 86400).'d '.gmdate('H:i', $row['data']);
+ if ($row['typeid'] === '~offline-length') {
+ $row['glyph'] = 'off';
+ $color = '#444';
+ } else {
+ $row['glyph'] = 'user';
+ $color = '#e77';
+ }
+ $spans['graph'] .= '<div style="background:'.$color.';left:'.round(($row['dateline'] - $cutoff) * $scale, 2).'%;width:'.round(($row['data']) * $scale, 2).'%">&nbsp;</div>';
+ $spans['rows'][] = $row;
+ $last = $row;
+ }
+ if ($first && $client['lastboot'] > $cutoff) {
+ // Special case: offline before
+ $spans['graph'] .= '<div style="background:#444;left:0%;width:'.round(($client['lastboot'] - $cutoff) * $scale, 2).'%">&nbsp;</div>';
+ }
+ if (isset($client['state_occupied'])) {
+ $spans['graph'] .= '<div style="background:#e99;left:'.round(($client['logintime'] - $cutoff) * $scale, 2).'%;width:'.round(($NOW - $client['logintime'] + 900) * $scale, 2).'%">&nbsp;</div>';
+ } elseif (isset($client['state_off'])) {
+ $spans['graph'] .= '<div style="background:#444;left:'.round(($client['lastseen'] - $cutoff) * $scale, 2).'%;width:'.round(($NOW - $client['lastseen'] + 900) * $scale, 2).'%">&nbsp;</div>';
+ }
+ $t = explode('-', date('Y-n-j-G', $cutoff));
+ if ($t[3] >= 8 && $t[3] <= 22) {
+ $start = mktime(22, 0, 0, $t[1], $t[2], $t[0]);
+ } else {
+ $start = mktime(22, 0, 0, $t[1], $t[2] - 1, $t[0]);
+ }
+ for ($i = $start; $i < $NOW; $i += 86400) {
+ $spans['graph'] .= '<div style="background:rgba(0,0,90,.2);left:'.round(($i - $cutoff) * $scale, 2).'%;width:'.round((10 * 3600) * $scale, 2).'%">&nbsp;</div>';
+ }
+ if (count($spans['rows']) > 10) {
+ $spans['hasrows2'] = true;
+ $spans['rows2'] = array_slice($spans['rows'], ceil(count($spans['rows']) / 2));
+ $spans['rows'] = array_slice($spans['rows'], 0, ceil(count($spans['rows']) / 2));
+ }
+ Render::addTemplate('machine-usage', $spans);
+ // Any hdds?
+ if (!empty($hdds['hdds'])) {
+ Render::addTemplate('machine-hdds', $hdds);
+ }
+ // Client log
+ $lres = Database::simpleQuery('SELECT logid, dateline, logtypeid, clientip, description, extra FROM clientlog'
+ .' WHERE clientip = :clientip ORDER BY logid DESC LIMIT 25', array('clientip' => $client['clientip']));
+ $today = date('d.m.Y');
+ $yesterday = date('d.m.Y', time() - 86400);
+ $count = 0;
+ $log = array();
+ while ($row = $lres->fetch(PDO::FETCH_ASSOC)) {
+ if (substr($row['description'], -5) === 'on :0' && strpos($row['description'], 'root logged') === false) {
+ continue;
+ }
+ $day = date('d.m.Y', $row['dateline']);
+ if ($day === $today) {
+ $day = Dictionary::translate('lang_today');
+ } elseif ($day === $yesterday) {
+ $day = Dictionary::translate('lang_yesterday');
+ }
+ $row['date'] = $day.date(' H:i', $row['dateline']);
+ $row['icon'] = $this->eventToIconName($row['logtypeid']);
+ $log[] = $row;
+ if (++$count === 10) {
+ break;
+ }
+ }
+ Render::addTemplate('syslog', array(
+ 'clientip' => $client['clientip'],
+ 'list' => $log,
+ ));
+ // Notes
+ Render::addTemplate('machine-notes', $client);
+ }
+
+ private function eventToIconName($event)
+ {
+ switch ($event) {
+ case 'session-open':
+ return 'glyphicon-log-in';
+ case 'session-close':
+ return 'glyphicon-log-out';
+ case 'partition-swap':
+ return 'glyphicon-info-sign';
+ case 'partition-temp':
+ case 'smartctl-realloc':
+ return 'glyphicon-exclamation-sign';
+ default:
+ return 'glyphicon-minus';
+ }
+ }
+
+ private function parseCpu(&$row, $data)
+ {
+ if (0 >= preg_match_all('/^(.+):\s+(\d+)$/im', $data, $out, PREG_SET_ORDER)) {
+ return;
+ }
+ foreach ($out as $entry) {
+ $row[str_replace(' ', '', $entry[1])] = $entry[2];
+ }
+ }
+
+ private function parseDmiDecode(&$row, $data)
+ {
+ $lines = preg_split("/[\r\n]+/", $data);
+ $section = false;
+ $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;
+ if (($ramForm || $ramType) && ($ramSpeed || $ramClockSpeed)) {
+ if (isset($row['ramtype']) && !$ramClockSpeed) {
+ continue;
+ }
+ $row['ramtype'] = $ramType.' '.$ramForm;
+ if ($ramClockSpeed) {
+ $row['ramtype'] .= ', '.$ramClockSpeed;
+ } elseif ($ramSpeed) {
+ $row['ramtype'] .= ', '.$ramSpeed;
+ }
+ $ramForm = false;
+ $ramType = false;
+ $ramClockSpeed = false;
+ }
+ continue;
+ }
+ if ($section === 'System Information' || $section === 'Base Board Information') {
+ if (empty($row['pcmodel']) && preg_match('/^\s*Product Name: +(\S.+?) *$/i', $line, $out)) {
+ $row['pcmodel'] = $out[1];
+ }
+ if (empty($row['manufacturer']) && preg_match('/^\s*Manufacturer: +(\S.+?) *$/i', $line, $out)) {
+ $row['manufacturer'] = $out[1];
+ }
+ } elseif ($section === 'Physical Memory Array') {
+ if (!$ramOk && preg_match('/Use: System Memory/i', $line)) {
+ $ramOk = true;
+ }
+ if ($ramOk && preg_match('/^\s*Number Of Devices: +(\S.+?) *$/i', $line, $out)) {
+ $row['ramslotcount'] = $out[1];
+ }
+ if ($ramOk && preg_match('/^\s*Maximum Capacity: +(\S.+?)\s*$/i', $line, $out)) {
+ $row['maxram'] = preg_replace('/([MGT])B/', '$1iB', $out[1]);
+ }
+ } elseif ($section === 'Memory Device') {
+ if (preg_match('/^\s*Size:\s*(.*?)\s*$/i', $line, $out)) {
+ $row['extram'] = true;
+ if (preg_match('/(\d+)\s*(\w)i?B/i', $out[1], $out)) {
+ $out[2] = strtoupper($out[2]);
+ if ($out[2] === 'K' || ($out[2] === 'M' && $out[1] < 500)) {
+ $ramForm = $ramType = $ramSpeed = $ramClockSpeed = false;
+ continue;
+ }
+ if ($out[2] === 'M' && $out[1] >= 1024) {
+ $out[2] = 'G';
+ $out[1] = floor(($out[1] + 100) / 1024);
+ }
+ $row['ramslot'][]['size'] = $out[1].' '.strtoupper($out[2]).'iB';
+ } elseif (!isset($row['ramslot']) || (count($row['ramslot']) < 8 && (!isset($row['ramslotcount']) || $row['ramslotcount'] <= 8))) {
+ $row['ramslot'][]['size'] = '_____';
+ }
+ }
+ if (preg_match('/^\s*Form Factor:\s*(.*?)\s*$/i', $line, $out) && $out[1] !== 'Unknown') {
+ $ramForm = $out[1];
+ }
+ if (preg_match('/^\s*Type:\s*(.*?)\s*$/i', $line, $out) && $out[1] !== 'Unknown') {
+ $ramType = $out[1];
+ }
+ if (preg_match('/^\s*Speed:\s*(\d.*?)\s*$/i', $line, $out)) {
+ $ramSpeed = $out[1];
+ }
+ if (preg_match('/^\s*Configured Clock Speed:\s*(\d.*?)\s*$/i', $line, $out)) {
+ $ramClockSpeed = $out[1];
+ }
+ }
+ }
+ if (empty($row['ramslotcount'])) {
+ $row['ramslotcount'] = count($row['ramslot']);
+ }
+ }
+
+ private function parseHdd(&$row, $data)
+ {
+ $hdds = array();
+ // Could have more than one disk - linear scan
+ $lines = preg_split("/[\r\n]+/", $data);
+ $dev = false;
+ $i = 0;
+ foreach ($lines as $line) {
+ if (preg_match('/^Disk (\S+):.* (\d+) bytes/i', $line, $out)) {
+ // disk total size and name
+ unset($hdd);
+ $unit = 0;
+ $hdd = array(
+ 'devid' => 'devid-'.++$i,
+ 'dev' => $out[1],
+ 'size' => round($out[2] / (1024 * 1024 * 1024)),
+ 'used' => 0,
+ 'partitions' => array(),
+ 'json' => array(),
+ );
+ $hdds[] = &$hdd;
+ } elseif (preg_match('/^Units =.*= (\d+) bytes/i', $line, $out)) {
+ // Unit for start and end
+ $unit = $out[1] / (1024 * 1024); // Convert so that multiplying by unit yields MiB
+ } elseif (isset($hdd) && $unit !== 0 && preg_match(',^/dev/(\S+)\s+.*\s(\d+)[\+\-]?\s+(\d+)[\+\-]?\s+\d+[\+\-]?\s+([0-9a-f]+)\s+(.*)$,i', $line, $out)) {
+ // Some partition
+ $type = strtolower($out[4]);
+ if ($type === '5' || $type === 'f' || $type === '85') {
+ continue;
+ }
+ $partsize = round(($out[3] - $out[2]) * $unit);
+ $hdd['partitions'][] = array(
+ 'id' => $out[1],
+ 'name' => $out[1],
+ 'size' => round($partsize / 1024, $partsize < 1024 ? 1 : 0),
+ 'type' => ($type === '44' ? 'OpenSLX' : $out[5]),
+ );
+ $hdd['json'][] = array(
+ 'label' => $out[1],
+ 'value' => $partsize,
+ 'color' => ($type === '44' ? '#4d4' : ($type === '82' ? '#48f' : '#e55')),
+ );
+ $hdd['used'] += $partsize;
+ }
+ }
+ unset($hdd);
+ $i = 0;
+ foreach ($hdds as &$hdd) {
+ $hdd['used'] = round($hdd['used'] / 1024);
+ $free = $hdd['size'] - $hdd['used'];
+ if ($free > 5) {
+ $hdd['partitions'][] = array(
+ 'id' => 'free-id-'.$i,
+ 'name' => Dictionary::translate('unused'),
+ 'size' => $free,
+ 'type' => '-',
+ );
+ $hdd['json'][] = array(
+ 'label' => 'free-id-'.$i,
+ 'value' => $free * 1024,
+ 'color' => '#aaa',
+ );
+ ++$i;
+ }
+ $hdd['json'] = json_encode($hdd['json']);
+ }
+ unset($hdd);
+ $row['hdds'] = &$hdds;
+ }
+
+ private function parsePci(&$pci1, &$pci2, $data)
+ {
+ preg_match_all('/[a-f0-9\:\.]{7}\s+"(Class\s*)?(?<class>[a-f0-9]{4})"\s+"(?<ven>[a-f0-9]{4})"\s+"(?<dev>[a-f0-9]{4})"/is', $data, $out, PREG_SET_ORDER);
+ $NOW = time();
+ $pci = array();
+ foreach ($out as $entry) {
+ if (!isset($pci[$entry['class']])) {
+ $class = 'c.'.$entry['class'];
+ $res = $this->getPciId('CLASS', $class);
+ if ($res === false || $res['dateline'] < $NOW) {
+ $pci[$entry['class']]['lookupClass'] = 'do-lookup';
+ $pci[$entry['class']]['class'] = $class;
+ } else {
+ $pci[$entry['class']]['class'] = $res['value'];
+ }
+ }
+ $new = array(
+ 'ven' => $entry['ven'],
+ 'dev' => $entry['ven'].':'.$entry['dev'],
+ );
+ $res = $this->getPciId('VENDOR', $new['ven']);
+ if ($res === false || $res['dateline'] < $NOW) {
+ $new['lookupVen'] = 'do-lookup';
+ } else {
+ $new['ven'] = $res['value'];
+ }
+ $res = $this->getPciId('DEVICE', $new['ven'].':'.$new['dev']);
+ if ($res === false || $res['dateline'] < $NOW) {
+ $new['lookupDev'] = 'do-lookup';
+ } else {
+ $new['dev'] = $res['value'];
+ }
+ $pci[$entry['class']]['entries'][] = $new;
+ }
+ ksort($pci);
+ foreach ($pci as $class => $entry) {
+ if ($class === '0300' || $class === '0200' || $class === '0403') {
+ $pci1[] = $entry;
+ } else {
+ $pci2[] = $entry;
+ }
+ }
+ }
+
+ private function parseSmartctl(&$hdds, $data)
+ {
+ $lines = preg_split("/[\r\n]+/", $data);
+ $i = 0;
+ foreach ($lines as $line) {
+ if (preg_match('/^NEXTHDD=(.+)$/', $line, $out)) {
+ unset($dev);
+ foreach ($hdds as &$hdd) {
+ if ($hdd['dev'] === $out[1]) {
+ $dev = &$hdd;
+ }
+ }
+ continue;
+ }
+ if (!isset($dev)) {
+ continue;
+ }
+ if (preg_match('/^([A-Z][^:]+):\s*(.*)$/', $line, $out)) {
+ $dev['s_'.preg_replace('/\s|-|_/', '', $out[1])] = $out[2];
+ } elseif (preg_match('/^\s*\d+\s+(\S+)\s+\S+\s+\d+\s+\d+\s+\d+\s+\S+\s+(\d+)(\s|$)/', $line, $out)) {
+ $dev['s_'.preg_replace('/\s|-|_/', '', $out[1])] = $out[2];
+ }
+ }
+ // Format strings
+ foreach ($hdds as &$hdd) {
+ if (isset($hdd['s_PowerOnHours'])) {
+ $hdd['PowerOnTime'] = '';
+ $val = (int) $hdd['s_PowerOnHours'];
+ if ($val > 8760) {
+ $hdd['PowerOnTime'] .= floor($val / 8760).'Y, ';
+ $val %= 8760;
+ }
+ if ($val > 720) {
+ $hdd['PowerOnTime'] .= floor($val / 720).'M, ';
+ $val %= 720;
+ }
+ if ($val > 24) {
+ $hdd['PowerOnTime'] .= floor($val / 24).'d, ';
+ $val %= 24;
+ }
+ $hdd['PowerOnTime'] .= $val.'h';
+ }
+ }
+ }
+
+ protected function doAjax()
+ {
+ $param = Request::any('lookup', false, 'string');
+ if ($param === false) {
+ die('No lookup given');
+ }
+ $add = '';
+ if (preg_match('/^([a-f0-9]{4}):([a-f0-9]{4})$/', $param, $out)) {
+ $cat = 'DEVICE';
+ $host = $out[2].'.'.$out[1];
+ $add = ' ('.$param.')';
+ } elseif (preg_match('/^([a-f0-9]{4})$/', $param, $out)) {
+ $cat = 'VENDOR';
+ $host = $out[1];
+ } elseif (preg_match('/^c\.([a-f0-9]{2})([a-f0-9]{2})$/', $param, $out)) {
+ $cat = 'CLASS';
+ $host = $out[2].'.'.$out[1].'.c';
+ } else {
+ die('Invalid format requested');
+ }
+ $cached = $this->getPciId($cat, $param);
+ if ($cached !== false && $cached['dateline'] > time()) {
+ echo $cached['value'], $add;
+ exit;
+ }
+ $res = dns_get_record($host.'.pci.id.ucw.cz', DNS_TXT);
+ if (is_array($res)) {
+ foreach ($res as $entry) {
+ if (isset($entry['txt']) && substr($entry['txt'], 0, 2) === 'i=') {
+ $string = substr($entry['txt'], 2);
+ $this->setPciId($cat, $param, $string);
+ echo $string, $add;
+ exit;
+ }
+ }
+ }
+ if ($cached !== false) {
+ echo $cached['value'], $add;
+ exit;
+ }
+ die('Not found');
+ }
+
+ private function getPciId($cat, $id)
+ {
+ return Database::queryFirst('SELECT value, dateline FROM pciid WHERE category = :cat AND id = :id LIMIT 1',
+ array('cat' => $cat, 'id' => $id));
+ }
+
+ private function setPciId($cat, $id, $value)
+ {
+ Database::exec('INSERT INTO pciid (category, id, value, dateline) VALUES (:cat, :id, :value, :timeout)'
+ .' ON DUPLICATE KEY UPDATE value = VALUES(value), dateline = VALUES(dateline)',
+ array(
+ 'cat' => $cat,
+ 'id' => $id,
+ 'value' => $value,
+ 'timeout' => time() + mt_rand(10, 30) * 86400,
+ ), true);
+ }
}
diff --git a/modules-available/statistics/templates/clientlist.html b/modules-available/statistics/templates/clientlist.html
index 8e8565fe..75ea1064 100644
--- a/modules-available/statistics/templates/clientlist.html
+++ b/modules-available/statistics/templates/clientlist.html
@@ -1,16 +1,102 @@
+
+<div id="modal-add-filter" class="modal modal-sm fade" role="dialog" style="position:absolute; min-width:600px; min-height: 400px;margin:auto">
+ <div class="modal-content">
+ <div class="modal-header">
+ {{lang_add_filter}}
+ </div>
+ <form class="modal-body form-inline center">
+ <div class="form-group">
+ <label for="column"></label>
+ <select id="columnSelect" name="column" class="form-control col-4-xs"> </select>
+ </div>
+ <div class="form-group">
+ <label for="operator"></label>
+ <select id="operatorSelect" name="operator" class="form-control col-4-xs"> </select>
+ </div>
+ <div class="form-group">
+ <label for="argument"></label>
+ <input name="argument" id="argumentInput" class="form-control col-4-xs"> </input>
+ <select name="argument" id="argumentSelect" class="form-control col-4-xs"> </select>
+ </div>
+ <button type="button" class="btn btn-primary" onclick="addFilter()" >
+ <span class="glyphicon glyphicon-plus"></span>
+ {{lang_add}}</button>
+ </form>
+ </div>
+</div>
+
+
+
<h1>{{lang_clientList}}</h1>
-<div class="pull-right">{{filter}} ~= {{argument}}</div>
-<div class="clearfix"></div>
+
+<div>
+ <!-- use GET here, to avoid the "resend form?" confirmation, and anyway this is stateless, so GET makes more sense -->
+<form id="queryForm" method="GET" action="?do=Statistics&show=list" class="" role="form">
+ <input type="hidden" name="token" value="{{token}}"/>
+ <input type="hidden" name="do" value="Statistics"/>
+ <input type="hidden" name="show" value="list"/>
+ <label for="filters">{{lang_labelFilter}}</label>
+ <input type="text" name="filters" class="" id="filterInput"/>
+ <input type="hidden" name="sortColumn" id="sortColumn" value="{{sortColumn}}"/>
+ <input type="hidden" name="sortDirection" id="sortDirection" value="{{sortDirection}}"/>
+
+ <button type="button" class="btn btn-success" onclick="popupFilter(null)">
+ <span class="glyphicon glyphicon-plus"></span>
+ {{lang_add_filter}}</button>
+ <button class="btn btn-primary pull-right" type="submit">
+ <span class="glyphicon glyphicon-refresh"></span>
+ {{lang_refresh}}</button>
+</form>
+<br/>
+<br/>
+</div>
<table class="table table-condensed table-striped">
<tr>
<th>{{lang_machine}}</th>
- <th>{{lang_address}}</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>
- <th>{{lang_cpuModel}}</th>
+ <th>{{lang_address}}
+ <div class="btn-group pull-right">
+ <button class="btn btn-default btn-xs" id="sortButton-clientip"></button>
+ </div>
+
+ </th>
+ <th class="text-right">{{lang_lastSeen}}
+ <div class="btn-group pull-right">
+ <button class="btn btn-default btn-xs" id="sortButton-lastseen"></button>
+ <button class="btn btn-xs btn-filter ">
+ <span id="btn_filter_lastseen" class="glyphicon glyphicon-filter" onclick="popupFilter('lastseen')"></span>
+ </button>
+ </div>
+ </th>
+ <th>
+ {{lang_kvmSupport}}
+ <div class="btn-group pull-right">
+ <button class="btn btn-default btn-xs" id="sortButton-kvmstate"></button>
+ <button class="btn btn-xs btn-filter ">
+ <span id="btn_filter_kvmstate" class="glyphicon glyphicon-filter" onclick="popupFilter('kvmstate')"></span>
+ </button>
+ </div>
+ </th>
+ <th class="text-right">
+ {{lang_gbRam}}
+ <div class="btn-group pull-right">
+ <button class="btn btn-default btn-xs" id="sortButton-gbram"></button>
+ <button class="btn btn-default btn-xs" ><span id="btn_filter_gbram" class="glyphicon glyphicon-filter" onclick="popupFilter('gbram')"></span></button>
+ </div>
+ </th>
+ <th class="text-right">
+ {{lang_tmpGb}}
+ <div class="btn-group pull-right">
+ <button class="btn btn-default btn-xs" id="sortButton-hddgb"></button>
+ <button class="btn btn-xs btn-filter "> <span id="btn_filter_hddgb" class="glyphicon glyphicon-filter" onclick="popupFilter('hddgb')"></span></button>
+ </div>
+ </th>
+ <th>{{lang_cpuModel}}
+ <div class="btn-group pull-right">
+ <button class="btn btn-default btn-xs" id="sortButton-realcores"></button>
+ <button class="btn btn-xs btn-filter"><span id="btn_filter_cpu" class="glyphicon glyphicon-filter" onclick="popupFilter('realcores')"></span></button>
+ </div>
+ </th>
</tr>
{{#rows}}
<tr>
@@ -28,7 +114,7 @@
<a href="?do=Statistics&amp;uuid={{machineuuid}}"><b>{{hostname}}</b></a>
<div class="small">{{machineuuid}}</div>
</td>
- <td><b><a href="?do=Statistics&amp;filter=subnet&amp;argument={{subnet}}">{{subnet}}</a>{{lastoctet}}</b><br>{{macaddr}}</td>
+ <td><b><a href="?do=Statistics&amp;show=list&amp;filters=subnet={{subnet}}">{{subnet}}</a>{{lastoctet}}</b><br>{{macaddr}}</td>
<td class="text-right">{{lastseen}}</td>
<td class="{{kvmclass}}">{{kvmstate}}</td>
<td class="text-right {{ramclass}}">{{gbram}}&thinsp;GiB</td>
@@ -43,3 +129,163 @@
</tr>
{{/rows}}
</table>
+<script type="application/javascript"><!--
+
+var filterSelectize;
+
+document.addEventListener("DOMContentLoaded", function () {
+
+ var initComplete = false;
+ var comp = ['=', '!='];
+ var ordered = ['<', '<=', '=', '>=', '>'];
+ // var filter = {
+ // 'machine' : {'op' : comp, 'type' : 'string'},
+ // 'ram' : {'op' : ordered, 'type' : 'string'},
+ // 'lastSeen' : {'op' : ordered, 'type' : 'date'},
+ // };
+
+ /* some objects */
+ var $columnSelect = $('#columnSelect');
+ $modal = $('#modal-add-filter');
+ $queryForm = $('#queryForm');
+
+ var columns= {{{columns}}};
+
+ /* add options to column select */
+ for (var key in columns) {
+ $columnSelect.append($('<option>', {
+ value: key, text: key}));
+ };
+
+
+ /* initialize selectize */
+ filterSelectize = $('#filterInput').selectize({
+ delimiter: ',',
+ plugins: ['restore_on_backspace', 'remove_button'],
+ create: function(input) {
+ return {value: input, text: input}
+ },
+ onChange: function() {
+ // if (initComplete && !$('#filterInput').is(':focus')) {
+ // reload();
+ // }
+ }
+ })[0].selectize;
+ /* add query */
+ var str = "{{{query}}}";
+ str.split(",").forEach(function(v) {
+ filterSelectize.addOption({value: v, text: v});
+ filterSelectize.addItem(v);
+ });
+
+ $('#columnSelect').on('change', function() {
+ $('#operatorSelect option').remove();
+ var col = $('#columnSelect').val();
+ var opS = $('#operatorSelect');
+ columns[col]['op'].sort(myOpSort);
+ columns[col]['op'].forEach(function (v) {
+ $(opS).append($('<option>', {
+ value: v, text: v
+ }));
+ });
+ /* also set the type of the input */
+ if (columns[col]['type'] == 'date') {
+ $('#argumentInput').datepicker({format : 'yyyy-mm-dd'});
+ $('#argumentSelect').hide();
+ } else if(columns[col]['type'] == 'enum') {
+ $('#argumentInput').hide();
+ $('#argumentSelect').show();
+ columns[col]['values'].forEach(function (v) {
+ $('#argumentSelect').append($('<option>', { value: v, text: v }));
+ });
+ } else {
+ $('#argumentInput').datepicker('remove');
+ $('#argumentSelect option').remove();
+ $('#argumentInput').show();
+ $('#argumentSelect').hide();
+ }
+ });
+
+ initButtons();
+ initComplete = true;
+
+}, false);
+
+
+function initButtons() {
+ ['gbram', 'hddgb', 'realcores', 'kvmstate', 'lastseen', 'clientip'].forEach(function (v) {
+ var $sortBtn = $('#sortButton-' + v);
+ var order = 'up'; /* default */
+ if ($('#sortColumn').val() == v) {
+ $sortBtn.addClass('btn-success');
+ order = $('#sortDirection').val() == 'ASC' ? 'up' : 'down';
+ }
+ $sortBtn.html('<span class="glyphicon glyphicon-arrow-' + order + '"></span>');
+ $sortBtn.attr('onclick', 'toggleButton(\'' + v + '\');');
+ });
+}
+
+function toggleButton(v) {
+ var $sortBtn = $('#sortButton-' + v);
+ var $col = $('#sortColumn');
+ var $dir = $('#sortDirection');
+ if ($col.val() == v) {
+ /* toggle direction */
+ var newDir = $dir.val() == 'ASC' ? 'DESC' : 'ASC';
+ $dir.val(newDir);
+ /* update button */
+ var order = newDir == 'ASC' ? 'up' : 'down';
+ $sortBtn.html('<span class="glyphicon glyphicon-arrow-' + order + '"></span>');
+ } else {
+ /* remove "btn-success" from current sorting */
+ $('#sortButton-'+v).removeClass('btn-success');
+ $sortBtn.addClass('btn-success');
+ $col.val(v);
+ $dir = 'ASC';
+ }
+ refresh();
+}
+
+function popupFilter(field) {
+ if (field != null) {
+ $('#columnSelect').val(field);
+ }
+ $('#columnSelect').change();
+ $modal.modal('show');
+}
+
+function addFilter() {
+ var argument1 = $('#argumentInput').val();
+ var argument2 = $('#argumentSelect').val();
+ var argument = argument1 == '' ? argument2 : argument1;
+
+ var ft = $('#columnSelect').val() + ' ' + $('#operatorSelect').val() + ' ' + argument;
+ filterSelectize.addOption({value: ft, text: ft});
+ filterSelectize.addItem(ft);
+
+ //$modal.modal('hide');
+ refresh();
+
+ // $('#columnSelect').val('');
+ // $('#operatorSelect option').remove();
+ // $('#argumentInput').val('');
+}
+
+function toggleSort(field) {
+ $('#sort').val(field + ' ' + order);
+ refresh();
+}
+
+/* equal sign should always be first, the rest doesn't matter*/
+function myOpSort(a,b) {
+ if (a == '=') { return -1; }
+ else if (a == b) {return 0}
+ else { return 1;}
+
+}
+
+function refresh() {
+ console.log('refresh');
+ $queryForm.submit(); /* TODO: use AJAX */
+}
+// --></script>
diff --git a/modules-available/statistics/templates/cpumodels.html b/modules-available/statistics/templates/cpumodels.html
index d87118ef..87f161bb 100644
--- a/modules-available/statistics/templates/cpumodels.html
+++ b/modules-available/statistics/templates/cpumodels.html
@@ -15,9 +15,9 @@
{{#rows}}
<tr id="{{id}}">
<td class="text-left slx-nowrap">
- <a href="?do=Statistics&amp;filter=systemmodel&amp;argument={{urlsystemmodel}}">{{systemmodel}}</a>
+ <a href="?do=Statistics&amp;show=list&amp;filters=systemmodel={{urlsystemmodel}}">{{systemmodel}}</a>
</td>
- <td class="text-right"><a href="?do=Statistics&amp;filter=realcores&amp;argument={{cores}}">{{cores}}</a></td>
+ <td class="text-right"><a href="?do=Statistics&amp;show=list&amp;filters=realcores={{cores}}">{{cores}}</a></td>
<td class="text-right">{{count}}</td>
</tr>
{{/rows}}
diff --git a/modules-available/statistics/templates/id44.html b/modules-available/statistics/templates/id44.html
index 4eea9413..1a00fc8c 100644
--- a/modules-available/statistics/templates/id44.html
+++ b/modules-available/statistics/templates/id44.html
@@ -13,7 +13,7 @@
</tr>
{{#rows}}
<tr id="tmpid{{gb}}" class="{{class}}">
- <td class="text-left slx-nowrap"><a href="?do=Statistics&amp;filter=hddgb&amp;argument={{gb}}">{{gb}}&thinsp;GiB</td>
+ <td class="text-left slx-nowrap"><a href="?do=Statistics&amp;show=list&amp;filters=hddgb={{gb}}">{{gb}}&thinsp;GiB</td>
<td class="text-right">{{count}}</td>
</tr>
{{/rows}}
@@ -45,4 +45,4 @@
</div>
</div>
</div>
-</div> \ No newline at end of file
+</div>
diff --git a/modules-available/statistics/templates/kvmstate.html b/modules-available/statistics/templates/kvmstate.html
index 07ecd558..af163893 100644
--- a/modules-available/statistics/templates/kvmstate.html
+++ b/modules-available/statistics/templates/kvmstate.html
@@ -13,7 +13,7 @@
</tr>
{{#rows}}
<tr id="kvm{{kvmstate}}">
- <td class="text-left slx-nowrap"><a href="?do=Statistics&amp;filter=kvmstate&amp;argument={{kvmstate}}">{{kvmstate}}</a></td>
+ <td class="text-left slx-nowrap"><a href="?do=Statistics&amp;show=list&amp;filters=kvmstate={{kvmstate}}">{{kvmstate}}</a></td>
<td class="text-right">{{count}}</td>
</tr>
{{/rows}}
@@ -44,4 +44,4 @@
</div>
</div>
</div>
-</div> \ No newline at end of file
+</div>
diff --git a/modules-available/statistics/templates/memory.html b/modules-available/statistics/templates/memory.html
index 537ee3bd..efa7c44b 100644
--- a/modules-available/statistics/templates/memory.html
+++ b/modules-available/statistics/templates/memory.html
@@ -13,7 +13,7 @@
</tr>
{{#rows}}
<tr id="ramid{{gb}}" class="{{class}}">
- <td class="text-left slx-nowrap"><a href="?do=Statistics&amp;filter=gbram&amp;argument={{gb}}">{{gb}}&thinsp;GiB</a></td>
+ <td class="text-left slx-nowrap"><a href="?do=Statistics&amp;show=list&amp;filters=gbram={{gb}}">{{gb}}&thinsp;GiB</a></td>
<td class="text-right">{{count}}</td>
</tr>
{{/rows}}
@@ -44,4 +44,4 @@
</div>
</div>
</div>
-</div> \ No newline at end of file
+</div>
diff --git a/modules-available/statistics/templates/summary.html b/modules-available/statistics/templates/summary.html
index 5f16fd89..f71620f9 100644
--- a/modules-available/statistics/templates/summary.html
+++ b/modules-available/statistics/templates/summary.html
@@ -3,13 +3,13 @@
<div class="panel-body">
<div>
{{lang_knownMachines}}: <b>{{known}}</b>&emsp;
- <a href="?do=Statistics&amp;filter=state&amp;argument=on">{{lang_onlineMachines}}</a>: <b>{{online}}</b>&emsp;
- <a href="?do=Statistics&amp;filter=state&amp;argument=occupied">{{lang_inUseMachines}}</a>: <b>{{used}}</b> (<b>{{usedpercent}}%</b>)
+ <a href="?do=Statistics&amp;show=list&amp;filters=state=on">{{lang_onlineMachines}}</a>: <b>{{online}}</b>&emsp;
+ <a href="?do=Statistics&amp;show=list&amp;filters=state=occupied">{{lang_inUseMachines}}</a>: <b>{{used}}</b> (<b>{{usedpercent}}%</b>)
</div>
{{#badhdd}}
<div>
<span class="glyphicon glyphicon-exclamation-sign red"></span>
- <a href="?do=Statistics&amp;filter=badsectors&amp;argument=10">
+ <a href="?do=Statistics&amp;show=list&amp;filters=badsectors>=10">
{{lang_withBadSectors}}: <b>{{badhdd}}</b>
</a>
</div>