From 4537b9b7c9eb63bf3b2d5066d6c50e68f7de4956 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 11 Jan 2018 11:19:14 +0100 Subject: [statistics] Add "replace machines" feature (not linked to anywhere yet) --- .../dnbd3/hooks/statistics-machine-replace.inc.php | 6 ++ .../hooks/statistics-machine-replace.inc.php | 7 ++ .../hooks/statistics-machine-replace.inc.php | 6 ++ modules-available/statistics/lang/de/messages.json | 6 +- .../statistics/lang/de/template-tags.json | 6 ++ modules-available/statistics/lang/en/messages.json | 6 +- .../statistics/lang/en/template-tags.json | 6 ++ modules-available/statistics/page.inc.php | 6 +- modules-available/statistics/pages/replace.inc.php | 119 +++++++++++++++++++++ .../statistics/templates/page-replace.html | 4 + .../hooks/statistics-machine-replace.inc.php | 5 + 11 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 modules-available/dnbd3/hooks/statistics-machine-replace.inc.php create mode 100644 modules-available/roomplanner/hooks/statistics-machine-replace.inc.php create mode 100644 modules-available/runmode/hooks/statistics-machine-replace.inc.php create mode 100644 modules-available/statistics/pages/replace.inc.php create mode 100644 modules-available/syslog/hooks/statistics-machine-replace.inc.php (limited to 'modules-available') diff --git a/modules-available/dnbd3/hooks/statistics-machine-replace.inc.php b/modules-available/dnbd3/hooks/statistics-machine-replace.inc.php new file mode 100644 index 00000000..5e4c4e75 --- /dev/null +++ b/modules-available/dnbd3/hooks/statistics-machine-replace.inc.php @@ -0,0 +1,6 @@ + $cat, 'id' => $id)); } diff --git a/modules-available/statistics/pages/replace.inc.php b/modules-available/statistics/pages/replace.inc.php new file mode 100644 index 00000000..80bbf0ce --- /dev/null +++ b/modules-available/statistics/pages/replace.inc.php @@ -0,0 +1,119 @@ + $split[0], 'new' => $split[1]]; + $old = Database::queryFirst('SELECT lastseen FROM machine WHERE machineuuid = :old', + ['old' => $entry['old']]); + if ($old === false) { + Message::addError('unknown-machine', $entry['old']); + continue; + } + $new = Database::queryFirst('SELECT firstseen FROM machine WHERE machineuuid = :new', + ['new' => $entry['new']]); + if ($new === false) { + Message::addError('unknown-machine', $entry['new']); + continue; + } + if ($old['lastseen'] - 86400*7 > $new['firstseen']) { + Message::addWarning('ignored-both-in-use', $entry['old'], $entry['new']); + continue; + } + $entry['datelimit'] = min($new['firstseen'], $old['lastseen']); + $list[] = $entry; + } + if (empty($list)) { + Message::addError('main.parameter-empty', 'replace'); + return; + } + + // First handle module internal tables + foreach ($list as $entry) { + Database::exec('UPDATE statistic SET machineuuid = :new WHERE machineuuid = :old AND dateline < :datelimit', $entry); + } + + // Let other modules do their thing + $fun = function($file, $list) { + include $file; + }; + foreach (Hook::load('statistics-machine-replace') as $hook) { + $fun($hook->file, $list); + } + + // Finalize by updating machine table + foreach ($list as $entry) { + unset($entry['datelimit']); + Database::exec('UPDATE machine old, machine new SET + new.fixedlocationid = old.fixedlocationid, + new.position = old.position, + old.position = NULL, + new.notes = old.notes, + old.notes = NULL, + old.lastseen = new.firstseen + WHERE old.machineuuid = :old AND new.machineuuid = :new', $entry); + } + Message::addSuccess('x-machines-replaced', count($list)); + } + + public static function doRender() + { + self::listSuggestions(); + } + + private static function listSuggestions() + { + if (Request::get('debug', false) !== false) { + $oldCutoff = time() - 86400 * 180; + $newCutoff = time() - 86400 * 180; + } else { + $oldCutoff = time() - 86400 * 90; + $newCutoff = time() - 86400 * 30; + } + $res = Database::simpleQuery("SELECT + old.machineuuid AS olduuid, old.locationid AS oldlid, old.hostname AS oldhost, + old.clientip AS oldip, old.macaddr AS oldmac, old.lastseen AS oldlastseen, old.systemmodel AS oldmodel, + new.machineuuid AS newuuid, new.locationid AS newlid, new.hostname AS newhost, + new.clientip AS newip, new.macaddr AS newmac, new.firstseen AS newfirstseen, new.systemmodel AS newmodel + FROM machine old INNER JOIN machine new ON (old.clientip = new.clientip AND old.lastseen < new.firstseen AND old.lastseen > $oldCutoff AND new.firstseen > $newCutoff) + ORDER BY oldhost ASC, oldip ASC"); + $list = []; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $row['oldlastseen_s'] = Util::prettyTime($row['oldlastseen']); + $row['newfirstseen_s'] = Util::prettyTime($row['newfirstseen']); + $list[] = $row; + } + $data = array('pairs' => $list); + Render::addTemplate('page-replace', $data); + if (empty($list)) { + Message::addInfo('no-replacement-matches'); + } + } + +} + diff --git a/modules-available/statistics/templates/page-replace.html b/modules-available/statistics/templates/page-replace.html index f87610a2..d0e9f766 100644 --- a/modules-available/statistics/templates/page-replace.html +++ b/modules-available/statistics/templates/page-replace.html @@ -17,6 +17,10 @@ } +

+ {{lang_replaceInstructions}} +

+
diff --git a/modules-available/syslog/hooks/statistics-machine-replace.inc.php b/modules-available/syslog/hooks/statistics-machine-replace.inc.php new file mode 100644 index 00000000..6be0dd76 --- /dev/null +++ b/modules-available/syslog/hooks/statistics-machine-replace.inc.php @@ -0,0 +1,5 @@ +