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) --- modules-available/statistics/pages/replace.inc.php | 119 +++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 modules-available/statistics/pages/replace.inc.php (limited to 'modules-available/statistics/pages') 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'); + } + } + +} + -- cgit v1.2.3-55-g7522