From 9192cb92901349a0ddb6940b4f0ebe6110f3b9ab Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 22 Aug 2016 12:12:30 +0200 Subject: [statistics] Handle lastboot = 0 as offline, fix hdd parsing, add raw display --- modules-available/statistics/inc/parser.inc.php | 4 +++- modules-available/statistics/page.inc.php | 28 +++++++++++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/modules-available/statistics/inc/parser.inc.php b/modules-available/statistics/inc/parser.inc.php index 1ecfbbf8..07bed8e5 100644 --- a/modules-available/statistics/inc/parser.inc.php +++ b/modules-available/statistics/inc/parser.inc.php @@ -103,6 +103,8 @@ class Parser { $i = 0; foreach ($lines as $line) { if (preg_match('/^Disk (\S+):.* (\d+) bytes/i', $line, $out)) { + if ($out[2] < 10000) // sometimes vmware reports lots of 512byte disks + continue; // disk total size and name unset($hdd); $unit = 0; @@ -144,7 +146,7 @@ class Parser { foreach ($hdds as &$hdd) { $hdd['used'] = round($hdd['used'] / 1024); $free = $hdd['size'] - $hdd['used']; - if ($free > 5) { + if ($free > 5 || ($free / $hdd['size']) > 0.1) { $hdd['partitions'][] = array( 'id' => 'free-id-' . $i, 'name' => Dictionary::translate('unused'), diff --git a/modules-available/statistics/page.inc.php b/modules-available/statistics/page.inc.php index ab0afcea..0639d784 100644 --- a/modules-available/statistics/page.inc.php +++ b/modules-available/statistics/page.inc.php @@ -136,6 +136,8 @@ class Page_Statistics extends Page Message::addSuccess('notes-saved'); Util::redirect('?do=Statistics&uuid=' . $uuid); } + // Fix online state of machines that crashed -- TODO: Make cronjob for this + Database::exec("UPDATE machine SET lastboot = 0 WHERE lastseen < UNIX_TIMESTAMP() - 610"); } protected function doRender() @@ -253,10 +255,9 @@ class Page_Statistics extends Page { $filterSet->makeFragments($where, $join, $sort, $args); - $online = time() - 610; $known = Database::queryFirst("SELECT Count(*) AS val FROM machine $join WHERE ($where)", $args); - $on = Database::queryFirst("SELECT Count(*) AS val FROM machine $join WHERE lastseen > $online AND ($where)", $args); - $used = Database::queryFirst("SELECT Count(*) AS val FROM machine $join WHERE lastseen > $online AND logintime <> 0 AND ($where)", $args); + $on = Database::queryFirst("SELECT Count(*) AS val FROM machine $join WHERE lastboot <> 0 AND ($where)", $args); + $used = Database::queryFirst("SELECT Count(*) AS val FROM machine $join WHERE lastboot <> 0 AND logintime <> 0 AND ($where)", $args); $hdd = Database::queryFirst("SELECT Count(*) AS val FROM machine $join WHERE badsectors >= 10 AND ($where)", $args); if ($on['val'] != 0) { $usedpercent = round($used['val'] / $on['val'] * 100); @@ -500,7 +501,7 @@ class Page_Statistics extends Page $rows = array(); $NOW = time(); while ($row = $res->fetch(PDO::FETCH_ASSOC)) { - if ($NOW - $row['lastseen'] > 610) { + if ($row['lastboot'] == 0) { $row['state_off'] = true; } elseif ($row['logintime'] == 0) { $row['state_idle'] = true; @@ -620,9 +621,14 @@ class Page_Statistics extends Page $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)); + // Hack: Get raw collected data + if (Request::get('raw', false)) { + Header('Content-Type: text/plain; charset=utf-8'); + die($client['data']); + } // Mangle fields $NOW = time(); - if ($NOW - $client['lastseen'] > 610) { + if ($client['lastboot'] == 0) { $client['state_off'] = true; } elseif ($client['logintime'] == 0) { $client['state_idle'] = true; @@ -632,10 +638,14 @@ class Page_Statistics extends Page } $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) . ')'; + if ($client['lastboot'] == 0) { + $client['lastboot_s'] = '-'; + } else { + $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); -- cgit v1.2.3-55-g7522