diff --git a/README.md b/README.md index 37e7239..df43f6e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # RemoteSupport -GLPI Plugin for direct VNC connection from browser inside computer from server +GLPI Plugin for direct VNC connection from browser inside computer from server and update status of online computers, it checks computer with go program in glpi cron and with fusion inventory agent... This Plugin add a simple button inside computer: diff --git a/bin/check_status b/bin/check_status deleted file mode 100755 index 1f6eb90..0000000 Binary files a/bin/check_status and /dev/null differ diff --git a/inc/remotesupport.class.php b/inc/remotesupport.class.php index 00e160d..4a56345 100644 --- a/inc/remotesupport.class.php +++ b/inc/remotesupport.class.php @@ -8,25 +8,26 @@ class PluginRemotesupportRemotesupport extends CommonDBTM { public static function showInfo($item) { - - $config = Config::getConfigurationValues('plugin:Remotesupport'); - if ($config["show_in_computers"] == false) - return true; - - if ($config["fusion"] == true && $config["easy_novnc"] == true) { - $pfInventoryComputerComputer = new PluginFusioninventoryInventoryComputerComputer(); - $a_computerextend = $pfInventoryComputerComputer->hasAutomaticInventory($item->getID()); - if (empty($a_computerextend)) { - return true; - } - - $href = "https://" . $_SERVER['SERVER_ADDR'] . "/vnc.html?path=vnc%2F" . $a_computerextend['remote_addr'] . "&autoconnect=true&resize=scale&reconnect=true&show_dot=true"; - $name = $a_computerextend['remote_addr']; - } else { - $href = "vnc://" . $item->fields["name"]; - $name = $item->fields["name"]; - } + $config = Config::getConfigurationValues('plugin:Remotesupport'); + + if ($config["show_in_computers"] == false) { + return true; + } + + if ($config["fusion"] == true && $config["easy_novnc"] == true) { + $pfInventoryComputerComputer = new PluginFusioninventoryInventoryComputerComputer(); + $a_computerextend = $pfInventoryComputerComputer->hasAutomaticInventory($item->getID()); + if (empty($a_computerextend)) { + return true; + } + + $href = "https://" . $_SERVER['SERVER_ADDR'] . "/vnc.html?path=vnc%2F" . $a_computerextend['remote_addr'] . "&autoconnect=true&resize=scale&reconnect=true&show_dot=true"; + $name = $a_computerextend['remote_addr']; + } else { + $href = "vnc://" . $item->fields["name"]; + $name = $item->fields["name"]; + } echo ''; echo ''; echo ''; @@ -34,7 +35,7 @@ class PluginRemotesupportRemotesupport extends CommonDBTM echo ''; echo '
' . __('Remote Support') . '
'; - $url = "
  • " . $name . "
  • "; + $url = "
  • " . $name . "
  • "; if ($url != "") { echo "

      VNC connect :

      "; @@ -69,74 +70,103 @@ class PluginRemotesupportRemotesupport extends CommonDBTM $req = $DB->request('glpi_computers', ['FIELDS' => ['glpi_computers' => ['id', 'contact']]]); - $contactids = []; - while ($ret = $req->next()) { - $contactids[$ret["id"]] = $ret["contact"]; - } - - return $contactids; + $contactids = []; + while ($ret = $req->next()) { + $contactids[$ret["id"]] = $ret["contact"]; + } + + return $contactids; } public static function cronRemotesupport($task) { global $DB; - $config = Config::getConfigurationValues('plugin:Remotesupport'); + $config = Config::getConfigurationValues('plugin:Remotesupport'); - if ($config["fusion"]) - return 0; - - Toolbox::logInFile("remotsupport", "Starting search of agents\n"); - $pfConfig = new PluginFusioninventoryConfig(); - $port = $pfConfig->getValue('agent_port'); - $rs_path = Plugin::getPhpDir('remotesupport'); - - $check_arr = []; - $comps = []; - $pfInventoryComputerComputer = new PluginFusioninventoryInventoryComputerComputer(); - foreach (getAllDataFromTable(PluginFusioninventoryAgent::getTable()) as $a) { - - $check = []; - $a_computerextend = $pfInventoryComputerComputer->hasAutomaticInventory($a["computers_id"]); - - $check["url"] = "http://" . $a_computerextend["remote_addr"] . ":" . $port . "/status"; - $check["id"] = $a["id"]; - $check["computers_id"] = $a["computers_id"]; - $check["status"] = "unknown"; - - $check_arr[] = $check; - $comps[$a["computers_id"]] = $check; - //print_r($agent->getAgentStatusURLs()); + if ($config["fusion"] || $config["run_mode"] == "None") { + return 0; } - $descriptorspec = array( - 0 => array("pipe", "r"), // stdin is a pipe that the child will read from - 1 => array("pipe", "w"), // stdout is a pipe that the child will write to - 2 => array("file", "/tmp/error-output.txt", "a"), // stderr is a file to write to - ); + Toolbox::logInFile("remotsupport", "Starting search of agents\n"); - $cwd = '/tmp'; - $env = array('debug' => 'false'); + if ($config["run_mode"] == "Parallel") { + $pfConfig = new PluginFusioninventoryConfig(); + $port = $pfConfig->getValue('agent_port'); + $rs_path = Plugin::getPhpDir('remotesupport'); - $process = proc_open($rs_path . '/bin/check_status', $descriptorspec, $pipes, $cwd, $env); + $check_arr = []; + $comps = []; + $pfInventoryComputerComputer = new PluginFusioninventoryInventoryComputerComputer(); + foreach (getAllDataFromTable(PluginFusioninventoryAgent::getTable()) as $a) { - if (is_resource($process)) { - // $pipes now looks like this: - // 0 => writeable handle connected to child stdin - // 1 => readable handle connected to child stdout - // Any error output will be appended to /tmp/error-output.txt + $check = []; + $a_computerextend = $pfInventoryComputerComputer->hasAutomaticInventory($a["computers_id"]); - fwrite($pipes[0], json_encode($check_arr)); - fclose($pipes[0]); + $check["url"] = "http://" . $a_computerextend["remote_addr"] . ":" . $port . "/status"; + $check["id"] = $a["id"]; + $check["computers_id"] = $a["computers_id"]; + $check["status"] = "unknown"; - $checked = json_decode(stream_get_contents($pipes[1])); - fclose($pipes[1]); + $check_arr[] = $check; + $comps[$a["computers_id"]] = $check; + //print_r($agent->getAgentStatusURLs()); + } - // It is important that you close any pipes before calling - // proc_close in order to avoid a deadlock - $return_value = proc_close($process); + $descriptorspec = array( + 0 => array("pipe", "r"), // stdin is a pipe that the child will read from + 1 => array("pipe", "w"), // stdout is a pipe that the child will write to + 2 => array("file", "/tmp/error-output.txt", "a"), // stderr is a file to write to + ); + + $cwd = '/tmp'; + $env = array('debug' => 'false'); + + $process = proc_open($rs_path . '/bin/check_status', $descriptorspec, $pipes, $cwd, $env); + + if (is_resource($process)) { + // $pipes now looks like this: + // 0 => writeable handle connected to child stdin + // 1 => readable handle connected to child stdout + // Any error output will be appended to /tmp/error-output.txt + + fwrite($pipes[0], json_encode($check_arr)); + fclose($pipes[0]); + + $checked = json_decode(stream_get_contents($pipes[1])); + fclose($pipes[1]); + + // It is important that you close any pipes before calling + // proc_close in order to avoid a deadlock + $return_value = proc_close($process); + + Toolbox::logInFile("remotsupport", "command returned $return_value\n"); + } + + } else { + $agents = []; + $data_set = []; + $checked = []; + + foreach (getAllDataFromTable(PluginFusioninventoryAgent::getTable()) as $a) { + + $data_set[] = $a["id"]; + $agents[$a["id"]] = $a; + } + + foreach ($data_set as $id) { + $agent = new PluginFusioninventoryAgent; + $agent->getFromDB((int) $id); + $st = $agent->getStatus(); + + if ($st["message"] != "noanswer") { + $check = new stdClass(); + $check->computers_id = $agents[$id]["computers_id"]; + $checked[] = $check; + } + + } - Toolbox::logInFile("remotsupport", "command returned $return_value\n"); } $stids = self::getStatesIds(); @@ -146,21 +176,21 @@ class PluginRemotesupportRemotesupport extends CommonDBTM ['1' => '1'] ); - $ids = []; - $cids = self::getContactArray(); + $ids = []; + $cids = self::getContactArray(); + foreach ($checked as $s) { $comp = new Computer(); $comp->getFromDB($s->computers_id); Toolbox::logInFile("remotsupport", $s->computers_id . " " . $cids[$s->computers_id] . "\n"); - $ids[] = $s->computers_id; - } - - $DB->update("glpi_computers", [ - 'states_id' => $stids["Online"] ], - ['id' => $ids ] - ); + $ids[] = $s->computers_id; + } + $DB->update("glpi_computers", [ + 'states_id' => $stids["Online"]], + ['id' => $ids] + ); return 0; }