From e8f1f1a46d1f188c1587855e78b40fb9c5b4c085 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Thu, 8 Feb 2018 18:27:48 +0100 Subject: [PATCH] fix(mqtt): follow best practices fix #313 Signed-off-by: Thierry Bugier --- inc/agent.class.php | 8 +-- inc/fleet.class.php | 2 +- inc/mqtthandler.class.php | 29 +++++----- install/upgrade/update_to_dev.php | 94 ++++++++++++++++++++++++++++++- 4 files changed, 114 insertions(+), 19 deletions(-) diff --git a/inc/agent.class.php b/inc/agent.class.php index 18d12488..4947022d 100644 --- a/inc/agent.class.php +++ b/inc/agent.class.php @@ -903,7 +903,7 @@ public function getTopic() { $serial = $computer->getField('serial'); if (strlen($serial)) { $entity = $this->getField('entities_id'); - $this->topic = "/$entity/agent/$serial"; + $this->topic = "$entity/agent/$serial"; } } } @@ -921,10 +921,10 @@ public function getByTopic($topic) { global $DB; $mqttPath = explode('/', $topic); - if (isset($mqttPath[3])) { - if ($mqttPath[2] == 'agent') { + if (isset($mqttPath[2])) { + if ($mqttPath[1] == 'agent') { $entity = intval($mqttPath[1]); - $serial = $DB->escape($mqttPath[3]); + $serial = $DB->escape($mqttPath[2]); if (strlen($serial)) { $computerTable = Computer::getTable(); $agentTable = self::getTable(); diff --git a/inc/fleet.class.php b/inc/fleet.class.php index 1e27b058..ecfeb1ea 100644 --- a/inc/fleet.class.php +++ b/inc/fleet.class.php @@ -295,7 +295,7 @@ public function getTopic() { return null; } - return '/' . $this->fields['entities_id'] . '/fleet/' . $this->fields['id']; + return $this->fields['entities_id'] . '/fleet/' . $this->fields['id']; } /** diff --git a/inc/mqtthandler.class.php b/inc/mqtthandler.class.php index b1da71c6..4649804d 100644 --- a/inc/mqtthandler.class.php +++ b/inc/mqtthandler.class.php @@ -117,21 +117,21 @@ public function publish(\sskaje\mqtt\MQTT $mqtt, \sskaje\mqtt\Message\PUBLISH $p $message = $publish_object->getMessage(); $this->log->saveIngoingMqttMessage($topic, $message); - $mqttPath = explode('/', $topic, 5); - if (isset($mqttPath[4])) { - if ($mqttPath[4] == "Status/Ping" && $message == "!") { + $mqttPath = explode('/', $topic, 4); + if (isset($mqttPath[3])) { + if ($mqttPath[3] == "Status/Ping") { $this->updateLastContact($topic, $message); - } else if ($mqttPath[4] == "Status/Geolocation" && $message != "?") { + } else if ($mqttPath[3] == "Status/Geolocation" && $message != "?") { $this->saveGeolocationPosition($topic, $message); - } else if ($mqttPath[4] == "Status/Unenroll") { + } else if ($mqttPath[3] == "Status/Unenroll") { $this->deleteAgent($topic, $message); - } else if ($mqttPath[4] == "Status/Inventory") { + } else if ($mqttPath[3] == "Status/Inventory") { $this->updateInventory($topic, $message); - } else if ($mqttPath[4] == "Status/Online") { + } else if ($mqttPath[3] == "Status/Online") { $this->updateOnlineStatus($topic, $message); - } else if ($mqttPath[4] == "Status/Task") { + } else if ($mqttPath[3] == "Status/Task") { $this->updateTaskStatus($topic, $message); - } else if ($mqttPath[4] == "FlyvemdmManifest/Status/Version") { + } else if ($mqttPath[3] == "FlyvemdmManifest/Status/Version") { $this->updateAgentVersion($topic, $message); } else if (strpos($topic, "/FlyvemdmManifest") === 0) { if ($topic == '/FlyvemdmManifest/Status/Version') { @@ -203,7 +203,7 @@ protected function updateInventory($topic, $message) { } } - $this->updateLastContact($topic, $message); + $this->updateLastContact($topic, '!'); } } @@ -216,6 +216,9 @@ protected function updateInventory($topic, $message) { * @param string $message */ protected function updateLastContact($topic, $message) { + if ($message !== '!') { + return; + } $agent = new \PluginFlyvemdmAgent(); if ($agent->getByTopic($topic)) { @@ -280,7 +283,7 @@ protected function saveGeolocationPosition($topic, $message) { } } - $this->updateLastContact($topic, $message); + $this->updateLastContact($topic, '!'); } } @@ -324,7 +327,7 @@ protected function updateTaskStatus($topic, $message) { $taskStatus->updateStatus($policy, $status); } - $this->updateLastContact($topic, $message); + $this->updateLastContact($topic, '!'); } } } @@ -355,7 +358,7 @@ protected function updateOnlineStatus($topic, $message) { 'is_online' => $status, ]); - $this->updateLastContact($topic, $message); + $this->updateLastContact($topic, '!'); } } } diff --git a/install/upgrade/update_to_dev.php b/install/upgrade/update_to_dev.php index 9c63e51e..9cd5f7cf 100644 --- a/install/upgrade/update_to_dev.php +++ b/install/upgrade/update_to_dev.php @@ -50,7 +50,6 @@ function plugin_flyvemdm_update_to_dev(Migration $migration) { PluginFlyvemdmInvitation::$rightname => ALLSTANDARDRIGHT , PluginFlyvemdmInvitationlog::$rightname => READ, PluginFlyvemdmGeolocation::$rightname => ALLSTANDARDRIGHT | READNOTE | UPDATENOTE, - PluginFlyvemdmTask::$rightname => READ, ]); $profileRight->updateProfileRights($profiles_id, $newRights); @@ -303,4 +302,97 @@ function plugin_flyvemdm_update_to_dev(Migration $migration) { SET `symbol` = 'maximumTimeToLock' WHERE `symbol`='MaximumTimeToLock'"; $DB->query($query); + + // change MQTT topics tree layout : remove leading slash + $mqttClient = PluginFlyvemdmMqttclient::getInstance(); + $request = [ + 'FIELDS' => [ + 'glpi_plugin_flyvemdm_agents' => ['entities_id'], + 'glpi_computers' => ['serial'], + ], + 'FROM' => 'glpi_plugin_flyvemdm_agents', + 'INNER JOIN' => [ + 'glpi_computers' => ['FKEY' => [ + 'glpi_plugin_flyvemdm_agents' => 'computers_id', + 'glpi_computers' => 'id' + ]] + ], + 'WHERE' => ['lock' => ['<>' => '0']] + ]; + $mqttMessage = ['lock' => 'now']; + $mqttMessage = json_encode($mqttMessage, JSON_UNESCAPED_SLASHES); + foreach ($DB->request($request) as $row) { + $topic = implode('/', [ + $row['entities_id'], + 'agent', + $row['serial'], + 'Command', + 'Lock', + ]); + $mqttClient->publish($topic, $mqttMessage, 0, 1); + $mqttClient->publish('/' . $topic, null, 0, 1); + } + + // re-use previous request array + $request['WHERE'] = ['wipe' => ['<>' => '0']]; + $mqttMessage = ['wipe' => 'now']; + $mqttMessage = json_encode($mqttMessage, JSON_UNESCAPED_SLASHES); + foreach ($DB->request($request) as $row) { + $topic = implode('/', [ + $row['entities_id'], + 'agent', + $row['serial'], + 'Command', + 'Wipe', + ]); + $mqttClient->publish($topic, $mqttMessage, 0, 1); + $mqttClient->publish('/' . $topic, null, 0, 1); + } + + $request['WHERE'] = ['enroll_status' => ['=' => 'unenrolling']]; + $mqttMessage = ['unenroll' => 'now']; + $mqttMessage = json_encode($mqttMessage, JSON_UNESCAPED_SLASHES); + foreach ($DB->request($request) as $row) { + $topic = implode('/', [ + $row['entities_id'], + 'agent', + $row['serial'], + 'Command', + 'Unenroll', + ]); + $mqttClient->publish($topic, $mqttMessage, 0, 1); + $mqttClient->publish('/' . $topic, null, 0, 1); + } + + $request = [ + 'FIELDS' => [ + 'glpi_plugin_flyvemdm_tasks' => ['id', 'plugin_flyvemdm_fleets_id'], + 'glpi_plugin_flyvemdm_policies' => ['symbol'], + 'glpi_plugin_flyvemdm_fleets' => ['entities_id'] + ], + 'FROM' => 'glpi_plugin_flyvemdm_tasks', + 'INNER JOIN' => [ + 'glpi_plugin_flyvemdm_policies' => [ + 'FKEY' => [ + 'glpi_plugin_flyvemdm_tasks' => 'plugin_flyvemdm_policies_id', 'glpi_plugin_flyvemdm_policies' => 'id' + ] + ], + 'glpi_plugin_flyvemdm_fleets' => [ + 'FKEY' => [ + 'glpi_plugin_flyvemdm_tasks' => 'plugin_flyvemdm_fleets_id', 'glpi_plugin_flyvemdm_fleets' => 'id' + ] + ] + ] + ]; + foreach ($DB->request($request) as $row) { + $topic = implode('/', [ + $row['entities_id'], + 'fleet', + $row['plugin_flyvemdm_fleets_id'], + 'Policy', + $row['symbol'], + ]); + $mqttClient->publish("$topic/Task/" . $row['id'], json_encode($mqttMessage, JSON_UNESCAPED_SLASHES), 0, 1); + $mqttClient->publish('/' . $topic, null, 0, 1); + } }