From 7e49d38c57bae516ca6fc155849bbeceeefb3b2b Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 13 Jul 2016 14:23:06 +0200 Subject: [statistics] Fix indentation --- modules-available/statistics/page.inc.php | 2146 +++++++++++++++-------------- 1 file changed, 1074 insertions(+), 1072 deletions(-) (limited to 'modules-available/statistics/page.inc.php') diff --git a/modules-available/statistics/page.inc.php b/modules-available/statistics/page.inc.php index 58239dad..d46468f9 100644 --- a/modules-available/statistics/page.inc.php +++ b/modules-available/statistics/page.inc.php @@ -4,7 +4,7 @@ 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); @@ -12,1123 +12,1125 @@ $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; + /* 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; - /* 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 = ['!~', '~', '=', '!=']; + /* 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, - ], + 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); - } - } + '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 doRender() - { - $uuid = Request::get('uuid', false, 'string'); - if ($uuid !== false) { - $this->showMachine($uuid); + 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); + } + } - 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); + protected function doRender() + { + $uuid = Request::get('uuid', false, 'string'); + if ($uuid !== false) { + $this->showMachine($uuid); - return; - } - Render::openTag('div', array('class' => 'row')); - $this->showSummary(); - $this->showMemory(); - $this->showId44(); - $this->showKvmState(); - $this->showLatestMachines(); - $this->showSystemModels(); - Render::closeTag('div'); - } + 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); - 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), - ); - } - } + return; + } + Render::openTag('div', array('class' => 'row')); + $this->showSummary(); + $this->showMemory(); + $this->showId44(); + $this->showKvmState(); + $this->showLatestMachines(); + $this->showSystemModels(); + Render::closeTag('div'); + } - 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 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 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 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 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 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 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 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 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 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 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 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); + private function showMachineList($filter, $argument) + { + $query = Request::any('filters'); + $sortColumn = Request::any('sortColumn'); + $sortDirection = Request::any('sortDirection'); - /* 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)); + $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 { - $sort = ""; - } + 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 { + $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 = ""; - // } + // /* 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')) { - 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'); + // /* 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 = ""; + // } - // 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 = []; - // } + 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'); - // $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), )); - } + // 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 = []; + // } - private function ramColorClass($mb) - { - if ($mb < 1500) { - return 'danger'; - } - if ($mb < 2500) { - return 'warning'; - } + // $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),)); + } - return ''; - } + private function ramColorClass($mb) + { + if ($mb < 1500) { + return 'danger'; + } + if ($mb < 2500) { + return 'warning'; + } - private function kvmColorClass($state) - { - if ($state === 'DISABLED') { - return 'danger'; - } - if ($state === 'UNKNOWN' || $state === 'UNSUPPORTED') { - return 'warning'; - } + return ''; + } - return ''; - } + private function kvmColorClass($state) + { + if ($state === 'DISABLED') { + return 'danger'; + } + if ($state === 'UNKNOWN' || $state === 'UNSUPPORTED') { + return 'warning'; + } - private function hddColorClass($gb) - { - if ($gb < 7) { - return 'danger'; - } - if ($gb < 25) { - return 'warning'; - } + return ''; + } - return ''; - } + private function hddColorClass($gb) + { + if ($gb < 7) { + return 'danger'; + } + if ($gb < 25) { + return 'warning'; + } - 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 ''; + } - return ($array[$best] + $array[$best - 1]) / 2; - } + 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; + } - 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']; - } - } + return ($array[$best] + $array[$best - 1]) / 2; + } - 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'] .= '