From 6d0f7fb39243dcd966f4702878078c8975f36507 Mon Sep 17 00:00:00 2001 From: Jaroslav Drzik Date: Thu, 9 Dec 2021 17:57:03 +0100 Subject: [PATCH] Array of states --- hook.php | 7 ++++++- inc/remote_status.php | 18 +++++++++++++++--- inc/remotesupport.class.php | 23 ++++++++++++++++++++--- 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/hook.php b/hook.php index c37ec5e..966e944 100644 --- a/hook.php +++ b/hook.php @@ -21,7 +21,12 @@ -------------------------------------------------------------------------- */ -function plugin_remotesupport_install(){ +function plugin_remotesupport_install() +{ + global $DB; + + + return true; } diff --git a/inc/remote_status.php b/inc/remote_status.php index a2f3a12..f5f8b99 100644 --- a/inc/remote_status.php +++ b/inc/remote_status.php @@ -48,6 +48,7 @@ declare(ticks=1); global $DB,$agents; $check_arr = []; + $comps = []; $pfInventoryComputerComputer = new PluginFusioninventoryInventoryComputerComputer(); foreach (getAllDataFromTable(PluginFusioninventoryAgent::getTable()) as $a) { @@ -60,6 +61,7 @@ global $DB,$agents; $check["status"] = "unknown"; $check_arr[] = $check; + $comps[$a["computers_id"]] = $check; //print_r($agent->getAgentStatusURLs()); } @@ -73,7 +75,7 @@ $descriptorspec = array( $cwd = '/tmp'; $env = array('debug' => 'false'); -$process = proc_open(__DIR__.'/bench_urls', $descriptorspec, $pipes, $cwd, $env); +$process = proc_open(__DIR__.'/check_status', $descriptorspec, $pipes, $cwd, $env); if (is_resource($process)) { // $pipes now looks like this: @@ -94,8 +96,18 @@ if (is_resource($process)) { echo "command returned $return_value\n"; } +$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); + + + $DB->update("glpi_computers", [ - 'states_id' => NULL ] , + 'states_id' => $states_ids["Offline"] ] , [ '1' => '1' ] ); @@ -105,7 +117,7 @@ foreach ($checked as $s) { $comp = new Computer(); $comp->getFromDB($s->computers_id); - $comp->fields["states_id"] = 2; + $comp->fields["states_id"] = $states_ids["Online"]; $DB->update("glpi_computers", [ 'states_id' => $comp->fields["states_id"] ], [ 'id' => $s->computers_id ] diff --git a/inc/remotesupport.class.php b/inc/remotesupport.class.php index 6b661bc..b60f9a8 100644 --- a/inc/remotesupport.class.php +++ b/inc/remotesupport.class.php @@ -40,15 +40,32 @@ class PluginRemotesupportRemotesupport extends CommonDBTM { } + static function getStatesIds() { + global $DB; + + $states_ids = []; + + $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']; + + return $states_ids; + } static function cronRemotesupport($task) { global $DB; - Toolbox::logInFile("remotsupport","Starting search of agents\n"); + Toolbox::logInFile("remotsupport","Starting search of agents\n"); $agents = []; $data_set = []; + $stids = self::getStatesIds(); + + foreach (getAllDataFromTable(PluginFusioninventoryAgent::getTable()) as $a) { $data_set[] = $a["id"]; @@ -67,9 +84,9 @@ class PluginRemotesupportRemotesupport extends CommonDBTM { $comp = new Computer(); $comp->getFromDB($agents[$id]["computers_id"]); if ($st["message"] == "noanswer") - $comp->fields["states_id"] = NULL; + $comp->fields["states_id"] = $stids["Offline"]; else - $comp->fields["states_id"] = 2; + $comp->fields["states_id"] = $stids["Online"]; $DB->update("glpi_computers", [ 'states_id' => $comp->fields["states_id"] ], [ 'id' => $agents[$id]["computers_id"] ]