From 50e93380596d5fc03dae6ce230f1086c6c0e2994 Mon Sep 17 00:00:00 2001 From: Jaroslav Drzik Date: Thu, 9 Dec 2021 18:30:30 +0100 Subject: [PATCH] Use go program for checks --- inc/remote_status.php | 4 +- inc/remotesupport.class.php | 84 ++++++++++++------ inc/test.php | 170 ++++++++++++++++++++++++++++++++++++ 3 files changed, 231 insertions(+), 27 deletions(-) create mode 100644 inc/test.php diff --git a/inc/remote_status.php b/inc/remote_status.php index f5f8b99..768bbe9 100644 --- a/inc/remote_status.php +++ b/inc/remote_status.php @@ -50,7 +50,7 @@ global $DB,$agents; $check_arr = []; $comps = []; $pfInventoryComputerComputer = new PluginFusioninventoryInventoryComputerComputer(); - foreach (getAllDataFromTable(PluginFusioninventoryAgent::getTable()) as $a) { + foreach (getAllDataFromTable(PluginFusioninventoryAgent::getTable()) as $a) { $check = []; $a_computerextend = $pfInventoryComputerComputer->hasAutomaticInventory($a["computers_id"]); @@ -63,7 +63,7 @@ global $DB,$agents; $check_arr[] = $check; $comps[$a["computers_id"]] = $check; //print_r($agent->getAgentStatusURLs()); -} + } $descriptorspec = array( diff --git a/inc/remotesupport.class.php b/inc/remotesupport.class.php index b60f9a8..746fea2 100644 --- a/inc/remotesupport.class.php +++ b/inc/remotesupport.class.php @@ -60,43 +60,77 @@ class PluginRemotesupportRemotesupport extends CommonDBTM { Toolbox::logInFile("remotsupport","Starting search of agents\n"); - $agents = []; - $data_set = []; + + $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"].":62354/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()); + } + + $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(__DIR__.'/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"); + } $stids = self::getStatesIds(); - - foreach (getAllDataFromTable(PluginFusioninventoryAgent::getTable()) as $a) { - - $data_set[] = $a["id"]; - $agents[$a["id"]] = $a; - } + $DB->update("glpi_computers", [ + 'states_id' => $stids["Offline"] ] , + [ '1' => '1' ] + ); - foreach ($data_set as $id) { - $agent = new PluginFusioninventoryAgent; - $agent->getFromDB((int)$id); - $st = $agent->getStatus(); - - if ($st["message"] != "noanswer") - Toolbox::logInFile("remotsupport",print_r($agents[$id],true)); + foreach ($checked as $s) { $comp = new Computer(); - $comp->getFromDB($agents[$id]["computers_id"]); - if ($st["message"] == "noanswer") - $comp->fields["states_id"] = $stids["Offline"]; - else - $comp->fields["states_id"] = $stids["Online"]; + $comp->getFromDB($s->computers_id); + $comp->fields["states_id"] = $stids["Online"]; $DB->update("glpi_computers", [ 'states_id' => $comp->fields["states_id"] ], - [ 'id' => $agents[$id]["computers_id"] ] + [ 'id' => $s->computers_id ] ); - $comp->fields["contact"]."\n"; + Toolbox::logInFile("remotsupport",$s->computers_id." ".$comp->fields["contact"]."\n"); + } - - - return true; + return 0; } static function cronInfo($name) { diff --git a/inc/test.php b/inc/test.php new file mode 100644 index 0000000..8034d0e --- /dev/null +++ b/inc/test.php @@ -0,0 +1,170 @@ +. + + ------------------------------------------------------------------------ + + @package FusionInventory + @author Walid Nouh + @co-author + @copyright Copyright (c) 2010-2011 FusionInventory team + @license AGPL License 3.0 or (at your option) any later version + http://www.gnu.org/licenses/agpl-3.0-standalone.html + @link http://www.fusioninventory.org/ + @link http://forge.fusioninventory.org/projects/fusioninventory-for-glpi/ + @since 2010 + + ------------------------------------------------------------------------ + */ + +include ("../../../inc/includes.php"); + +declare(ticks=1); + + + $state_online = [ + 'name' => 'Online', + 'entities_id' => 0, + 'is_recursive' => 0, + 'comment' => '', + 'states_id' => 0, + 'completename' => 'Online', + 'level' => 1, + 'ancestors_cache' => '[]', + 'sons_cache' => 'NULL', + 'is_visible_computer' => 1, + 'is_visible_monitor' => 0, + 'is_visible_networkequipment' => 0, + 'is_visible_peripheral' => 0, + 'is_visible_phone' => 0, + 'is_visible_printer' => 0, + 'is_visible_softwareversion' => 0, + 'is_visible_softwarelicense' => 0, + 'is_visible_line' => 0, + 'is_visible_certificate' => 0, + 'is_visible_rack' => 0, + 'is_visible_passivedcequipment' => 0, + 'is_visible_enclosure' => 0, + 'is_visible_pdu' => 0, + 'is_visible_cluster' => 0, + 'is_visible_contract' => 0, + 'is_visible_appliance' => 0]; +/* +$ret = $DB->insert( + 'glpi_states', $state_online +); + +$state_offline = $state_online; +$state_offline["name"] = 'Offline'; +$state_offline["completename"] = 'Offline'; + +$ret = $DB->insert( + 'glpi_states', $state_offline +); + +*/ + +$req = $DB->request('glpi_states', ['FIELDS' => ['glpi_states' => ['id', 'name']]], [ 'OR' => [ 'name' => 'Online', 'name' => 'Offline']]); + +$ret = $req->next(); +$states_ids[$ret['name']] = $ret['id']; +$ret = $req->next(); +$states_ids[$ret['name']] = $ret['id']; +print_r($states_ids); + +exit(0); + +global $DB,$agents; + + $check_arr = []; + $pfInventoryComputerComputer = new PluginFusioninventoryInventoryComputerComputer(); + foreach (getAllDataFromTable(PluginFusioninventoryAgent::getTable()) as $a) { + + $check = []; + $a_computerextend = $pfInventoryComputerComputer->hasAutomaticInventory($a["computers_id"]); + + $check["url"] = "http://".$a_computerextend["remote_addr"].":62354/status"; + $check["id"] = $a["id"]; + $check["computers_id"] = $a["computers_id"]; + $check["status"] = "unknown"; + + $check_arr[] = $check; + //print_r($agent->getAgentStatusURLs()); +} + + +$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(__DIR__.'/bench_urls', $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); + + echo "command returned $return_value\n"; +} + + $DB->update("glpi_computers", [ + 'states_id' => NULL ] , + [ '1' => '1' ] + ); + + +foreach ($checked as $s) { + echo $s->computers_id." "; + + $comp = new Computer(); + $comp->getFromDB($s->computers_id); + $comp->fields["states_id"] = 2; + $DB->update("glpi_computers", [ + 'states_id' => $comp->fields["states_id"] ], + [ 'id' => $s->computers_id ] + ); + echo $comp->fields["contact"]."\n"; + +} + +// print_r($a_computerextend); +exit(0);